UNPKG

@labshare/services-auth

Version:

Loopback 4 plugin for resource scope-based HTTP route authz

76 lines (75 loc) 2.66 kB
/** * Binding keys used by this component. */ import { BindingKey } from "@loopback/context"; import type { AuthenticationMetadata } from "./decorators/authenticate.decorator"; import { MetadataAccessor } from "@loopback/metadata"; import type { Request, Response } from "express"; import type { GetVerificationKey, IsRevoked } from "express-jwt"; export { GetVerificationKey, IsRevoked }; /** * Interface definition of a function which accepts a request * and returns an authenticated user */ export interface AuthenticateFn { (request: Request, response: Response): Promise<any>; } export declare namespace AuthenticationBindings { /** * Key used to inject the authentication function into the sequence. * * ```ts * class MySequence implements SequenceHandler { * constructor( * @inject(AuthenticationBindings.AUTH_ACTION) * protected authenticateRequest: AuthenticateFn, * // ... other sequence action injections * ) {} * * async handle(context: RequestContext) { * try { * const {request, response} = context; * const route = this.findRoute(request); * * // Authenticate * await this.authenticateRequest(request); * * // Authentication successful, proceed to invoke controller * const args = await this.parseParams(request, route); * const result = await this.invoke(route, args); * this.send(response, result); * } catch (err) { * this.reject(context, err); * } * } * } * ``` */ const AUTH_ACTION: BindingKey<AuthenticateFn>; const USER_INFO_ACTION: BindingKey<AuthenticateFn>; /** * Key used to set configuration for the authentication action * @type {BindingKey<any>} */ const AUTH_CONFIG: BindingKey<any>; /** * The key used to set the custom RS256/HS256 secret provider */ const SECRET_PROVIDER: BindingKey<GetVerificationKey>; /** * The key used to get a custom JWT audience */ const AUDIENCE_PROVIDER: BindingKey<any>; /** * The key used to set the custom RS256/HS256 secret provider */ const IS_REVOKED_CALLBACK_PROVIDER: BindingKey<IsRevoked>; } /** * The key used to store method authentication decorator metadata */ export declare const AUTHENTICATION_METADATA_KEY: MetadataAccessor<AuthenticationMetadata, MethodDecorator>; /** * The key used to store class-level metadata for `@authenticate` */ export declare const AUTHENTICATION_METADATA_CLASS_KEY: MetadataAccessor<AuthenticationMetadata, ClassDecorator>;