Skip to content

Commit 1ca33ca

Browse files
committed
Make DPoP IatClaimValidator public to allow configuring clock and clockSkew
Issue gh-16574 Closes gh-16921
1 parent 546dba7 commit 1ca33ca

File tree

2 files changed

+110
-34
lines changed

2 files changed

+110
-34
lines changed

oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/DPoPProofJwtDecoderFactory.java

+1-34
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.nio.charset.StandardCharsets;
2020
import java.security.MessageDigest;
2121
import java.time.Clock;
22-
import java.time.Duration;
2322
import java.time.Instant;
2423
import java.util.Base64;
2524
import java.util.Collections;
@@ -122,7 +121,7 @@ private static Function<DPoPProofContext, OAuth2TokenValidator<Jwt>> defaultJwtV
122121
return (context) -> new DelegatingOAuth2TokenValidator<>(
123122
new JwtClaimValidator<>("htm", context.getMethod()::equals),
124123
new JwtClaimValidator<>("htu", context.getTargetUri()::equals), new JtiClaimValidator(),
125-
new IatClaimValidator());
124+
new JwtIssuedAtValidator(true));
126125
}
127126

128127
private static final class JtiClaimValidator implements OAuth2TokenValidator<Jwt> {
@@ -168,36 +167,4 @@ private static String computeSHA256(String value) throws Exception {
168167

169168
}
170169

171-
private static final class IatClaimValidator implements OAuth2TokenValidator<Jwt> {
172-
173-
private final Duration clockSkew = Duration.ofSeconds(60);
174-
175-
private final Clock clock = Clock.systemUTC();
176-
177-
@Override
178-
public OAuth2TokenValidatorResult validate(Jwt jwt) {
179-
Assert.notNull(jwt, "DPoP proof jwt cannot be null");
180-
Instant issuedAt = jwt.getIssuedAt();
181-
if (issuedAt == null) {
182-
OAuth2Error error = createOAuth2Error("iat claim is required.");
183-
return OAuth2TokenValidatorResult.failure(error);
184-
}
185-
186-
// Check time window of validity
187-
Instant now = Instant.now(this.clock);
188-
Instant notBefore = now.minus(this.clockSkew);
189-
Instant notAfter = now.plus(this.clockSkew);
190-
if (issuedAt.isBefore(notBefore) || issuedAt.isAfter(notAfter)) {
191-
OAuth2Error error = createOAuth2Error("iat claim is invalid.");
192-
return OAuth2TokenValidatorResult.failure(error);
193-
}
194-
return OAuth2TokenValidatorResult.success();
195-
}
196-
197-
private static OAuth2Error createOAuth2Error(String reason) {
198-
return new OAuth2Error(OAuth2ErrorCodes.INVALID_DPOP_PROOF, reason, null);
199-
}
200-
201-
}
202-
203170
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2002-2025 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+
17+
package org.springframework.security.oauth2.jwt;
18+
19+
import java.time.Clock;
20+
import java.time.Duration;
21+
import java.time.Instant;
22+
23+
import org.springframework.security.oauth2.core.OAuth2Error;
24+
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
25+
import org.springframework.security.oauth2.core.OAuth2TokenValidator;
26+
import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
27+
import org.springframework.util.Assert;
28+
29+
/**
30+
* An {@link OAuth2TokenValidator} responsible for validating the {@link JwtClaimNames#IAT
31+
* "iat"} claim in the {@link Jwt}.
32+
*
33+
* @author Joe Grandja
34+
* @since 6.5
35+
* @see OAuth2TokenValidator
36+
* @see Jwt
37+
* @see <a target="_blank" href="https://datatracker.ietf.org/doc/html/rfc7519">JSON Web
38+
* Token (JWT)</a>
39+
*/
40+
public final class JwtIssuedAtValidator implements OAuth2TokenValidator<Jwt> {
41+
42+
private final boolean required;
43+
44+
private Duration clockSkew = Duration.ofSeconds(60);
45+
46+
private Clock clock = Clock.systemUTC();
47+
48+
/**
49+
* Constructs a {@code JwtIssuedAtValidator} with the defaults.
50+
*/
51+
public JwtIssuedAtValidator() {
52+
this(false);
53+
}
54+
55+
/**
56+
* Constructs a {@code JwtIssuedAtValidator} using the provided parameters.
57+
* @param required {@code true} if the {@link JwtClaimNames#IAT "iat"} claim is
58+
* REQUIRED in the {@link Jwt}, {@code false} otherwise
59+
*/
60+
public JwtIssuedAtValidator(boolean required) {
61+
this.required = required;
62+
}
63+
64+
@Override
65+
public OAuth2TokenValidatorResult validate(Jwt jwt) {
66+
Assert.notNull(jwt, "jwt cannot be null");
67+
Instant issuedAt = jwt.getIssuedAt();
68+
if (issuedAt == null && this.required) {
69+
OAuth2Error error = createOAuth2Error("iat claim is required.");
70+
return OAuth2TokenValidatorResult.failure(error);
71+
}
72+
73+
if (issuedAt != null) {
74+
// Check time window of validity
75+
Instant now = Instant.now(this.clock);
76+
Instant notBefore = now.minus(this.clockSkew);
77+
Instant notAfter = now.plus(this.clockSkew);
78+
if (issuedAt.isBefore(notBefore) || issuedAt.isAfter(notAfter)) {
79+
OAuth2Error error = createOAuth2Error("iat claim is invalid.");
80+
return OAuth2TokenValidatorResult.failure(error);
81+
}
82+
}
83+
return OAuth2TokenValidatorResult.success();
84+
}
85+
86+
/**
87+
* Sets the clock skew. The default is 60 seconds.
88+
* @param clockSkew the clock skew
89+
*/
90+
public void setClockSkew(Duration clockSkew) {
91+
Assert.notNull(clockSkew, "clockSkew cannot be null");
92+
Assert.isTrue(clockSkew.getSeconds() >= 0, "clockSkew must be >= 0");
93+
this.clockSkew = clockSkew;
94+
}
95+
96+
/**
97+
* Sets the {@link Clock} used in {@link Instant#now(Clock)}.
98+
* @param clock the clock
99+
*/
100+
public void setClock(Clock clock) {
101+
Assert.notNull(clock, "clock cannot be null");
102+
this.clock = clock;
103+
}
104+
105+
private static OAuth2Error createOAuth2Error(String reason) {
106+
return new OAuth2Error(OAuth2ErrorCodes.INVALID_TOKEN, reason, null);
107+
}
108+
109+
}

0 commit comments

Comments
 (0)