2021-06-24 10:09:10 +02:00
|
|
|
import { ADMIN_API_URL, ALLOW_ARTILLERY, SECRET_KEY } from "../Enum/EnvironmentVariable";
|
|
|
|
import { uuid } from "uuidv4";
|
2021-07-27 16:37:01 +02:00
|
|
|
import Jwt, { verify } from "jsonwebtoken";
|
2021-06-24 10:09:10 +02:00
|
|
|
import { TokenInterface } from "../Controller/AuthenticateController";
|
|
|
|
import { adminApi, AdminBannedData } from "../Services/AdminApi";
|
2020-11-13 18:00:22 +01:00
|
|
|
|
2021-07-27 16:37:01 +02:00
|
|
|
export interface AuthTokenData {
|
|
|
|
identifier: string; //will be a email if logged in or an uuid if anonymous
|
2021-09-05 18:17:49 +02:00
|
|
|
hydraAccessToken?: string;
|
2021-07-27 16:37:01 +02:00
|
|
|
}
|
|
|
|
export const tokenInvalidException = "tokenInvalid";
|
|
|
|
|
2020-11-13 18:00:22 +01:00
|
|
|
class JWTTokenManager {
|
2021-09-05 18:17:49 +02:00
|
|
|
public createAuthToken(identifier: string, hydraAccessToken?: string) {
|
|
|
|
return Jwt.sign({ identifier, hydraAccessToken }, SECRET_KEY, { expiresIn: "30d" });
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
|
|
|
|
2021-08-15 22:51:06 +02:00
|
|
|
public verifyJWTToken(token: string, ignoreExpiration: boolean = false): AuthTokenData {
|
2021-07-27 16:37:01 +02:00
|
|
|
try {
|
2021-08-15 22:51:06 +02:00
|
|
|
return Jwt.verify(token, SECRET_KEY, { ignoreExpiration }) as AuthTokenData;
|
2021-07-27 16:37:01 +02:00
|
|
|
} catch (e) {
|
|
|
|
throw { reason: tokenInvalidException, message: e.message };
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const jwtTokenManager = new JWTTokenManager();
|