16
16
17
17
package org .springframework .security .config .annotation .web .configurers .oauth2 .client ;
18
18
19
+ import com .nimbusds .jose .JOSEObjectType ;
20
+ import com .nimbusds .jose .proc .DefaultJOSEObjectTypeVerifier ;
21
+ import com .nimbusds .jose .proc .JOSEObjectTypeVerifier ;
22
+ import com .nimbusds .jose .proc .SecurityContext ;
23
+
19
24
import org .springframework .security .authentication .AuthenticationProvider ;
20
25
import org .springframework .security .authentication .AuthenticationServiceException ;
21
26
import org .springframework .security .core .Authentication ;
26
31
import org .springframework .security .oauth2 .core .OAuth2AuthenticationException ;
27
32
import org .springframework .security .oauth2 .core .OAuth2Error ;
28
33
import org .springframework .security .oauth2 .core .OAuth2ErrorCodes ;
34
+ import org .springframework .security .oauth2 .core .converter .ClaimTypeConverter ;
29
35
import org .springframework .security .oauth2 .jwt .BadJwtException ;
30
36
import org .springframework .security .oauth2 .jwt .Jwt ;
31
37
import org .springframework .security .oauth2 .jwt .JwtDecoder ;
32
38
import org .springframework .security .oauth2 .jwt .JwtDecoderFactory ;
39
+ import org .springframework .security .oauth2 .jwt .NimbusJwtDecoder ;
33
40
import org .springframework .util .Assert ;
41
+ import org .springframework .util .StringUtils ;
34
42
35
43
/**
36
44
* An {@link AuthenticationProvider} that authenticates an OIDC Logout Token; namely
@@ -56,9 +64,27 @@ final class OidcBackChannelLogoutAuthenticationProvider implements Authenticatio
56
64
* Construct an {@link OidcBackChannelLogoutAuthenticationProvider}
57
65
*/
58
66
OidcBackChannelLogoutAuthenticationProvider () {
59
- OidcIdTokenDecoderFactory logoutTokenDecoderFactory = new OidcIdTokenDecoderFactory ();
60
- logoutTokenDecoderFactory .setJwtValidatorFactory (new DefaultOidcLogoutTokenValidatorFactory ());
61
- this .logoutTokenDecoderFactory = logoutTokenDecoderFactory ;
67
+ DefaultOidcLogoutTokenValidatorFactory jwtValidator = new DefaultOidcLogoutTokenValidatorFactory ();
68
+ this .logoutTokenDecoderFactory = (clientRegistration ) -> {
69
+ String jwkSetUri = clientRegistration .getProviderDetails ().getJwkSetUri ();
70
+ if (!StringUtils .hasText (jwkSetUri )) {
71
+ OAuth2Error oauth2Error = new OAuth2Error ("missing_signature_verifier" ,
72
+ "Failed to find a Signature Verifier for Client Registration: '"
73
+ + clientRegistration .getRegistrationId ()
74
+ + "'. Check to ensure you have configured the JwkSet URI." ,
75
+ null );
76
+ throw new OAuth2AuthenticationException (oauth2Error , oauth2Error .toString ());
77
+ }
78
+ JOSEObjectTypeVerifier <SecurityContext > typeVerifier = new DefaultJOSEObjectTypeVerifier <>(null ,
79
+ JOSEObjectType .JWT , new JOSEObjectType ("logout+jwt" ));
80
+ NimbusJwtDecoder decoder = NimbusJwtDecoder .withJwkSetUri (jwkSetUri )
81
+ .jwtProcessorCustomizer ((processor ) -> processor .setJWSTypeVerifier (typeVerifier ))
82
+ .build ();
83
+ decoder .setJwtValidator (jwtValidator .apply (clientRegistration ));
84
+ decoder .setClaimSetConverter (
85
+ new ClaimTypeConverter (OidcIdTokenDecoderFactory .createDefaultClaimTypeConverters ()));
86
+ return decoder ;
87
+ };
62
88
}
63
89
64
90
/**
0 commit comments