-
Notifications
You must be signed in to change notification settings - Fork 15
/
azureIdentityCredentialAdapter.ts
38 lines (34 loc) · 1.6 KB
/
azureIdentityCredentialAdapter.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
import { ServiceClientCredentials } from "@azure/ms-rest-js";
import { DefaultAzureCredential, TokenCredential } from "@azure/identity";
import { Constants as MSRestConstants, WebResource } from "@azure/ms-rest-js";
import { TokenResponse } from "@azure/ms-rest-nodeauth/dist/lib/credentials/tokenClientCredentials";
const DEFAULT_AUTHORIZATION_SCHEME = "Bearer";
/**
* This class provides a simple extension to use {@link TokenCredential} from com.azure:azure-identity library to
* use with legacy Azure SDKs that accept {@link ServiceClientCredentials} family of credentials for authentication.
*/
export class AzureIdentityCredentialAdapter implements ServiceClientCredentials {
private azureTokenCredential: TokenCredential;
private scopes: string | string[];
constructor(
azureTokenCredential = new DefaultAzureCredential(),
scopes: string | string[] = "https://management.azure.com/.default",
) {
this.azureTokenCredential = azureTokenCredential;
this.scopes = scopes;
}
public async getToken(): Promise<TokenResponse>{
const accessToken = (await this.azureTokenCredential.getToken(this.scopes));
const result: TokenResponse = {
accessToken: accessToken.token,
tokenType: DEFAULT_AUTHORIZATION_SCHEME,
expiresOn: accessToken.expiresOnTimestamp,
};
return result;
}
public async signRequest(webResource: WebResource) {
const tokenResponse = await this.getToken();
webResource.headers.set(MSRestConstants.HeaderConstants.AUTHORIZATION, `${tokenResponse.tokenType} ${tokenResponse.accessToken}`);
return Promise.resolve(webResource);
}
}