UNPKG

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