UNPKG

@zestic/oauth-core

Version:

Framework-agnostic OAuth authentication library with support for multiple OAuth flows

157 lines 4.82 kB
"use strict"; /** * Registry for managing OAuth flow handlers */ Object.defineProperty(exports, "__esModule", { value: true }); exports.CallbackFlowRegistry = void 0; const OAuthTypes_1 = require("../types/OAuthTypes"); const ErrorHandler_1 = require("../utils/ErrorHandler"); class CallbackFlowRegistry { constructor(options = {}) { this.handlers = new Map(); this.options = { allowDuplicates: false, defaultPriority: 50, ...options, }; } /** * Register a flow handler */ register(handler) { if (!this.options.allowDuplicates && this.handlers.has(handler.name)) { throw ErrorHandler_1.ErrorHandler.createError(`Flow handler '${handler.name}' is already registered`, OAuthTypes_1.OAUTH_ERROR_CODES.INVALID_CONFIGURATION); } this.handlers.set(handler.name, handler); } /** * Unregister a flow handler */ unregister(name) { return this.handlers.delete(name); } /** * Get a specific flow handler by name */ getHandler(name) { return this.handlers.get(name); } /** * Get all registered handlers */ getAllHandlers() { return Array.from(this.handlers.values()); } /** * Get handlers sorted by priority */ getHandlersByPriority() { return Array.from(this.handlers.values()) .sort((a, b) => a.priority - b.priority); } /** * Auto-detect which flow to use based on parameters */ detectFlow(params, config) { const sortedHandlers = this.getHandlersByPriority(); for (const handler of sortedHandlers) { if (handler.canHandle(params, config)) { return handler; } } return undefined; } /** * Detect flow with confidence scoring */ detectFlowWithConfidence(params, config) { const sortedHandlers = this.getHandlersByPriority(); for (const handler of sortedHandlers) { if (handler.canHandle(params, config)) { // Simple confidence calculation based on priority // Lower priority number = higher confidence const confidence = Math.max(0, 100 - handler.priority); return { handler, confidence, reason: `Handler '${handler.name}' can process the provided parameters`, }; } } return undefined; } /** * Get all handlers that can handle the given parameters */ getCompatibleHandlers(params, config) { // Handle null/undefined parameters gracefully if (!params || !config) { return []; } const compatibleHandlers = this.getAllHandlers().filter(handler => { try { return handler.canHandle(params, config); } catch (error) { // Log error but don't let it break the filtering console.warn(`Handler '${handler.name}' threw error in canHandle:`, error); return false; } }); // Sort by priority (descending - higher priority first) return compatibleHandlers.sort((a, b) => b.priority - a.priority); } /** * Check if a specific flow is registered */ hasHandler(name) { return this.handlers.has(name); } /** * Get the number of registered handlers */ getHandlerCount() { return this.handlers.size; } /** * Clear all registered handlers */ clear() { this.handlers.clear(); } /** * Get handler names */ getHandlerNames() { return Array.from(this.handlers.keys()); } /** * Validate that all required handlers are registered */ validateRequiredHandlers(requiredHandlers) { const missing = requiredHandlers.filter(name => !this.hasHandler(name)); if (missing.length > 0) { throw ErrorHandler_1.ErrorHandler.createError(`Missing required flow handlers: ${missing.join(', ')}`, OAuthTypes_1.OAUTH_ERROR_CODES.INVALID_CONFIGURATION); } } /** * Register multiple handlers at once */ registerMultiple(handlers) { for (const handler of handlers) { this.register(handler); } } /** * Create a copy of the registry */ clone() { const newRegistry = new CallbackFlowRegistry(this.options); for (const handler of this.getAllHandlers()) { newRegistry.register(handler); } return newRegistry; } } exports.CallbackFlowRegistry = CallbackFlowRegistry; //# sourceMappingURL=CallbackFlowRegistry.js.map