Add configurable Clock in OidcIdTokenValidator
Fixes gh-8019
This commit is contained in:
parent
7734d049eb
commit
3e5600f83f
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2019 the original author or authors.
|
* Copyright 2002-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -27,6 +27,7 @@ import org.springframework.util.Assert;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.time.Clock;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
@ -48,6 +49,7 @@ public final class OidcIdTokenValidator implements OAuth2TokenValidator<Jwt> {
|
||||||
private static final Duration DEFAULT_CLOCK_SKEW = Duration.ofSeconds(60);
|
private static final Duration DEFAULT_CLOCK_SKEW = Duration.ofSeconds(60);
|
||||||
private final ClientRegistration clientRegistration;
|
private final ClientRegistration clientRegistration;
|
||||||
private Duration clockSkew = DEFAULT_CLOCK_SKEW;
|
private Duration clockSkew = DEFAULT_CLOCK_SKEW;
|
||||||
|
private Clock clock = Clock.systemUTC();
|
||||||
|
|
||||||
public OidcIdTokenValidator(ClientRegistration clientRegistration) {
|
public OidcIdTokenValidator(ClientRegistration clientRegistration) {
|
||||||
Assert.notNull(clientRegistration, "clientRegistration cannot be null");
|
Assert.notNull(clientRegistration, "clientRegistration cannot be null");
|
||||||
|
|
@ -95,7 +97,7 @@ public final class OidcIdTokenValidator implements OAuth2TokenValidator<Jwt> {
|
||||||
// TODO Depends on gh-4413
|
// TODO Depends on gh-4413
|
||||||
|
|
||||||
// 9. The current time MUST be before the time represented by the exp Claim.
|
// 9. The current time MUST be before the time represented by the exp Claim.
|
||||||
Instant now = Instant.now();
|
Instant now = Instant.now(this.clock);
|
||||||
if (now.minus(this.clockSkew).isAfter(idToken.getExpiresAt())) {
|
if (now.minus(this.clockSkew).isAfter(idToken.getExpiresAt())) {
|
||||||
invalidClaims.put(IdTokenClaimNames.EXP, idToken.getExpiresAt());
|
invalidClaims.put(IdTokenClaimNames.EXP, idToken.getExpiresAt());
|
||||||
}
|
}
|
||||||
|
|
@ -128,6 +130,19 @@ public final class OidcIdTokenValidator implements OAuth2TokenValidator<Jwt> {
|
||||||
this.clockSkew = clockSkew;
|
this.clockSkew = clockSkew;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the {@link Clock} used in {@link Instant#now(Clock)}
|
||||||
|
* when validating the {@link JwtClaimNames#EXP exp}
|
||||||
|
* and {@link JwtClaimNames#IAT iat} claims.
|
||||||
|
*
|
||||||
|
* @since 5.3
|
||||||
|
* @param clock the clock
|
||||||
|
*/
|
||||||
|
public void setClock(Clock clock) {
|
||||||
|
Assert.notNull(clock, "clock cannot be null");
|
||||||
|
this.clock = clock;
|
||||||
|
}
|
||||||
|
|
||||||
private static OAuth2Error invalidIdToken(Map<String, Object> invalidClaims) {
|
private static OAuth2Error invalidIdToken(Map<String, Object> invalidClaims) {
|
||||||
return new OAuth2Error("invalid_id_token",
|
return new OAuth2Error("invalid_id_token",
|
||||||
"The ID Token contains invalid claims: " + invalidClaims,
|
"The ID Token contains invalid claims: " + invalidClaims,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2019 the original author or authors.
|
* Copyright 2002-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -15,6 +15,15 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.security.oauth2.client.oidc.authentication;
|
package org.springframework.security.oauth2.client.oidc.authentication;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
||||||
|
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
|
||||||
|
import org.springframework.security.oauth2.core.OAuth2Error;
|
||||||
|
import org.springframework.security.oauth2.core.oidc.IdTokenClaimNames;
|
||||||
|
import org.springframework.security.oauth2.jose.jws.JwsAlgorithms;
|
||||||
|
import org.springframework.security.oauth2.jwt.Jwt;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
@ -23,16 +32,6 @@ import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
|
||||||
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
|
|
||||||
import org.springframework.security.oauth2.core.OAuth2Error;
|
|
||||||
import org.springframework.security.oauth2.core.oidc.IdTokenClaimNames;
|
|
||||||
import org.springframework.security.oauth2.jose.jws.JwsAlgorithms;
|
|
||||||
import org.springframework.security.oauth2.jwt.Jwt;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
|
|
@ -77,6 +76,13 @@ public class OidcIdTokenValidatorTests {
|
||||||
.isInstanceOf(IllegalArgumentException.class);
|
.isInstanceOf(IllegalArgumentException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setClockWhenNullThenThrowIllegalArgumentException() {
|
||||||
|
OidcIdTokenValidator idTokenValidator = new OidcIdTokenValidator(this.registration.build());
|
||||||
|
assertThatThrownBy(() -> idTokenValidator.setClock(null))
|
||||||
|
.isInstanceOf(IllegalArgumentException.class);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void validateWhenIssuerNullThenHasErrors() {
|
public void validateWhenIssuerNullThenHasErrors() {
|
||||||
this.claims.remove(IdTokenClaimNames.ISS);
|
this.claims.remove(IdTokenClaimNames.ISS);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue