|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2019 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.
|
|
26 | 26 | import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserRequest;
|
27 | 27 | import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
28 | 28 | import org.springframework.security.oauth2.client.userinfo.ReactiveOAuth2UserService;
|
29 |
| -import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator; |
30 | 29 | import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
31 | 30 | import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
32 | 31 | import org.springframework.security.oauth2.core.OAuth2AuthorizationException;
|
33 | 32 | import org.springframework.security.oauth2.core.OAuth2Error;
|
34 |
| -import org.springframework.security.oauth2.core.OAuth2TokenValidator; |
35 | 33 | import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
|
36 | 34 | import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
|
37 | 35 | import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponse;
|
38 | 36 | import org.springframework.security.oauth2.core.oidc.OidcIdToken;
|
39 | 37 | import org.springframework.security.oauth2.core.oidc.endpoint.OidcParameterNames;
|
40 | 38 | import org.springframework.security.oauth2.core.oidc.user.OidcUser;
|
41 | 39 | import org.springframework.security.oauth2.core.user.OAuth2User;
|
42 |
| -import org.springframework.security.oauth2.jwt.Jwt; |
43 | 40 | import org.springframework.security.oauth2.jwt.JwtException;
|
44 |
| -import org.springframework.security.oauth2.jwt.JwtTimestampValidator; |
45 |
| -import org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder; |
46 | 41 | import org.springframework.security.oauth2.jwt.ReactiveJwtDecoder;
|
47 | 42 | import org.springframework.security.oauth2.jwt.ReactiveJwtDecoderFactory;
|
48 | 43 | import org.springframework.util.Assert;
|
49 |
| -import org.springframework.util.StringUtils; |
50 | 44 | import reactor.core.publisher.Mono;
|
51 | 45 |
|
52 | 46 | import java.util.Collection;
|
53 | 47 | import java.util.Map;
|
54 |
| -import java.util.concurrent.ConcurrentHashMap; |
55 | 48 |
|
56 | 49 | /**
|
57 | 50 | * An implementation of an {@link org.springframework.security.authentication.AuthenticationProvider} for OAuth 2.0 Login,
|
@@ -83,15 +76,14 @@ public class OidcAuthorizationCodeReactiveAuthenticationManager implements
|
83 | 76 | private static final String INVALID_STATE_PARAMETER_ERROR_CODE = "invalid_state_parameter";
|
84 | 77 | private static final String INVALID_REDIRECT_URI_PARAMETER_ERROR_CODE = "invalid_redirect_uri_parameter";
|
85 | 78 | private static final String INVALID_ID_TOKEN_ERROR_CODE = "invalid_id_token";
|
86 |
| - private static final String MISSING_SIGNATURE_VERIFIER_ERROR_CODE = "missing_signature_verifier"; |
87 | 79 |
|
88 | 80 | private final ReactiveOAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> accessTokenResponseClient;
|
89 | 81 |
|
90 | 82 | private final ReactiveOAuth2UserService<OidcUserRequest, OidcUser> userService;
|
91 | 83 |
|
92 | 84 | private GrantedAuthoritiesMapper authoritiesMapper = (authorities -> authorities);
|
93 | 85 |
|
94 |
| - private ReactiveJwtDecoderFactory<ClientRegistration> jwtDecoderFactory = new DefaultJwtDecoderFactory(); |
| 86 | + private ReactiveJwtDecoderFactory<ClientRegistration> jwtDecoderFactory = new ReactiveOidcIdTokenDecoderFactory(); |
95 | 87 |
|
96 | 88 | public OidcAuthorizationCodeReactiveAuthenticationManager(
|
97 | 89 | ReactiveOAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> accessTokenResponseClient,
|
@@ -199,30 +191,4 @@ private Mono<OidcIdToken> createOidcToken(ClientRegistration clientRegistration,
|
199 | 191 | return jwtDecoder.decode(rawIdToken)
|
200 | 192 | .map(jwt -> new OidcIdToken(jwt.getTokenValue(), jwt.getIssuedAt(), jwt.getExpiresAt(), jwt.getClaims()));
|
201 | 193 | }
|
202 |
| - |
203 |
| - private static class DefaultJwtDecoderFactory implements ReactiveJwtDecoderFactory<ClientRegistration> { |
204 |
| - private final Map<String, ReactiveJwtDecoder> jwtDecoders = new ConcurrentHashMap<>(); |
205 |
| - |
206 |
| - @Override |
207 |
| - public ReactiveJwtDecoder createDecoder(ClientRegistration clientRegistration) { |
208 |
| - return this.jwtDecoders.computeIfAbsent(clientRegistration.getRegistrationId(), key -> { |
209 |
| - if (!StringUtils.hasText(clientRegistration.getProviderDetails().getJwkSetUri())) { |
210 |
| - OAuth2Error oauth2Error = new OAuth2Error( |
211 |
| - MISSING_SIGNATURE_VERIFIER_ERROR_CODE, |
212 |
| - "Failed to find a Signature Verifier for Client Registration: '" + |
213 |
| - clientRegistration.getRegistrationId() + |
214 |
| - "'. Check to ensure you have configured the JwkSet URI.", |
215 |
| - null |
216 |
| - ); |
217 |
| - throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString()); |
218 |
| - } |
219 |
| - NimbusReactiveJwtDecoder jwtDecoder = new NimbusReactiveJwtDecoder( |
220 |
| - clientRegistration.getProviderDetails().getJwkSetUri()); |
221 |
| - OAuth2TokenValidator<Jwt> jwtValidator = new DelegatingOAuth2TokenValidator<>( |
222 |
| - new JwtTimestampValidator(), new OidcIdTokenValidator(clientRegistration)); |
223 |
| - jwtDecoder.setJwtValidator(jwtValidator); |
224 |
| - return jwtDecoder; |
225 |
| - }); |
226 |
| - } |
227 |
| - } |
228 | 194 | }
|
0 commit comments