Skip to content

Commit faa94fd

Browse files
authored
Synching the generation of the wwwAuthenticate header (#185)
in TokenAcquisition.cs from the code which is currently in the ASP.NET Core Web API tutorial
1 parent 8138f5f commit faa94fd

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

Diff for: Microsoft.Identity.Web/TokenAcquisition.cs

+12-11
Original file line numberDiff line numberDiff line change
@@ -448,29 +448,31 @@ private async Task AddAccountToCacheFromJwtAsync(IEnumerable<string> scopes, Jwt
448448
}
449449

450450
/// <summary>
451-
/// Used by Web APIs, which cannot interact with the user, when they run into a situation where user consent is required for additional scopes.
452-
/// This method appends in the HttpResponse being sent back to the client, a 403 (forbidden) status code and populates the 'WWW-Authenticate' header with additional information.
453-
/// The client, when it receives the 403 code with this header, can use the additional information provided in the header to trigger an interaction with the user where the user
454-
/// can then consent to additional scopes.
451+
/// Used in Web APIs (which therefore cannot have an interaction with the user).
452+
/// Replies to the client through the HttpReponse by sending a 403 (forbidden) and populating wwwAuthenticateHeaders so that
453+
/// the client can trigger an iteraction with the user so that the user consents to more scopes
455454
/// </summary>
456-
/// <param name="scopes">The additional scopes that the user needs to consent to</param>
457-
/// <param name="msalServiceException"><see cref="MsalUiRequiredException"/> The MsalUiRequiredException that is examined to see if there is case for this response.</param>
458-
455+
/// <param name="scopes">Scopes to consent to</param>
456+
/// <param name="msalServiceException"><see cref="MsalUiRequiredException"/> triggering the challenge</param>
459457
public void ReplyForbiddenWithWwwAuthenticateHeader(IEnumerable<string> scopes, MsalUiRequiredException msalServiceException)
460458
{
461459
// A user interaction is required, but we are in a Web API, and therefore, we need to report back to the client through an wwww-Authenticate header https://tools.ietf.org/html/rfc6750#section-3.1
462460
string proposedAction = "consent";
463461
if (msalServiceException.ErrorCode == MsalError.InvalidGrantError)
464462
{
465-
if (AcceptedTokenVersionIsNotTheSameAsTokenVersion(msalServiceException))
463+
if (AcceptedTokenVersionMismatch(msalServiceException))
466464
{
467465
throw msalServiceException;
468466
}
469467
}
470468

469+
string consentUrl = $"{application.Authority}/oauth2/v2.0/authorize?client_id={_azureAdOptions.ClientId}"
470+
+ $"&response_type=code&redirect_uri={application.AppConfig.RedirectUri}"
471+
+ $"&response_mode=query&scope=offline_access%20{string.Join("%20", scopes)}";
472+
471473
IDictionary<string, string> parameters = new Dictionary<string, string>()
472474
{
473-
{ "clientId", _azureAdOptions.ClientId },
475+
{ "consentUri", consentUrl },
474476
{ "claims", msalServiceException.Claims },
475477
{ "scopes", string.Join(",", scopes) },
476478
{ "proposedAction", proposedAction }
@@ -480,7 +482,6 @@ public void ReplyForbiddenWithWwwAuthenticateHeader(IEnumerable<string> scopes,
480482
string scheme = "Bearer";
481483
StringValues v = new StringValues($"{scheme} {parameterString}");
482484

483-
// StringValues v = new StringValues(new string[] { $"Bearer clientId=\"{jwtToken.Audiences.First()}\", claims=\"{ex.Claims}\", scopes=\" {string.Join(",", scopes)}\"" });
484485
var httpResponse = CurrentHttpContext.Response;
485486
var headers = httpResponse.Headers;
486487
httpResponse.StatusCode = (int)HttpStatusCode.Forbidden;
@@ -491,7 +492,7 @@ public void ReplyForbiddenWithWwwAuthenticateHeader(IEnumerable<string> scopes,
491492
headers.Add(HeaderNames.WWWAuthenticate, v);
492493
}
493494

494-
private static bool AcceptedTokenVersionIsNotTheSameAsTokenVersion(MsalUiRequiredException msalSeviceException)
495+
private static bool AcceptedTokenVersionMismatch(MsalUiRequiredException msalSeviceException)
495496
{
496497
// Normally app developers should not make decisions based on the internal AAD code
497498
// however until the STS sends sub-error codes for this error, this is the only

0 commit comments

Comments
 (0)