UNPKG

@infosel-sdk/core

Version:

Core SDK for Infosel financial services platform. Provides essential infrastructure for authentication, HTTP/GraphQL communication, storage management, and error handling.

134 lines 4.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigurationValidator = void 0; class ConfigurationValidator { /** * Validates an AuthConfiguration and returns validation result */ validate(config) { const errors = []; // Validate required fields this.validateRequiredFields(config, errors); // Validate realm format this.validateRealm(config.realm, errors); // Validate environment this.validateEnvironment(config.environment, errors); // Validate auth type specific fields this.validateAuthTypeSpecificFields(config, errors); return { isValid: errors.length === 0, errors, }; } /** * Validates required fields are present */ validateRequiredFields(config, errors) { if (!config.type) { errors.push('Authentication type is required'); } if (!config.realm) { errors.push('Realm is required'); } if (!config.environment) { errors.push('Environment is required'); } } /** * Validates realm format and content */ validateRealm(realm, errors) { if (!realm) { return; // Already handled in required fields } // Check if realm is empty or only whitespace if (realm.trim() === '') { errors.push('Realm cannot be empty'); return; } // Validate realm format (alphanumeric, hyphens, underscores only) if (!/^[a-zA-Z0-9-_]+$/.test(realm)) { errors.push('Realm contains invalid characters. Only alphanumeric, hyphens, and underscores are allowed'); } // Check length constraints if (realm.length > 50) { errors.push('Realm cannot be longer than 50 characters'); } } /** * Validates environment value */ validateEnvironment(environment, errors) { if (!environment) { return; // Already handled in required fields } const validEnvironments = ['qa', 'prod']; if (!validEnvironments.includes(environment)) { errors.push(`Invalid environment: ${environment}. Valid values are: ${validEnvironments.join(', ')}`); } } /** * Validates auth type specific fields */ validateAuthTypeSpecificFields(config, errors) { switch (config.type) { case 'key-cloak': this.validateKeyClockConfig(config, errors); break; case 'existing-token': this.validateTokenConfig(config, errors); break; default: errors.push(`Unknown authentication type: ${config.type}`); } } /** * Validates KeyCloak specific configuration */ validateKeyClockConfig(config, errors) { if (!config.credentials) { errors.push('Credentials are required for KeyCloak authentication'); return; } const { credentials } = config; if (!credentials.grant_type) { errors.push('Grant type is required for KeyCloak authentication'); } if (!credentials.client_id) { errors.push('Client ID is required for KeyCloak authentication'); } if (!credentials.client_secret) { errors.push('Client secret is required for KeyCloak authentication'); } // Validate client_id format if (credentials.client_id !== undefined && credentials.client_id.trim() === '') { errors.push('Client ID cannot be empty'); } } /** * Validates token-based authentication configuration */ validateTokenConfig(config, errors) { if (!config.token) { errors.push('Token is required for token-based authentication'); return; } const { token } = config; if (!token.accessToken) { errors.push('Access token is required'); } if (!token.refreshToken) { errors.push('Refresh token is required'); } // Validate access token format (basic validation) if (token.accessToken && !token.accessToken.trim()) { errors.push('Access token cannot be empty'); } // Validate client ID if provided if (config.clientId && !config.clientId.trim()) { errors.push('Client ID cannot be empty'); } } } exports.ConfigurationValidator = ConfigurationValidator; //# sourceMappingURL=configuration_validator.js.map