import { ClassDecoratorFactory , Constructor , DecoratorFactory , MetadataInspector , MethodDecoratorFactory , } from '@loopback/core' ; import { AUTHENTICATION_METADATA_CLASS_KEY , AUTHENTICATION_METADATA_KEY , AUTHENTICATION_METADATA_METHOD_KEY , } from '../keys' ; import { AuthenticationMetadata } from '../types' ; class AuthenticateClassDecoratorFactory extends ClassDecoratorFactory < AuthenticationMetadata [ ] > { } param strategies export function authenticate ( ... strategies ) { return function authenticateDecoratorForClassOrMethod ( target , method ? , methodDescriptor ? ) { const specs = [ ] ; for ( const strategy of strategies ) { if ( typeof strategy === 'object' ) { specs . push ( strategy ) ; } else { specs . push ( { strategy : strategy } ) ; } } if ( method && methodDescriptor ) { return MethodDecoratorFactory . createDecorator < AuthenticationMetadata [ ] > ( AUTHENTICATION_METADATA_KEY , specs , { decoratorName : '@authenticate' } ) ( target , method , methodDescriptor ) ; } if ( typeof target === 'function' && ! method && ! methodDescriptor ) { return AuthenticateClassDecoratorFactory . createDecorator < AuthenticationMetadata [ ] > ( AUTHENTICATION_METADATA_CLASS_KEY , specs , { decoratorName : '@authenticate' , } ) ( target ) ; } throw new Error ( '@authenticate cannot be used on a property: ' + DecoratorFactory . getTargetName ( target , method , methodDescriptor ) ) ; } ; } export namespace authenticate { /**
     * `@authenticate.skip()` - a sugar decorator to skip authentication
     */ export const skip = ( ) => authenticate ( { strategy : '' , skip : true } ) ; } param targetClass param methodName export function getAuthenticateMetadata ( targetClass , methodName ) { let metadata = MetadataInspector . getMethodMetadata < AuthenticationMetadata [ ] > ( AUTHENTICATION_METADATA_METHOD_KEY , targetClass . prototype , methodName ) ; if ( metadata ) return metadata ; metadata = MetadataInspector . getClassMetadata < AuthenticationMetadata [ ] > ( AUTHENTICATION_METADATA_CLASS_KEY , targetClass ) ; return metadata ; }