@re-auth/http-adapters
Version:
HTTP adapters for ReAuth authentication framework
251 lines • 8.26 kB
TypeScript
import { type ReAuthEngine, type AuthOutput } from "@re-auth/reauth";
/**
* Context extraction rule for specific plugin/step combinations
*/
export interface ContextExtractionRule {
pluginName: string;
stepName?: string;
/**
* Cookie names to extract from request and add to step inputs
*/
extractCookies?: string[];
/**
* Header names to extract from request and add to step inputs
* Format: { headerName: inputName } or just headerName (uses same name)
*/
extractHeaders?: string[] | Record<string, string>;
/**
* Cookie names to set in response (will be populated from step outputs)
*/
setCookies?: string[];
/**
* Header names to set in response (will be populated from step outputs)
*/
setHeaders?: string[] | Record<string, string>;
/**
* Transform function for input values
*/
transformInput?: (key: string, value: any, request: any) => any;
/**
* Transform function for output values
*/
transformOutput?: (key: string, value: any, result: AuthOutput, request: any) => any;
}
/**
* Auto-generated route information
*/
export interface AutoGeneratedRoute {
pluginName: string;
stepName: string;
method: string;
path: string;
requiresAuth: boolean;
description: string;
inputs: string[];
}
/**
* Auto-introspection configuration
*/
export interface AutoIntrospectionConfig {
/**
* Whether to enable auto-introspection (default: true)
*/
enabled?: boolean;
/**
* Plugins to include (if not specified, all plugins are included)
*/
includePlugins?: string[];
/**
* Plugins to exclude
*/
excludePlugins?: string[];
/**
* Steps to exclude (format: 'pluginName.stepName')
*/
excludeSteps?: string[];
/**
* Custom path generator for auto-generated routes
*/
pathGenerator?: (pluginName: string, stepName: string, basePath: string) => string;
/**
* Callback when routes are auto-generated
*/
onRoutesGenerated?: (routes: AutoGeneratedRoute[]) => void;
}
/**
* Base HTTP configuration for all adapters
*/
export interface BaseHttpConfig {
basePath?: string;
cookieName?: string;
cookieOptions?: {
httpOnly?: boolean;
secure?: boolean;
sameSite?: "strict" | "lax" | "none" | boolean;
maxAge?: number;
domain?: string;
path?: string;
};
/**
* Auto-introspection configuration
*/
autoIntrospection?: AutoIntrospectionConfig;
/**
* Context extraction rules for plugins and steps
* Defines what cookies/headers to extract from requests and set in responses
*/
contextRules?: ContextExtractionRule[];
/**
* Custom route overrides
* Allows overriding specific plugin step routes
*/
routeOverrides?: RouteOverride[];
/**
* Custom routes for external plugins or additional functionality
*/
customRoutes?: CustomRoute[];
/**
* Global middleware to apply to all routes
*/
globalMiddleware?: any[];
/**
* Custom error handler
*/
errorHandler?: (error: Error, context: any) => any;
}
/**
* Route override configuration
*/
export interface RouteOverride {
pluginName: string;
stepName: string;
/**
* Override the HTTP method
*/
method?: string;
/**
* Override the route path
*/
path?: string;
/**
* Override the route handler completely
*/
handler?: any;
/**
* Add middleware before the default handler
*/
middleware?: any[];
/**
* Custom input extraction logic
*/
extractInputs?: (request: any, pluginName: string, stepName: string) => Promise<Record<string, any>>;
/**
* Custom response handling logic
*/
handleResponse?: (request: any, response: any, result: AuthOutput, httpConfig: any) => any;
}
/**
* Custom route configuration
*/
export interface CustomRoute {
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
path: string;
handler: any;
middleware?: any[];
/**
* Whether this route requires authentication
*/
requireAuth?: boolean;
}
/**
* HTTP adapter context for handlers
*/
export interface HttpAdapterContext {
engine: ReAuthEngine;
config: Required<Omit<BaseHttpConfig, "errorHandler">> & {
errorHandler?: (error: Error, context: any) => any;
};
extractToken: (request: any) => string | null;
requireAuth: () => any;
handleStepResponse: (request: any, response: any, result: AuthOutput, httpConfig: any) => any;
errorResponse: (response: any, error: Error) => any;
autoGeneratedRoutes: AutoGeneratedRoute[];
}
/**
* HTTP adapter factory function type
*/
export type HttpAdapterFactory<T extends BaseHttpConfig, R = any> = (engine: ReAuthEngine, config: T) => R;
/**
* Framework-specific adapter implementation
*/
export interface FrameworkAdapter<T extends BaseHttpConfig> {
setupMiddleware(context: HttpAdapterContext): void;
createRoute(method: string, path: string, handler: any, middleware?: any[]): void;
extractInputs(request: any, pluginName: string, stepName: string): Promise<Record<string, any>>;
handleStepResponse(request: any, response: any, result: AuthOutput, httpConfig: any): any;
/**
* Add configurable context inputs from cookies and headers based on context rules
*/
addConfigurableContextInputs(request: any, inputs: Record<string, any>, pluginName: string, stepName: string, contextRules: ContextExtractionRule[]): void;
/**
* Handle configurable context outputs (set cookies and headers) based on context rules
*/
handleConfigurableContextOutputs(request: any, response: any, result: AuthOutput, pluginName: string, stepName: string, contextRules: ContextExtractionRule[]): void;
extractToken(request: any): string | null;
requireAuth(): any;
errorResponse(response: any, error: Error): any;
getAdapter(): any;
}
/**
* Introspect ReAuth engine and extract all available routes
*/
export declare function introspectReAuthEngine(engine: ReAuthEngine, config?: AutoIntrospectionConfig): AutoGeneratedRoute[];
/**
* Creates a generic HTTP adapter for any framework
*/
export declare function createHttpAdapter<T extends BaseHttpConfig>(frameworkAdapter: FrameworkAdapter<T>): HttpAdapterFactory<T>;
/**
* Find applicable context extraction rules for a plugin/step combination
*/
export declare function findContextRules(pluginName: string, stepName: string, contextRules: ContextExtractionRule[]): ContextExtractionRule[];
/**
* Utility function to create route override
*/
export declare function createRouteOverride(pluginName: string, stepName: string, override: Partial<Omit<RouteOverride, "pluginName" | "stepName">>): RouteOverride;
/**
* Utility function to create custom route
*/
export declare function createCustomRoute(method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH", path: string, handler: any, options?: {
middleware?: any[];
requireAuth?: boolean;
}): CustomRoute;
/**
* Utility function to create auto-introspection config
*/
export declare function createAutoIntrospectionConfig(options?: Partial<AutoIntrospectionConfig>): AutoIntrospectionConfig;
/**
* Utility function to create context extraction rule
*/
export declare function createContextRule(pluginName: string, options?: Partial<Omit<ContextExtractionRule, "pluginName">>): ContextExtractionRule;
/**
* Predefined context rules for common OAuth scenarios
*/
export declare const OAuth2ContextRules: {
/**
* OAuth 2.0 with PKCE context rule for extracting state and code verifier
*/
pkce: (pluginName: string) => ContextExtractionRule;
/**
* OAuth 2.0 regular flow context rule for extracting state
*/
regular: (pluginName: string) => ContextExtractionRule;
/**
* OAuth callback context rule for all OAuth steps
*/
callback: (pluginName: string) => ContextExtractionRule;
/**
* OAuth start context rule for setting state and code verifier
*/
start: (pluginName: string) => ContextExtractionRule;
};
//# sourceMappingURL=http-adapter-factory.d.ts.map