feat: add CAT/BAT (NUT-21/22) authentication support#94
Draft
4xvgal wants to merge 2 commits intocashubtc:masterfrom
Draft
feat: add CAT/BAT (NUT-21/22) authentication support#944xvgal wants to merge 2 commits intocashubtc:masterfrom
4xvgal wants to merge 2 commits intocashubtc:masterfrom
Conversation
authentication. Adds `mgr.auth.*` API following existing Service -> API
-> Manager pattern.
New components:
- AuthApi: orchestrates per-mint AuthManager + OIDCAuth lifecycle
- startDeviceAuth(): OIDC Device Code Flow
- login(): manual token injection
- restore(): session recovery on app restart
- logout(): cleanup session + AuthProvider
- getSession() / hasSession() / getAuthProvider()
- AuthSession model + AuthSessionRepository interface
- AuthSessionService: session CRUD + expiration validation
- MemoryAuthSessionRepository: in-memory impl for tests
- MintAdapter: setAuthProvider/clearAuthProvider with cache invalidation
- Domain errors: AuthSessionError, AuthSessionExpiredError
- Events: auth-session:updated, auth-session:deleted, auth-session:expired
CAT flow is fully functional — AuthProvider is injected into Mint
constructor.
BAT flow (NUT-22) requires multi-unit support (unit:'auth' keyset) and WalletService authProvider wiring - tracked as future work.
Tests: 9 unit tests (AuthApi) + integration test (Device Code FLow)
|
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.
Description
This PR adds NUT-21/22 authentication support (CAT/BAT) to CoCo, enabling OIDC-based mint authentication via
mgr.auth.*. It integrates cashu-ts AuthManager which handles the full CAT/BAT lifecycle (CAT
storage/refresh, BAT auto-minting/pool management, DLEQ validation, per-endpoint NUT-21 vs NUT-22 detection).
The CAT flow is fully functional end-to-end. BAT (Blind Auth Token) flow requires another repository to handle it
Key changes:
(auto-includes CAT/BAT headers in all HTTP requests)
Notes to Reviewers
CAT Flow (working now): After mgr.auth.startDeviceAuth(mintUrl) or mgr.auth.login(mintUrl, tokens), the AuthProvider
is injected into MintAdapter. All subsequent Mint HTTP requests automatically include the Clear-auth header.
Session tokens are persisted via AuthSessionService and can be restored on app restart with
mgr.auth.restore(mintUrl).
BAT Flow (blocked): cashu-ts AuthManager handles BAT minting/pooling internally, but two things are
needed:
No Breaking Changes: All new APIs are additive. Existing code continues to work without modification.
No Database Migration Required: MemoryAuthSessionRepository is provided for tests. Platform storage adapters
(sqlite3, indexeddb, expo-sqlite) are tracked as future work.
API Changes
New: mgr.auth (AuthApi)
}
New: MintAdapter
New: AuthSessionService
Suggested CHANGELOG Updates
ADDED
MODIFIED
Remaining Work (tracked in FEATURE_TODO.md)
Test Coverage
Unit tests (9 new AuthApi tests, 441 total pass)
bun test packages/core/test/unit/AuthApi.test.ts
Integration test (requires mint with NUT-21/22 enabled + manual OIDC authorization)
MINT_URL=http://localhost:8085 bun test packages/core/test/integration/auth-session.test.ts --timeout 300000
I use customized oidc server nostr-oidc-ts for testing instead of KeyCloak
Which is pass cashu-ts AuthWallet device code flow test.