Browse Source

Add public to setClock method in InMemoryOneTimeTokenService

Closes gh-15863
Max Batischev 11 months ago
parent
commit
0c216f0b59

+ 6 - 1
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;
 	}

+ 10 - 0
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<OneTimeToken> generate(int howMany) {
 		List<OneTimeToken> generated = new ArrayList<>(howMany);
 		for (int i = 0; i < howMany; i++) {