Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/azure-pipelines/common/getPublishAuthTokens.ts
3520 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { AccessToken } from '@azure/core-auth';
7
import { ConfidentialClientApplication } from '@azure/msal-node';
8
9
function e(name: string): string {
10
const result = process.env[name];
11
12
if (typeof result !== 'string') {
13
throw new Error(`Missing env: ${name}`);
14
}
15
16
return result;
17
}
18
19
export async function getAccessToken(endpoint: string, tenantId: string, clientId: string, idToken: string): Promise<AccessToken> {
20
const app = new ConfidentialClientApplication({
21
auth: {
22
clientId,
23
authority: `https://login.microsoftonline.com/${tenantId}`,
24
clientAssertion: idToken
25
}
26
});
27
28
const result = await app.acquireTokenByClientCredential({ scopes: [`${endpoint}.default`] });
29
30
if (!result) {
31
throw new Error('Failed to get access token');
32
}
33
34
return {
35
token: result.accessToken,
36
expiresOnTimestamp: result.expiresOn!.getTime(),
37
refreshAfterTimestamp: result.refreshOn?.getTime()
38
};
39
}
40
41
async function main() {
42
const cosmosDBAccessToken = await getAccessToken(e('AZURE_DOCUMENTDB_ENDPOINT')!, e('AZURE_TENANT_ID')!, e('AZURE_CLIENT_ID')!, e('AZURE_ID_TOKEN')!);
43
const blobServiceAccessToken = await getAccessToken(`https://${e('VSCODE_STAGING_BLOB_STORAGE_ACCOUNT_NAME')}.blob.core.windows.net/`, process.env['AZURE_TENANT_ID']!, process.env['AZURE_CLIENT_ID']!, process.env['AZURE_ID_TOKEN']!);
44
console.log(JSON.stringify({ cosmosDBAccessToken, blobServiceAccessToken }));
45
}
46
47
if (require.main === module) {
48
main().then(() => {
49
process.exit(0);
50
}, err => {
51
console.error(err);
52
process.exit(1);
53
});
54
}
55
56