2021-06-24 10:09:10 +02:00
|
|
|
import { ADMIN_API_URL, ALLOW_ARTILLERY, SECRET_KEY } from "../Enum/EnvironmentVariable";
|
|
|
|
import { uuid } from "uuidv4";
|
2020-11-13 18:00:22 +01:00
|
|
|
import Jwt 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
|
|
|
|
|
|
|
class JWTTokenManager {
|
|
|
|
public createJWTToken(userUuid: string) {
|
2021-06-24 10:09:10 +02:00
|
|
|
return Jwt.sign({ userUuid: userUuid }, SECRET_KEY, { expiresIn: "200d" }); //todo: add a mechanic to refresh or recreate token
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
|
|
|
|
2021-07-13 19:09:07 +02:00
|
|
|
public async getUserUuidFromToken(token: unknown, ipAddress?: string, roomUrl?: string): Promise<string> {
|
2020-11-13 18:00:22 +01:00
|
|
|
if (!token) {
|
2021-06-24 10:09:10 +02:00
|
|
|
throw new Error("An authentication error happened, a user tried to connect without a token.");
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
2021-06-24 10:09:10 +02:00
|
|
|
if (typeof token !== "string") {
|
|
|
|
throw new Error("Token is expected to be a string");
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
if (token === "test") {
|
2020-11-13 18:00:22 +01:00
|
|
|
if (ALLOW_ARTILLERY) {
|
|
|
|
return uuid();
|
|
|
|
} else {
|
2021-06-24 10:09:10 +02:00
|
|
|
throw new Error(
|
|
|
|
"In order to perform a load-testing test on this environment, you must set the ALLOW_ARTILLERY environment variable to 'true'"
|
|
|
|
);
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise<string>((resolve, reject) => {
|
2021-06-24 10:09:10 +02:00
|
|
|
Jwt.verify(token, SECRET_KEY, {}, (err, tokenDecoded) => {
|
2020-11-13 18:00:22 +01:00
|
|
|
const tokenInterface = tokenDecoded as TokenInterface;
|
|
|
|
if (err) {
|
2021-06-24 10:09:10 +02:00
|
|
|
console.error("An authentication error happened, invalid JsonWebToken.", err);
|
|
|
|
reject(new Error("An authentication error happened, invalid JsonWebToken. " + err.message));
|
2020-11-13 18:00:22 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tokenDecoded === undefined) {
|
2021-06-24 10:09:10 +02:00
|
|
|
console.error("Empty token found.");
|
|
|
|
reject(new Error("Empty token found."));
|
2020-11-13 18:00:22 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//verify token
|
|
|
|
if (!this.isValidToken(tokenInterface)) {
|
2021-06-24 10:09:10 +02:00
|
|
|
reject(new Error("Authentication error, invalid token structure."));
|
2020-11-13 18:00:22 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ADMIN_API_URL) {
|
|
|
|
//verify user in admin
|
2021-01-16 20:14:21 +01:00
|
|
|
let promise = new Promise((resolve) => resolve());
|
2021-07-13 19:09:07 +02:00
|
|
|
if (ipAddress && roomUrl) {
|
|
|
|
promise = this.verifyBanUser(tokenInterface.userUuid, ipAddress, roomUrl);
|
2021-01-16 20:14:21 +01:00
|
|
|
}
|
2021-06-24 10:09:10 +02:00
|
|
|
promise
|
|
|
|
.then(() => {
|
|
|
|
adminApi
|
|
|
|
.fetchCheckUserByToken(tokenInterface.userUuid)
|
|
|
|
.then(() => {
|
|
|
|
resolve(tokenInterface.userUuid);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
//anonymous user
|
|
|
|
if (err.response && err.response.status && err.response.status === 404) {
|
|
|
|
resolve(tokenInterface.userUuid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2021-01-16 20:14:21 +01:00
|
|
|
reject(err);
|
|
|
|
});
|
2020-11-13 18:00:22 +01:00
|
|
|
} else {
|
|
|
|
resolve(tokenInterface.userUuid);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-13 19:09:07 +02:00
|
|
|
private verifyBanUser(userUuid: string, ipAddress: string, roomUrl: string): Promise<AdminBannedData> {
|
2021-06-24 10:09:10 +02:00
|
|
|
return adminApi
|
2021-07-13 19:09:07 +02:00
|
|
|
.verifyBanUser(userUuid, ipAddress, roomUrl)
|
2021-06-24 10:09:10 +02:00
|
|
|
.then((data: AdminBannedData) => {
|
|
|
|
if (data && data.is_banned) {
|
|
|
|
throw new Error("User was banned");
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
throw err;
|
|
|
|
});
|
2021-01-15 03:19:58 +01:00
|
|
|
}
|
|
|
|
|
2020-11-13 18:00:22 +01:00
|
|
|
private isValidToken(token: object): token is TokenInterface {
|
2021-06-24 10:09:10 +02:00
|
|
|
return !(typeof (token as TokenInterface).userUuid !== "string");
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const jwtTokenManager = new JWTTokenManager();
|