Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Omondi <[email protected]>
  • Loading branch information
Ndiritu and andrueastman committed Nov 5, 2024
1 parent d5882b3 commit 2c77cce
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 17 deletions.
12 changes: 1 addition & 11 deletions src/http/httpClient/KiotaClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,7 @@ public static HttpClient Create(BaseBearerTokenAuthenticationProvider authentica
{
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();
return Create(defaultHandlersEnumerable, finalHandler);
}

/// <summary>
Expand Down
5 changes: 2 additions & 3 deletions src/http/httpClient/Middleware/AuthorizationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public class AuthorizationHandler : DelegatingHandler
/// <exception cref="ArgumentNullException"></exception>
public AuthorizationHandler(BaseBearerTokenAuthenticationProvider authenticationProvider)
{
if(authenticationProvider == null) throw new ArgumentNullException(nameof(authenticationProvider));
this.authenticationProvider = authenticationProvider;
this.authenticationProvider = authenticationProvider ?? throw new ArgumentNullException(nameof(authenticationProvider));
}

/// <summary>
Expand Down Expand Up @@ -73,7 +72,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
return response;
activity?.AddEvent(new ActivityEvent("com.microsoft.kiota.handler.authorization.challenge_received"));
additionalAuthenticationContext[ContinuousAccessEvaluation.ClaimsKey] = claims;
HttpRequestMessage retryRequest = response.RequestMessage;
var retryRequest = await response.RequestMessage.CloneAsync(cancellationToken);
await AuthenticateRequestAsync(retryRequest, additionalAuthenticationContext, cancellationToken, activity).ConfigureAwait(false);
activity?.SetTag("http.request.resend_count", 1);
return await base.SendAsync(retryRequest, cancellationToken).ConfigureAwait(false);
Expand Down
6 changes: 3 additions & 3 deletions tests/http/httpClient/Middleware/AuthorizationHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ namespace Microsoft.Kiota.Http.HttpClientLibrary.Tests.Middleware
public class AuthorizationHandlerTests : IDisposable
{
private readonly MockRedirectHandler _testHttpMessageHandler;
private readonly string _expectedAccessToken = "token";
private const string _expectedAccessToken = "token";

private readonly string _expectedAccessTokenAfterCAE = "token2";
private const string _expectedAccessTokenAfterCAE = "token2";
private AuthorizationHandler _authorizationHandler;
private readonly BaseBearerTokenAuthenticationProvider _authenticationProvider;
private readonly HttpMessageInvoker _invoker;

private readonly string _claimsChallengeHeaderValue = "authorization_uri=\"https://login.windows.net/common/oauth2/authorize\","
private const string _claimsChallengeHeaderValue = "authorization_uri=\"https://login.windows.net/common/oauth2/authorize\","
+ "error=\"insufficient_claims\","
+ "claims=\"eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwgInZhbHVlIjoiMTYwNDEwNjY1MSJ9fX0=\"";

Expand Down

0 comments on commit 2c77cce

Please sign in to comment.