/
applicationCredential.ts
63 lines (56 loc) · 2.24 KB
/
applicationCredential.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { TokenCredential } from "@azure/core-auth";
import { TokenCredentialOptions } from "../client/identityClient";
import { ChainedTokenCredential } from "./chainedTokenCredential";
import { EnvironmentCredential } from "./environmentCredential";
import { CredentialPersistenceOptions } from "./credentialPersistenceOptions";
import { DefaultManagedIdentityCredential } from "./defaultAzureCredential";
/**
* Provides options to configure the {@link ApplicationCredential} class.
*/
export interface ApplicationCredentialOptions
extends TokenCredentialOptions,
CredentialPersistenceOptions {
/**
* Optionally pass in a user assigned client ID to be used by the {@link ManagedIdentityCredential}.
* This client ID can also be passed through to the {@link ManagedIdentityCredential} through the environment variable: AZURE_CLIENT_ID.
*/
managedIdentityClientId?: string;
}
/**
* The type of a class that implements TokenCredential and accepts
* `ApplicationCredentialOptions`.
*/
interface ApplicationCredentialConstructor {
new (options?: ApplicationCredentialOptions): TokenCredential;
}
export const ApplicationCredentials: ApplicationCredentialConstructor[] = [
EnvironmentCredential,
DefaultManagedIdentityCredential
];
/**
* Provides a default {@link ChainedTokenCredential} configuration that should
* work for most applications that use the Azure SDK. The following credential
* types will be tried, in order:
*
* - {@link EnvironmentCredential}
* - {@link ManagedIdentityCredential}
*
* Consult the documentation of these credential types for more information
* on how they attempt authentication.
*/
export class ApplicationCredential extends ChainedTokenCredential {
/**
* Creates an instance of the ApplicationCredential class.
*
* @param options - Optional parameters. See {@link ApplicationCredentialOptions}.
*/
constructor(options?: ApplicationCredentialOptions) {
super(...ApplicationCredentials.map((ctor) => new ctor(options)));
this.UnavailableMessage =
"ApplicationCredential => failed to retrieve a token from the included credentials";
}
}