@infosel-sdk/core
Version:
Core SDK for Infosel financial services platform. Provides essential infrastructure for authentication, HTTP/GraphQL communication, storage management, and error handling.
111 lines • 3.18 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthConfigurationBuilder = void 0;
const configuration_validator_1 = require("./configuration_validator");
class AuthConfigurationBuilder {
constructor() {
this.config = {};
this.validator = new configuration_validator_1.ConfigurationValidator();
}
/**
* Sets the authentication type
*/
withType(type) {
this.config.type = type;
return this;
}
/**
* Sets the realm for authentication
*/
withRealm(realm) {
this.config.realm = realm;
return this;
}
/**
* Sets the environment (qa or prod)
*/
withEnvironment(environment) {
this.config.environment = environment;
return this;
}
/**
* Sets the client ID (optional, defaults per auth type)
*/
withClientId(clientId) {
this.config.clientId = clientId;
return this;
}
/**
* Sets the credentials for KeyCloak authentication
*/
withCredentials(credentials) {
this.config.credentials = credentials;
return this;
}
/**
* Sets the token for token-based authentication
*/
withToken(token) {
this.config.token = token;
return this;
}
/**
* Creates a KeyCloak configuration
*/
static keyCloak() {
return new AuthConfigurationBuilder().withType('key-cloak');
}
/**
* Creates a token-based configuration
*/
static existingToken() {
return new AuthConfigurationBuilder().withType('existing-token');
}
/**
* Validates the current configuration
*/
validate() {
// Create a copy and apply defaults for validation
const configCopy = Object.assign({}, this.config);
this.applyDefaultsToConfig(configCopy);
return this.validator.validate(configCopy);
}
/**
* Builds the final AuthConfiguration with validation and defaults
*/
build() {
// Apply defaults
this.applyDefaults();
// Validate configuration (without applying defaults again)
const validationResult = this.validator.validate(this.config);
if (!validationResult.isValid) {
throw new Error(`Invalid authentication configuration: ${validationResult.errors.join(', ')}`);
}
return this.config;
}
/**
* Applies default values based on the auth type
*/
applyDefaults() {
this.applyDefaultsToConfig(this.config);
}
/**
* Applies default values to a configuration object
*/
applyDefaultsToConfig(config) {
// Default realm
if (!config.realm) {
config.realm = 'hub';
}
// Default environment
if (!config.environment) {
config.environment = 'prod';
}
// Default client ID based on auth type
if (!config.clientId && config.type === 'existing-token') {
config.clientId = 'hub-app';
}
}
}
exports.AuthConfigurationBuilder = AuthConfigurationBuilder;
//# sourceMappingURL=auth_configuration_builder.js.map