/** * Mock JWT Auth Plugin for Testing * * This file provides a mock implementation of the JWT Auth Plugin * to enable testing OAuth flows without requiring a real JWT Auth Plugin instance. */ import jwtSimple from 'jwt-simple'; /** * Mock JWT Auth Plugin that mimics the interface of @flink-app/jwt-auth-plugin */ export interface MockJwtAuthPlugin { id: string; createToken: (payload: any, roles: string[]) => Promise; authenticateRequest: (request: any, permission?: string) => Promise; } /** * Create a mock JWT Auth Plugin for testing * * @param secret - Secret key for JWT encoding/decoding (default: 'test-secret') * @param shouldFailTokenCreation - If true, createToken will throw an error (for testing error handling) * @returns Mock JWT Auth Plugin instance */ export function createMockJwtAuthPlugin( secret: string = 'test-secret', shouldFailTokenCreation: boolean = false ): MockJwtAuthPlugin { return { id: 'jwtAuth', createToken: async (payload: any, roles: string[]): Promise => { if (shouldFailTokenCreation) { throw new Error('JWT token generation failed'); } // Create JWT token with payload and roles const tokenData = { ...payload, roles, iat: Math.floor(Date.now() / 1000), exp: Math.floor(Date.now() / 1000) + (60 * 60) // 1 hour expiration }; return jwtSimple.encode(tokenData, secret); }, authenticateRequest: async (request: any, permission?: string): Promise => { try { const authHeader = request.headers?.authorization; if (!authHeader || !authHeader.startsWith('Bearer ')) { return false; } const token = authHeader.substring(7); const decoded = jwtSimple.decode(token, secret); return !!decoded; } catch (error) { return false; } } }; } /** * Create a mock JWT Auth Plugin that fails token creation * Useful for testing error handling in OAuth callback flows */ export function createFailingMockJwtAuthPlugin(): MockJwtAuthPlugin { return createMockJwtAuthPlugin('test-secret', true); } /** * Decode a JWT token for test assertions */ export function decodeTestToken(token: string, secret: string = 'test-secret'): any { return jwtSimple.decode(token, secret); } /** * Verify a JWT token contains expected data */ export function verifyTestToken( token: string, expectedPayload: Record, secret: string = 'test-secret' ): boolean { try { const decoded = decodeTestToken(token, secret); // Check all expected payload fields for (const [key, value] of Object.entries(expectedPayload)) { if (decoded[key] !== value) { return false; } } return true; } catch (error) { return false; } }