UNPKG

@webundsoehne/nestjs-auth0-guard

Version:

NestJS Auth0 Guard

50 lines 2.29 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); const common_1 = require("@nestjs/common"); const moment = require("moment"); const auth0_constant_1 = require("./auth0.constant"); let Auth0Service = class Auth0Service { verifyPayload(payload) { if (!payload || !payload.sub || !payload.exp) { return false; } return moment() .utc() .isSameOrBefore(moment(payload.exp, 'X')); } verifyPermissions({ payload: { sub, permissions: userPermissions }, auth0: { permissions: requiredPermissions }, id }) { if (!Array.isArray(requiredPermissions) || requiredPermissions.length <= 0) { return true; } const isCurrent = sub === id; const verifyPermission = (permission) => { if (permission.includes(auth0_constant_1.AUTH0_CURRENT_PART) && !isCurrent) { return false; } return userPermissions.includes(permission); }; return requiredPermissions.every((permissions) => permissions.some(verifyPermission)); } prepareUserPayload({ sub, exp, permissions, ...others }, namespace) { const customProperties = Object.keys(others) .filter((name) => name.includes(namespace)) .reduce((props, name) => ({ ...props, [name.replace(namespace, '')]: others[name] }), {}); return { ...customProperties, userId: sub, expiresAt: moment(exp, 'X').toDate(), permissions }; } }; Auth0Service = __decorate([ common_1.Injectable() ], Auth0Service); exports.Auth0Service = Auth0Service; //# sourceMappingURL=auth0.service.js.map