Package io.github.pratiyush.totp
Class InMemoryReplayGuard
java.lang.Object
io.github.pratiyush.totp.InMemoryReplayGuard
- All Implemented Interfaces:
ReplayGuard,AutoCloseable
Thread-safe in-memory implementation of
ReplayGuard.
This implementation stores used codes in a ConcurrentHashMap with automatic expiration. Suitable for single-instance deployments.
Features
- Thread-safe concurrent access
- Automatic time-based expiration
- Configurable retention period
- Background cleanup thread
Limitations
- Not suitable for clustered deployments (use Redis-backed implementation)
- Data lost on JVM restart
Usage Example
// Create with 2-minute retention (covers 30s period + drift)
ReplayGuard guard = new InMemoryReplayGuard(Duration.ofMinutes(2));
// Use with TOTP verification
TOTP totp = TOTP.builder().replayGuard(guard).build();
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionInMemoryReplayGuard(Duration retention) Creates a new in-memory replay guard with the specified retention period. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all tracked codes.voidclose()Shuts down the background cleanup thread.static InMemoryReplayGuardforConfig(TOTPConfig config) Creates a replay guard configured for a specific TOTP configuration.Returns the configured retention period.booleanAttempts to mark a code as used.intsize()Returns the number of codes currently tracked.booleanChecks if a code has been used without marking it.static InMemoryReplayGuardCreates a replay guard with default retention of 2 minutes.
-
Constructor Details
-
InMemoryReplayGuard
Creates a new in-memory replay guard with the specified retention period.The retention period should be at least:
period * (1 + 2 * drift)to ensure codes remain tracked for their entire validity window.- Parameters:
retention- how long to track used codes- Throws:
NullPointerException- if retention is nullIllegalArgumentException- if retention is negative or zero
-
-
Method Details
-
withDefaultRetention
Creates a replay guard with default retention of 2 minutes.This default covers the standard 30-second period with drift of 1, plus additional margin for clock skew.
- Returns:
- new replay guard with default settings
-
forConfig
Creates a replay guard configured for a specific TOTP configuration.- Parameters:
config- the TOTP configuration- Returns:
- new replay guard with appropriate retention
-
markUsed
Description copied from interface:ReplayGuardAttempts to mark a code as used.This method should be called after successful TOTP verification. Returns true only if the code was not previously used.
- Specified by:
markUsedin interfaceReplayGuard- Parameters:
key- unique key combining user identifier and code- Returns:
- true if the code was successfully marked as used (first use), false if the code was already used
-
wasUsed
Description copied from interface:ReplayGuardChecks if a code has been used without marking it.- Specified by:
wasUsedin interfaceReplayGuard- Parameters:
key- the key to check- Returns:
- true if the code was previously used
-
clear
public void clear()Description copied from interface:ReplayGuardClears all tracked codes.Use with caution - this resets replay protection.
- Specified by:
clearin interfaceReplayGuard
-
size
public int size()Description copied from interface:ReplayGuardReturns the number of codes currently tracked.- Specified by:
sizein interfaceReplayGuard- Returns:
- count of tracked codes
-
getRetention
Returns the configured retention period.- Returns:
- the retention duration
-
close
public void close()Shuts down the background cleanup thread.- Specified by:
closein interfaceAutoCloseable
-