-
Notifications
You must be signed in to change notification settings - Fork 14
Fix intermittent 401 errors from token expiration race condition #109
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: master
Are you sure you want to change the base?
Conversation
Co-authored-by: Subterrane <5290140+Subterrane@users.noreply.github.com>
Co-authored-by: Subterrane <5290140+Subterrane@users.noreply.github.com>
Co-authored-by: Subterrane <5290140+Subterrane@users.noreply.github.com>
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.
Pull Request Overview
Fixes intermittent 401 Unauthorized errors caused by OAuth token expiration race conditions by implementing proactive token refresh with configurable buffer time and automatic fallback mechanisms.
- Added configurable token expiration buffer (default: 30s) to refresh tokens before they expire
- Implemented automatic fallback from token refresh to new token generation when refresh fails
- Enhanced token management with improved expiration checking and error handling
Reviewed Changes
Copilot reviewed 4 out of 948 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/onelogin/api/client.rb | Core implementation of token expiration buffer, enhanced expiration checking, and fallback logic |
| spec/lib/onelogin/api/token_expiration_spec.rb | Comprehensive test suite covering token expiration scenarios and fallback behavior |
| onelogin.gemspec | Added webmock development dependency for testing |
| README.md | Updated documentation to include new token_expiration_buffer configuration option |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Internal method to get a new access token | ||
| # This is separate from the public access_token method to allow internal use | ||
| # | ||
| def get_new_token | ||
| access_token | ||
| end |
Copilot
AI
Oct 29, 2025
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.
This method simply delegates to access_token without adding any value. Consider directly calling access_token in prepare_token instead of introducing this wrapper method, or add meaningful functionality if this separation is intended for future extensibility.
Intermittent 401 Unauthorized errors occurred when OAuth tokens expired between the expiration check and API call execution. No retry or fallback mechanism existed when token refresh failed.
Changes
Token Expiration Buffer
token_expiration_buffer(default: 30s) to refresh tokens proactively before expirationexpired?to checkTime.now.utc > (@expiration - @token_expiration_buffer)instead of exact expirationAutomatic Fallback
prepare_tokennow attemptsregenerate_token || get_new_tokenwhen expiredConfiguration
The buffer prevents race conditions by ensuring tokens are refreshed before they expire during API calls. The fallback ensures credentials remain valid even when refresh endpoints fail.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.