UNPKG

@breautek/storm

Version:

Object-Oriented REST API framework

67 lines (64 loc) 2.75 kB
"use strict"; /* Copyright 2017-2021 Norman Breau Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.BackendAuthenticationMiddleware = void 0; const StatusCode_1 = require("./StatusCode"); const ResponseData_1 = require("./ResponseData"); const instance_1 = require("./instance"); const TAG = 'BackendAuthenticationMiddleware'; /** * A base authentication strategy that handles 90% of the authentication process. * This will verify that the token hasn't been manipulated or tainted. * The authenticate API must be implemented by subclasses to further validate the token data * for their specific use cases. */ class BackendAuthenticationMiddleware { constructor() { this.$logger = (0, instance_1.getInstance)().getLogger(); } /** * * @param request * @param response * @param options Arbituary object containing any relevant information used for authentication. */ execute(request, options) { let config = (0, instance_1.getInstance)().getConfig(); let backendAuthHeader = config.backend_authentication_header; let backend = request.getHeader(backendAuthHeader); if (backend) { if (config.backend_authentication_secret === null) { this.$logger.warn(TAG, 'Backend secret not implemented.'); return Promise.reject(new ResponseData_1.ResponseData(StatusCode_1.StatusCode.INTERNAL_ERROR)); } if (backend === config.backend_authentication_secret) { return Promise.resolve(null); } else { return Promise.reject(new ResponseData_1.ResponseData(StatusCode_1.StatusCode.ERR_UNAUTHORIZED, { code: 0, reason: 'Missing secret' })); } } else { return Promise.reject(new ResponseData_1.ResponseData(StatusCode_1.StatusCode.ERR_UNAUTHORIZED, { code: 0, reason: 'Missing secret' })); } } } exports.BackendAuthenticationMiddleware = BackendAuthenticationMiddleware; //# sourceMappingURL=BackendAuthenticationMiddleware.js.map