-
Notifications
You must be signed in to change notification settings - Fork 254
Extract shared WWW-Authenticate challenge handling as internal helper #3545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
7aaf454
bb4c0dd
46aa9e9
2d591ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| #nullable enable | ||
| const Microsoft.Identity.Web.Constants.UserIdKey = "IDWEB_USER_ID" -> string! | ||
| Microsoft.Identity.Web.WwwAuthenticateChallengeHelper | ||
| readonly Microsoft.Identity.Web.TokenAcquisition._certificatesObservers -> System.Collections.Generic.IReadOnlyList<Microsoft.Identity.Web.Experimental.ICertificatesObserver!>! | ||
| static Microsoft.Identity.Web.WwwAuthenticateChallengeHelper.CloneHttpContentAsync(System.Net.Http.HttpContent? originalContent, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<System.Net.Http.HttpContent?>! | ||
| static Microsoft.Identity.Web.WwwAuthenticateChallengeHelper.ExtractClaimsChallenge(System.Net.Http.Headers.HttpResponseHeaders! responseHeaders) -> string? | ||
| static Microsoft.Identity.Web.WwwAuthenticateChallengeHelper.ShouldAttemptClaimsChallengeRetry(System.Net.Http.HttpResponseMessage! response) -> bool |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| #nullable enable | ||
| const Microsoft.Identity.Web.Constants.UserIdKey = "IDWEB_USER_ID" -> string! | ||
| Microsoft.Identity.Web.WwwAuthenticateChallengeHelper | ||
| readonly Microsoft.Identity.Web.TokenAcquisition._certificatesObservers -> System.Collections.Generic.IReadOnlyList<Microsoft.Identity.Web.Experimental.ICertificatesObserver!>! | ||
| static Microsoft.Identity.Web.WwwAuthenticateChallengeHelper.CloneHttpContentAsync(System.Net.Http.HttpContent? originalContent, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<System.Net.Http.HttpContent?>! | ||
| static Microsoft.Identity.Web.WwwAuthenticateChallengeHelper.ExtractClaimsChallenge(System.Net.Http.Headers.HttpResponseHeaders! responseHeaders) -> string? | ||
| static Microsoft.Identity.Web.WwwAuthenticateChallengeHelper.ShouldAttemptClaimsChallengeRetry(System.Net.Http.HttpResponseMessage! response) -> bool |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| #nullable enable | ||
| const Microsoft.Identity.Web.Constants.UserIdKey = "IDWEB_USER_ID" -> string! | ||
| Microsoft.Identity.Web.WwwAuthenticateChallengeHelper | ||
| readonly Microsoft.Identity.Web.TokenAcquisition._certificatesObservers -> System.Collections.Generic.IReadOnlyList<Microsoft.Identity.Web.Experimental.ICertificatesObserver!>! | ||
| static Microsoft.Identity.Web.WwwAuthenticateChallengeHelper.CloneHttpContentAsync(System.Net.Http.HttpContent? originalContent, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<System.Net.Http.HttpContent?>! | ||
| static Microsoft.Identity.Web.WwwAuthenticateChallengeHelper.ExtractClaimsChallenge(System.Net.Http.Headers.HttpResponseHeaders! responseHeaders) -> string? | ||
| static Microsoft.Identity.Web.WwwAuthenticateChallengeHelper.ShouldAttemptClaimsChallengeRetry(System.Net.Http.HttpResponseMessage! response) -> bool |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| #nullable enable | ||
| const Microsoft.Identity.Web.Constants.UserIdKey = "IDWEB_USER_ID" -> string! | ||
| Microsoft.Identity.Web.WwwAuthenticateChallengeHelper | ||
| readonly Microsoft.Identity.Web.TokenAcquisition._certificatesObservers -> System.Collections.Generic.IReadOnlyList<Microsoft.Identity.Web.Experimental.ICertificatesObserver!>! | ||
| static Microsoft.Identity.Web.WwwAuthenticateChallengeHelper.CloneHttpContentAsync(System.Net.Http.HttpContent? originalContent, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<System.Net.Http.HttpContent?>! | ||
| static Microsoft.Identity.Web.WwwAuthenticateChallengeHelper.ExtractClaimsChallenge(System.Net.Http.Headers.HttpResponseHeaders! responseHeaders) -> string? | ||
| static Microsoft.Identity.Web.WwwAuthenticateChallengeHelper.ShouldAttemptClaimsChallengeRetry(System.Net.Http.HttpResponseMessage! response) -> bool |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| #nullable enable | ||
| const Microsoft.Identity.Web.Constants.UserIdKey = "IDWEB_USER_ID" -> string! | ||
| Microsoft.Identity.Web.WwwAuthenticateChallengeHelper | ||
| readonly Microsoft.Identity.Web.TokenAcquisition._certificatesObservers -> System.Collections.Generic.IReadOnlyList<Microsoft.Identity.Web.Experimental.ICertificatesObserver!>! | ||
| static Microsoft.Identity.Web.WwwAuthenticateChallengeHelper.CloneHttpContentAsync(System.Net.Http.HttpContent? originalContent, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<System.Net.Http.HttpContent?>! | ||
| static Microsoft.Identity.Web.WwwAuthenticateChallengeHelper.ExtractClaimsChallenge(System.Net.Http.Headers.HttpResponseHeaders! responseHeaders) -> string? | ||
| static Microsoft.Identity.Web.WwwAuthenticateChallengeHelper.ShouldAttemptClaimsChallengeRetry(System.Net.Http.HttpResponseMessage! response) -> bool |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.IO; | ||
| using System.Net.Http; | ||
| using System.Net.Http.Headers; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Identity.Client; | ||
|
|
||
| namespace Microsoft.Identity.Web | ||
| { | ||
| /// <summary> | ||
| /// Internal helper for handling WWW-Authenticate challenges from downstream APIs. | ||
| /// This helper provides shared logic for detecting claims challenges and preparing retry requests. | ||
| /// </summary> | ||
| internal static class WwwAuthenticateChallengeHelper | ||
| { | ||
| /// <summary> | ||
| /// Extracts the claims challenge from WWW-Authenticate response headers. | ||
| /// </summary> | ||
| /// <param name="responseHeaders">The HTTP response headers to examine.</param> | ||
| /// <returns>The claims challenge string if present; otherwise, null.</returns> | ||
| public static string? ExtractClaimsChallenge(HttpResponseHeaders responseHeaders) | ||
| { | ||
| return WwwAuthenticateParameters.GetClaimChallengeFromResponseHeaders(responseHeaders); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Clones HttpContent for retry scenarios. This is necessary because HttpContent can only be | ||
| /// read once, especially with non-seekable streams. The clone allows the content to be sent | ||
| /// again in a retry request. | ||
| /// </summary> | ||
| /// <param name="originalContent">The original HttpContent to clone.</param> | ||
| /// <param name="cancellationToken">Cancellation token.</param> | ||
| /// <returns>A new HttpContent instance with the same data and headers, or null if original was null.</returns> | ||
| /// <remarks> | ||
| /// This method defensively handles content cloning by reading the content into a byte array | ||
| /// and creating new ByteArrayContent. This ensures the content can be sent multiple times, | ||
| /// even if the original stream was non-seekable. | ||
| /// </remarks> | ||
| public static async Task<HttpContent?> CloneHttpContentAsync( | ||
| HttpContent? originalContent, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| if (originalContent == null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| // Read the content into a byte array to ensure it can be reused | ||
| #if NET | ||
| byte[] contentBytes = await originalContent.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there worries along the same thoughts as #3532 ? |
||
| #else | ||
| byte[] contentBytes = await originalContent.ReadAsByteArrayAsync().ConfigureAwait(false); | ||
| #endif | ||
|
|
||
| // Create new content with the same data | ||
| var clonedContent = new ByteArrayContent(contentBytes); | ||
|
|
||
| // Copy headers from original content | ||
| foreach (var header in originalContent.Headers) | ||
| { | ||
| clonedContent.Headers.TryAddWithoutValidation(header.Key, header.Value); | ||
| } | ||
|
|
||
| return clonedContent; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Determines if a response should trigger a claims challenge retry. | ||
| /// </summary> | ||
| /// <param name="response">The HTTP response to evaluate.</param> | ||
| /// <returns>True if the response is a 401 Unauthorized; otherwise, false.</returns> | ||
| /// <remarks> | ||
| /// A 401 Unauthorized response may include a WWW-Authenticate header with a claims challenge. | ||
| /// The actual claims extraction should be done using <see cref="ExtractClaimsChallenge"/>. | ||
| /// </remarks> | ||
| public static bool ShouldAttemptClaimsChallengeRetry(HttpResponseMessage response) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we consider a merge it with ExtractClaimsChallenge(), e.g., in form of |
||
| { | ||
| return response.StatusCode == System.Net.HttpStatusCode.Unauthorized; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need this method at all. Why can't we just use:
This should buffer the content into memory allowing multiple reads.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also provide an upper bound of the size of the buffer