UNPKG

edge-master

Version:
76 lines (75 loc) 2.19 kB
/// <reference types="@cloudflare/workers-types" /> import { IRequestInterceptor } from '../types/interceptor'; export interface JWTPayload { [key: string]: any; } export interface JWTOptions { /** * Function to verify and decode JWT token * Should throw an error if token is invalid */ verify: (token: string) => Promise<JWTPayload> | JWTPayload; /** * Function to extract token from request * @default Extracts from Authorization header (Bearer token) */ getToken?: (req: Request) => string | null; /** * Paths to exclude from JWT authentication * Can be exact paths or patterns */ exclude?: string[] | ((req: Request) => boolean); /** * Custom response when authentication fails */ onAuthenticationFailed?: (req: Request, error: Error) => Response; /** * Key to store decoded JWT payload in context state * @default 'user' */ stateKey?: string; } /** * Creates a JWT authentication interceptor * * Example usage: * ```typescript * import { jwtInterceptor } from 'edge-master/interceptors'; * * // Using a JWT library like jose * const jwt = jwtInterceptor({ * verify: async (token) => { * const { payload } = await jwtVerify(token, secret); * return payload; * }, * exclude: ['/public', '/login'], * }); * * app.addInterceptor(jwt); * ``` */ export declare function jwtInterceptor(options: JWTOptions): IRequestInterceptor; /** * Helper to create a simple API key authentication interceptor */ export interface ApiKeyOptions { /** * Function to verify API key * Should return true if valid, false otherwise */ verify: (apiKey: string) => Promise<boolean> | boolean; /** * Header name to extract API key from * @default 'X-API-Key' */ headerName?: string; /** * Paths to exclude from API key authentication */ exclude?: string[] | ((req: Request) => boolean); /** * Custom response when authentication fails */ onAuthenticationFailed?: (req: Request) => Response; } export declare function apiKeyInterceptor(options: ApiKeyOptions): IRequestInterceptor;