@infosel-sdk/core
Version:
Core SDK for Infosel financial services platform. Provides essential infrastructure for authentication, HTTP/GraphQL communication, storage management, and error handling.
93 lines • 4.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const app_memory_storage_1 = tslib_1.__importDefault(require("../data/data_access/app_memory_storage"));
const infosel_auth_interceptor_1 = tslib_1.__importDefault(require("../data/data_access/infosel_auth_interceptor"));
const key_cloak_auth_provider_1 = tslib_1.__importDefault(require("../data/data_access/key_cloak_auth_provider"));
const token_auth_provider_1 = tslib_1.__importDefault(require("../data/data_access/token_auth_provider"));
const configuration_1 = require("../domain/configuration");
const sdk_error_1 = tslib_1.__importStar(require("../errors/sdk_error"));
class InfoselSdkManager {
constructor(mode, authProvider, storage = new app_memory_storage_1.default(), realm) {
this.mode = mode;
this.authProvider = authProvider;
this.storage = storage;
this.realm = realm || 'hub';
}
// Método para obtener el realm configurado
getRealm() {
return this.realm;
}
get authInterceptor() {
if (!this._authInterceptor) {
this._authInterceptor = new infosel_auth_interceptor_1.default(this);
}
return this._authInterceptor;
}
static init({ mode = 'prod', realm, authProviderConfig, storage, }) {
// Determine the realm to use based on priority:
// 1. SDK config realm (highest priority)
// 2. Auth provider config realm
// 3. Default 'hub' (lowest priority)
const finalRealm = realm || authProviderConfig.realm || 'hub';
switch (authProviderConfig.type) {
case 'key-cloak': {
const keyCloak = new key_cloak_auth_provider_1.default(mode, Object.assign(Object.assign({}, authProviderConfig), { realm: finalRealm }));
return new InfoselSdkManager(mode, keyCloak, storage, finalRealm);
}
case 'existing-token': {
const tokenAuthProvider = new token_auth_provider_1.default(mode, authProviderConfig.token, finalRealm, authProviderConfig.clientId);
const sdkManager = new InfoselSdkManager(mode, tokenAuthProvider, storage, finalRealm);
// Inject storage into TokenAuthProvider for auto-initialization
tokenAuthProvider.setStorage(sdkManager.storage);
return sdkManager;
}
default:
throw new sdk_error_1.default(sdk_error_1.SdkErrorType.INVALID_AUTH_PROVIDER_CONFIG);
}
}
/**
* Modern initialization method using AuthConfigurationBuilder
* This provides better validation and centralized configuration management
*/
static initWithConfiguration({ authConfiguration, storage = new app_memory_storage_1.default(), }) {
// Create appropriate auth provider based on configuration
let authProvider;
switch (authConfiguration.type) {
case 'key-cloak': {
if (!authConfiguration.credentials) {
throw new sdk_error_1.default(sdk_error_1.SdkErrorType.INVALID_AUTH_PROVIDER_CONFIG, 'Credentials are required for KeyCloak authentication');
}
authProvider = new key_cloak_auth_provider_1.default(authConfiguration.environment, {
type: 'key-cloak',
credentials: authConfiguration.credentials,
realm: authConfiguration.realm,
});
break;
}
case 'existing-token': {
if (!authConfiguration.token) {
throw new sdk_error_1.default(sdk_error_1.SdkErrorType.INVALID_AUTH_PROVIDER_CONFIG, 'Token is required for token-based authentication');
}
authProvider = new token_auth_provider_1.default(authConfiguration.environment, authConfiguration.token, authConfiguration.realm, authConfiguration.clientId);
break;
}
default:
throw new sdk_error_1.default(sdk_error_1.SdkErrorType.INVALID_AUTH_PROVIDER_CONFIG);
}
const sdkManager = new InfoselSdkManager(authConfiguration.environment, authProvider, storage, authConfiguration.realm);
// Inject storage into TokenAuthProvider for auto-initialization if needed
if (authConfiguration.type === 'existing-token') {
authProvider.setStorage(sdkManager.storage);
}
return sdkManager;
}
/**
* Creates an AuthConfigurationBuilder for fluent configuration
*/
static configBuilder() {
return new configuration_1.AuthConfigurationBuilder();
}
}
exports.default = InfoselSdkManager;
//# sourceMappingURL=index.js.map