UNPKG

@lock-sdk/csrf

Version:

CSRF protection module for Lock security framework

120 lines (111 loc) 4.86 kB
import * as _lock_sdk_core from '@lock-sdk/core'; declare enum CSRFEventType { CSRF_TOKEN_MISSING = "csrf.token.missing", CSRF_TOKEN_INVALID = "csrf.token.invalid", CSRF_DOUBLE_SUBMIT_FAILURE = "csrf.double.submit.failure", CSRF_VALIDATED = "csrf.validated", CSRF_ERROR = "csrf.error" } type TokenLocation = 'cookie' | 'header' | 'cookie-header' | 'session'; type TokenStorage = 'memory' | 'redis'; interface CookieOptions { httpOnly?: boolean; secure?: boolean; sameSite?: 'strict' | 'lax' | 'none'; path?: string; domain?: string; maxAge?: number; } interface RedisOptions { host?: string; port?: number; password?: string; username?: string; db?: number; keyPrefix?: string; url?: string; client?: any; } interface TokenStorageProvider { init(): Promise<void>; saveToken(token: string, identifier: string, ttl: number): Promise<void>; getToken(identifier: string): Promise<string | null>; validateToken(token: string, identifier: string): Promise<boolean>; deleteToken(identifier: string): Promise<void>; deleteExpiredTokens(): Promise<void>; } interface CSRFConfig { enabled: boolean; tokenName: string; tokenLength: number; headerName: string; cookieName: string; cookieOptions: CookieOptions; storage: TokenStorage; tokenLocation: TokenLocation; ignoredMethods: string[]; ignoredPaths: (string | RegExp)[]; ignoredContentTypes: string[]; failureStatusCode: number; failureMessage: string; refreshToken: boolean; tokenTtl: number; doubleSubmit: boolean; samesite: boolean; redisOptions?: RedisOptions; customStorage?: TokenStorageProvider; hashAlgorithm?: string; secret?: string; includeFormToken?: boolean; angularCompatible?: boolean; } declare function generateToken(length: number, identifier: string, storage: TokenStorageProvider, config: CSRFConfig): Promise<string>; declare function validateToken(token: string, identifier: string, storage: TokenStorageProvider, config: CSRFConfig): Promise<boolean>; declare function hashToken(token: string, secret: string, algorithm?: string): string; declare function generateSecret(): string; declare function generateTokenSync(length: number): string; declare function secureCompare(a: string, b: string): boolean; declare function extractToken(req: any, config: CSRFConfig): string | null; declare function extractFromHeader(req: any, config: CSRFConfig): string | null; declare function extractFromCookie(req: any, config: CSRFConfig): string | null; declare function extractFromBody(req: any, config: CSRFConfig): string | null; declare function extractFromQuery(req: any, config: CSRFConfig): string | null; declare function extractFromSession(req: any, config: CSRFConfig): string | null; declare function parseCookies(cookieString: string): Record<string, string>; declare class MemoryStorage implements TokenStorageProvider { private store; private cleanupInterval; constructor(); init(): Promise<void>; saveToken(token: string, identifier: string, ttl: number): Promise<void>; getToken(identifier: string): Promise<string | null>; validateToken(token: string, identifier: string): Promise<boolean>; deleteToken(identifier: string): Promise<void>; deleteExpiredTokens(): Promise<void>; private scheduleCleanup; } declare class RedisStorage implements TokenStorageProvider { private client; private keyPrefix; private options; private externalClient; constructor(options?: RedisOptions); init(): Promise<void>; saveToken(token: string, identifier: string, ttl: number): Promise<void>; getToken(identifier: string): Promise<string | null>; validateToken(token: string, identifier: string): Promise<boolean>; deleteToken(identifier: string): Promise<void>; deleteExpiredTokens(): Promise<void>; private ensureInitialized; private getKey; private isRedisV4; close(): Promise<void>; } declare function createStorage(config: CSRFConfig): TokenStorageProvider; /** * Create a CSRF protection security module * @param config Module configuration */ declare const csrfProtection: (config?: Partial<CSRFConfig> | undefined) => _lock_sdk_core.SecurityModule; declare function csrfToken(config?: Partial<CSRFConfig>): (req: any, res: any, next: Function) => Promise<void>; export { type CSRFConfig, CSRFEventType, type CookieOptions, MemoryStorage, type RedisOptions, RedisStorage, type TokenLocation, type TokenStorage, type TokenStorageProvider, createStorage, csrfProtection, csrfToken, extractFromBody, extractFromCookie, extractFromHeader, extractFromQuery, extractFromSession, extractToken, generateSecret, generateToken, generateTokenSync, hashToken, parseCookies, secureCompare, validateToken };