UNPKG

4.75 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.AuthenticationHandler = void 0;
10var tslib_1 = require("tslib");
11/**
12 * @module AuthenticationHandler
13 */
14var GraphRequestUtil_1 = require("../GraphRequestUtil");
15var MiddlewareControl_1 = require("./MiddlewareControl");
16var MiddlewareUtil_1 = require("./MiddlewareUtil");
17var AuthenticationHandlerOptions_1 = require("./options/AuthenticationHandlerOptions");
18var TelemetryHandlerOptions_1 = require("./options/TelemetryHandlerOptions");
19/**
20 * @class
21 * @implements Middleware
22 * Class representing AuthenticationHandler
23 */
24var AuthenticationHandler = /** @class */ (function () {
25 /**
26 * @public
27 * @constructor
28 * Creates an instance of AuthenticationHandler
29 * @param {AuthenticationProvider} authenticationProvider - The authentication provider for the authentication handler
30 */
31 function AuthenticationHandler(authenticationProvider) {
32 this.authenticationProvider = authenticationProvider;
33 }
34 /**
35 * @public
36 * @async
37 * To execute the current middleware
38 * @param {Context} context - The context object of the request
39 * @returns A Promise that resolves to nothing
40 */
41 AuthenticationHandler.prototype.execute = function (context) {
42 return tslib_1.__awaiter(this, void 0, void 0, function () {
43 var url, options, authenticationProvider, authenticationProviderOptions, token, bearerKey;
44 return tslib_1.__generator(this, function (_a) {
45 switch (_a.label) {
46 case 0:
47 url = typeof context.request === "string" ? context.request : context.request.url;
48 if (!((0, GraphRequestUtil_1.isGraphURL)(url) || (context.customHosts && (0, GraphRequestUtil_1.isCustomHost)(url, context.customHosts)))) return [3 /*break*/, 2];
49 options = void 0;
50 if (context.middlewareControl instanceof MiddlewareControl_1.MiddlewareControl) {
51 options = context.middlewareControl.getMiddlewareOptions(AuthenticationHandlerOptions_1.AuthenticationHandlerOptions);
52 }
53 authenticationProvider = void 0;
54 authenticationProviderOptions = void 0;
55 if (options) {
56 authenticationProvider = options.authenticationProvider;
57 authenticationProviderOptions = options.authenticationProviderOptions;
58 }
59 if (!authenticationProvider) {
60 authenticationProvider = this.authenticationProvider;
61 }
62 return [4 /*yield*/, authenticationProvider.getAccessToken(authenticationProviderOptions)];
63 case 1:
64 token = _a.sent();
65 bearerKey = "Bearer ".concat(token);
66 (0, MiddlewareUtil_1.appendRequestHeader)(context.request, context.options, AuthenticationHandler.AUTHORIZATION_HEADER, bearerKey);
67 TelemetryHandlerOptions_1.TelemetryHandlerOptions.updateFeatureUsageFlag(context, TelemetryHandlerOptions_1.FeatureUsageFlag.AUTHENTICATION_HANDLER_ENABLED);
68 return [3 /*break*/, 3];
69 case 2:
70 if (context.options.headers) {
71 delete context.options.headers[AuthenticationHandler.AUTHORIZATION_HEADER];
72 }
73 _a.label = 3;
74 case 3: return [4 /*yield*/, this.nextMiddleware.execute(context)];
75 case 4: return [2 /*return*/, _a.sent()];
76 }
77 });
78 });
79 };
80 /**
81 * @public
82 * To set the next middleware in the chain
83 * @param {Middleware} next - The middleware instance
84 * @returns Nothing
85 */
86 AuthenticationHandler.prototype.setNext = function (next) {
87 this.nextMiddleware = next;
88 };
89 /**
90 * @private
91 * A member representing the authorization header name
92 */
93 AuthenticationHandler.AUTHORIZATION_HEADER = "Authorization";
94 return AuthenticationHandler;
95}());
96exports.AuthenticationHandler = AuthenticationHandler;
97//# sourceMappingURL=AuthenticationHandler.js.map
\No newline at end of file