UNPKG

@bestcodetools/api-node-base

Version:

BestCodeTools Node Base for Backend API

52 lines 2.02 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const command_1 = __importDefault(require("../../../../@types/interfaces/command")); const jwt_1 = __importDefault(require("../../../../@types/interfaces/jwt")); const __1 = __importDefault(require("..")); const base_1 = __importDefault(require("../../exceptions/base")); class HttpAuthenticatedHandler extends __1.default { constructor(request, response, next, command, additional) { super(request, response, next, command); Object.assign(this, { ...additional }); } buildInput() { const baseInput = super.buildInput(); const xAppToken = baseInput.headers['x-app-token']; if (!xAppToken) { throw new base_1.default('missing.authentication.token', 401); } if (!this.jwt) { throw new base_1.default('missing.token.validator', 500); } if (!this.authSchema) { throw new base_1.default('missing.authentication.validation.schema', 500); } const authorizationInfo = this.jwt .verify(xAppToken); const schemaValidationResult = this.authSchema .validate(authorizationInfo); if (schemaValidationResult.error) { throw new base_1.default('invalid.token', 403); } return { ...baseInput, authorizationInfo }; } async handle() { try { const payload = this.buildInput(); await this.command.execute(payload); } catch (error) { if (error instanceof base_1.default) { this.send(error.statusCode, { errors: { '*': error.message } }); } else { this.send(500, { errors: { '*': error } }); } } } } exports.default = HttpAuthenticatedHandler; //# sourceMappingURL=index.js.map