-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConfigurationLoader.ts
99 lines (83 loc) · 3 KB
/
ConfigurationLoader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* Copyright © 2023 Nevis Security AG. All rights reserved.
*/
import { Configuration } from '@nevis-security/nevis-mobile-authentication-sdk-react';
import { AppConfiguration } from './AppConfiguration';
import authCloud from '../../assets/config_authentication_cloud.json';
import identitySuite from '../../assets/config_identity_suite.json';
import { AppErrorLoadAppConfigurationError } from '../error/AppError';
import { ErrorHandler } from '../error/ErrorHandler';
import { OperationType } from '../model/OperationType';
export enum AppEnvironment {
AuthenticationCloud,
IdentitySuite,
}
export class ConfigurationLoader {
private static instance: ConfigurationLoader;
private readonly _appEnvironment: AppEnvironment;
private _appConfiguration?: AppConfiguration;
private _sdkConfiguration?: Configuration;
private constructor() {
this._appEnvironment = AppEnvironment.AuthenticationCloud;
}
static getInstance(): ConfigurationLoader {
if (!ConfigurationLoader.instance) {
ConfigurationLoader.instance = new ConfigurationLoader();
}
return ConfigurationLoader.instance;
}
get appEnvironment() {
return this._appEnvironment;
}
get appConfiguration(): AppConfiguration {
if (this._appConfiguration) {
return this._appConfiguration;
}
this._appConfiguration = AppConfiguration.fromJson(this.configJson());
if (!this._appConfiguration) {
ErrorHandler.handle(
OperationType.unknown,
new AppErrorLoadAppConfigurationError('Could not load configuration')
);
}
return this._appConfiguration;
}
get sdkConfiguration(): Configuration {
if (this._sdkConfiguration) {
return this._sdkConfiguration;
}
const appConfiguration = this.appConfiguration;
switch (this._appEnvironment) {
case AppEnvironment.AuthenticationCloud:
this._sdkConfiguration = Configuration.authCloudBuilder()
.hostname(appConfiguration.sdk.hostname)
.facetId(appConfiguration.sdk.facetId)
.build();
break;
case AppEnvironment.IdentitySuite:
this._sdkConfiguration = Configuration.builder()
.baseUrl(appConfiguration.sdk.baseUrl)
.facetId(appConfiguration.sdk.facetId)
.registrationRequestPath(appConfiguration.sdk.registrationRequestPath!)
.registrationResponsePath(appConfiguration.sdk.registrationResponsePath!)
.authenticationRequestPath(appConfiguration.sdk.authenticationRequestPath!)
.authenticationResponsePath(appConfiguration.sdk.authenticationResponsePath!)
.deregistrationRequestPath(appConfiguration.sdk.deregistrationRequestPath!)
.dispatchTargetResourcePath(appConfiguration.sdk.dispatchTargetResourcePath!)
.deviceResourcePath(appConfiguration.sdk.deviceResourcePath!)
.build();
break;
}
return this._sdkConfiguration;
}
private configJson() {
switch (this._appEnvironment) {
case AppEnvironment.AuthenticationCloud:
return authCloud;
case AppEnvironment.IdentitySuite:
return identitySuite;
default:
throw new AppErrorLoadAppConfigurationError('Unsupported environment.');
}
}
}