2019-11-17 16:11:41 -05:00
|
|
|
/* eslint-disable consistent-return */
|
|
|
|
import { Request, Response, NextFunction, Router as router } from 'express';
|
2020-06-29 02:50:26 -04:00
|
|
|
import { Server } from '.';
|
2019-11-16 19:24:43 -05:00
|
|
|
|
|
|
|
export default class Route {
|
|
|
|
public server: Server;
|
|
|
|
|
|
|
|
public router: router;
|
|
|
|
|
2019-11-19 18:26:59 -05:00
|
|
|
public conf: { path: string, deprecated?: boolean, maintenance?: boolean };
|
2019-11-16 19:24:43 -05:00
|
|
|
|
2020-01-20 23:41:24 -05:00
|
|
|
protected constructor(server: Server, conf: { path: string, deprecated?: boolean, maintenance?: boolean }) {
|
2020-06-29 17:31:18 -04:00
|
|
|
this.conf = {
|
|
|
|
path: null,
|
|
|
|
deprecated: false,
|
|
|
|
maintenance: false,
|
|
|
|
};
|
2019-11-16 19:24:43 -05:00
|
|
|
this.server = server;
|
|
|
|
this.router = router();
|
2019-11-16 20:04:00 -05:00
|
|
|
this.conf = conf;
|
2019-11-16 19:24:43 -05:00
|
|
|
}
|
|
|
|
|
2019-11-16 19:51:29 -05:00
|
|
|
public bind() {}
|
|
|
|
|
2019-11-17 20:52:54 -05:00
|
|
|
public deprecated(): void {
|
2019-11-17 16:11:41 -05:00
|
|
|
this.router.all('*', (_req, res) => {
|
2019-11-17 18:02:40 -05:00
|
|
|
res.status(501).json({ code: this.constants.codes.DEPRECATED, message: this.constants.messages.DEPRECATED });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-17 20:52:54 -05:00
|
|
|
public maintenance(): void {
|
2019-11-17 18:13:09 -05:00
|
|
|
this.router.all('*', (_req, res) => {
|
2019-11-17 18:02:40 -05:00
|
|
|
res.status(503).json({ code: this.constants.codes.MAINTENANCE_OR_UNAVAILABLE, message: this.constants.messages.MAINTENANCE_OR_UNAVAILABLE });
|
2019-11-17 16:11:41 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:57:22 -04:00
|
|
|
public init(): void {
|
|
|
|
this.router.all('*', (req, res, next) => {
|
|
|
|
this.server.client.signale.log(`'${req.method}' request from '${req.ip}' to '${req.hostname}${req.path}'.`);
|
|
|
|
if (this.conf.maintenance === true) res.status(503).json({ code: this.constants.codes.MAINTENANCE_OR_UNAVAILABLE, message: this.constants.messages.MAINTENANCE_OR_UNAVAILABLE });
|
|
|
|
else if (this.conf.deprecated === true) res.status(501).json({ code: this.constants.codes.DEPRECATED, message: this.constants.messages.DEPRECATED });
|
|
|
|
else next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-17 20:52:54 -05:00
|
|
|
/**
|
|
|
|
* This function checks for the presense of a Bearer token with Security.extractBearer(),
|
|
|
|
* then it will attempt to validate it with Security.checkBearer().
|
|
|
|
* If it can authenticate the request, it'll add a custom property on Request called
|
|
|
|
* `account`, which will hold an the bearer token's account owner. The account is of the
|
|
|
|
* type `AccountInterface`.
|
|
|
|
* @param req The Request object from Express.
|
|
|
|
* @param res The Response object from Express.
|
|
|
|
* @param next The NextFunction from Express.
|
|
|
|
* @example Security.authorize(req, res, next);
|
|
|
|
*/
|
2019-11-17 16:11:41 -05:00
|
|
|
public async authorize(req: Request, res: Response, next: NextFunction) {
|
|
|
|
const account = await this.server.security.checkBearer(this.server.security.extractBearer(req));
|
2019-11-17 18:02:40 -05:00
|
|
|
if (!account) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED });
|
2019-11-17 16:11:41 -05:00
|
|
|
Object.defineProperty(req, 'account', { value: account, writable: true, enumerable: true, configurable: true });
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
2019-11-17 20:52:54 -05:00
|
|
|
/**
|
|
|
|
* This function calls Util.handleError() internally, however it also sends a generic
|
|
|
|
* response to the user.
|
|
|
|
* @param error The Error object.
|
|
|
|
* @param res The Response object from Express.
|
|
|
|
*/
|
|
|
|
public handleError(error: Error, res: Response): void {
|
2019-11-17 16:11:41 -05:00
|
|
|
this.server.client.util.handleError(error);
|
2019-11-17 18:02:40 -05:00
|
|
|
res.status(500).json({ code: this.constants.codes.SERVER_ERROR, message: this.constants.messages.SERVER_ERROR });
|
2019-11-17 16:11:41 -05:00
|
|
|
}
|
|
|
|
|
2019-11-16 19:24:43 -05:00
|
|
|
get constants() {
|
|
|
|
return {
|
|
|
|
codes: {
|
2020-01-20 23:41:24 -05:00
|
|
|
/**
|
|
|
|
* SUCCESS 100
|
|
|
|
* Used if the request was processed successfully.
|
|
|
|
*/
|
2019-11-16 19:24:43 -05:00
|
|
|
SUCCESS: 100,
|
2020-01-20 23:41:24 -05:00
|
|
|
/**
|
|
|
|
* UNAUTHORIZED 101
|
|
|
|
* Used if the client calling the request couldn't be correctly authenticated.
|
|
|
|
*/
|
2019-11-16 19:24:43 -05:00
|
|
|
UNAUTHORIZED: 101,
|
2020-01-20 23:41:24 -05:00
|
|
|
/**
|
|
|
|
* PERMISSION DENIED 103
|
|
|
|
* Used if the client calling the request doesn't have access to the resource specified.
|
|
|
|
*/
|
|
|
|
PERMISSION_DENIED: 103,
|
|
|
|
/**
|
|
|
|
* NOT FOUND 104
|
|
|
|
* Used if the resource the client requested doesn't exist.
|
|
|
|
*/
|
2019-11-16 19:24:43 -05:00
|
|
|
NOT_FOUND: 104,
|
2020-01-20 23:41:24 -05:00
|
|
|
/**
|
|
|
|
* ACCOUNT NOT FOUND 1041
|
|
|
|
* Used if the account specified by the client couldn't be found.
|
|
|
|
*/
|
2019-11-16 19:24:43 -05:00
|
|
|
ACCOUNT_NOT_FOUND: 1041,
|
2020-01-20 23:41:24 -05:00
|
|
|
/**
|
|
|
|
* CLIENT ERROR 1044
|
|
|
|
* Used in cases of user error. Examples are incorrect parameters, incorrect headers, or an invalid request.
|
|
|
|
*/
|
2019-11-16 19:24:43 -05:00
|
|
|
CLIENT_ERROR: 1044,
|
2020-01-20 23:41:24 -05:00
|
|
|
/**
|
|
|
|
* SERVER ERROR 105
|
|
|
|
* Used in cases of an internal error that caused the bind() function to throw.
|
|
|
|
*/
|
2019-11-16 19:24:43 -05:00
|
|
|
SERVER_ERROR: 105,
|
2020-01-20 23:41:24 -05:00
|
|
|
/**
|
|
|
|
* DEPRECATED 1051
|
|
|
|
* Returned back to the user if the resource requested is deprecated.
|
|
|
|
*/
|
2019-11-17 16:11:41 -05:00
|
|
|
DEPRECATED: 1051,
|
2020-01-20 23:41:24 -05:00
|
|
|
/**
|
|
|
|
* MAINTENANCE OR UNAVAILABLE 1053
|
|
|
|
* Used if the resource requested is currently in maintenance, not finished, or temporarily disabled.
|
|
|
|
*/
|
2019-11-17 18:02:40 -05:00
|
|
|
MAINTENANCE_OR_UNAVAILABLE: 1053,
|
|
|
|
},
|
|
|
|
messages: {
|
2020-01-20 23:41:24 -05:00
|
|
|
/**
|
|
|
|
* The credentials you supplied are invalid.
|
|
|
|
*/
|
2019-11-17 18:02:40 -05:00
|
|
|
UNAUTHORIZED: ['CREDENTIALS_INVALID', 'The credentials you supplied are invalid.'],
|
2020-01-20 23:41:24 -05:00
|
|
|
/**
|
|
|
|
* You do not have valid credentials to access this resource.
|
|
|
|
*/
|
2019-11-17 18:02:40 -05:00
|
|
|
PERMISSION_DENIED: ['PERMISSION_DENIED', 'You do not have valid credentials to access this resource.'],
|
2020-01-20 23:41:24 -05:00
|
|
|
/**
|
|
|
|
* The resource you requested cannot be located.
|
|
|
|
*/
|
2019-11-17 18:02:40 -05:00
|
|
|
NOT_FOUND: ['NOT_FOUND', 'The resource you requested cannot be located.'],
|
2020-01-20 23:41:24 -05:00
|
|
|
/**
|
|
|
|
* An internal error has occurred, Engineers have been notified.
|
|
|
|
*/
|
2019-11-17 18:02:40 -05:00
|
|
|
SERVER_ERROR: ['INTERNAL_ERROR', 'An internal error has occurred, Engineers have been notified.'],
|
2020-01-20 23:41:24 -05:00
|
|
|
/**
|
|
|
|
* The endpoint or resource you\'re trying to access has been deprecated.
|
|
|
|
*/
|
2019-11-17 18:02:40 -05:00
|
|
|
DEPRECATED: ['ENDPOINT_OR_RESOURCE_DEPRECATED', 'The endpoint or resource you\'re trying to access has been deprecated.'],
|
2020-01-20 23:41:24 -05:00
|
|
|
/**
|
|
|
|
* The endpoint or resource you\'re trying to access is either in maintenance or is not available.
|
|
|
|
*/
|
2019-11-17 18:02:40 -05:00
|
|
|
MAINTENANCE_OR_UNAVAILABLE: ['SERVICE_UNAVAILABLE', 'The endpoint or resource you\'re trying to access is either in maintenance or is not available.'],
|
2019-11-16 19:24:43 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|