UNPKG

@re-auth/http-adapters

Version:

HTTP adapters for ReAuth authentication framework

110 lines 4.99 kB
import { FastifyInstance, FastifyRequest, FastifyReply, FastifyPluginOptions } from 'fastify'; import { ReAuthEngine, AuthOutput, Entity, AuthToken } from '@re-auth/reauth'; import { BaseHttpConfig, FrameworkAdapter, HttpAdapterContext, RouteOverride, CustomRoute, createRouteOverride, createCustomRoute, createAutoIntrospectionConfig, introspectReAuthEngine, type AutoGeneratedRoute, type AutoIntrospectionConfig, createContextRule, OAuth2ContextRules, type ContextExtractionRule } from '../../utils/http-adapter-factory'; /** * Fastify-specific configuration */ export interface FastifyAdapterConfig extends BaseHttpConfig { prefix?: string; } /** * Fastify framework adapter implementation */ declare class FastifyFrameworkAdapter implements FrameworkAdapter<FastifyAdapterConfig> { private fastify; private engine?; private contextRules; private adapterConfig; private routes; constructor(fastify?: FastifyInstance, engine?: ReAuthEngine); /** * Set the ReAuth engine instance (for shared adapter pattern) */ setEngine(engine: ReAuthEngine): void; /** * Set context rules (for shared adapter pattern) */ setContextRules(rules: ContextExtractionRule[]): void; /** * Set adapter config (for shared adapter pattern) */ setAdapterConfig(config: any): void; /** * Get expected inputs for a plugin step */ private getExpectedInputs; setFastifyInstance(fastify: FastifyInstance): void; setupMiddleware(context: HttpAdapterContext): void; createRoute(method: string, path: string, handler: any, middleware?: any[]): void; extractInputs(request: FastifyRequest, pluginName: string, stepName: string): Promise<Record<string, any>>; /** * Add configurable context inputs from cookies and headers based on context rules */ addConfigurableContextInputs(request: FastifyRequest, inputs: Record<string, any>, pluginName: string, stepName: string, contextRules: ContextExtractionRule[]): void; handleStepResponse(request: FastifyRequest, reply: FastifyReply, result: AuthOutput, httpConfig: any): void; /** * Handle configurable context outputs (set cookies and headers) based on context rules */ handleConfigurableContextOutputs(request: FastifyRequest, reply: FastifyReply, result: AuthOutput, pluginName: string, stepName: string, contextRules: ContextExtractionRule[]): void; private getStatusCode; extractToken(request: FastifyRequest): string | null; requireAuth(): any; errorResponse(reply: FastifyReply, error: Error): void; getAdapter(): FastifyInstance; } /** * Create Fastify adapter using the factory pattern with shared instance * Note: Fastify instance must be provided when calling this function */ export declare function createFastifyAdapterV2(fastify: FastifyInstance, frameworkAdapter: FastifyFrameworkAdapter): import("../../utils/http-adapter-factory").HttpAdapterFactory<BaseHttpConfig, any>; /** * Fastify adapter class with additional features */ export declare class FastifyAdapterV2 { private fastify; private engine; private config; private frameworkAdapter; private adapterFunction; constructor(fastify: FastifyInstance, engine: ReAuthEngine, config?: FastifyAdapterConfig, frameworkAdapter?: FastifyFrameworkAdapter); /** * Get the Fastify instance */ getFastify(): FastifyInstance; /** * Add a custom route */ addRoute(method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH', path: string, handler: (request: FastifyRequest, reply: FastifyReply) => void | Promise<void>, options?: { middleware?: any[]; requireAuth?: boolean; }): void; /** * Protection middleware for routes */ protect(options?: ProtectOptions): (request: any, reply: FastifyReply) => Promise<undefined>; /** * Register this adapter as a Fastify plugin */ static createPlugin(engine: ReAuthEngine, config?: FastifyAdapterConfig): Promise<(fastify: FastifyInstance, options: FastifyPluginOptions) => Promise<void>>; } /** * Protection options */ interface ProtectOptions { roles?: string[]; authorize?: (entity: Entity, request: FastifyRequest, reply: FastifyReply) => Promise<boolean> | boolean; } /** * Convenience function to create Fastify adapter with options */ export declare function createFastifyAdapter(fastify: FastifyInstance, engine: ReAuthEngine, config?: FastifyAdapterConfig): FastifyAdapterV2; declare module 'fastify' { interface FastifyRequest { user?: Entity; token?: AuthToken; isAuthenticated: boolean; } } export { createRouteOverride, createCustomRoute, createAutoIntrospectionConfig, introspectReAuthEngine, createContextRule, OAuth2ContextRules, FastifyFrameworkAdapter, }; export type { RouteOverride, CustomRoute, AutoGeneratedRoute, AutoIntrospectionConfig, ContextExtractionRule, }; //# sourceMappingURL=fastify-adapter-v2.d.ts.map