- Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathAuthenticationHandler.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
*/
exportclassAuthenticationHandlerimplementsMiddleware{
/**
* @private
* A member representing the authorization header name
*/
privatestaticAUTHORIZATION_HEADER="Authorization";
/**
* @private
* A member to hold an AuthenticationProvider instance
*/
privateauthenticationProvider: AuthenticationProvider;
/**
* @private
* A member to hold next middleware in the middleware chain
*/
privatenextMiddleware: Middleware;
/**
* @public
* @constructor
* Creates an instance of AuthenticationHandler
* @param {AuthenticationProvider} authenticationProvider - The authentication provider for the authentication handler
*/
publicconstructor(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
*/
publicasyncexecute(context: Context): Promise<void>{
consturl=typeofcontext.request==="string" ? context.request : context.request.url;
if(isGraphURL(url)||(context.customHosts&&isCustomHost(url,context.customHosts))){
letoptions: AuthenticationHandlerOptions;
if(context.middlewareControlinstanceofMiddlewareControl){
options=context.middlewareControl.getMiddlewareOptions(AuthenticationHandlerOptions)asAuthenticationHandlerOptions;
}
letauthenticationProvider: AuthenticationProvider;
letauthenticationProviderOptions: AuthenticationProviderOptions;
if(options){
authenticationProvider=options.authenticationProvider;
authenticationProviderOptions=options.authenticationProviderOptions;
}
if(!authenticationProvider){
authenticationProvider=this.authenticationProvider;
}
consttoken: string=awaitauthenticationProvider.getAccessToken(authenticationProviderOptions);
constbearerKey=`Bearer ${token}`;
appendRequestHeader(context.request,context.options,AuthenticationHandler.AUTHORIZATION_HEADER,bearerKey);
TelemetryHandlerOptions.updateFeatureUsageFlag(context,FeatureUsageFlag.AUTHENTICATION_HANDLER_ENABLED);
}else{
if(context.options.headers){
deletecontext.options.headers[AuthenticationHandler.AUTHORIZATION_HEADER];
}
}
returnawaitthis.nextMiddleware.execute(context);
}
/**
* @public
* To set the next middleware in the chain
* @param {Middleware} next - The middleware instance
* @returns Nothing
*/
publicsetNext(next: Middleware): void{
this.nextMiddleware=next;
}
}