workadventure/back/src/Controller/AuthenticateController.ts

130 lines
4.1 KiB
TypeScript
Raw Normal View History

2020-10-01 16:51:51 +02:00
import { v4 } from 'uuid';
2020-09-28 18:52:54 +02:00
import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js";
import {BaseController} from "./BaseController";
import {adminApi} from "../Services/AdminApi";
2020-10-09 14:53:18 +02:00
import {jwtTokenManager} from "../Services/JWTTokenManager";
import {parse} from "query-string";
export interface TokenInterface {
2020-09-28 15:02:37 +02:00
userUuid: string
}
2020-09-28 18:52:54 +02:00
export class AuthenticateController extends BaseController {
2020-09-28 18:52:54 +02:00
constructor(private App : TemplatedApp) {
super();
this.register();
this.verify();
this.anonymLogin();
}
//Try to login with an admin token
private register(){
this.App.options("/register", (res: HttpResponse, req: HttpRequest) => {
2020-09-28 18:52:54 +02:00
this.addCorsHeaders(res);
res.end();
});
this.App.post("/register", (res: HttpResponse, req: HttpRequest) => {
2020-09-29 16:12:17 +02:00
(async () => {
this.addCorsHeaders(res);
res.onAborted(() => {
console.warn('Login request was aborted');
})
const host = req.getHeader('host');
2020-09-29 16:12:17 +02:00
const param = await res.json();
//todo: what to do if the organizationMemberToken is already used?
const organizationMemberToken:string|null = param.organizationMemberToken;
try {
if (typeof organizationMemberToken != 'string') throw new Error('No organization token');
const data = await adminApi.fetchMemberDataByToken(organizationMemberToken);
const userUuid = data.userUuid;
const organizationSlug = data.organizationSlug;
const worldSlug = data.worldSlug;
const roomSlug = data.roomSlug;
const mapUrlStart = data.mapUrlStart;
2020-10-09 14:53:18 +02:00
const authToken = jwtTokenManager.createJWTToken(userUuid);
res.writeStatus("200 OK").end(JSON.stringify({
authToken,
userUuid,
organizationSlug,
worldSlug,
roomSlug,
mapUrlStart,
}));
} catch (e) {
console.log("An error happened", e)
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
})();
});
}
private verify(){
this.App.options("/verify", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res);
res.end();
});
this.App.get("/verify", (res: HttpResponse, req: HttpRequest) => {
(async () => {
this.addCorsHeaders(res);
const query = parse(req.getQuery());
res.onAborted(() => {
console.warn('verify request was aborted');
})
try {
2020-10-15 16:54:04 +02:00
await jwtTokenManager.getUserUuidFromToken(query.token as string);
} catch (e) {
res.writeStatus("400 Bad Request").end(JSON.stringify({
"success": false,
"message": "Invalid JWT token"
}));
}
res.writeStatus("200 OK").end(JSON.stringify({
"success": true
}));
})();
});
}
//permit to login on application. Return token to connect on Websocket IO.
private anonymLogin(){
this.App.options("/anonymLogin", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res);
res.end();
});
this.App.post("/anonymLogin", (res: HttpResponse, req: HttpRequest) => {
2020-10-13 15:55:30 +02:00
this.addCorsHeaders(res);
2020-10-13 15:55:30 +02:00
res.onAborted(() => {
console.warn('Login request was aborted');
})
2020-10-13 15:55:30 +02:00
const userUuid = v4();
const authToken = jwtTokenManager.createJWTToken(userUuid);
res.writeStatus("200 OK").end(JSON.stringify({
authToken,
userUuid,
}));
});
2020-09-25 18:29:22 +02:00
}
}