UNPKG

@zestic/oauth-core

Version:

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

150 lines 5.01 kB
"use strict"; /** * Base flow handler interface and abstract implementation */ Object.defineProperty(exports, "__esModule", { value: true }); exports.FlowHandlerFactory = exports.SimpleCallbackFlowHandler = exports.BaseCallbackFlowHandler = void 0; const OAuthTypes_1 = require("../types/OAuthTypes"); const ErrorHandler_1 = require("../utils/ErrorHandler"); const UrlParser_1 = require("../utils/UrlParser"); /** * Abstract base class for callback flow handlers */ class BaseCallbackFlowHandler { /** * Default validation implementation - can be overridden by subclasses * Provides common validation logic that all flows should have */ async validate(params, config) { try { // Check for OAuth errors first (access_denied, invalid_request, etc.) this.checkForOAuthError(params); // Check if this flow is explicitly disabled in config if (config.flows?.disabledFlows?.includes(this.name)) { return false; } return true; } catch { return false; } } /** * Check for OAuth errors in parameters */ checkForOAuthError(params) { if (UrlParser_1.UrlParser.hasOAuthError(params)) { const { error, errorDescription } = UrlParser_1.UrlParser.extractOAuthError(params); const message = errorDescription || error || 'OAuth error occurred'; throw ErrorHandler_1.ErrorHandler.createError(message, error && typeof error === 'string' ? error : OAuthTypes_1.OAUTH_ERROR_CODES.INVALID_GRANT); } } /** * Validate required parameters */ validateRequiredParams(params, requiredParams) { const missing = requiredParams.filter(param => !params.has(param)); if (missing.length > 0) { throw ErrorHandler_1.ErrorHandler.handleMissingParameter(missing.join(', ')); } } /** * Extract parameter safely with validation */ getRequiredParam(params, key) { const value = params.get(key); if (!value) { throw ErrorHandler_1.ErrorHandler.handleMissingParameter(key); } return value; } /** * Get optional parameter with default value */ getOptionalParam(params, key, defaultValue) { return params.get(key) ?? defaultValue; } /** * Create success result */ createSuccessResult(accessToken, refreshToken, expiresIn) { return { success: true, accessToken, refreshToken, expiresIn, }; } /** * Create error result */ createErrorResult(error, errorCode) { return { success: false, error, errorCode, }; } /** * Log flow execution */ logFlowExecution(message, params) { const sanitizedParams = params ? UrlParser_1.UrlParser.sanitizeForLogging(params) : {}; console.log(`[${this.name}] ${message}`, sanitizedParams); } /** * Measure execution time */ async measureExecutionTime(operation, operationName) { const startTime = Date.now(); try { const result = await operation(); const duration = Date.now() - startTime; console.log(`[${this.name}] ${operationName} completed in ${duration}ms`); return result; } catch (error) { const duration = Date.now() - startTime; console.error(`[${this.name}] ${operationName} failed after ${duration}ms:`, error); throw error; } } } exports.BaseCallbackFlowHandler = BaseCallbackFlowHandler; /** * Simple flow handler implementation for basic flows */ class SimpleCallbackFlowHandler extends BaseCallbackFlowHandler { constructor(name, priority, canHandleFunc, handleFunc, validateFunc) { super(); this.name = name; this.priority = priority; this.canHandleFunc = canHandleFunc; this.handleFunc = handleFunc; this.validateFunc = validateFunc; } canHandle(params, config) { return this.canHandleFunc(params, config); } async handle(params, adapters, config) { return this.handleFunc(params, adapters, config); } async validate(params, config) { if (this.validateFunc) { return this.validateFunc(params, config); } // If no custom validation function provided, use parent's default validation return super.validate(params, config); } } exports.SimpleCallbackFlowHandler = SimpleCallbackFlowHandler; /** * Factory for creating simple flow handlers */ class FlowHandlerFactory { static create(name, priority, canHandle, handle, validate) { return new SimpleCallbackFlowHandler(name, priority, canHandle, handle, validate); } } exports.FlowHandlerFactory = FlowHandlerFactory; //# sourceMappingURL=BaseCallbackFlowHandler.js.map