Skip to content

Commit ac38232

Browse files
committed
ID Token validation uses JwtTimestampValidator
Fixes gh-6964
1 parent 6ad46da commit ac38232

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.oauth2.client.oidc.authentication;
17+
18+
import org.springframework.security.oauth2.client.registration.ClientRegistration;
19+
import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator;
20+
import org.springframework.security.oauth2.core.OAuth2TokenValidator;
21+
import org.springframework.security.oauth2.jwt.Jwt;
22+
import org.springframework.security.oauth2.jwt.JwtTimestampValidator;
23+
24+
import java.util.function.Function;
25+
26+
/**
27+
*
28+
* @author Joe Grandja
29+
* @since 5.2
30+
*/
31+
class DefaultOidcIdTokenValidatorFactory implements Function<ClientRegistration, OAuth2TokenValidator<Jwt>> {
32+
33+
@Override
34+
public OAuth2TokenValidator<Jwt> apply(ClientRegistration clientRegistration) {
35+
return new DelegatingOAuth2TokenValidator<>(
36+
new JwtTimestampValidator(), new OidcIdTokenValidator(clientRegistration));
37+
}
38+
}

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/oidc/authentication/OidcIdTokenDecoderFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.security.oauth2.jwt.Jwt;
3333
import org.springframework.security.oauth2.jwt.JwtDecoder;
3434
import org.springframework.security.oauth2.jwt.JwtDecoderFactory;
35+
import org.springframework.security.oauth2.jwt.JwtTimestampValidator;
3536
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
3637
import org.springframework.util.Assert;
3738
import org.springframework.util.StringUtils;
@@ -73,7 +74,7 @@ public final class OidcIdTokenDecoderFactory implements JwtDecoderFactory<Client
7374
private static final Converter<Map<String, Object>, Map<String, Object>> DEFAULT_CLAIM_TYPE_CONVERTER =
7475
new ClaimTypeConverter(createDefaultClaimTypeConverters());
7576
private final Map<String, JwtDecoder> jwtDecoders = new ConcurrentHashMap<>();
76-
private Function<ClientRegistration, OAuth2TokenValidator<Jwt>> jwtValidatorFactory = OidcIdTokenValidator::new;
77+
private Function<ClientRegistration, OAuth2TokenValidator<Jwt>> jwtValidatorFactory = new DefaultOidcIdTokenValidatorFactory();
7778
private Function<ClientRegistration, JwsAlgorithm> jwsAlgorithmResolver = clientRegistration -> SignatureAlgorithm.RS256;
7879
private Function<ClientRegistration, Converter<Map<String, Object>, Map<String, Object>>> claimTypeConverterFactory =
7980
clientRegistration -> DEFAULT_CLAIM_TYPE_CONVERTER;
@@ -189,7 +190,7 @@ private NimbusJwtDecoder buildDecoder(ClientRegistration clientRegistration) {
189190

190191
/**
191192
* Sets the factory that provides an {@link OAuth2TokenValidator}, which is used by the {@link JwtDecoder}.
192-
* The default is {@link OidcIdTokenValidator}.
193+
* The default composes {@link JwtTimestampValidator} and {@link OidcIdTokenValidator}.
193194
*
194195
* @param jwtValidatorFactory the factory that provides an {@link OAuth2TokenValidator}
195196
*/

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/oidc/authentication/ReactiveOidcIdTokenDecoderFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.security.oauth2.jose.jws.MacAlgorithm;
3131
import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
3232
import org.springframework.security.oauth2.jwt.Jwt;
33+
import org.springframework.security.oauth2.jwt.JwtTimestampValidator;
3334
import org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder;
3435
import org.springframework.security.oauth2.jwt.ReactiveJwtDecoder;
3536
import org.springframework.security.oauth2.jwt.ReactiveJwtDecoderFactory;
@@ -73,7 +74,7 @@ public final class ReactiveOidcIdTokenDecoderFactory implements ReactiveJwtDecod
7374
private static final Converter<Map<String, Object>, Map<String, Object>> DEFAULT_CLAIM_TYPE_CONVERTER =
7475
new ClaimTypeConverter(createDefaultClaimTypeConverters());
7576
private final Map<String, ReactiveJwtDecoder> jwtDecoders = new ConcurrentHashMap<>();
76-
private Function<ClientRegistration, OAuth2TokenValidator<Jwt>> jwtValidatorFactory = OidcIdTokenValidator::new;
77+
private Function<ClientRegistration, OAuth2TokenValidator<Jwt>> jwtValidatorFactory = new DefaultOidcIdTokenValidatorFactory();
7778
private Function<ClientRegistration, JwsAlgorithm> jwsAlgorithmResolver = clientRegistration -> SignatureAlgorithm.RS256;
7879
private Function<ClientRegistration, Converter<Map<String, Object>, Map<String, Object>>> claimTypeConverterFactory =
7980
clientRegistration -> DEFAULT_CLAIM_TYPE_CONVERTER;
@@ -189,7 +190,7 @@ private NimbusReactiveJwtDecoder buildDecoder(ClientRegistration clientRegistrat
189190

190191
/**
191192
* Sets the factory that provides an {@link OAuth2TokenValidator}, which is used by the {@link ReactiveJwtDecoder}.
192-
* The default is {@link OidcIdTokenValidator}.
193+
* The default composes {@link JwtTimestampValidator} and {@link OidcIdTokenValidator}.
193194
*
194195
* @param jwtValidatorFactory the factory that provides an {@link OAuth2TokenValidator}
195196
*/

0 commit comments

Comments
 (0)