From 0c216f0b592fbfec5f80b986711c517b1534ab25 Mon Sep 17 00:00:00 2001 From: Max Batischev Date: Sat, 28 Sep 2024 21:42:05 +0300 Subject: [PATCH] Add public to setClock method in InMemoryOneTimeTokenService Closes gh-15863 --- .../ott/InMemoryOneTimeTokenService.java | 7 ++++++- .../ott/InMemoryOneTimeTokenServiceTests.java | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/springframework/security/authentication/ott/InMemoryOneTimeTokenService.java b/core/src/main/java/org/springframework/security/authentication/ott/InMemoryOneTimeTokenService.java index 9683ca4984..6365bdb5f1 100644 --- a/core/src/main/java/org/springframework/security/authentication/ott/InMemoryOneTimeTokenService.java +++ b/core/src/main/java/org/springframework/security/authentication/ott/InMemoryOneTimeTokenService.java @@ -75,7 +75,12 @@ public final class InMemoryOneTimeTokenService implements OneTimeTokenService { return this.clock.instant().isAfter(ott.getExpiresAt()); } - void setClock(Clock clock) { + /** + * Sets the {@link Clock} used when generating one-time token and checking token + * expiry. + * @param clock the clock + */ + public void setClock(Clock clock) { Assert.notNull(clock, "clock cannot be null"); this.clock = clock; } diff --git a/core/src/test/java/org/springframework/security/authentication/ott/InMemoryOneTimeTokenServiceTests.java b/core/src/test/java/org/springframework/security/authentication/ott/InMemoryOneTimeTokenServiceTests.java index 23b398708b..8e76c1538c 100644 --- a/core/src/test/java/org/springframework/security/authentication/ott/InMemoryOneTimeTokenServiceTests.java +++ b/core/src/test/java/org/springframework/security/authentication/ott/InMemoryOneTimeTokenServiceTests.java @@ -28,6 +28,7 @@ import java.util.UUID; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.assertj.core.api.Assertions.assertThatNoException; /** @@ -100,6 +101,15 @@ class InMemoryOneTimeTokenServiceTests { // @formatter:on } + @Test + void setClockWhenNullThenThrowIllegalArgumentException() { + // @formatter:off + assertThatIllegalArgumentException() + .isThrownBy(() -> this.oneTimeTokenService.setClock(null)) + .withMessage("clock cannot be null"); + // @formatter:on + } + private List generate(int howMany) { List generated = new ArrayList<>(howMany); for (int i = 0; i < howMany; i++) {