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";
|
2020-10-12 16:23:07 +02:00
|
|
|
import {adminApi} from "../Services/AdminApi";
|
2020-10-09 14:53:18 +02:00
|
|
|
import {jwtTokenManager} from "../Services/JWTTokenManager";
|
2020-10-15 16:48:42 +02:00
|
|
|
import {parse} from "query-string";
|
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-10-12 16:23:07 +02:00
|
|
|
this.register();
|
2020-10-15 16:48:42 +02:00
|
|
|
this.verify();
|
2020-10-12 16:23:07 +02:00
|
|
|
this.anonymLogin();
|
2020-04-04 17:22:02 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 16:23:07 +02:00
|
|
|
//Try to login with an admin token
|
2020-10-15 16:48:42 +02:00
|
|
|
private register(){
|
2020-10-12 16:23:07 +02:00
|
|
|
this.App.options("/register", (res: HttpResponse, req: HttpRequest) => {
|
2020-09-28 18:52:54 +02:00
|
|
|
this.addCorsHeaders(res);
|
|
|
|
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
2020-10-12 16:23:07 +02:00
|
|
|
this.App.post("/register", (res: HttpResponse, req: HttpRequest) => {
|
2020-09-29 16:12:17 +02:00
|
|
|
(async () => {
|
|
|
|
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;
|
2020-10-15 17:59:32 +02:00
|
|
|
|
2020-09-29 17:12:28 +02:00
|
|
|
try {
|
2020-10-12 16:23:07 +02:00
|
|
|
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-09-29 17:12:28 +02:00
|
|
|
|
2020-10-09 14:53:18 +02:00
|
|
|
const authToken = jwtTokenManager.createJWTToken(userUuid);
|
2020-10-15 17:27:40 +02:00
|
|
|
res.writeStatus("200 OK");
|
|
|
|
this.addCorsHeaders(res);
|
|
|
|
res.end(JSON.stringify({
|
2020-09-29 17:12:28 +02:00
|
|
|
authToken,
|
|
|
|
userUuid,
|
2020-10-12 16:23:07 +02:00
|
|
|
organizationSlug,
|
|
|
|
worldSlug,
|
|
|
|
roomSlug,
|
2020-09-29 17:12:28 +02:00
|
|
|
mapUrlStart,
|
|
|
|
}));
|
|
|
|
|
|
|
|
} catch (e) {
|
2020-10-15 09:44:37 +02:00
|
|
|
console.error("An error happened", e)
|
2020-10-15 17:27:40 +02:00
|
|
|
res.writeStatus(e.status || "500 Internal Server Error");
|
|
|
|
this.addCorsHeaders(res);
|
|
|
|
res.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-10-12 16:23:07 +02:00
|
|
|
|
2020-04-04 17:22:02 +02:00
|
|
|
}
|
2020-09-29 17:12:28 +02:00
|
|
|
|
2020-10-15 16:48:42 +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 () => {
|
|
|
|
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);
|
2020-10-15 16:48:42 +02:00
|
|
|
} catch (e) {
|
2020-10-15 17:27:40 +02:00
|
|
|
res.writeStatus("400 Bad Request");
|
|
|
|
this.addCorsHeaders(res);
|
|
|
|
res.end(JSON.stringify({
|
2020-10-15 16:48:42 +02:00
|
|
|
"success": false,
|
|
|
|
"message": "Invalid JWT token"
|
|
|
|
}));
|
2020-10-15 17:27:40 +02:00
|
|
|
return;
|
2020-10-15 16:48:42 +02:00
|
|
|
}
|
2020-10-15 17:27:40 +02:00
|
|
|
res.writeStatus("200 OK");
|
|
|
|
this.addCorsHeaders(res);
|
|
|
|
res.end(JSON.stringify({
|
2020-10-15 16:48:42 +02:00
|
|
|
"success": true
|
|
|
|
}));
|
|
|
|
})();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-12 16:23:07 +02:00
|
|
|
//permit to login on application. Return token to connect on Websocket IO.
|
2020-10-15 16:48:42 +02:00
|
|
|
private anonymLogin(){
|
2020-10-12 16:23:07 +02:00
|
|
|
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
|
|
|
res.onAborted(() => {
|
|
|
|
console.warn('Login request was aborted');
|
|
|
|
})
|
2020-10-12 16:23:07 +02:00
|
|
|
|
2020-10-13 15:55:30 +02:00
|
|
|
const userUuid = v4();
|
|
|
|
const authToken = jwtTokenManager.createJWTToken(userUuid);
|
2020-10-15 17:27:40 +02:00
|
|
|
res.writeStatus("200 OK");
|
|
|
|
this.addCorsHeaders(res);
|
|
|
|
res.end(JSON.stringify({
|
2020-10-13 15:55:30 +02:00
|
|
|
authToken,
|
|
|
|
userUuid,
|
|
|
|
}));
|
2020-10-12 16:23:07 +02:00
|
|
|
});
|
2020-09-25 18:29:22 +02:00
|
|
|
}
|
2020-05-14 23:19:48 +02:00
|
|
|
}
|