@@ -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