UNPKG

3.69 kBPlain TextView Raw
1/**
2 * -------------------------------------------------------------------------------------------
3 * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
4 * See License in the project root for license information.
5 * -------------------------------------------------------------------------------------------
6 */
7
8/**
9 * @module AuthenticationHandler
10 */
11
12import { isCustomHost, isGraphURL } from "../GraphRequestUtil";
13import { AuthenticationProvider } from "../IAuthenticationProvider";
14import { AuthenticationProviderOptions } from "../IAuthenticationProviderOptions";
15import { Context } from "../IContext";
16import { Middleware } from "./IMiddleware";
17import { MiddlewareControl } from "./MiddlewareControl";
18import { appendRequestHeader } from "./MiddlewareUtil";
19import { AuthenticationHandlerOptions } from "./options/AuthenticationHandlerOptions";
20import { FeatureUsageFlag, TelemetryHandlerOptions } from "./options/TelemetryHandlerOptions";
21
22/**
23 * @class
24 * @implements Middleware
25 * Class representing AuthenticationHandler
26 */
27export class AuthenticationHandler implements Middleware {
28 /**
29 * @private
30 * A member representing the authorization header name
31 */
32 private static AUTHORIZATION_HEADER = "Authorization";
33
34 /**
35 * @private
36 * A member to hold an AuthenticationProvider instance
37 */
38 private authenticationProvider: AuthenticationProvider;
39
40 /**
41 * @private
42 * A member to hold next middleware in the middleware chain
43 */
44 private nextMiddleware: Middleware;
45
46 /**
47 * @public
48 * @constructor
49 * Creates an instance of AuthenticationHandler
50 * @param {AuthenticationProvider} authenticationProvider - The authentication provider for the authentication handler
51 */
52 public constructor(authenticationProvider: AuthenticationProvider) {
53 this.authenticationProvider = authenticationProvider;
54 }
55
56 /**
57 * @public
58 * @async
59 * To execute the current middleware
60 * @param {Context} context - The context object of the request
61 * @returns A Promise that resolves to nothing
62 */
63 public async execute(context: Context): Promise<void> {
64 const url = typeof context.request === "string" ? context.request : context.request.url;
65 if (isGraphURL(url) || (context.customHosts && isCustomHost(url, context.customHosts))) {
66 let options: AuthenticationHandlerOptions;
67 if (context.middlewareControl instanceof MiddlewareControl) {
68 options = context.middlewareControl.getMiddlewareOptions(AuthenticationHandlerOptions) as AuthenticationHandlerOptions;
69 }
70 let authenticationProvider: AuthenticationProvider;
71 let authenticationProviderOptions: AuthenticationProviderOptions;
72 if (options) {
73 authenticationProvider = options.authenticationProvider;
74 authenticationProviderOptions = options.authenticationProviderOptions;
75 }
76 if (!authenticationProvider) {
77 authenticationProvider = this.authenticationProvider;
78 }
79 const token: string = await authenticationProvider.getAccessToken(authenticationProviderOptions);
80 const bearerKey = `Bearer ${token}`;
81 appendRequestHeader(context.request, context.options, AuthenticationHandler.AUTHORIZATION_HEADER, bearerKey);
82 TelemetryHandlerOptions.updateFeatureUsageFlag(context, FeatureUsageFlag.AUTHENTICATION_HANDLER_ENABLED);
83 } else {
84 if (context.options.headers) {
85 delete context.options.headers[AuthenticationHandler.AUTHORIZATION_HEADER];
86 }
87 }
88 return await this.nextMiddleware.execute(context);
89 }
90
91 /**
92 * @public
93 * To set the next middleware in the chain
94 * @param {Middleware} next - The middleware instance
95 * @returns Nothing
96 */
97 public setNext(next: Middleware): void {
98 this.nextMiddleware = next;
99 }
100}