Fix intermittent 401 errors from token expiration race condition#109
Draft
Fix intermittent 401 errors from token expiration race condition#109
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>
Copilot
AI
changed the title
[WIP] Fix 401 Unauthorized error for OneLogin credentials
Fix intermittent 401 errors from token expiration race condition
Oct 29, 2025
There was a problem hiding this comment.
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.
Comment on lines
+85
to
+90
| # 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 |
There was a problem hiding this comment.
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.
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.
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.