1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .security .config .web .server ;
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 .JWKSecurityContext ;
19
23
import reactor .core .publisher .Mono ;
20
24
21
25
import org .springframework .security .authentication .AuthenticationProvider ;
22
26
import org .springframework .security .authentication .AuthenticationServiceException ;
23
27
import org .springframework .security .authentication .ReactiveAuthenticationManager ;
24
28
import org .springframework .security .core .Authentication ;
25
29
import org .springframework .security .core .AuthenticationException ;
26
- import org .springframework .security .oauth2 .client .oidc .authentication .ReactiveOidcIdTokenDecoderFactory ;
30
+ import org .springframework .security .oauth2 .client .oidc .authentication .OidcIdTokenDecoderFactory ;
27
31
import org .springframework .security .oauth2 .client .oidc .authentication .logout .OidcLogoutToken ;
28
32
import org .springframework .security .oauth2 .client .registration .ClientRegistration ;
29
33
import org .springframework .security .oauth2 .core .OAuth2AuthenticationException ;
30
34
import org .springframework .security .oauth2 .core .OAuth2Error ;
31
35
import org .springframework .security .oauth2 .core .OAuth2ErrorCodes ;
36
+ import org .springframework .security .oauth2 .core .converter .ClaimTypeConverter ;
32
37
import org .springframework .security .oauth2 .jwt .BadJwtException ;
33
38
import org .springframework .security .oauth2 .jwt .Jwt ;
34
39
import org .springframework .security .oauth2 .jwt .JwtDecoder ;
35
40
import org .springframework .security .oauth2 .jwt .JwtDecoderFactory ;
41
+ import org .springframework .security .oauth2 .jwt .NimbusReactiveJwtDecoder ;
36
42
import org .springframework .security .oauth2 .jwt .ReactiveJwtDecoder ;
37
43
import org .springframework .security .oauth2 .jwt .ReactiveJwtDecoderFactory ;
38
44
import org .springframework .util .Assert ;
45
+ import org .springframework .util .StringUtils ;
39
46
40
47
/**
41
48
* An {@link AuthenticationProvider} that authenticates an OIDC Logout Token; namely
@@ -61,9 +68,27 @@ final class OidcBackChannelLogoutReactiveAuthenticationManager implements Reacti
61
68
* Construct an {@link OidcBackChannelLogoutReactiveAuthenticationManager}
62
69
*/
63
70
OidcBackChannelLogoutReactiveAuthenticationManager () {
64
- ReactiveOidcIdTokenDecoderFactory logoutTokenDecoderFactory = new ReactiveOidcIdTokenDecoderFactory ();
65
- logoutTokenDecoderFactory .setJwtValidatorFactory (new DefaultOidcLogoutTokenValidatorFactory ());
66
- this .logoutTokenDecoderFactory = logoutTokenDecoderFactory ;
71
+ DefaultOidcLogoutTokenValidatorFactory jwtValidator = new DefaultOidcLogoutTokenValidatorFactory ();
72
+ this .logoutTokenDecoderFactory = (clientRegistration ) -> {
73
+ String jwkSetUri = clientRegistration .getProviderDetails ().getJwkSetUri ();
74
+ if (!StringUtils .hasText (jwkSetUri )) {
75
+ OAuth2Error oauth2Error = new OAuth2Error ("missing_signature_verifier" ,
76
+ "Failed to find a Signature Verifier for Client Registration: '"
77
+ + clientRegistration .getRegistrationId ()
78
+ + "'. Check to ensure you have configured the JwkSet URI." ,
79
+ null );
80
+ throw new OAuth2AuthenticationException (oauth2Error , oauth2Error .toString ());
81
+ }
82
+ JOSEObjectTypeVerifier <JWKSecurityContext > typeVerifier = new DefaultJOSEObjectTypeVerifier <>(null ,
83
+ JOSEObjectType .JWT , new JOSEObjectType ("logout+jwt" ));
84
+ NimbusReactiveJwtDecoder decoder = NimbusReactiveJwtDecoder .withJwkSetUri (jwkSetUri )
85
+ .jwtProcessorCustomizer ((processor ) -> processor .setJWSTypeVerifier (typeVerifier ))
86
+ .build ();
87
+ decoder .setJwtValidator (jwtValidator .apply (clientRegistration ));
88
+ decoder .setClaimSetConverter (
89
+ new ClaimTypeConverter (OidcIdTokenDecoderFactory .createDefaultClaimTypeConverters ()));
90
+ return decoder ;
91
+ };
67
92
}
68
93
69
94
/**
0 commit comments