|
@@ -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,
|