2020-04-05 14:31:49 +02:00
|
|
|
import Jwt from "jsonwebtoken";
|
2020-09-25 18:29:22 +02:00
|
|
|
import {ADMIN_API_TOKEN, ADMIN_API_URL, SECRET_KEY, URL_ROOM_STARTED} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
|
2020-04-10 12:54:05 +02:00
|
|
|
import { uuid } from 'uuidv4';
|
2020-09-28 18:52:54 +02:00
|
|
|
import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js";
|
|
|
|
import {BaseController} from "./BaseController";
|
2020-10-06 15:37:00 +02:00
|
|
|
import {adminApi, AdminApiData} from "../Services/AdminApi";
|
2020-04-04 17:22:02 +02:00
|
|
|
|
2020-06-09 23:07:19 +02:00
|
|
|
export interface TokenInterface {
|
2020-09-28 15:02:37 +02:00
|
|
|
userUuid: string
|
2020-06-09 23:07:19 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
export class AuthenticateController extends BaseController {
|
2020-04-04 17:22:02 +02:00
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
constructor(private App : TemplatedApp) {
|
|
|
|
super();
|
2020-04-04 17:22:02 +02:00
|
|
|
this.login();
|
|
|
|
}
|
|
|
|
|
|
|
|
//permit to login on application. Return token to connect on Websocket IO.
|
|
|
|
login(){
|
2020-09-28 18:52:54 +02:00
|
|
|
this.App.options("/login", (res: HttpResponse, req: HttpRequest) => {
|
|
|
|
this.addCorsHeaders(res);
|
|
|
|
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
2020-09-29 16:12:17 +02:00
|
|
|
this.App.post("/login", (res: HttpResponse, req: HttpRequest) => {
|
|
|
|
(async () => {
|
|
|
|
this.addCorsHeaders(res);
|
|
|
|
|
|
|
|
res.onAborted(() => {
|
|
|
|
console.warn('Login request was aborted');
|
|
|
|
})
|
2020-09-30 10:12:40 +02:00
|
|
|
const host = req.getHeader('host');
|
2020-09-29 16:12:17 +02:00
|
|
|
const param = await res.json();
|
2020-09-29 17:12:28 +02:00
|
|
|
|
|
|
|
//todo: what to do if the organizationMemberToken is already used?
|
|
|
|
const organizationMemberToken:string|null = param.organizationMemberToken;
|
|
|
|
|
|
|
|
try {
|
|
|
|
let userUuid;
|
|
|
|
let mapUrlStart;
|
2020-10-01 14:11:34 +02:00
|
|
|
let newUrl: string|null = null;
|
2020-09-29 17:12:28 +02:00
|
|
|
|
|
|
|
if (organizationMemberToken) {
|
2020-10-06 15:37:00 +02:00
|
|
|
const data = await adminApi.fetchMemberDataByToken(organizationMemberToken);
|
2020-09-29 17:12:28 +02:00
|
|
|
|
|
|
|
userUuid = data.userUuid;
|
|
|
|
mapUrlStart = data.mapUrlStart;
|
|
|
|
newUrl = this.getNewUrlOnAdminAuth(data)
|
|
|
|
} else {
|
|
|
|
userUuid = uuid();
|
2020-09-30 10:12:40 +02:00
|
|
|
mapUrlStart = host.replace('api.', 'maps.') + URL_ROOM_STARTED;
|
2020-09-29 17:12:28 +02:00
|
|
|
newUrl = null;
|
2020-09-25 18:29:22 +02:00
|
|
|
}
|
2020-09-29 17:12:28 +02:00
|
|
|
|
|
|
|
const authToken = Jwt.sign({userUuid: userUuid}, SECRET_KEY, {expiresIn: '24h'});
|
|
|
|
res.writeStatus("200 OK").end(JSON.stringify({
|
|
|
|
authToken,
|
|
|
|
userUuid,
|
|
|
|
mapUrlStart,
|
|
|
|
newUrl,
|
|
|
|
}));
|
|
|
|
|
|
|
|
} catch (e) {
|
2020-09-30 10:12:40 +02:00
|
|
|
console.log("An error happened", e)
|
2020-09-29 17:12:28 +02:00
|
|
|
res.writeStatus(e.status || "500 Internal Server Error").end('An error happened');
|
2020-09-25 18:29:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-29 16:12:17 +02:00
|
|
|
})();
|
2020-05-23 14:00:36 +02:00
|
|
|
});
|
2020-04-04 17:22:02 +02:00
|
|
|
}
|
2020-09-29 17:12:28 +02:00
|
|
|
|
2020-09-28 15:02:37 +02:00
|
|
|
private getNewUrlOnAdminAuth(data:AdminApiData): string {
|
2020-09-25 18:29:22 +02:00
|
|
|
const organizationSlug = data.organizationSlug;
|
|
|
|
const worldSlug = data.worldSlug;
|
|
|
|
const roomSlug = data.roomSlug;
|
|
|
|
return '/@/'+organizationSlug+'/'+worldSlug+'/'+roomSlug;
|
|
|
|
}
|
2020-05-14 23:19:48 +02:00
|
|
|
}
|