@mdf.js/openc2
Version:
MMS - API - Observability
94 lines • 3.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthZ = void 0;
const tslib_1 = require("tslib");
/**
* Copyright 2024 Mytra Control S.L. All rights reserved.
*
* Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
* or at https://opensource.org/licenses/MIT.
*/
const crash_1 = require("@mdf.js/crash");
const jsonwebtoken_1 = tslib_1.__importDefault(require("jsonwebtoken"));
const uuid_1 = require("uuid");
const __1 = require("..");
const DEFAULT_CONFIG_JWT_TOKEN_SECRET = (0, uuid_1.v4)();
const DEFAULT_CONFIG_JWT_TOKEN_ALGORITHMS = ['HS256'];
const DEFAULT_CONFIG_JWT_ON_AUTHORIZATION = () => Promise.resolve();
/**
* Check the token of the request
* @param options - authorization options
* @returns
*/
function authZ(options = {}) {
return (socket, next) => {
var _a, _b, _c;
const token = socket.handshake.auth['token'];
const secret = (_a = options.secret) !== null && _a !== void 0 ? _a : DEFAULT_CONFIG_JWT_TOKEN_SECRET;
const algorithms = (_b = options.algorithms) !== null && _b !== void 0 ? _b : DEFAULT_CONFIG_JWT_TOKEN_ALGORITHMS;
const onAuthorization = (_c = options.onAuthorization) !== null && _c !== void 0 ? _c : DEFAULT_CONFIG_JWT_ON_AUTHORIZATION;
const requestId = (0, uuid_1.v4)();
hasValidAuthenticationInformation(token, requestId)
.then(reportedToken => verify(reportedToken, { secret, algorithms }, requestId))
.then(onAuthorization)
.then(() => next())
.catch(error => next((0, __1.transformError)(error)));
};
}
/**
* Check if the token is present and an string
* @param token - token to checked
* @param uuid - request identifier
* @returns
*/
function hasValidAuthenticationInformation(token, uuid) {
return new Promise((resolve, reject) => {
if (!token) {
reject(crash_1.BoomHelpers.badRequest(`No present authorization information`, uuid));
}
else if (typeof token !== 'string') {
reject(crash_1.BoomHelpers.badRequest(`Malformed authorization information`, uuid));
}
else {
resolve(token);
}
});
}
/**
* Check if the token is a valid token
* @param token - token to be verified
* @param options - authorization options
* @param uuid - request identifier
* @returns
*/
function verify(token, options, uuid) {
return new Promise((resolve, reject) => {
jsonwebtoken_1.default.verify(token, options.secret, { algorithms: options.algorithms }, (error, decoded) => {
if (error) {
reject(crash_1.BoomHelpers.unauthorized(`No valid token: ${error.message}`, uuid));
}
else if (!decoded) {
reject(new crash_1.Crash(`Error verifying the JWT token`, uuid));
}
else if (typeof decoded === 'string') {
reject(crash_1.BoomHelpers.badRequest(`Malformed request, malformed authorization token`, uuid));
}
else {
resolve(decoded);
}
});
});
}
/** AuthZ */
class AuthZ {
/**
* Perform the authorization based on jwt token for Socket.IO
* @param options - authorization options
* @returns
*/
static handler(options) {
return authZ(options);
}
}
exports.AuthZ = AuthZ;
//# sourceMappingURL=authz.js.map