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
|
|
|
|
}
|
|
|
|
export const tokenInvalidException = "tokenInvalid";
|
|
|
|
|
2020-11-13 18:00:22 +01:00
|
|
|
class JWTTokenManager {
|
2021-07-27 16:37:01 +02:00
|
|
|
public createAuthToken(identifier: string) {
|
2021-08-09 16:37:24 +02:00
|
|
|
//TODO fix me 200d when ory authentication will be available
|
|
|
|
return Jwt.sign({ identifier }, SECRET_KEY, { expiresIn: "200d" });
|
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();
|