feat(oauth): Add TokenStorage trait for custom token persistence#529
Merged
Conversation
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>
61cd4c7 to
5e5fec0
Compare
- 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>
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.
Summary
TokenStoragetrait 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 storageFileTokenStorage— default impl that preserves existing file-based behavior (no breaking change)OAuthBuilder::token_storage()— opt-in method to override the backendOAuthInnernow holds the storage so automatic token refresh also goes through the custom backendNo breaking changes
Callers that do not call
.token_storage()continue to useFileTokenStoragetransparently.Test plan
cargo test -p longbridge-oauth— 17 tests pass (added 5 new tests coveringMemoryStorage,StoredTokenround-trip, and conversion betweenOAuthToken↔StoredToken)🤖 Generated with Claude Code