From 773fbbe03fc1c60cb477b7f8d44fe4f505c94d61 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Thu, 10 Oct 2024 04:00:18 +0300 Subject: [PATCH] feat: Add create() overload in KiotaClientFactory to add authentication middleware to the chain --- .../httpClient/ContinuousAccessEvaluation.cs | 3 +-- src/http/httpClient/KiotaClientFactory.cs | 24 +++++++++++++++++++ .../Middleware/AuthorizationHandler.cs | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/http/httpClient/ContinuousAccessEvaluation.cs b/src/http/httpClient/ContinuousAccessEvaluation.cs index 8bdc4c10..4f840668 100644 --- a/src/http/httpClient/ContinuousAccessEvaluation.cs +++ b/src/http/httpClient/ContinuousAccessEvaluation.cs @@ -9,9 +9,8 @@ namespace Microsoft.Kiota.Http.HttpClientLibrary /// /// Process continuous access evaluation /// - internal class ContinuousAccessEvaluation + static internal class ContinuousAccessEvaluation { - internal const string ClaimsKey = "claims"; internal const string BearerAuthenticationScheme = "Bearer"; private static readonly char[] ComaSplitSeparator = [',']; diff --git a/src/http/httpClient/KiotaClientFactory.cs b/src/http/httpClient/KiotaClientFactory.cs index 5a89c01a..c13f8b7b 100644 --- a/src/http/httpClient/KiotaClientFactory.cs +++ b/src/http/httpClient/KiotaClientFactory.cs @@ -61,6 +61,30 @@ public static HttpClient Create(IList handlers, HttpMessageHa return handler != null ? new HttpClient(handler) : new HttpClient(); } + /// + /// Initializes the with the default configuration and authentication middleware using the if provided. + /// + /// + /// + /// + /// + public static HttpClient Create(BaseBearerTokenAuthenticationProvider authenticationProvider, IRequestOption[]? optionsForHandlers = null, HttpMessageHandler? finalHandler = null) + { + var defaultHandlersEnumerable = CreateDefaultHandlers(optionsForHandlers); + defaultHandlersEnumerable.Add(new AuthorizationHandler(authenticationProvider)); + int count = 0; + foreach(var _ in defaultHandlersEnumerable) count++; + + var defaultHandlersArray = new DelegatingHandler[count]; + int index = 0; + foreach(var handler2 in defaultHandlersEnumerable) + { + defaultHandlersArray[index++] = handler2; + } + var handler = ChainHandlersCollectionAndGetFirstLink(finalHandler ?? GetDefaultHttpMessageHandler(), defaultHandlersArray); + return handler != null ? new HttpClient(handler) : new HttpClient(); + } + /// /// Creates a default set of middleware to be used by the . /// diff --git a/src/http/httpClient/Middleware/AuthorizationHandler.cs b/src/http/httpClient/Middleware/AuthorizationHandler.cs index 795115d5..6eb3ef9e 100644 --- a/src/http/httpClient/Middleware/AuthorizationHandler.cs +++ b/src/http/httpClient/Middleware/AuthorizationHandler.cs @@ -24,7 +24,7 @@ public class AuthorizationHandler : DelegatingHandler { private const string AuthorizationHeader = "Authorization"; - private BaseBearerTokenAuthenticationProvider authenticationProvider; + private readonly BaseBearerTokenAuthenticationProvider authenticationProvider; /// /// Constructs an