1010 */
1111
1212import { AuthenticationProvider } from "../IAuthenticationProvider" ;
13+ import { AuthenticationProviderOptions } from "../IAuthenticationProviderOptions" ;
1314import { Context } from "../IContext" ;
1415
1516import { Middleware } from "./IMiddleware" ;
17+ import { MiddlewareControl } from "./MiddlewareControl" ;
1618import { setRequestHeader } from "./MiddlewareUtil" ;
19+ import { AuthenticationHandlerOptions } from "./options/AuthenticationHandlerOptions" ;
1720
1821/**
1922 * @class
@@ -31,7 +34,7 @@ export class AuthenticationHandler implements Middleware {
3134 * @private
3235 * A member to hold an AuthenticationProvider instance
3336 */
34- private authProvider : AuthenticationProvider ;
37+ private authenticationProvider : AuthenticationProvider ;
3538
3639 /**
3740 * @private
@@ -43,23 +46,36 @@ export class AuthenticationHandler implements Middleware {
4346 * @public
4447 * @constructor
4548 * Creates an instance of AuthenticationHandler
46- * @param {AuthenticationProvider } authProvider - The authentication provider for the authentication handler
49+ * @param {AuthenticationProvider } authenticationProvider - The authentication provider for the authentication handler
4750 */
48- public constructor ( authProvider : AuthenticationProvider ) {
49- this . authProvider = authProvider ;
51+ public constructor ( authenticationProvider : AuthenticationProvider ) {
52+ this . authenticationProvider = authenticationProvider ;
5053 }
5154
5255 /**
5356 * @public
5457 * @async
5558 * To execute the current middleware
56- * @param {context } context - The context object of the request
59+ * @param {Context } context - The context object of the request
5760 * @returns A Promise that resolves to nothing
5861 */
5962 public async execute ( context : Context ) : Promise < void > {
6063 try {
61- const token = await this . authProvider . getAccessToken ( ) ;
62- const bearerKey = `Bearer ${ token } ` ;
64+ let options : AuthenticationHandlerOptions ;
65+ if ( context . middlewareControl instanceof MiddlewareControl ) {
66+ options = context . middlewareControl . getMiddlewareOptions ( AuthenticationHandler . name ) as AuthenticationHandlerOptions ;
67+ }
68+ let authenticationProvider : AuthenticationProvider ;
69+ let authenticationProviderOptions : AuthenticationProviderOptions ;
70+ if ( typeof options !== "undefined" ) {
71+ authenticationProvider = options . authenticationProvider ;
72+ authenticationProviderOptions = options . authenticationProviderOptions ;
73+ }
74+ if ( typeof authenticationProvider === "undefined" ) {
75+ authenticationProvider = this . authenticationProvider ;
76+ }
77+ const token : string = await authenticationProvider . getAccessToken ( authenticationProviderOptions ) ;
78+ const bearerKey : string = `Bearer ${ token } ` ;
6379 setRequestHeader ( context . request , context . options , AuthenticationHandler . AUTHORIZATION_HEADER , bearerKey ) ;
6480 return await this . nextMiddleware . execute ( context ) ;
6581 } catch ( error ) {
0 commit comments