Skip to content

Commit 36643e2

Browse files
committed
Enable CAE for AuthorizeRequestOnChallenge
1 parent 71190d0 commit 36643e2

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

sdk/keyvault/Azure.Security.KeyVault.Shared/src/ChallengeBasedAuthenticationPolicy.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal class ChallengeBasedAuthenticationPolicy : BearerTokenAuthenticationPol
1515
private const string KeyVaultStashedContentKey = "KeyVaultContent";
1616
private readonly bool _verifyChallengeResource;
1717

18-
private readonly bool _enableCAE;
18+
private readonly bool _enableCAE = false;
1919

2020
/// <summary>
2121
/// Challenges are cached using the Key Vault or Managed HSM endpoint URI authority as the key.
@@ -96,6 +96,21 @@ private async ValueTask<bool> AuthorizeRequestOnChallengeAsyncInternal(HttpMessa
9696

9797
string authority = GetRequestAuthority(message.Request);
9898
string scope = AuthorizationChallengeParser.GetChallengeParameterFromResponse(message.Response, "Bearer", "resource");
99+
100+
string error = AuthorizationChallengeParser.GetChallengeParameterFromResponse(message.Response, "Bearer", "error");
101+
string claims = null;
102+
103+
if (error != null)
104+
{
105+
// The challenge response contained an error.
106+
string base64Claims = AuthorizationChallengeParser.GetChallengeParameterFromResponse(message.Response, "Bearer", "claims");
107+
108+
if (error == "insufficient_claims" && base64Claims != null)
109+
{
110+
claims = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(base64Claims));
111+
}
112+
}
113+
99114
if (scope != null)
100115
{
101116
scope += "/.default";
@@ -111,6 +126,11 @@ private async ValueTask<bool> AuthorizeRequestOnChallengeAsyncInternal(HttpMessa
111126
{
112127
return false;
113128
}
129+
else if (claims is not null)
130+
{
131+
_challenge.Claims = claims;
132+
s_challengeCache[authority] = _challenge;
133+
}
114134
}
115135
else
116136
{
@@ -139,11 +159,11 @@ private async ValueTask<bool> AuthorizeRequestOnChallengeAsyncInternal(HttpMessa
139159
throw new UriFormatException($"The challenge authorization URI '{authorization}' is invalid.");
140160
}
141161

142-
_challenge = new ChallengeParameters(authorizationUri, new string[] { scope });
162+
_challenge = new ChallengeParameters(authorizationUri, new string[] { scope }, claims);
143163
s_challengeCache[authority] = _challenge;
144164
}
145165

146-
var context = new TokenRequestContext(_challenge.Scopes, parentRequestId: message.Request.ClientRequestId, tenantId: _challenge.TenantId);
166+
var context = new TokenRequestContext(_challenge.Scopes, parentRequestId: message.Request.ClientRequestId, tenantId: _challenge.TenantId, isCaeEnabled: _enableCAE, claims: _challenge.Claims);
147167
if (async)
148168
{
149169
await AuthenticateAndAuthorizeRequestAsync(message, context).ConfigureAwait(false);

0 commit comments

Comments
 (0)