import { addExtension , Binding , BindingTemplate , Constructor , Context , extensionFor , } from '@loopback/core' ; import { RedirectRoute , Request } from '@loopback/rest' ; import { UserProfile } from '@loopback/security' ; import { AuthenticationBindings } from './keys' ; /**
 * Authentication metadata stored via Reflection API
 */ export interface AuthenticationMetadata { /**
     * Name of the authentication strategy
     */ strategy : string ; /**
     * Options for the authentication strategy
     */ options ? : { [ name ] : any ; } ; /**
     * A flag to skip authentication
     */ skip ? : boolean ; } /**
 * interface definition of a factory function which
 * accepts a user definition and returns the user profile
 */ export interface UserProfileFactory < U > { ( user ) : UserProfile ; } /**
 * interface definition of a function which accepts a request
 * and returns an authenticated user
 */ export interface AuthenticateFn { ( request ) : Promise < UserProfile | undefined > ; } /**
 * Options for authentication component
 */ export interface AuthenticationOptions { /**
     * Default authentication metadata if a method or class is not decorated with
     * `@authenticate`. If not set, no default authentication will be enforced for
     * those methods without authentication metadata.
     */ defaultMetadata ? : AuthenticationMetadata [ ] ; /**
     * This flag allows an authentication strategy to abort the authentication by
     * throwing an error if `failOnError` is set to `true`. By default, the
     * authentication process continues to the next one even when a strategy
     * throws an error. If one of other strategies succeed, the error will be
     * discarded.
     */ failOnError ? : boolean ; } /**
 * An interface that describes the common authentication strategy.
 *
 * An authentication strategy is a class with an
 * 'authenticate' method that verifies a user's credentials and
 * returns the corresponding user profile.
 *
 */ export interface AuthenticationStrategy { /**
     * The 'name' property is a unique identifier for the
     * authentication strategy ( for example : 'basic', 'jwt', etc)
     */ name : string ; param request authenticate ( request ) : Promise < UserProfile | RedirectRoute | undefined > ; } export const AUTHENTICATION_STRATEGY_NOT_FOUND = 'AUTHENTICATION_STRATEGY_NOT_FOUND' ; export const USER_PROFILE_NOT_FOUND = 'USER_PROFILE_NOT_FOUND' ; param context param strategyClass export function registerAuthenticationStrategy ( context , strategyClass ) { return addExtension ( context , AuthenticationBindings . AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME , strategyClass , { namespace : AuthenticationBindings . AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME , } ) ; } /**
 * A binding template for auth strategy contributor extensions
 */ export const asAuthStrategy = binding => { extensionFor ( AuthenticationBindings . AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME ) ( binding ) ; binding . tag ( { namespace : AuthenticationBindings . AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME , } ) ; } ; param metadata param strategyName export function getAuthenticationMetadataForStrategy ( metadata , strategyName ) { return metadata . find ( data => data . strategy === strategyName ) ; }