import * as _homer0_jimple from '@homer0/jimple'; import { J as Jimpex } from '../../index-DAUqbQ6h.js'; import { ExpressMiddleware } from '../../types/express.js'; import { Statuses } from '../../utils/fns/statuses.js'; import { DeepPartial } from '../../types/utils.js'; import { HTTPErrorClass } from '../common/httpError.js'; import '../../types/http.js'; import 'https'; import 'http'; import 'spdy'; import 'node-fetch'; import 'express'; import '@homer0/path-utils'; import '@homer0/simple-logger'; import '@homer0/simple-config'; import '@homer0/events-hub'; import 'statuses'; import '../common/appError.js'; /** * The options for the error the middleware can generate. * * @group Services/EnsureBearerToken */ type EnsureBearerTokenErrorOptions = { /** * The error message for the response. * * @default 'Unauthorized' */ message: string; /** * The HTTP status that will be added to the error context. * * @default 401 */ status: number; /** * Context information for the error handler and that can be added to the actual * response. */ response: unknown; }; /** * The options to customize the service/middleware. * * @group Services/EnsureBearerToken */ type EnsureBearerTokenOptions = { error: EnsureBearerTokenErrorOptions; expression: RegExp; local: string; }; /** * The options to construct a {@link EnsureBearerToken}. * * @group Services/EnsureBearerToken */ type EnsureBearerConstructorOptions = DeepPartial & { /** * A dictionary with the dependencies to inject. */ inject: { HTTPError: HTTPErrorClass; statuses: Statuses; }; }; /** * Custom options for the provider that will register an instance of * {@link EnsureBearerToken} * as a service. * * @group Services/EnsureBearerToken */ type EnsureBearerTokenProviderOptions = DeepPartial & { /** * The name that will be used to register the service on the container. This is to allow * multiple "instances" of the service to be created. * * @default 'ensureBearerToken' */ serviceName?: string; }; /** * This service gives you a middleware that verifies if a request has an `Authorization` * header with a bearer token; if it does, the token will be saved on the `res.locals`, * otherwise, it will generate an error. * * This is a "service middleware" to allow certain flexibility: you can have controllers * where some routes are protected and others are not. For those cases, you get the * service from the container, and include it only in the routes that need it. * * @group Services * @group Services/EnsureBearerToken */ declare class EnsureBearerToken { /** * To generate the errors when the validation fails. */ protected readonly _HTTPError: HTTPErrorClass; /** * The customization options for the service. */ protected readonly _options: EnsureBearerTokenOptions; /** * @param options The options to construct the class. */ constructor({ inject: { HTTPError, statuses }, ...options }: EnsureBearerConstructorOptions); /** * Generates the middleware that verifies if a request has an `Authorization` header * with a bearer token. */ getMiddleware(): ExpressMiddleware; /** * The customization options. */ get options(): Readonly; } /** * Generates a "service middleware" that can be used on route controllers in order to * validate the presence of a bearer token on the requests authorization header. * * The registered service is an instance of {@link EnsureBearerToken}, and it uses the key * `ensureBearerToken`. * * Since it's a "provider creator", when registering it, you can pass custom options. * * @example * * Basic usage * * // Register it on the container * container.register(ensureBearerTokenProvider); * * // Let's assume we are in a controller now... * // Getting access to the middleware. * const ensureBearerToken = container.get('ensureBearerToken'); * * @example * * Customizing the service * * // Register it on the container * container.register( * ensureBearerTokenProvider({ * serviceName: 'ensureBearerTokenCustom', * error: { * message: 'Missing token!', * }, * }), * ); * * @group Providers * @group Services/EnsureBearerToken */ declare const ensureBearerTokenProvider: _homer0_jimple.ResourceCreator<"provider", "register", (options?: EnsureBearerTokenProviderOptions) => (app: Jimpex) => void, _homer0_jimple.ProviderRegisterFn>; export { type EnsureBearerConstructorOptions, EnsureBearerToken, type EnsureBearerTokenErrorOptions, type EnsureBearerTokenOptions, type EnsureBearerTokenProviderOptions, ensureBearerTokenProvider };