UNPKG

@labshare/services-auth

Version:

Loopback 4 plugin for resource scope-based HTTP route authz

52 lines 2.43 kB
"use strict"; // Copyright IBM Corp. 2017,2018. All Rights Reserved. // Node module: @loopback/authentication // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT Object.defineProperty(exports, "__esModule", { value: true }); exports.getAuthenticateMetadata = exports.authenticate = void 0; const context_1 = require("@loopback/context"); const keys_1 = require("../keys"); const metadata_1 = require("@loopback/metadata"); class AuthenticateClassDecoratorFactory extends metadata_1.ClassDecoratorFactory { } /** * Mark a controller method as requiring authenticated user. * * @param options Additional options to configure the authentication. * @param options.scope Resource Scopes required by the method */ function authenticate(options) { return function authenticateDecoratorForClassOrMethod(target, method, methodDescriptor) { let spec = options || {}; if (method && methodDescriptor) { // Method return context_1.MethodDecoratorFactory.createDecorator(keys_1.AUTHENTICATION_METADATA_KEY, spec, { decoratorName: '@authenticate' })(target, method, methodDescriptor); } if (typeof target === 'function' && !method && !methodDescriptor) { // Class return AuthenticateClassDecoratorFactory.createDecorator(keys_1.AUTHENTICATION_METADATA_CLASS_KEY, spec, { decoratorName: '@authenticate' })(target); } // Not on a class or method throw new Error('@intercept cannot be used on a property: ' + metadata_1.DecoratorFactory.getTargetName(target, method, methodDescriptor)); }; } exports.authenticate = authenticate; /** * Fetch authentication metadata stored by `@authenticate` decorator. * * @param controllerClass Target controller * @param methodName Target method */ function getAuthenticateMetadata(targetClass, methodName) { // First check method level let metadata = metadata_1.MetadataInspector.getMethodMetadata(keys_1.AUTHENTICATION_METADATA_KEY, targetClass.prototype, methodName); if (metadata) { return metadata; } // Check if the class level has `@authenticate` metadata = metadata_1.MetadataInspector.getClassMetadata(keys_1.AUTHENTICATION_METADATA_CLASS_KEY, targetClass); return metadata; } exports.getAuthenticateMetadata = getAuthenticateMetadata; //# sourceMappingURL=authenticate.decorator.js.map