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