1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | import { isCustomHost, isGraphURL } from "../GraphRequestUtil";
|
13 | import { AuthenticationProvider } from "../IAuthenticationProvider";
|
14 | import { AuthenticationProviderOptions } from "../IAuthenticationProviderOptions";
|
15 | import { Context } from "../IContext";
|
16 | import { Middleware } from "./IMiddleware";
|
17 | import { MiddlewareControl } from "./MiddlewareControl";
|
18 | import { appendRequestHeader } from "./MiddlewareUtil";
|
19 | import { AuthenticationHandlerOptions } from "./options/AuthenticationHandlerOptions";
|
20 | import { FeatureUsageFlag, TelemetryHandlerOptions } from "./options/TelemetryHandlerOptions";
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | export class AuthenticationHandler implements Middleware {
|
28 | |
29 |
|
30 |
|
31 |
|
32 | private static AUTHORIZATION_HEADER = "Authorization";
|
33 |
|
34 | |
35 |
|
36 |
|
37 |
|
38 | private authenticationProvider: AuthenticationProvider;
|
39 |
|
40 | |
41 |
|
42 |
|
43 |
|
44 | private nextMiddleware: Middleware;
|
45 |
|
46 | |
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | public constructor(authenticationProvider: AuthenticationProvider) {
|
53 | this.authenticationProvider = authenticationProvider;
|
54 | }
|
55 |
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
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 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | public setNext(next: Middleware): void {
|
98 | this.nextMiddleware = next;
|
99 | }
|
100 | }
|