This repository has been archived by the owner on Jun 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathWebAppAuthClientBuilder.ts
56 lines (46 loc) · 2.24 KB
/
WebAppAuthClientBuilder.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
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { Configuration } from '@azure/msal-node';
import { BaseAuthClientBuilder } from '../BaseAuthClientBuilder';
import { MsalWebAppAuthClient } from './MsalWebAppAuthClient';
import { AppServiceWebAppAuthClient } from './AppServiceWebAppAuthClient';
import { KeyVaultManager } from '../../network/KeyVaultManager';
import { MsalConfiguration } from '../../config/MsalConfiguration';
import { AppSettings, AppType } from '../../config/AppSettings';
import { EnvironmentUtils } from '../../utils/EnvironmentUtils';
import { ErrorMessages } from '../../utils/Constants';
export class WebAppAuthClientBuilder extends BaseAuthClientBuilder {
appSettings!: AppSettings;
private msalConfig!: Configuration;
constructor(appSettings: AppSettings) {
super(appSettings, AppType.WebApp);
}
build(): MsalWebAppAuthClient | AppServiceWebAppAuthClient {
// TODO: throw error if key vault credential is being built
this.msalConfig = MsalConfiguration.getMsalConfiguration(this.appSettings);
if (EnvironmentUtils.isAppServiceAuthEnabled()) {
return new AppServiceWebAppAuthClient(this.appSettings, this.msalConfig);
} else {
return new MsalWebAppAuthClient(this.appSettings, this.msalConfig);
}
}
async buildAsync(): Promise<MsalWebAppAuthClient | AppServiceWebAppAuthClient> {
try {
if (this.keyVaultCredential) {
const keyVaultManager = new KeyVaultManager();
const credential = await keyVaultManager.getCredentialFromKeyVault(this.keyVaultCredential);
this.appSettings.appCredentials[credential.type] = credential.value;
}
this.msalConfig = MsalConfiguration.getMsalConfiguration(this.appSettings);
if (EnvironmentUtils.isAppServiceAuthEnabled()) {
return new AppServiceWebAppAuthClient(this.appSettings, this.msalConfig);
} else {
return new MsalWebAppAuthClient(this.appSettings, this.msalConfig);
}
} catch (error) {
throw new Error(ErrorMessages.CANNOT_OBTAIN_CREDENTIALS_FROM_KEY_VAULT);
}
}
}