UNPKG

3.13 kBJavaScriptView Raw
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License. See License.txt in the project root for license information.
3import { __awaiter, __generator } from "tslib";
4import { Constants as MSRestConstants } from "../util/constants";
5var DEFAULT_AUTHORIZATION_SCHEME = "Bearer";
6/**
7 * Resource manager endpoints to match in order to specify a valid scope to the AzureIdentityCredentialAdapter.
8 */
9export var azureResourceManagerEndpoints = [
10 "https://management.windows.net",
11 "https://management.chinacloudapi.cn",
12 "https://management.usgovcloudapi.net",
13 "https://management.cloudapi.de",
14];
15/**
16 * This class provides a simple extension to use {@link TokenCredential} from `@azure/identity` library to
17 * use with legacy Azure SDKs that accept {@link ServiceClientCredentials} family of credentials for authentication.
18 */
19var AzureIdentityCredentialAdapter = /** @class */ (function () {
20 function AzureIdentityCredentialAdapter(azureTokenCredential, scopes) {
21 if (scopes === void 0) { scopes = "https://management.azure.com/.default"; }
22 this.azureTokenCredential = azureTokenCredential;
23 this.scopes = scopes;
24 }
25 AzureIdentityCredentialAdapter.prototype.getToken = function () {
26 return __awaiter(this, void 0, void 0, function () {
27 var accessToken, result;
28 return __generator(this, function (_a) {
29 switch (_a.label) {
30 case 0: return [4 /*yield*/, this.azureTokenCredential.getToken(this.scopes)];
31 case 1:
32 accessToken = _a.sent();
33 if (accessToken !== null) {
34 result = {
35 accessToken: accessToken.token,
36 tokenType: DEFAULT_AUTHORIZATION_SCHEME,
37 expiresOn: accessToken.expiresOnTimestamp,
38 };
39 return [2 /*return*/, result];
40 }
41 else {
42 throw new Error("Could find token for scope");
43 }
44 return [2 /*return*/];
45 }
46 });
47 });
48 };
49 AzureIdentityCredentialAdapter.prototype.signRequest = function (webResource) {
50 return __awaiter(this, void 0, void 0, function () {
51 var tokenResponse;
52 return __generator(this, function (_a) {
53 switch (_a.label) {
54 case 0: return [4 /*yield*/, this.getToken()];
55 case 1:
56 tokenResponse = _a.sent();
57 webResource.headers.set(MSRestConstants.HeaderConstants.AUTHORIZATION, tokenResponse.tokenType + " " + tokenResponse.accessToken);
58 return [2 /*return*/, Promise.resolve(webResource)];
59 }
60 });
61 });
62 };
63 return AzureIdentityCredentialAdapter;
64}());
65export { AzureIdentityCredentialAdapter };
66//# sourceMappingURL=azureIdentityTokenCredentialAdapter.js.map
\No newline at end of file