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