Skip to content

Commit

Permalink
Fix SonarCloud issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndiritu committed Nov 5, 2024
1 parent 77f946f commit d45b7d4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/http/httpClient/HttpClientRequestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,6 @@ private async Task ThrowIfFailedResponseAsync(HttpResponseMessage response, Dict
}
private const string ClaimsKey = "claims";
private const string BearerAuthenticationScheme = "Bearer";

Check warning on line 496 in src/http/httpClient/HttpClientRequestAdapter.cs

View workflow job for this annotation

GitHub Actions / Build

Remove the unused private field 'BearerAuthenticationScheme'. (https://rules.sonarsource.com/csharp/RSPEC-1144)

Check warning on line 496 in src/http/httpClient/HttpClientRequestAdapter.cs

View workflow job for this annotation

GitHub Actions / Build

Remove the unused private field 'BearerAuthenticationScheme'. (https://rules.sonarsource.com/csharp/RSPEC-1144)
private static Func<AuthenticationHeaderValue, bool> filterAuthHeader = static x => x.Scheme.Equals(BearerAuthenticationScheme, StringComparison.OrdinalIgnoreCase);
private async Task<HttpResponseMessage> GetHttpResponseMessageAsync(RequestInformation requestInfo, CancellationToken cancellationToken, Activity? activityForAttributes, string? claims = default, bool isStreamResponse = false)
{
using var span = activitySource?.StartActivity(nameof(GetHttpResponseMessageAsync));
Expand Down
9 changes: 4 additions & 5 deletions src/http/httpClient/Middleware/AuthorizationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
CancellationToken cancellationToken)
{
if(request == null) throw new ArgumentNullException(nameof(request));

Activity? activity = null;
if(request.GetRequestOption<ObservabilityOptions>() is { } obsOptions)
{
Expand All @@ -62,7 +61,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
Dictionary<string, object> additionalAuthenticationContext = new Dictionary<string, object>();
await AuthenticateRequestAsync(request, additionalAuthenticationContext, cancellationToken, activity).ConfigureAwait(false);
await AuthenticateRequestAsync(request, additionalAuthenticationContext, activity, cancellationToken).ConfigureAwait(false);
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
if(response.StatusCode != HttpStatusCode.Unauthorized || response.RequestMessage == null || !response.RequestMessage.IsBuffered())
return response;
Expand All @@ -73,7 +72,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
activity?.AddEvent(new ActivityEvent("com.microsoft.kiota.handler.authorization.challenge_received"));
additionalAuthenticationContext[ContinuousAccessEvaluation.ClaimsKey] = claims;
var retryRequest = await response.RequestMessage.CloneAsync(cancellationToken);
await AuthenticateRequestAsync(retryRequest, additionalAuthenticationContext, cancellationToken, activity).ConfigureAwait(false);
await AuthenticateRequestAsync(retryRequest, additionalAuthenticationContext, activity, cancellationToken).ConfigureAwait(false);
activity?.SetTag("http.request.resend_count", 1);
return await base.SendAsync(retryRequest, cancellationToken).ConfigureAwait(false);
}
Expand All @@ -85,8 +84,8 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage

private async Task AuthenticateRequestAsync(HttpRequestMessage request,
Dictionary<string, object> additionalAuthenticationContext,
CancellationToken cancellationToken,
Activity? activityForAttributes)
Activity? activityForAttributes,
CancellationToken cancellationToken)
{
var accessTokenProvider = authenticationProvider.AccessTokenProvider;
if(request.RequestUri == null || !accessTokenProvider.AllowedHostsValidator.IsUrlHostValid(
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 @@ -78,7 +78,7 @@ public async Task AuthorizationHandlerShouldAddAuthHeaderIfNotPresent()
// Assert
Assert.NotNull(response.RequestMessage);
Assert.True(response.RequestMessage.Headers.Contains("Authorization"));
Assert.True(response.RequestMessage.Headers.GetValues("Authorization").Count() == 1);
Assert.Single(response.RequestMessage.Headers.GetValues("Authorization"));
Assert.Equal($"Bearer {_expectedAccessToken}", response.RequestMessage.Headers.GetValues("Authorization").First());
}

Expand All @@ -98,7 +98,7 @@ public async Task AuthorizationHandlerShouldNotAddAuthHeaderIfPresent()
// Assert
Assert.NotNull(response.RequestMessage);
Assert.True(response.RequestMessage.Headers.Contains("Authorization"));
Assert.True(response.RequestMessage.Headers.GetValues("Authorization").Count() == 1);
Assert.Single(response.RequestMessage.Headers.GetValues("Authorization"));
Assert.Equal($"Bearer existing", response.RequestMessage.Headers.GetValues("Authorization").First());
}

Expand Down Expand Up @@ -138,7 +138,7 @@ public async Task AuthorizationHandlerShouldAttemptCAEClaimsChallenge()
// Assert
Assert.NotNull(response.RequestMessage);
Assert.True(response.RequestMessage.Headers.Contains("Authorization"));
Assert.True(response.RequestMessage.Headers.GetValues("Authorization").Count() == 1);
Assert.Single(response.RequestMessage.Headers.GetValues("Authorization"));
Assert.Equal($"Bearer {_expectedAccessTokenAfterCAE}", response.RequestMessage.Headers.GetValues("Authorization").First());
Assert.Equal("test", await response.RequestMessage.Content!.ReadAsStringAsync());
}
Expand Down

0 comments on commit d45b7d4

Please sign in to comment.