From ed9552b62b531166332b09182481a13ac3bdca00 Mon Sep 17 00:00:00 2001 From: arp Date: Thu, 17 Sep 2020 18:08:20 +0200 Subject: [PATCH 1/3] added a register route via token --- back/package.json | 1 + back/src/App.ts | 3 ++ back/src/Controller/AdminController.ts | 40 ++++++++++++++++++++++++++ back/src/Enum/EnvironmentVariable.ts | 8 ++++-- back/yarn.lock | 12 ++++++++ front/src/index.ts | 3 +- front/src/register.ts | 12 ++++++++ 7 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 back/src/Controller/AdminController.ts create mode 100644 front/src/register.ts diff --git a/back/package.json b/back/package.json index a20c876f..396991f2 100644 --- a/back/package.json +++ b/back/package.json @@ -41,6 +41,7 @@ "@types/jsonwebtoken": "^8.3.8", "@types/socket.io": "^2.1.4", "@types/uuidv4": "^5.0.0", + "axios": "^0.20.0", "body-parser": "^1.19.0", "express": "^4.17.1", "generic-type-guard": "^3.2.0", diff --git a/back/src/App.ts b/back/src/App.ts index e12afdb4..d1f7392f 100644 --- a/back/src/App.ts +++ b/back/src/App.ts @@ -7,6 +7,7 @@ import bodyParser = require('body-parser'); import * as http from "http"; import {MapController} from "./Controller/MapController"; import {PrometheusController} from "./Controller/PrometheusController"; +import {AdminController} from "./Controller/AdminController"; class App { public app: Application; @@ -15,6 +16,7 @@ class App { public authenticateController: AuthenticateController; public mapController: MapController; public prometheusController: PrometheusController; + private adminController: AdminController; constructor() { this.app = express(); @@ -32,6 +34,7 @@ class App { this.authenticateController = new AuthenticateController(this.app); this.mapController = new MapController(this.app); this.prometheusController = new PrometheusController(this.app, this.ioSocketController); + this.adminController = new AdminController(this.app); } // TODO add session user diff --git a/back/src/Controller/AdminController.ts b/back/src/Controller/AdminController.ts new file mode 100644 index 00000000..d6f1d23d --- /dev/null +++ b/back/src/Controller/AdminController.ts @@ -0,0 +1,40 @@ +import {Application, Request, Response} from "express"; +import {OK} from "http-status-codes"; +import {ADMIN_API_URL} from "../Enum/EnvironmentVariable"; +import Axios, {AxiosError} from "axios"; + +export class AdminController { + App : Application; + + constructor(App : Application) { + this.App = App; + this.getLoginUrlByToken(); + } + + + getLoginUrlByToken(){ + this.App.get("/register/:token", async (req: Request, res: Response) => { + if (!ADMIN_API_URL) { + return res.status(500).send('No admin backoffice set!'); + } + const token:string = req.params.token; + + //todo add ADMIN_API_TOKEN authorization + let response = null + try { + console.log(ADMIN_API_URL+'/api/login-url/'+token); + response = await Axios.get(ADMIN_API_URL+'/api/login-url/'+token) + } catch (e) { + console.log(e.message) + return res.status(500).send('An error happened'); + } + + const teamSlug = response.data.teamSlug; + const worldSlug = response.data.worldSlug; + const roomSlug = response.data.roomSlug; + return res.status(OK).send({ + loginUrl: '/@/'+teamSlug+'/'+worldSlug+'/'+roomSlug, + }); + }); + } +} diff --git a/back/src/Enum/EnvironmentVariable.ts b/back/src/Enum/EnvironmentVariable.ts index d8baaf89..c910bb66 100644 --- a/back/src/Enum/EnvironmentVariable.ts +++ b/back/src/Enum/EnvironmentVariable.ts @@ -3,11 +3,15 @@ const URL_ROOM_STARTED = "/Floor0/floor0.json"; const MINIMUM_DISTANCE = process.env.MINIMUM_DISTANCE ? Number(process.env.MINIMUM_DISTANCE) : 64; const GROUP_RADIUS = process.env.GROUP_RADIUS ? Number(process.env.GROUP_RADIUS) : 48; const ALLOW_ARTILLERY = process.env.ALLOW_ARTILLERY ? process.env.ALLOW_ARTILLERY == 'true' : false; +const ADMIN_API_URL = process.env.ADMIN_API_URL || null; +const ADMIN_API_TOKEN = process.env.ADMIN_API_TOKEN || null; export { SECRET_KEY, URL_ROOM_STARTED, MINIMUM_DISTANCE, + ADMIN_API_URL, + ADMIN_API_TOKEN, GROUP_RADIUS, - ALLOW_ARTILLERY -} + ALLOW_ARTILLERY, +} \ No newline at end of file diff --git a/back/yarn.lock b/back/yarn.lock index f660a5c8..c498ff43 100644 --- a/back/yarn.lock +++ b/back/yarn.lock @@ -244,6 +244,13 @@ async-limiter@~1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.1.tgz#dd379e94f0db8310b08291f9d64c3209766617fd" +axios@^0.20.0: + version "0.20.0" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.20.0.tgz#057ba30f04884694993a8cd07fa394cff11c50bd" + integrity sha512-ANA4rr2BDcmmAQLOKft2fufrtuvlqR+cXNNinUmvfeSNCOF98PZL+7M/v1zIdGo7OLjEA9J2gXJL+j4zGsl0bA== + dependencies: + follow-redirects "^1.10.0" + backo2@1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/backo2/-/backo2-1.0.2.tgz#31ab1ac8b129363463e35b3ebb69f4dfcfba7947" @@ -774,6 +781,11 @@ flatted@^2.0.0: version "2.0.2" resolved "https://registry.yarnpkg.com/flatted/-/flatted-2.0.2.tgz#4575b21e2bcee7434aa9be662f4b7b5f9c2b5138" +follow-redirects@^1.10.0: + version "1.13.0" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.0.tgz#b42e8d93a2a7eea5ed88633676d6597bc8e384db" + integrity sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA== + forwarded@~0.1.2: version "0.1.2" resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.1.2.tgz#98c23dab1175657b8c0573e8ceccd91b0ff18c84" diff --git a/front/src/index.ts b/front/src/index.ts index 2356bd0a..8be675fb 100644 --- a/front/src/index.ts +++ b/front/src/index.ts @@ -10,10 +10,11 @@ import {FourOFourScene} from "./Phaser/Reconnecting/FourOFourScene"; import WebGLRenderer = Phaser.Renderer.WebGL.WebGLRenderer; import {OutlinePipeline} from "./Phaser/Shaders/OutlinePipeline"; import {CustomizeScene} from "./Phaser/Login/CustomizeScene"; -import {HtmlUtils} from "./WebRtc/HtmlUtils"; import {CoWebsiteManager} from "./WebRtc/CoWebsiteManager"; +import {redirectIfToken} from "./register"; //CoWebsiteManager.loadCoWebsite('https://thecodingmachine.com'); +redirectIfToken(); // Load Jitsi if the environment variable is set. if (JITSI_URL) { diff --git a/front/src/register.ts b/front/src/register.ts new file mode 100644 index 00000000..0cc9dd13 --- /dev/null +++ b/front/src/register.ts @@ -0,0 +1,12 @@ +import Axios from "axios"; +import {API_URL} from "./Enum/EnvironmentVariable"; +declare let window:Window; + +export function redirectIfToken() { + const match = window.location.toString().match(/\/register\/(.+)/); + if (match) { + Axios.get(`${API_URL}/register/`+match[1]).then((res) => { + window.location = res.data.loginUrl; + }); + } +} \ No newline at end of file From 3a17795ad33bdbcc95d1b420ff179a560f60c2f5 Mon Sep 17 00:00:00 2001 From: arp Date: Fri, 18 Sep 2020 15:48:30 +0200 Subject: [PATCH 2/3] added basic token auth --- back/src/Controller/AdminController.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/back/src/Controller/AdminController.ts b/back/src/Controller/AdminController.ts index d6f1d23d..7c20498f 100644 --- a/back/src/Controller/AdminController.ts +++ b/back/src/Controller/AdminController.ts @@ -1,6 +1,6 @@ import {Application, Request, Response} from "express"; import {OK} from "http-status-codes"; -import {ADMIN_API_URL} from "../Enum/EnvironmentVariable"; +import {ADMIN_API_TOKEN, ADMIN_API_URL} from "../Enum/EnvironmentVariable"; import Axios, {AxiosError} from "axios"; export class AdminController { @@ -19,14 +19,12 @@ export class AdminController { } const token:string = req.params.token; - //todo add ADMIN_API_TOKEN authorization let response = null try { - console.log(ADMIN_API_URL+'/api/login-url/'+token); - response = await Axios.get(ADMIN_API_URL+'/api/login-url/'+token) + response = await Axios.get(ADMIN_API_URL+'/api/login-url/'+token, { headers: {"Authorization" : `${ADMIN_API_TOKEN}`} }) } catch (e) { console.log(e.message) - return res.status(500).send('An error happened'); + return res.status(e.status || 500).send('An error happened'); } const teamSlug = response.data.teamSlug; From a19f09bef2fdaa43041e3c604f70b1ed04906b2e Mon Sep 17 00:00:00 2001 From: arp Date: Fri, 18 Sep 2020 16:29:53 +0200 Subject: [PATCH 3/3] improve the register workflow --- back/src/Controller/AdminController.ts | 8 +++---- front/src/index.ts | 3 ++- front/src/register.ts | 31 ++++++++++++++++++++------ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/back/src/Controller/AdminController.ts b/back/src/Controller/AdminController.ts index 7c20498f..c4905a8a 100644 --- a/back/src/Controller/AdminController.ts +++ b/back/src/Controller/AdminController.ts @@ -1,7 +1,7 @@ import {Application, Request, Response} from "express"; import {OK} from "http-status-codes"; import {ADMIN_API_TOKEN, ADMIN_API_URL} from "../Enum/EnvironmentVariable"; -import Axios, {AxiosError} from "axios"; +import Axios from "axios"; export class AdminController { App : Application; @@ -27,12 +27,10 @@ export class AdminController { return res.status(e.status || 500).send('An error happened'); } - const teamSlug = response.data.teamSlug; + const organizationSlug = response.data.organizationSlug; const worldSlug = response.data.worldSlug; const roomSlug = response.data.roomSlug; - return res.status(OK).send({ - loginUrl: '/@/'+teamSlug+'/'+worldSlug+'/'+roomSlug, - }); + return res.status(OK).send({organizationSlug, worldSlug, roomSlug}); }); } } diff --git a/front/src/index.ts b/front/src/index.ts index 8be675fb..1f6b1d4a 100644 --- a/front/src/index.ts +++ b/front/src/index.ts @@ -14,7 +14,8 @@ import {CoWebsiteManager} from "./WebRtc/CoWebsiteManager"; import {redirectIfToken} from "./register"; //CoWebsiteManager.loadCoWebsite('https://thecodingmachine.com'); -redirectIfToken(); +let connectionData //todo: do something with this data +redirectIfToken().then(res => connectionData = res); // Load Jitsi if the environment variable is set. if (JITSI_URL) { diff --git a/front/src/register.ts b/front/src/register.ts index 0cc9dd13..98fe0d1e 100644 --- a/front/src/register.ts +++ b/front/src/register.ts @@ -1,12 +1,29 @@ import Axios from "axios"; import {API_URL} from "./Enum/EnvironmentVariable"; -declare let window:Window; +declare let history:History; -export function redirectIfToken() { - const match = window.location.toString().match(/\/register\/(.+)/); - if (match) { - Axios.get(`${API_URL}/register/`+match[1]).then((res) => { - window.location = res.data.loginUrl; - }); +//todo: better naming +export interface ConnexionData { + organizationSlug: string, + worldSlug: string, + roomSlug: string, +} + +export async function redirectIfToken(): Promise { + const match = /\/register\/(.+)/.exec(window.location.toString()); + if (!match) { + return null } + let res = null; + try { + res = await Axios.get(`${API_URL}/register/`+match[1]) + } catch (e) { + return null; + } + const organizationSlug = res.data.organizationSlug; + const worldSlug = res.data.worldSlug; + const roomSlug = res.data.roomSlug; + const connexionUrl = '/@/'+organizationSlug+'/'+worldSlug+'/'+roomSlug; + history.pushState({}, '', connexionUrl); + return {organizationSlug, worldSlug, roomSlug}; } \ No newline at end of file