Skip to content

feat(oauth): Add TokenStorage trait for custom token persistence#529

Merged
huacnlee merged 4 commits into
mainfrom
feat/oauth-token-storage
May 21, 2026
Merged

feat(oauth): Add TokenStorage trait for custom token persistence#529
huacnlee merged 4 commits into
mainfrom
feat/oauth-token-storage

Conversation

@huacnlee
Copy link
Copy Markdown
Member

Summary

  • Introduces a TokenStorage trait so callers can plug in their own persistence backend (OS keychain, encrypted store, in-memory cache for tests) instead of always writing to ~/.longbridge/openapi/tokens/<client_id>
  • StoredToken — public struct carrying the token fields passed to/from storage
  • FileTokenStorage — default impl that preserves existing file-based behavior (no breaking change)
  • OAuthBuilder::token_storage() — opt-in method to override the backend
  • OAuthInner now holds the storage so automatic token refresh also goes through the custom backend

No breaking changes

Callers that do not call .token_storage() continue to use FileTokenStorage transparently.

Test plan

  • cargo test -p longbridge-oauth — 17 tests pass (added 5 new tests covering MemoryStorage, StoredToken round-trip, and conversion between OAuthTokenStoredToken)

🤖 Generated with Claude Code

Introduce a `TokenStorage` trait so callers can supply their own backend
(e.g. OS keychain, encrypted store, in-memory cache for tests) instead of
always writing to `~/.longbridge/openapi/tokens/<client_id>`.

- `StoredToken` — public struct carrying the token fields
- `TokenStorage` — `load` / `save` trait; `Send + Sync + 'static`
- `FileTokenStorage` — default impl, preserves existing file-based behavior
- `OAuthBuilder::token_storage()` — opt-in override
- `OAuthInner` now holds the storage so auto-refresh also persists via it

No breaking changes: existing callers that do not call `token_storage()`
continue to use `FileTokenStorage` transparently.

Co-Authored-By: Jason Lee <huacnlee.apply@longbridge-inc.com>
@huacnlee huacnlee force-pushed the feat/oauth-token-storage branch from 61cd4c7 to 5e5fec0 Compare May 21, 2026 07:44
huacnlee and others added 3 commits May 21, 2026 15:48
- Fix test_oauth_builder_token_storage: previous version compared two
  unrelated MemoryStorage instances, making the assertion meaningless
- Add save_count field to MemoryStorage helper for verifying call counts
- Add test_build_loads_valid_token_from_custom_storage: confirms build()
  reads from custom storage and skips the browser flow entirely
- Add test_build_does_not_call_save_for_valid_token: save() must not be
  called when an already-valid token is loaded
- Add test_file_token_storage_round_trip: exercises FileTokenStorage
  save + load using a process-scoped client_id to avoid test collisions
- Add test_file_token_storage_missing_returns_none
- Add test_memory_storage_save_increments_count

Co-Authored-By: Jason Lee <huacnlee.apply@longbridge-inc.com>
@huacnlee huacnlee merged commit 86c3fe5 into main May 21, 2026
56 checks passed
@huacnlee huacnlee deleted the feat/oauth-token-storage branch May 21, 2026 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant