32
32
import org .springframework .security .authentication .AuthenticationProvider ;
33
33
import org .springframework .security .core .Authentication ;
34
34
import org .springframework .security .core .AuthenticationException ;
35
+ import org .springframework .security .core .GrantedAuthority ;
35
36
import org .springframework .security .crypto .keygen .Base64StringKeyGenerator ;
36
37
import org .springframework .security .crypto .keygen .StringKeyGenerator ;
37
38
import org .springframework .security .oauth2 .core .AuthorizationGrantType ;
@@ -329,19 +330,6 @@ private Authentication authenticateAuthorizationConsent(Authentication authentic
329
330
Set <String > currentAuthorizedScopes = currentAuthorizationConsent != null ?
330
331
currentAuthorizationConsent .getScopes () : Collections .emptySet ();
331
332
332
- if (authorizedScopes .isEmpty () && currentAuthorizedScopes .isEmpty () &&
333
- authorizationCodeRequestAuthentication .getAdditionalParameters ().isEmpty ()) {
334
- // Authorization consent denied
335
- this .authorizationService .remove (authorization );
336
- throwError (OAuth2ErrorCodes .ACCESS_DENIED , OAuth2ParameterNames .CLIENT_ID ,
337
- authorizationCodeRequestAuthentication , registeredClient , authorizationRequest );
338
- }
339
-
340
- if (requestedScopes .contains (OidcScopes .OPENID )) {
341
- // 'openid' scope is auto-approved as it does not require consent
342
- authorizedScopes .add (OidcScopes .OPENID );
343
- }
344
-
345
333
if (!currentAuthorizedScopes .isEmpty ()) {
346
334
for (String requestedScope : requestedScopes ) {
347
335
if (currentAuthorizedScopes .contains (requestedScope )) {
@@ -350,6 +338,11 @@ private Authentication authenticateAuthorizationConsent(Authentication authentic
350
338
}
351
339
}
352
340
341
+ if (!authorizedScopes .isEmpty () && requestedScopes .contains (OidcScopes .OPENID )) {
342
+ // 'openid' scope is auto-approved as it does not require consent
343
+ authorizedScopes .add (OidcScopes .OPENID );
344
+ }
345
+
353
346
OAuth2AuthorizationConsent .Builder authorizationConsentBuilder ;
354
347
if (currentAuthorizationConsent != null ) {
355
348
authorizationConsentBuilder = OAuth2AuthorizationConsent .from (currentAuthorizationConsent );
@@ -371,6 +364,19 @@ private Authentication authenticateAuthorizationConsent(Authentication authentic
371
364
this .authorizationConsentCustomizer .accept (authorizationConsentAuthenticationContext );
372
365
}
373
366
367
+ Set <GrantedAuthority > authorities = new HashSet <>();
368
+ authorizationConsentBuilder .authorities (authorities ::addAll );
369
+
370
+ if (authorities .isEmpty ()) {
371
+ // Authorization consent denied (or revoked)
372
+ if (currentAuthorizationConsent != null ) {
373
+ this .authorizationConsentService .remove (currentAuthorizationConsent );
374
+ }
375
+ this .authorizationService .remove (authorization );
376
+ throwError (OAuth2ErrorCodes .ACCESS_DENIED , OAuth2ParameterNames .CLIENT_ID ,
377
+ authorizationCodeRequestAuthentication , registeredClient , authorizationRequest );
378
+ }
379
+
374
380
OAuth2AuthorizationConsent authorizationConsent = authorizationConsentBuilder .build ();
375
381
if (!authorizationConsent .equals (currentAuthorizationConsent )) {
376
382
this .authorizationConsentService .save (authorizationConsent );
0 commit comments