/
appServiceMsi2017.ts
68 lines (58 loc) · 1.88 KB
/
appServiceMsi2017.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { AccessToken, GetTokenOptions } from "@azure/core-auth";
import { RequestPrepareOptions } from "@azure/core-http";
import { IdentityClient } from "../../client/identityClient";
import { credentialLogger } from "../../util/logging";
import { MSI } from "./models";
import { msiGenericGetToken } from "./utils";
const logger = credentialLogger("ManagedIdentityCredential - AppServiceMSI 2017");
function expiresInParser(requestBody: any): number {
// Parse a date format like "06/20/2019 02:57:58 +00:00" and
// convert it into a JavaScript-formatted date
return Date.parse(requestBody.expires_on);
}
function prepareRequestOptions(resource: string, clientId?: string): RequestPrepareOptions {
const queryParameters: any = {
resource,
"api-version": "2017-09-01"
};
if (clientId) {
queryParameters.clientid = clientId;
}
return {
url: process.env.MSI_ENDPOINT,
method: "GET",
queryParameters,
headers: {
Accept: "application/json",
secret: process.env.MSI_SECRET
}
};
}
export const appServiceMsi2017: MSI = {
async isAvailable(): Promise<boolean> {
const env = process.env;
const result = Boolean(env.MSI_ENDPOINT && env.MSI_SECRET);
if (!result) {
logger.info("The Azure App Service MSI 2017 is unavailable.");
}
return result;
},
async getToken(
identityClient: IdentityClient,
resource: string,
clientId?: string,
getTokenOptions: GetTokenOptions = {}
): Promise<AccessToken | null> {
logger.info(
`Using the endpoint and the secret coming form the environment variables: MSI_ENDPOINT=${process.env.MSI_ENDPOINT} and MSI_SECRET=[REDACTED].`
);
return msiGenericGetToken(
identityClient,
prepareRequestOptions(resource, clientId),
expiresInParser,
getTokenOptions
);
}
};