Add Support GenerateOneTimeTokenRequestResolver
Closes gh-16291 Signed-off-by: Max Batischev <mblancer@mail.ru>
This commit is contained in:
+17
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.security.authentication.ott;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -26,15 +28,29 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public class GenerateOneTimeTokenRequest {
|
||||
|
||||
private static final Duration DEFAULT_EXPIRES_IN = Duration.ofMinutes(5);
|
||||
|
||||
private final String username;
|
||||
|
||||
private final Duration expiresIn;
|
||||
|
||||
public GenerateOneTimeTokenRequest(String username) {
|
||||
this(username, DEFAULT_EXPIRES_IN);
|
||||
}
|
||||
|
||||
public GenerateOneTimeTokenRequest(String username, Duration expiresIn) {
|
||||
Assert.hasText(username, "username cannot be empty");
|
||||
Assert.notNull(expiresIn, "expiresIn cannot be null");
|
||||
this.username = username;
|
||||
this.expiresIn = expiresIn;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
public Duration getExpiresIn() {
|
||||
return this.expiresIn;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -44,8 +44,8 @@ public final class InMemoryOneTimeTokenService implements OneTimeTokenService {
|
||||
@NonNull
|
||||
public OneTimeToken generate(GenerateOneTimeTokenRequest request) {
|
||||
String token = UUID.randomUUID().toString();
|
||||
Instant fiveMinutesFromNow = this.clock.instant().plusSeconds(300);
|
||||
OneTimeToken ott = new DefaultOneTimeToken(token, request.getUsername(), fiveMinutesFromNow);
|
||||
Instant expiresAt = this.clock.instant().plus(request.getExpiresIn());
|
||||
OneTimeToken ott = new DefaultOneTimeToken(token, request.getUsername(), expiresAt);
|
||||
this.oneTimeTokenByToken.put(token, ott);
|
||||
cleanExpiredTokensIfNeeded();
|
||||
return ott;
|
||||
|
||||
+3
-4
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,7 +21,6 @@ import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -132,8 +131,8 @@ public final class JdbcOneTimeTokenService implements OneTimeTokenService, Dispo
|
||||
public OneTimeToken generate(GenerateOneTimeTokenRequest request) {
|
||||
Assert.notNull(request, "generateOneTimeTokenRequest cannot be null");
|
||||
String token = UUID.randomUUID().toString();
|
||||
Instant fiveMinutesFromNow = this.clock.instant().plus(Duration.ofMinutes(5));
|
||||
OneTimeToken oneTimeToken = new DefaultOneTimeToken(token, request.getUsername(), fiveMinutesFromNow);
|
||||
Instant expiresAt = this.clock.instant().plus(request.getExpiresIn());
|
||||
OneTimeToken oneTimeToken = new DefaultOneTimeToken(token, request.getUsername(), expiresAt);
|
||||
insertOneTimeToken(oneTimeToken);
|
||||
return oneTimeToken;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user