fix(erc8128): add setIfNotExists to SecondaryStorage, use atomic nonce consumption#2
Closed
jacobot01 wants to merge 43 commits into
Closed
fix(erc8128): add setIfNotExists to SecondaryStorage, use atomic nonce consumption#2jacobot01 wants to merge 43 commits into
jacobot01 wants to merge 43 commits into
Conversation
…gnatures The invalidation endpoint sets notBefore = floor(now). If a replayable sig was created in the same second, created < notBefore is false (X < X). Using now+1 ensures same-second sigs are invalidated.
…le, tighten prose
…d auth, agent-native auth
…nonce + invalidation When secondaryStorage (e.g. Redis) is configured, nonces and invalidation records are now stored there by default with TTL-based auto-cleanup. Set storeInDatabase: true to dual-write (DB + secondaryStorage), using secondaryStorage as a fast read-through layer. Follows Better Auth's session.storeSessionInDatabase pattern. New files: - invalidation-store.ts: InvalidationOps abstraction (DB / secondaryStorage / dual) Modified: - nonce-store.ts: added createSecondaryStorageNonceStore() - index.ts: added storeInDatabase option, replaced inline DB queries with InvalidationOps abstraction, lazy-initialized storage selection
…tion stores Docs: - Replaced 'Verification Cache' section with 'Storage' section - Documents storeInDatabase option, storage resolution table - Links to BA's session.storeSessionInDatabase pattern - Added storeInDatabase to options table Tests (22 new): - nonce-store: secondaryStorage nonce store (consume, TTL, error handling), adapter fallback on error - invalidation-store: DB ops (upsert, find, normalize), secondaryStorage ops (store, retrieve, TTL, error swallowing), dual ops (dual-write, SS-first read, DB fallback)
Previously getNonceStore() fell back to DB-only when storeInDatabase was true. Now it creates a dual nonce store that consumes from both secondaryStorage (fast path) and DB (durability), matching the invalidation store's dual-write behavior. Also adds createDualNonceStore() and 2 new tests for it.
- add storage capability detection (secondary-storage | database | none) - disable /erc8128/verify and /erc8128/invalidate in no-storage mode (404) - expose discovery capabilities in /.well-known/erc8128 - enforce replayable routes require storage (401 replayable_requires_storage) - add process-local in-memory nonce + invalidation fallback stores - make nonce store singleton per plugin instance - tighten docs quickstart and storage matrix for stateless behavior - add memory nonce store tests and update discovery expectation Tested: 108/108 passing across erc8128 plugin suites
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Problem
When using the erc8128 plugin with Redis as secondaryStorage, nonce consumption does
GET+SET(2 round-trips) instead of a single atomicSET NX.Changes
packages/core/src/db/type.tssetIfNotExistsmethod to theSecondaryStorageinterfacepackages/better-auth/src/plugins/erc8128/nonce-store.tscreateSecondaryStorageNonceStorenow usesstorage.setIfNotExists()when available (single atomicSET NXcommand)get+setpattern for storage implementations that don't support itImpact