From 340f571a760f986cca7619354779e36f7dbbcfc4 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 22 Nov 2019 08:50:15 +0100 Subject: [PATCH] Ready to check API tokens --- src/controllers/AccountController.ts | 21 +++++++ src/controllers/Routes.ts | 10 +-- src/controllers/WelcomeController.ts | 2 +- src/entities/RequestHandler.ts | 94 ++++++++++++++++++++++++++++ src/helpers/APIHelper.ts | 9 +++ src/main.ts | 15 ++++- src/models/RequestHandler.ts | 33 ---------- 7 files changed, 143 insertions(+), 41 deletions(-) create mode 100644 src/controllers/AccountController.ts create mode 100644 src/entities/RequestHandler.ts create mode 100644 src/helpers/APIHelper.ts delete mode 100644 src/models/RequestHandler.ts diff --git a/src/controllers/AccountController.ts b/src/controllers/AccountController.ts new file mode 100644 index 0000000..e4affd2 --- /dev/null +++ b/src/controllers/AccountController.ts @@ -0,0 +1,21 @@ +import { RequestHandler } from "../entities/RequestHandler"; + +/** + * Account controller + * + * @author Pierre HUBERT + */ + +export class AccountController { + + /** + * Attempt to login user + * + * @param handler + */ + public static LoginUser(handler: RequestHandler) { + + handler.success("Successful operation."); + } + +} \ No newline at end of file diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 5af7d17..c5acadb 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -1,5 +1,6 @@ import { WelcomeController } from "./WelcomeController"; -import { RequestHandler } from "../models/RequestHandler"; +import { RequestHandler } from "../entities/RequestHandler"; +import { AccountController } from "./AccountController"; /** * Controllers routes @@ -16,13 +17,14 @@ export interface Route { type ?: RouteType, path: string, cb: (req : RequestHandler) => void, - } export const Routes : Route[] = [ // Welcome controller - {type: RouteType.GET, path: "/", cb: WelcomeController.HomeMessage} - + {type: RouteType.GET, path: "/", cb: WelcomeController.HomeMessage}, + // Account controller + {path: "/account/login", cb: AccountController.LoginUser}, + {path: "/user/connectUSER", cb: AccountController.LoginUser}, // Legacy ] \ No newline at end of file diff --git a/src/controllers/WelcomeController.ts b/src/controllers/WelcomeController.ts index 9a40c91..a1619e0 100644 --- a/src/controllers/WelcomeController.ts +++ b/src/controllers/WelcomeController.ts @@ -1,4 +1,4 @@ -import { RequestHandler } from "../models/RequestHandler"; +import { RequestHandler } from "../entities/RequestHandler"; /** * Welcome controller diff --git a/src/entities/RequestHandler.ts b/src/entities/RequestHandler.ts new file mode 100644 index 0000000..f74b592 --- /dev/null +++ b/src/entities/RequestHandler.ts @@ -0,0 +1,94 @@ +import { Response, Request } from "express"; + +/** + * Response to a request + * + * @author Pierre HUBERT + */ + +export class RequestHandler { + public constructor(private req : Request, private response : Response) {} + + /** + * Get a parameter included in the post request + * + * @param name Name of the parameter to get + */ + private getPostParam(name : string) : any { + return this.req.body[name]; + } + + /** + * Get a String from the request + * + * @param name The name of the string to get + * @param minLength Minimal required size of the string + * @param required If set to true (true by default), an error will + * be thrown if the string is not included in the request + */ + public getString(name : string, minLength : number = 1, required : boolean = true) : string { + const param = this.getPostParam(name); + + // Check if parameter was not found + if(param == undefined) { + if(required) + this.error(400, "Could not find required string: '"+name+"'"); + return ""; + } + + if(param.length < minLength) + this.error(400, "Parameter "+name+" is too short!"); + + return ""; + } + + + /** + * Validate API tokens + * + * @throws If the tokens could not be validated + */ + public async checkAPITokens() { + + // Extract API name & token from request + const apiName = this.getString("serviceName"); + const apiToken = this.getString("serviceToken"); + + // Validate the token + } + + /** + * Output an error code + * + * @param code HTTP Status code + * @param message The message to send + */ + public error(code : number, message : string) { + this.response.status(code).send({ + code: code, + message: message + }); + + throw Error("Could not complete request!"); + } + + /** + * Return a successful operation + * + * @param message Message associated to success + */ + public success(message: string) { + this.response.send({ + success: message + }); + } + + /** + * Send some data to the server + * + * @param data The data to send back to the server + */ + public send(data: any) { + this.response.send(data); + } +} \ No newline at end of file diff --git a/src/helpers/APIHelper.ts b/src/helpers/APIHelper.ts new file mode 100644 index 0000000..9e1568b --- /dev/null +++ b/src/helpers/APIHelper.ts @@ -0,0 +1,9 @@ +/** + * API Helper + * + * @author Pierre HUBERT + */ + +export class APIHelper { + +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 4489e24..15e0b29 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,7 +2,7 @@ import * as express from 'express'; import { ConfigurationHelper, conf } from "./helpers/ConfigHelper"; import { DatabaseHelper } from "./helpers/DatabaseHelper"; import { Routes, RouteType } from './controllers/Routes'; -import { RequestHandler } from './models/RequestHandler'; +import { RequestHandler } from './entities/RequestHandler'; /** * Main project script @@ -23,11 +23,20 @@ async function init() { // Start HTTP Server const app = express(); + app.use(express.urlencoded()); + // Process the list of routes Routes.forEach(route => { - const cb = (req : express.Request, res : express.Response)=> { - route.cb(new RequestHandler(req, res)); + // Callback is common to all requests type + const cb = async (req : express.Request, res : express.Response)=> { + const handler = new RequestHandler(req, res); + + // Check API tokens + await handler.checkAPITokens(); + + + route.cb(handler); }; if(route.type == RouteType.GET) diff --git a/src/models/RequestHandler.ts b/src/models/RequestHandler.ts deleted file mode 100644 index f48fe5c..0000000 --- a/src/models/RequestHandler.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { Response, Request } from "express"; - -/** - * Response to a request - * - * @author Pierre HUBERT - */ - -export class RequestHandler { - public constructor(private req : Request, private response : Response) {} - - /** - * Output an error code - * - * @param code HTTP Status code - * @param message The message to send - */ - public error(code : number, message : string) { - this.response.status(code).send({ - code: code, - message: message - }) - } - - /** - * Send some data to the server - * - * @param data The data to send back to the server - */ - public send(data: any) { - this.response.send(data); - } -} \ No newline at end of file