-
Notifications
You must be signed in to change notification settings - Fork 100
[Feature] Add thread-safe token caching with auto-renewal for confidential clients #588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[Feature] Add thread-safe token caching with auto-renewal for confidential clients #588
Conversation
- Add TokenCache struct with RWMutex for concurrent access - Implement automatic token renewal with configurable buffer - Add token validation and cache statistics - Support cache management operations (clear, stats) - Generate deterministic cache keys from scopes and tenant
- Add EnhancedClient struct with built-in token caching - Add EnhancedClientOptions for configurable renewal buffer - Import cache package for token management
- Add NewEnhancedClient with default 2-minute renewal buffer - Add NewEnhancedClientWithOptions for custom configuration - Integrate token cache with existing client functionality
- Add AcquireTokenByCredentialWithCaching with automatic token reuse - Add ForceRefreshToken for explicit token refresh - Add cache management methods (ClearTokenCache, IsTokenCached) - Add GetCacheStats for monitoring and debugging - Integrate with existing token acquisition flow
- Test token caching and reuse functionality - Test force refresh capability - Test cache expiry with configurable renewal buffer - Verify cache statistics and management operations
- Demonstrate automatic token caching and renewal - Show force refresh capability - Display cache statistics and management - Provide production-ready usage patterns
|
|
@microsoft-github-policy-service agree company="Sprinklr" |
|
Hi @mynameispathak , thanks for the PR. Today, MSAL Go caches tokens in memory, associated with each ConfidentialClientApplication object. What are you trying to achieve that you cannot do with MSAL Go today? Note:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not consistent with other MSALs
|
Hi @mynameispathak , apologies for the delayed response — I wanted to ensure I reviewed everything thoroughly. Regarding issue #587, here are my thoughts:
Lastly, regarding the issues you linked at the bottom in #587 — #569 and #570 — please note that these are related to Managed Identity, not Confidential Client. They don't share the same implementation. |



Summary
This PR introduces enhanced token caching capabilities for confidential clients, providing thread-safe token storage with automatic renewal to improve performance and reduce unnecessary network requests.
Changes Made
New Features
sync.RWMutexNew Methods
NewEnhancedClient()- Creates enhanced client with default settingsNewEnhancedClientWithOptions()- Creates enhanced client with custom renewal bufferAcquireTokenByCredentialWithCaching()- Token acquisition with automatic cachingForceRefreshToken()- Force refresh and cache new tokenClearTokenCache()- Clear all cached tokensIsTokenCached()- Check if valid token exists in cacheGetCacheStats()- Get cache statisticsImplementation Details
Benefits
Clientremains unchangedUsage Example
Testing
Related Issues
Closes #587
Breaking Changes
None - this is purely additive functionality.