UNPKG

4.99 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.AuthCodeMSALBrowserAuthenticationProvider = void 0;
10var tslib_1 = require("tslib");
11/**
12 * @module AuthCodeMSALBrowserAuthenticationProvider
13 */
14var msal_browser_1 = require("@azure/msal-browser");
15var GraphClientError_1 = require("../../GraphClientError");
16/**
17 * an AuthenticationProvider implementation supporting msal-browser library.
18 * This feature is introduced in Version 3.0.0
19 * @class
20 * @extends AuthenticationProvider
21 */
22var AuthCodeMSALBrowserAuthenticationProvider = /** @class */ (function () {
23 /**
24 * @public
25 * @constructor
26 * Creates an instance of ImplicitMSALAuthenticationProvider
27 * @param {PublicClientApplication} msalApplication - An instance of MSAL PublicClientApplication
28 * @param {AuthCodeMSALBrowserAuthenticationProviderOptions} options - An instance of MSALAuthenticationProviderOptions
29 * @returns An instance of ImplicitMSALAuthenticationProvider
30 */
31 function AuthCodeMSALBrowserAuthenticationProvider(publicClientApplication, options) {
32 this.publicClientApplication = publicClientApplication;
33 this.options = options;
34 if (!options || !publicClientApplication) {
35 throw new GraphClientError_1.GraphClientError("Please pass valid PublicClientApplication instance and AuthCodeMSALBrowserAuthenticationProviderOptions instance to instantiate MSALBrowserAuthenticationProvider");
36 }
37 }
38 /**
39 * @public
40 * @async
41 * To get the access token for the request
42 * @returns The promise that resolves to an access token
43 */
44 AuthCodeMSALBrowserAuthenticationProvider.prototype.getAccessToken = function () {
45 return tslib_1.__awaiter(this, void 0, void 0, function () {
46 var scopes, account, error, response, error_1, response;
47 return tslib_1.__generator(this, function (_a) {
48 switch (_a.label) {
49 case 0:
50 scopes = this.options && this.options.scopes;
51 account = this.options && this.options.account;
52 error = new GraphClientError_1.GraphClientError();
53 if (!scopes || scopes.length === 0) {
54 error.name = "Empty Scopes";
55 error.message = "Scopes cannot be empty, Please provide scopes";
56 throw error;
57 }
58 _a.label = 1;
59 case 1:
60 _a.trys.push([1, 3, , 9]);
61 return [4 /*yield*/, this.publicClientApplication.acquireTokenSilent({
62 scopes: scopes,
63 account: account,
64 })];
65 case 2:
66 response = _a.sent();
67 if (!response || !response.accessToken) {
68 error.name = "Access token is undefined";
69 error.message = "Received empty access token from PublicClientApplication";
70 throw error;
71 }
72 return [2 /*return*/, response.accessToken];
73 case 3:
74 error_1 = _a.sent();
75 if (!(error_1 instanceof msal_browser_1.InteractionRequiredAuthError)) return [3 /*break*/, 7];
76 if (!(this.options.interactionType === msal_browser_1.InteractionType.Redirect)) return [3 /*break*/, 4];
77 this.publicClientApplication.acquireTokenRedirect({ scopes: scopes });
78 return [3 /*break*/, 6];
79 case 4:
80 if (!(this.options.interactionType === msal_browser_1.InteractionType.Popup)) return [3 /*break*/, 6];
81 return [4 /*yield*/, this.publicClientApplication.acquireTokenPopup({ scopes: scopes })];
82 case 5:
83 response = _a.sent();
84 return [2 /*return*/, response.accessToken];
85 case 6: return [3 /*break*/, 8];
86 case 7: throw error_1;
87 case 8: return [3 /*break*/, 9];
88 case 9: return [2 /*return*/];
89 }
90 });
91 });
92 };
93 return AuthCodeMSALBrowserAuthenticationProvider;
94}());
95exports.AuthCodeMSALBrowserAuthenticationProvider = AuthCodeMSALBrowserAuthenticationProvider;
96//# sourceMappingURL=AuthCodeMSALBrowserAuthenticationProvider.js.map
\No newline at end of file