UNPKG

vulcain-corejs

Version:
70 lines (68 loc) 3.68 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments)).next()); }); }; const annotations_1 = require("../../di/annotations"); const system_1 = require("../../configurations/globals/system"); const abstractExpressAuthentication_1 = require("./abstractExpressAuthentication"); class ExpressAuthentication extends abstractExpressAuthentication_1.AbstractExpressAuthentication { constructor() { super(); this.addOrReplaceStrategy('bearer', this.bearerAuthentication); this.addOrReplaceStrategy('apikey', this.apiKeyAuthentication); } bearerAuthentication(ctx, accessToken) { return __awaiter(this, void 0, void 0, function* () { try { let tokens = ctx.container.get(annotations_1.DefaultServiceNames.TokenService); let token = yield tokens.verifyTokenAsync({ token: accessToken, tenant: ctx.tenant }); // No token found if (!token) { system_1.System.log.info(ctx, "Bearer authentication: Invalid jwtToken : " + accessToken); return null; } token.user.tenant = token.user.tenant || token.tenantId; token.user.scopes = token.scopes; token.user.data = token.user.data || token.data; token.user.bearer = accessToken; // Must be set by the bearer authentication return token.user; // Return the current user with its scopes and tenant } catch (err) { system_1.System.log.error(ctx, err, "Bearer authentication: Error with jwtToken " + accessToken); return null; } }); } apiKeyAuthentication(ctx, accessToken) { return __awaiter(this, void 0, void 0, function* () { try { let apiKeys = ctx.container.get(annotations_1.DefaultServiceNames.ApiKeyService, true); if (!apiKeys) { system_1.System.log.info(ctx, `ApiKey authentication ERROR: Apikey is not enabled. Use enableApiKeyAuthentication in application.ts.`); return null; } let token = yield apiKeys.verifyTokenAsync({ token: accessToken, tenant: ctx.tenant }); // No token found if (!token) { system_1.System.log.info(ctx, `ApiKey authentication: Invalid apiKey ${accessToken} for tenant ${ctx.tenant}`); return null; } token.user.data = token.user.data || token.data; token.user.scopes = Array.isArray(token.token.scopes) ? token.token.scopes : [token.token.scopes]; // token.user.tenant = token.token.tenant; TODO return token.user; } catch (err) { system_1.System.log.error(ctx, err, `ApiKey authentication: Error with apiKey ${accessToken} for tenant ${ctx.tenant}`); throw err; } }); } } exports.ExpressAuthentication = ExpressAuthentication; //# sourceMappingURL=expressAuthentication.js.map