1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 05:19:22 +00:00

Ready to check API tokens

This commit is contained in:
Pierre HUBERT 2019-11-22 08:50:15 +01:00
parent fe46644511
commit 340f571a76
7 changed files with 143 additions and 41 deletions

View File

@ -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.");
}
}

View File

@ -1,5 +1,6 @@
import { WelcomeController } from "./WelcomeController"; import { WelcomeController } from "./WelcomeController";
import { RequestHandler } from "../models/RequestHandler"; import { RequestHandler } from "../entities/RequestHandler";
import { AccountController } from "./AccountController";
/** /**
* Controllers routes * Controllers routes
@ -16,13 +17,14 @@ export interface Route {
type ?: RouteType, type ?: RouteType,
path: string, path: string,
cb: (req : RequestHandler) => void, cb: (req : RequestHandler) => void,
} }
export const Routes : Route[] = [ export const Routes : Route[] = [
// Welcome controller // 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
] ]

View File

@ -1,4 +1,4 @@
import { RequestHandler } from "../models/RequestHandler"; import { RequestHandler } from "../entities/RequestHandler";
/** /**
* Welcome controller * Welcome controller

View File

@ -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);
}
}

9
src/helpers/APIHelper.ts Normal file
View File

@ -0,0 +1,9 @@
/**
* API Helper
*
* @author Pierre HUBERT
*/
export class APIHelper {
}

View File

@ -2,7 +2,7 @@ import * as express from 'express';
import { ConfigurationHelper, conf } from "./helpers/ConfigHelper"; import { ConfigurationHelper, conf } from "./helpers/ConfigHelper";
import { DatabaseHelper } from "./helpers/DatabaseHelper"; import { DatabaseHelper } from "./helpers/DatabaseHelper";
import { Routes, RouteType } from './controllers/Routes'; import { Routes, RouteType } from './controllers/Routes';
import { RequestHandler } from './models/RequestHandler'; import { RequestHandler } from './entities/RequestHandler';
/** /**
* Main project script * Main project script
@ -23,11 +23,20 @@ async function init() {
// Start HTTP Server // Start HTTP Server
const app = express(); const app = express();
app.use(express.urlencoded());
// Process the list of routes // Process the list of routes
Routes.forEach(route => { Routes.forEach(route => {
const cb = (req : express.Request, res : express.Response)=> { // Callback is common to all requests type
route.cb(new RequestHandler(req, res)); 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) if(route.type == RouteType.GET)

View File

@ -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);
}
}