2021-06-24 10:09:10 +02:00
|
|
|
import { v4 } from "uuid";
|
|
|
|
import { HttpRequest, HttpResponse, TemplatedApp } from "uWebSockets.js";
|
|
|
|
import { BaseController } from "./BaseController";
|
|
|
|
import { adminApi } from "../Services/AdminApi";
|
|
|
|
import { jwtTokenManager } from "../Services/JWTTokenManager";
|
|
|
|
import { parse } from "query-string";
|
2020-11-13 18:00:22 +01:00
|
|
|
|
|
|
|
export interface TokenInterface {
|
2021-06-24 10:09:10 +02:00
|
|
|
userUuid: string;
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class AuthenticateController extends BaseController {
|
2021-06-24 10:09:10 +02:00
|
|
|
constructor(private App: TemplatedApp) {
|
2020-11-13 18:00:22 +01:00
|
|
|
super();
|
|
|
|
this.register();
|
|
|
|
this.verify();
|
|
|
|
this.anonymLogin();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Try to login with an admin token
|
2021-06-24 10:09:10 +02:00
|
|
|
private register() {
|
2020-11-13 18:00:22 +01:00
|
|
|
this.App.options("/register", (res: HttpResponse, req: HttpRequest) => {
|
|
|
|
this.addCorsHeaders(res);
|
|
|
|
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.App.post("/register", (res: HttpResponse, req: HttpRequest) => {
|
|
|
|
(async () => {
|
|
|
|
res.onAborted(() => {
|
2021-06-24 10:09:10 +02:00
|
|
|
console.warn("Login request was aborted");
|
|
|
|
});
|
2020-11-13 18:00:22 +01:00
|
|
|
const param = await res.json();
|
|
|
|
|
|
|
|
//todo: what to do if the organizationMemberToken is already used?
|
2021-06-24 10:09:10 +02:00
|
|
|
const organizationMemberToken: string | null = param.organizationMemberToken;
|
2020-11-13 18:00:22 +01:00
|
|
|
|
|
|
|
try {
|
2021-06-24 10:09:10 +02:00
|
|
|
if (typeof organizationMemberToken != "string") throw new Error("No organization token");
|
2020-11-13 18:00:22 +01:00
|
|
|
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;
|
|
|
|
const textures = data.textures;
|
|
|
|
|
|
|
|
const authToken = jwtTokenManager.createJWTToken(userUuid);
|
|
|
|
res.writeStatus("200 OK");
|
|
|
|
this.addCorsHeaders(res);
|
2021-06-24 10:09:10 +02:00
|
|
|
res.end(
|
|
|
|
JSON.stringify({
|
|
|
|
authToken,
|
|
|
|
userUuid,
|
|
|
|
organizationSlug,
|
|
|
|
worldSlug,
|
|
|
|
roomSlug,
|
|
|
|
mapUrlStart,
|
|
|
|
organizationMemberToken,
|
|
|
|
textures,
|
|
|
|
})
|
|
|
|
);
|
2020-11-13 18:00:22 +01:00
|
|
|
} catch (e) {
|
2021-01-17 20:42:33 +01:00
|
|
|
this.errorToResponse(e, res);
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
private verify() {
|
2020-11-13 18:00:22 +01:00
|
|
|
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(() => {
|
2021-06-24 10:09:10 +02:00
|
|
|
console.warn("verify request was aborted");
|
|
|
|
});
|
2020-11-13 18:00:22 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
await jwtTokenManager.getUserUuidFromToken(query.token as string);
|
|
|
|
} catch (e) {
|
|
|
|
res.writeStatus("400 Bad Request");
|
|
|
|
this.addCorsHeaders(res);
|
2021-06-24 10:09:10 +02:00
|
|
|
res.end(
|
|
|
|
JSON.stringify({
|
|
|
|
success: false,
|
|
|
|
message: "Invalid JWT token",
|
|
|
|
})
|
|
|
|
);
|
2020-11-13 18:00:22 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
res.writeStatus("200 OK");
|
|
|
|
this.addCorsHeaders(res);
|
2021-06-24 10:09:10 +02:00
|
|
|
res.end(
|
|
|
|
JSON.stringify({
|
|
|
|
success: true,
|
|
|
|
})
|
|
|
|
);
|
2020-11-13 18:00:22 +01:00
|
|
|
})();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//permit to login on application. Return token to connect on Websocket IO.
|
2021-06-24 10:09:10 +02:00
|
|
|
private anonymLogin() {
|
2020-11-13 18:00:22 +01:00
|
|
|
this.App.options("/anonymLogin", (res: HttpResponse, req: HttpRequest) => {
|
|
|
|
this.addCorsHeaders(res);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
this.App.post("/anonymLogin", (res: HttpResponse, req: HttpRequest) => {
|
2020-11-13 18:00:22 +01:00
|
|
|
res.onAborted(() => {
|
2021-06-24 10:09:10 +02:00
|
|
|
console.warn("Login request was aborted");
|
|
|
|
});
|
2020-11-13 18:00:22 +01:00
|
|
|
|
|
|
|
const userUuid = v4();
|
|
|
|
const authToken = jwtTokenManager.createJWTToken(userUuid);
|
|
|
|
res.writeStatus("200 OK");
|
|
|
|
this.addCorsHeaders(res);
|
2021-06-24 10:09:10 +02:00
|
|
|
res.end(
|
|
|
|
JSON.stringify({
|
|
|
|
authToken,
|
|
|
|
userUuid,
|
|
|
|
})
|
|
|
|
);
|
2020-11-13 18:00:22 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|