UNPKG

@mbc-cqrs-serverless/core

Version:
37 lines 1.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserContext = void 0; exports.getUserContext = getUserContext; const constants_1 = require("../constants"); const invoke_1 = require("./invoke"); class UserContext { constructor(partial) { Object.assign(this, partial); } } exports.UserContext = UserContext; function getUserContext(ctx) { if ('getHandler' in ctx) { ctx = (0, invoke_1.extractInvokeContext)(ctx); } const claims = (0, invoke_1.getAuthorizerClaims)(ctx); const userId = claims.sub; const tenantCode = claims['custom:tenant'] || (ctx?.event?.headers || {})[constants_1.HEADER_TENANT_CODE]; // find tenantRole const roles = JSON.parse(claims['custom:roles'] || '[]').map((role) => ({ ...role, tenant: (role.tenant || '').toLowerCase() })); let tenantRole = ''; for (const { tenant, role } of roles) { if (tenant === '' || tenant === tenantCode) { tenantRole = role; if (tenant !== '') { break; } } } return { userId, tenantRole, tenantCode, }; } //# sourceMappingURL=user.js.map