UNPKG

3.93 kBJavaScriptView Raw
1"use strict";
2/**
3 * -------------------------------------------------------------------------------------------
4 * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
5 * See License in the project root for license information.
6 * -------------------------------------------------------------------------------------------
7 */
8Object.defineProperty(exports, "__esModule", { value: true });
9exports.TokenCredentialAuthenticationProvider = void 0;
10var tslib_1 = require("tslib");
11var GraphClientError_1 = require("../../GraphClientError");
12/**
13 * @module TokenCredentialAuthenticationProvider
14 */
15/**
16 * @class
17 * Class representing TokenCredentialAuthenticationProvider
18 * This feature is introduced in Version 3.0.0
19 * @extends AuthenticationProvider
20 * Reference - https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/identity/identity/README.md
21 */
22var TokenCredentialAuthenticationProvider = /** @class */ (function () {
23 /**
24 * @public
25 * @constructor
26 * Creates an instance of TokenCredentialAuthenticationProvider
27 * @param {TokenCredential} tokenCredential - An instance of @azure/identity TokenCredential
28 * @param {TokenCredentialAuthenticationProviderOptions} authenticationProviderOptions - An instance of TokenCredentialAuthenticationProviderOptions
29 * @returns An instance of TokenCredentialAuthenticationProvider
30 */
31 function TokenCredentialAuthenticationProvider(tokenCredential, authenticationProviderOptions) {
32 if (!tokenCredential) {
33 throw new GraphClientError_1.GraphClientError("Please pass a token credential object to the TokenCredentialAuthenticationProvider class constructor");
34 }
35 if (!authenticationProviderOptions) {
36 throw new GraphClientError_1.GraphClientError("Please pass the TokenCredentialAuthenticationProviderOptions with scopes to the TokenCredentialAuthenticationProvider class constructor");
37 }
38 this.authenticationProviderOptions = authenticationProviderOptions;
39 this.tokenCredential = tokenCredential;
40 }
41 /**
42 * @public
43 * @async
44 * To get the access token
45 * @param {TokenCredentialAuthenticationProviderOptions} authenticationProviderOptions - The authentication provider options object
46 * @returns The promise that resolves to an access token
47 */
48 TokenCredentialAuthenticationProvider.prototype.getAccessToken = function () {
49 return tslib_1.__awaiter(this, void 0, void 0, function () {
50 var scopes, error, response;
51 return tslib_1.__generator(this, function (_a) {
52 switch (_a.label) {
53 case 0:
54 scopes = this.authenticationProviderOptions.scopes;
55 error = new GraphClientError_1.GraphClientError();
56 if (!scopes || scopes.length === 0) {
57 error.name = "Empty Scopes";
58 error.message = "Scopes cannot be empty, Please provide scopes";
59 throw error;
60 }
61 return [4 /*yield*/, this.tokenCredential.getToken(scopes, this.authenticationProviderOptions.getTokenOptions)];
62 case 1:
63 response = _a.sent();
64 if (response) {
65 return [2 /*return*/, response.token];
66 }
67 error.message = "Cannot retrieve accessToken from the Token Credential object";
68 error.name = "Access token is undefined";
69 throw error;
70 }
71 });
72 });
73 };
74 return TokenCredentialAuthenticationProvider;
75}());
76exports.TokenCredentialAuthenticationProvider = TokenCredentialAuthenticationProvider;
77//# sourceMappingURL=TokenCredentialAuthenticationProvider.js.map
\No newline at end of file