Add optional CounterStorage to prevent TOTP replay #69#90
Merged
Conversation
A CounterStorage is bound to a single identity and records the last used counter. When configured on the builder, verify() accepts a valid code only once. Includes a built-in in-memory implementation; custom implementations can use a shared store (Redis, Hazelcast) for distributed systems.
BastiaanJansen
requested changes
Jul 2, 2026
| long counter = calculateCounter(clock, period); | ||
| return hotpGenerator.verify(code, counter, delayWindow); | ||
|
|
||
| if (counterStorage == null) |
Owner
|
Please merge main to your branch. The GitHub actions should succeed from now on. |
BastiaanJansen
approved these changes
Jul 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements the counter storage proposed in #69, so TOTP codes can be truly one-time.
What this adds
CounterStorageinterface with a single method,markAsUsed(long counter).It atomically checks whether the given counter is greater than the last used counter and, if so, stores it.
A
storage instance is bound to a single identity (for example a user) — the same way a
TOTPGeneratoris bound to a single secret.InMemoryCounterStorage: one shared backend for the whole application, bound to an identity per verification withforIdentifier(userId).TOTPGenerator.Builder.withCounterStorage(...).When configured,
verify()accepts a valid code only once; when a code matches within the delay window, the matched counter is atomicallyrecorded and any replay is rejected.
Usage
For distributed systems, implement
CounterStorageagainst a shared store such as Redis or Hazelcast (e.g.new RedisCounterStorage(pool, user.getId())), so a code consumed on one node cannot be replayed on another.Entries may safely expire once outside the delay window.
Design notes
Fully backward compatible and opt-in: without a configured storage,
verify()behaves exactly as before.The interface is a single atomic check-and-store rather than separate get/set methods, so two concurrent verifications (or two nodes) can never both accept the same code.
Counters are monotonic per identity: consuming a code also invalidates older, not yet used codes within the delay window (in line with RFC 6238 §5.2).
On a match within the delay window, the matched counter is stored — consuming a delayed code from the previous window does not block the current window's code.
Testing
Replay rejection, per-identity isolation, delay-window replay, current-code-after-older-code, invalid codes.
A concurrency test asserting that of 10 threads verifying the same code, exactly one succeeds.