|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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 | +import java.time.ZoneId; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.security.oauth2.core.OAuth2Error; |
| 27 | +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; |
| 28 | +import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for {@link JwtAuthTimeValidator} |
| 35 | + * |
| 36 | + * @author Max Batischev |
| 37 | + */ |
| 38 | +public class JwtAuthTimeValidatorTests { |
| 39 | + |
| 40 | + private static final Clock MOCK_NOW = Clock.fixed(Instant.ofEpochMilli(0), ZoneId.systemDefault()); |
| 41 | + |
| 42 | + private static final long MAX_AGE_FIVE_MINS = 300L; |
| 43 | + |
| 44 | + private static final String ERROR_DESCRIPTION = "\"More recent authentication is required\", max_age=\"300\""; |
| 45 | + |
| 46 | + private static final String ERROR_URI = "https://datatracker.ietf.org/doc/html/rfc9470#name-authentication-requirements"; |
| 47 | + |
| 48 | + @Test |
| 49 | + public void validateWhenDifferenceBetweenCurrentTimeAndAuthTimeLessThanMaxAgeThenReturnsSuccess() { |
| 50 | + Instant authTime = Instant.now().minusSeconds(240); |
| 51 | + JwtAuthTimeValidator jwtValidator = new JwtAuthTimeValidator(MAX_AGE_FIVE_MINS); |
| 52 | + Jwt jwt = TestJwts.jwt().claim(JwtClaimNames.AUTH_TIME, authTime.getEpochSecond()).build(); |
| 53 | + |
| 54 | + OAuth2TokenValidatorResult result = jwtValidator.validate(jwt); |
| 55 | + |
| 56 | + assertThat(result.hasErrors()).isFalse(); |
| 57 | + assertThat(result).isEqualTo(OAuth2TokenValidatorResult.success()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void validateWhenDifferenceBetweenCurrentTimeAndAuthTimeGreaterThanMaxAgeThenReturnsError() { |
| 62 | + Instant authTime = Instant.now().minusSeconds(720); |
| 63 | + |
| 64 | + JwtAuthTimeValidator jwtValidator = new JwtAuthTimeValidator(MAX_AGE_FIVE_MINS); |
| 65 | + Jwt jwt = TestJwts.jwt().claim(JwtClaimNames.AUTH_TIME, authTime.getEpochSecond()).build(); |
| 66 | + |
| 67 | + OAuth2TokenValidatorResult result = jwtValidator.validate(jwt); |
| 68 | + |
| 69 | + assertThat(result.hasErrors()).isTrue(); |
| 70 | + // @formatter:off |
| 71 | + OAuth2Error error = result.getErrors().stream() |
| 72 | + .findAny() |
| 73 | + .get(); |
| 74 | + // @formatter:on |
| 75 | + assertThat(error.getUri()).isEqualTo(ERROR_URI); |
| 76 | + assertThat(error.getDescription()).isEqualTo(ERROR_DESCRIPTION); |
| 77 | + assertThat(error.getErrorCode()).isEqualTo(OAuth2ErrorCodes.INSUFFICIENT_USER_AUTHENTICATION); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void validateWhenConfiguredWithFixedClockThenValidatesUsingFixedTime() { |
| 82 | + Instant authTime = Instant.now(MOCK_NOW).minusSeconds(240); |
| 83 | + Jwt jwt = TestJwts.jwt().claim(JwtClaimNames.AUTH_TIME, authTime.getEpochSecond()).build(); |
| 84 | + JwtAuthTimeValidator jwtValidator = new JwtAuthTimeValidator(MAX_AGE_FIVE_MINS); |
| 85 | + jwtValidator.setClock(MOCK_NOW); |
| 86 | + |
| 87 | + OAuth2TokenValidatorResult result = jwtValidator.validate(jwt); |
| 88 | + |
| 89 | + assertThat(result.hasErrors()).isFalse(); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void validateWhenJwtIsNullThenThrowsIllegalArgumentException() { |
| 94 | + JwtAuthTimeValidator jwtValidator = new JwtAuthTimeValidator(MAX_AGE_FIVE_MINS); |
| 95 | + |
| 96 | + assertThatIllegalArgumentException().isThrownBy(() -> jwtValidator.validate(null)); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void setClockWhenInvokedWithNullThenThrowsIllegalArgumentException() { |
| 101 | + JwtAuthTimeValidator jwtValidator = new JwtAuthTimeValidator(MAX_AGE_FIVE_MINS); |
| 102 | + assertThatIllegalArgumentException().isThrownBy(() -> jwtValidator.setClock(null)); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void constructorWhenInvokedWithNullDurationThenThrowsIllegalArgumentException() { |
| 107 | + assertThatIllegalArgumentException().isThrownBy(() -> new JwtAuthTimeValidator(MAX_AGE_FIVE_MINS, null)); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void constructorWhenInvokedWithZeroMaxAgeThenThrowsIllegalArgumentException() { |
| 112 | + assertThatIllegalArgumentException().isThrownBy(() -> new JwtAuthTimeValidator(0, Duration.ZERO)); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments