Skip to content

Commit e8c71df

Browse files
committed
Use private Inner JdbcOneTimeTokenService classes
Issue gh-15735
1 parent 612b15a commit e8c71df

File tree

2 files changed

+19
-36
lines changed

2 files changed

+19
-36
lines changed

core/src/main/java/org/springframework/security/authentication/ott/JdbcOneTimeTokenService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public void setClock(Clock clock) {
224224
* @author Max Batischev
225225
* @since 6.4
226226
*/
227-
public static class OneTimeTokenParametersMapper implements Function<OneTimeToken, List<SqlParameterValue>> {
227+
private static class OneTimeTokenParametersMapper implements Function<OneTimeToken, List<SqlParameterValue>> {
228228

229229
@Override
230230
public List<SqlParameterValue> apply(OneTimeToken oneTimeToken) {
@@ -244,7 +244,7 @@ public List<SqlParameterValue> apply(OneTimeToken oneTimeToken) {
244244
* @author Max Batischev
245245
* @since 6.4
246246
*/
247-
public static class OneTimeTokenRowMapper implements RowMapper<OneTimeToken> {
247+
private static class OneTimeTokenRowMapper implements RowMapper<OneTimeToken> {
248248

249249
@Override
250250
public OneTimeToken mapRow(ResultSet rs, int rowNum) throws SQLException {

core/src/test/java/org/springframework/security/authentication/ott/JdbcOneTimeTokenServiceTests.java

+17-34
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,25 @@
1717
package org.springframework.security.authentication.ott;
1818

1919
import java.time.Clock;
20+
import java.time.Duration;
2021
import java.time.Instant;
2122
import java.time.ZoneOffset;
2223
import java.time.temporal.ChronoUnit;
23-
import java.util.List;
2424

2525
import org.junit.jupiter.api.AfterEach;
2626
import org.junit.jupiter.api.BeforeEach;
2727
import org.junit.jupiter.api.Test;
2828

29-
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
3029
import org.springframework.jdbc.core.JdbcOperations;
3130
import org.springframework.jdbc.core.JdbcTemplate;
32-
import org.springframework.jdbc.core.PreparedStatementSetter;
33-
import org.springframework.jdbc.core.SqlParameterValue;
3431
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
3532
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
3633
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
37-
import org.springframework.util.CollectionUtils;
3834

3935
import static org.assertj.core.api.Assertions.assertThat;
4036
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
37+
import static org.mockito.BDDMockito.given;
38+
import static org.mockito.Mockito.mock;
4139

4240
/**
4341
* Tests for {@link JdbcOneTimeTokenService}.
@@ -58,8 +56,6 @@ public class JdbcOneTimeTokenServiceTests {
5856

5957
private JdbcOneTimeTokenService oneTimeTokenService;
6058

61-
private final JdbcOneTimeTokenService.OneTimeTokenParametersMapper oneTimeTokenParametersMapper = new JdbcOneTimeTokenService.OneTimeTokenParametersMapper();
62-
6359
@BeforeEach
6460
void setUp() {
6561
this.db = createDb();
@@ -115,7 +111,8 @@ void consumeWhenAuthenticationTokenIsNullThenThrowIllegalArgumentException() {
115111
void generateThenTokenValueShouldBeValidUuidAndProvidedUsernameIsUsed() {
116112
OneTimeToken oneTimeToken = this.oneTimeTokenService.generate(new GenerateOneTimeTokenRequest(USERNAME));
117113

118-
OneTimeToken persistedOneTimeToken = selectOneTimeToken(oneTimeToken.getTokenValue());
114+
OneTimeToken persistedOneTimeToken = this.oneTimeTokenService
115+
.consume(new OneTimeTokenAuthenticationToken(oneTimeToken.getTokenValue()));
119116
assertThat(persistedOneTimeToken).isNotNull();
120117
assertThat(persistedOneTimeToken.getUsername()).isNotNull();
121118
assertThat(persistedOneTimeToken.getTokenValue()).isNotNull();
@@ -134,7 +131,8 @@ void consumeWhenTokenExistsThenReturnItself() {
134131
assertThat(consumedOneTimeToken.getUsername()).isNotNull();
135132
assertThat(consumedOneTimeToken.getTokenValue()).isNotNull();
136133
assertThat(consumedOneTimeToken.getExpiresAt()).isNotNull();
137-
OneTimeToken persistedOneTimeToken = selectOneTimeToken(consumedOneTimeToken.getTokenValue());
134+
OneTimeToken persistedOneTimeToken = this.oneTimeTokenService
135+
.consume(new OneTimeTokenAuthenticationToken(consumedOneTimeToken.getTokenValue()));
138136
assertThat(persistedOneTimeToken).isNull();
139137
}
140138

@@ -162,15 +160,19 @@ void consumeWhenTokenIsExpiredThenReturnNull() {
162160

163161
@Test
164162
void cleanupExpiredTokens() {
165-
OneTimeToken token1 = new DefaultOneTimeToken("123", USERNAME, Instant.now().minusSeconds(300));
166-
OneTimeToken token2 = new DefaultOneTimeToken("456", USERNAME, Instant.now().minusSeconds(300));
167-
saveToken(token1);
168-
saveToken(token2);
163+
Clock clock = mock(Clock.class);
164+
Instant fiveMinutesAgo = Instant.now().minus(Duration.ofMinutes(5));
165+
given(clock.instant()).willReturn(fiveMinutesAgo);
166+
this.oneTimeTokenService.setClock(clock);
167+
OneTimeToken token1 = this.oneTimeTokenService.generate(new GenerateOneTimeTokenRequest(USERNAME));
168+
OneTimeToken token2 = this.oneTimeTokenService.generate(new GenerateOneTimeTokenRequest(USERNAME));
169169

170170
this.oneTimeTokenService.cleanupExpiredTokens();
171171

172-
OneTimeToken deletedOneTimeToken1 = selectOneTimeToken("123");
173-
OneTimeToken deletedOneTimeToken2 = selectOneTimeToken("456");
172+
OneTimeToken deletedOneTimeToken1 = this.oneTimeTokenService
173+
.consume(new OneTimeTokenAuthenticationToken(token1.getTokenValue()));
174+
OneTimeToken deletedOneTimeToken2 = this.oneTimeTokenService
175+
.consume(new OneTimeTokenAuthenticationToken(token2.getTokenValue()));
174176
assertThat(deletedOneTimeToken1).isNull();
175177
assertThat(deletedOneTimeToken2).isNull();
176178
}
@@ -186,23 +188,4 @@ void setCleanupChronWhenAlreadyNullThenNoException() {
186188
this.oneTimeTokenService.setCleanupCron(null);
187189
}
188190

189-
private void saveToken(OneTimeToken oneTimeToken) {
190-
List<SqlParameterValue> parameters = this.oneTimeTokenParametersMapper.apply(oneTimeToken);
191-
PreparedStatementSetter pss = new ArgumentPreparedStatementSetter(parameters.toArray());
192-
this.jdbcOperations.update("INSERT INTO one_time_tokens (token_value, username, expires_at) VALUES (?, ?, ?)",
193-
pss);
194-
}
195-
196-
private OneTimeToken selectOneTimeToken(String tokenValue) {
197-
// @formatter:off
198-
List<OneTimeToken> result = this.jdbcOperations.query(
199-
"select token_value, username, expires_at from one_time_tokens where token_value = ?",
200-
new JdbcOneTimeTokenService.OneTimeTokenRowMapper(), tokenValue);
201-
if (CollectionUtils.isEmpty(result)) {
202-
return null;
203-
}
204-
return result.get(0);
205-
// @formatter:on
206-
}
207-
208191
}

0 commit comments

Comments
 (0)