Interface PasscodeService<P extends PasswordToken,U extends User>
- All Known Implementing Classes:
DefaultPasscodeService
public interface PasscodeService<P extends PasswordToken,U extends User>
This service provides methods for creating and consuming random passcodes.
- Author:
- Cade Rea (cade-rea)
-
Method Summary
Modifier and TypeMethodDescriptionConsume a passcode.createPasscode(U user, String purpose) Create a new token for a User with a specific purpose.Generate a new random token String with only letters and numbers.generateSecureToken(boolean includeSpecialCharacters) Generate a new random token String that may contain special characters.booleaninvalidate(String passcodeId) Invalidate an active passcode.voidpurgeTokensOlderThan(Duration duration, int batchSize) Purge all tokens older than the given duration.org.springframework.data.domain.Page<String>readIdsByDateCreatedBefore(Instant createdDate, org.springframework.data.domain.Pageable page) Find all tokens by id that were created before the given date.readTokensForUser(U user, String purpose) Return all of a user's tokens for a particular purpose.booleanGet the status of aPasswordToken.
-
Method Details
-
createPasscode
Create a new token for a User with a specific purpose.- Parameters:
user- The User who will own the token.purpose- The purpose for this token.- Returns:
- A new
PasswordTokenobject with the new plaintext passcode on the token field.
-
consume
Consume a passcode.- Parameters:
user- The User that owns the token.purpose- The purpose of the token. This will be validated against the purpose stored with the token.passcode- The passcode String to consume.- Returns:
- An optional containing the used token if successful, empty if unsuccessful.
-
status
Get the status of aPasswordToken.- Parameters:
passcodeId- The id of thePasswordToken.- Returns:
- True if the token is valid, false otherwise.
-
invalidate
Invalidate an active passcode.- Parameters:
passcodeId- The id of thePasswordToken.- Returns:
- True if the passcode was invalidated, false otherwise.
-
readTokensForUser
Return all of a user's tokens for a particular purpose. The token fields of the returned objects will contain the encoded passcode.- Parameters:
user- The User that owns the tokens.purpose- The purpose of the tokens.- Returns:
- A List of
PasswordToken.
-
generateSecureToken
String generateSecureToken()Generate a new random token String with only letters and numbers. This String is not persisted. This token is not encoded. For a random password suitable for persistence, usePasswordGenerator.- Returns:
- A secure random String.
-
generateSecureToken
Generate a new random token String that may contain special characters. The String is not persisted. This token is not encoded. For a random password suitable for persistence, usePasswordGenerator.- Parameters:
includeSpecialCharacters- True if special characters should be included in the token. If false, only letters and numbers will be used.- Returns:
- A secure random String.
-
readIdsByDateCreatedBefore
org.springframework.data.domain.Page<String> readIdsByDateCreatedBefore(Instant createdDate, org.springframework.data.domain.Pageable page) Find all tokens by id that were created before the given date.- Parameters:
createdDate- The date to compare to.page- The page request.- Returns:
- A page of ids.
-
purgeTokensOlderThan
Purge all tokens older than the given duration.- Parameters:
duration- The duration before which the tokens were created. e.g. if this is 1 day, tokens created more than 1 day ago will be purged.batchSize- The number of tokens to purge in each batch. If less than or equal to 0, defaults to 500.
-