@@ -98,6 +98,7 @@ public OAuth2AuthorizationCodeRequestAuthenticationProvider(RegisteredClientRepo
98
98
this .registeredClientRepository = registeredClientRepository ;
99
99
this .authorizationService = authorizationService ;
100
100
this .authorizationConsentService = authorizationConsentService ;
101
+ this .requiresAuthorizationConsent = this ::requireAuthorizationConsent ;
101
102
}
102
103
103
104
@ Override
@@ -173,7 +174,14 @@ public Authentication authenticate(Authentication authentication) throws Authent
173
174
OAuth2AuthorizationConsent currentAuthorizationConsent = this .authorizationConsentService .findById (
174
175
registeredClient .getId (), principal .getName ());
175
176
176
- if (requireAuthorizationConsent (registeredClient , authorizationRequest , currentAuthorizationConsent , authenticationContext )) {
177
+ OAuth2AuthorizationCodeRequestAuthenticationContext contextWithAuthorizationRequestAndAuthorizationConsent =
178
+ OAuth2AuthorizationCodeRequestAuthenticationContext .with (authorizationCodeRequestAuthentication )
179
+ .registeredClient (registeredClient )
180
+ .context (context -> context .put (OAuth2AuthorizationRequest .class , authorizationRequest ))
181
+ .context (context -> context .put (OAuth2AuthorizationConsent .class , currentAuthorizationConsent ))
182
+ .build ();
183
+
184
+ if (requiresAuthorizationConsent .test (contextWithAuthorizationRequestAndAuthorizationConsent )) {
177
185
String state = DEFAULT_STATE_GENERATOR .generateKey ();
178
186
OAuth2Authorization authorization = authorizationBuilder (registeredClient , principal , authorizationRequest )
179
187
.attribute (OAuth2ParameterNames .STATE , state )
@@ -275,30 +283,27 @@ public void setAuthenticationValidator(Consumer<OAuth2AuthorizationCodeRequestAu
275
283
* {@link OAuth2AuthorizationCodeRequestAuthenticationContext#getRegisteredClient()} containing {@link RegisteredClient} used to make the request.
276
284
*
277
285
* @param requiresAuthorizationConsent the {@link Predicate} that determines if authorization consent is required.
278
- * @since 1.2.3
286
+ * @since 1.3.0
279
287
*/
280
288
public void setRequiresAuthorizationConsent (Predicate <OAuth2AuthorizationCodeRequestAuthenticationContext > requiresAuthorizationConsent ) {
281
289
Assert .notNull (requiresAuthorizationConsent , "requiresAuthorizationConsent cannot be null" );
282
290
this .requiresAuthorizationConsent = requiresAuthorizationConsent ;
283
291
}
284
292
285
- private boolean requireAuthorizationConsent (RegisteredClient registeredClient ,
286
- OAuth2AuthorizationRequest authorizationRequest , OAuth2AuthorizationConsent authorizationConsent ,
287
- OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext ) {
288
-
289
- if (requiresAuthorizationConsent != null ) {
290
- return requiresAuthorizationConsent .test (authenticationContext );
291
- }
292
-
293
+ private boolean requireAuthorizationConsent (OAuth2AuthorizationCodeRequestAuthenticationContext context ) {
294
+ RegisteredClient registeredClient = context .getRegisteredClient ();
293
295
if (!registeredClient .getClientSettings ().isRequireAuthorizationConsent ()) {
294
296
return false ;
295
297
}
298
+
299
+ OAuth2AuthorizationRequest authorizationRequest = context .get (OAuth2AuthorizationRequest .class );
296
300
// 'openid' scope does not require consent
297
301
if (authorizationRequest .getScopes ().contains (OidcScopes .OPENID ) &&
298
302
authorizationRequest .getScopes ().size () == 1 ) {
299
303
return false ;
300
304
}
301
305
306
+ OAuth2AuthorizationConsent authorizationConsent = context .get (OAuth2AuthorizationConsent .class );
302
307
if (authorizationConsent != null &&
303
308
authorizationConsent .getScopes ().containsAll (authorizationRequest .getScopes ())) {
304
309
return false ;
0 commit comments