Remove middleware to secure access API.
authorgparant <g.parant@thecodingmachine.com>
Sun, 10 May 2020 15:31:27 +0000 (17:31 +0200)
committergparant <g.parant@thecodingmachine.com>
Sun, 10 May 2020 15:31:27 +0000 (17:31 +0200)
back/src/App.ts
back/src/Controller/MapController.ts
back/src/Middleware/AuthenticateMiddleware.ts [deleted file]
front/src/Connexion.ts
front/src/Phaser/Login/LogincScene.ts

index 27006d8c3950d8d967fb1d352b9faf102684b74d..06e08ca6c679b4a35c4b8535b846f7423fea04a0 100644 (file)
@@ -6,27 +6,28 @@ import {Application, Request, Response} from 'express';
 import bodyParser = require('body-parser');
 import * as http from "http";
 import {MapController} from "./Controller/MapController";
-import {AuthenticateMiddleware} from "./Middleware/AuthenticateMiddleware";
 
 class App {
     public app: Application;
     public server: http.Server;
     public ioSocketController: IoSocketController;
     public authenticateController: AuthenticateController;
-    //public AuthenticateMiddleware: AuthenticateMiddleware;
     public mapController: MapController;
 
     constructor() {
         this.app = express();
 
         //config server http
-        this.config();
         this.server = http.createServer(this.app);
 
+        this.config();
+        this.crossOrigin();
+
+        //TODO add middleware with access token to secure api
+
         //create socket controllers
         this.ioSocketController = new IoSocketController(this.server);
         this.authenticateController = new AuthenticateController(this.app);
-        //this.AuthenticateMiddleware = new AuthenticateMiddleware(this.app);
         this.mapController = new MapController(this.app);
     }
 
@@ -34,9 +35,15 @@ class App {
     private config(): void {
         this.app.use(bodyParser.json());
         this.app.use(bodyParser.urlencoded({extended: false}));
+    }
+
+    private crossOrigin(){
         this.app.use((req: Request, res: Response, next) => {
-            res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
-            res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
+            res.setHeader("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
+            // Request methods you wish to allow
+            res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
+            // Request headers you wish to allow
+            res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
             next();
         });
     }
index 2e35d5acc29018c4c43455c0991533998396fe5d..5a19644b994fbd49c81fecd06239acb36f93c797 100644 (file)
@@ -1,5 +1,4 @@
 import express from "express";
-import path from "path";
 import {Application, Request, Response} from "express";
 import {OK} from "http-status-codes";
 import {ROOM_STARTED, ROOMS, URL_ROOM_STARTED} from "../Enum/EnvironmentVariable";
@@ -9,7 +8,7 @@ export class MapController {
 
     constructor(App: Application) {
         this.App = App;
-        this.getMpas();
+        this.getMaps();
         this.assetMaps();
     }
 
diff --git a/back/src/Middleware/AuthenticateMiddleware.ts b/back/src/Middleware/AuthenticateMiddleware.ts
deleted file mode 100644 (file)
index e482eaa..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-import {Application, Request, Response} from "express";
-import {BAD_REQUEST} from "http-status-codes";
-import Jwt, {JsonWebTokenError} from "jsonwebtoken";
-import {SECRET_KEY} from "../Enum/EnvironmentVariable";
-
-export class AuthenticateMiddleware{
-    App: Application;
-
-    constructor(App: Application) {
-        this.App = App;
-        this.tokenVerification();
-    }
-
-    tokenVerification() {
-        this.App.use((req: Request, res: Response, next: any) => {
-            let token = req.header("Access-Token");
-            if (!token) {
-                return res.status(BAD_REQUEST).send({
-                    message: "you must to be connected to get the map"
-                });
-            }
-            return Jwt.verify(token, SECRET_KEY, (err: JsonWebTokenError, tokenDecoded: object) => {
-                if (err) {
-                    return res.status(BAD_REQUEST).send({
-                        message: "you must to be connected to get the map"
-                    });
-                }
-                return next();
-            });
-        })
-    }
-}
index d1c3938d76a968510897e2abe48313a41a57b80d..e7d07efe678d5e322e2e6c614b51d348f6a3a26e 100644 (file)
@@ -2,7 +2,7 @@ import {GameManager} from "./Phaser/Game/GameManager";
 
 const SocketIo = require('socket.io-client');
 import Axios from "axios";
-import {API_URL, ROOM} from "./Enum/EnvironmentVariable";
+import {API_URL} from "./Enum/EnvironmentVariable";
 
 enum EventMessage{
     WEBRTC_SIGNAL = "webrtc-signal",
@@ -211,14 +211,16 @@ export class Connexion implements ConnexionInterface {
                 throw err;
             });
     }
-    
-    loadMaps() : Promise<any>{
-        return Axios.get(`${API_URL}/maps`).then((res) => {
-            return res.data;
-        }).catch((err) => {
-            console.error(err);
-            throw err;
-        });
+
+    //TODO add middleware with access token to secure api
+    loadMaps() : Promise<any> {
+        return Axios.get(`${API_URL}/maps`)
+            .then((res) => {
+                return res.data;
+            }).catch((err) => {
+                console.error(err);
+                throw err;
+            });
     }
 
     /**
index dc577419329b2f39f1483ca1dccd64c9beddcf51..b747fdf697d8aff489d88c5ddfb96d528ebe79f1 100644 (file)
@@ -93,17 +93,19 @@ export class LogincScene extends Phaser.Scene implements GameSceneInterface {
     }
 
     private async login(name: string) {
-        Promise.all([
-            gameManager.connect(name, this.selectedPlayer.texture.key),
-            gameManager.loadMaps()
-        ]).then((data) => {
-            if (!data) {
-                return;
-            }
-            let scene: any = data[1];
-            let game = new GameScene(scene.mapStart.key, `${API_URL}${scene.mapStart.url}`);
-            this.scene.add(scene.mapStart.key, game, false);
-            this.scene.start(scene.mapStart.key);
+        return gameManager.connect(name, this.selectedPlayer.texture.key).then(() => {
+            return gameManager.loadMaps().then((scene : any) => {
+                if (!scene) {
+                    return;
+                }
+                let game = new GameScene(scene.mapStart.key, `${API_URL}${scene.mapStart.url}`);
+                this.scene.add(scene.mapStart.key, game, false);
+                this.scene.start(scene.mapStart.key);
+                return scene;
+            }).catch((err) => {
+                console.error(err);
+                throw err;
+            });
         }).catch((err) => {
             console.error(err);
             throw err;