-
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
Draft
Copilot
wants to merge
4
commits into
master
Choose a base branch
from
copilot/fix-401-unauthorized-error
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| /pkg/ | ||
| /spec/reports/ | ||
| /tmp/ | ||
| /vendor/ | ||
|
|
||
| .DS_Store | ||
| .rspec_status | ||
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| require "spec_helper" | ||
| require "webmock/rspec" | ||
|
|
||
| RSpec.describe "Token Expiration Handling" do | ||
| let(:client_id) { 'test_client_id' } | ||
| let(:client_secret) { 'test_client_secret' } | ||
| let(:region) { 'us' } | ||
| let(:token_url) { 'https://api.us.onelogin.com/auth/oauth2/v2/token' } | ||
|
|
||
| let(:test_time) { Time.utc(2025, 1, 1, 12, 0, 0) } | ||
|
|
||
| let(:valid_token_response) { | ||
| { | ||
| access_token: 'test_access_token', | ||
| refresh_token: 'test_refresh_token', | ||
| token_type: 'bearer', | ||
| expires_in: 36000, | ||
| created_at: test_time.iso8601 | ||
| }.to_json | ||
| } | ||
|
|
||
| let(:refreshed_token_response) { | ||
| { | ||
| access_token: 'refreshed_access_token', | ||
| refresh_token: 'refreshed_refresh_token', | ||
| token_type: 'bearer', | ||
| expires_in: 36000, | ||
| created_at: test_time.iso8601 | ||
| }.to_json | ||
| } | ||
|
|
||
| before(:each) do | ||
| WebMock.disable_net_connect!(allow_localhost: true) | ||
| end | ||
|
|
||
| after(:each) do | ||
| WebMock.reset! | ||
| end | ||
|
|
||
| context 'when token is nil' do | ||
| it 'expired? should return true' do | ||
| client = OneLogin::Api::Client.new( | ||
| client_id: client_id, | ||
| client_secret: client_secret, | ||
| region: region | ||
| ) | ||
|
|
||
| expect(client.send(:expired?)).to be true | ||
| end | ||
|
|
||
| it 'prepare_token should get a new token' do | ||
| client = OneLogin::Api::Client.new( | ||
| client_id: client_id, | ||
| client_secret: client_secret, | ||
| region: region | ||
| ) | ||
|
|
||
| stub_request(:post, token_url) | ||
| .with( | ||
| body: { 'grant_type' => 'client_credentials' }.to_json, | ||
| headers: { | ||
| 'Authorization' => "client_id:#{client_id},client_secret:#{client_secret}", | ||
| 'Content-Type' => 'application/json' | ||
| } | ||
| ) | ||
| .to_return(status: 200, body: valid_token_response, headers: {}) | ||
|
|
||
| client.send(:prepare_token) | ||
|
|
||
| expect(client.instance_variable_get(:@access_token)).to eq('test_access_token') | ||
| end | ||
| end | ||
|
|
||
| context 'when token is about to expire' do | ||
| it 'expired? should return true when within expiration buffer' do | ||
| client = OneLogin::Api::Client.new( | ||
| client_id: client_id, | ||
| client_secret: client_secret, | ||
| region: region, | ||
| token_expiration_buffer: 30 | ||
| ) | ||
|
|
||
| # Set token to expire in 25 seconds (less than 30 second buffer) | ||
| client.instance_variable_set(:@access_token, 'test_token') | ||
| client.instance_variable_set(:@refresh_token, 'test_refresh') | ||
| client.instance_variable_set(:@expiration, Time.now.utc + 25) | ||
|
|
||
| expect(client.send(:expired?)).to be true | ||
| end | ||
|
|
||
| it 'expired? should return false when outside expiration buffer' do | ||
| client = OneLogin::Api::Client.new( | ||
| client_id: client_id, | ||
| client_secret: client_secret, | ||
| region: region, | ||
| token_expiration_buffer: 30 | ||
| ) | ||
|
|
||
| # Set token to expire in 60 seconds (more than 30 second buffer) | ||
| client.instance_variable_set(:@access_token, 'test_token') | ||
| client.instance_variable_set(:@refresh_token, 'test_refresh') | ||
| client.instance_variable_set(:@expiration, Time.now.utc + 60) | ||
|
|
||
| expect(client.send(:expired?)).to be false | ||
| end | ||
|
|
||
| it 'prepare_token should regenerate token when expired' do | ||
| client = OneLogin::Api::Client.new( | ||
| client_id: client_id, | ||
| client_secret: client_secret, | ||
| region: region, | ||
| token_expiration_buffer: 30 | ||
| ) | ||
|
|
||
| # Set token to expire in 25 seconds | ||
| client.instance_variable_set(:@access_token, 'old_token') | ||
| client.instance_variable_set(:@refresh_token, 'old_refresh') | ||
| client.instance_variable_set(:@expiration, Time.now.utc + 25) | ||
|
|
||
| stub_request(:post, token_url) | ||
| .with( | ||
| body: { | ||
| 'grant_type' => 'refresh_token', | ||
| 'access_token' => 'old_token', | ||
| 'refresh_token' => 'old_refresh' | ||
| }.to_json, | ||
| headers: { | ||
| 'Content-Type' => 'application/json' | ||
| } | ||
| ) | ||
| .to_return(status: 200, body: refreshed_token_response, headers: {}) | ||
|
|
||
| client.send(:prepare_token) | ||
|
|
||
| expect(client.instance_variable_get(:@access_token)).to eq('refreshed_access_token') | ||
| end | ||
| end | ||
|
|
||
| context 'when token regeneration fails' do | ||
| it 'should fall back to getting a new token' do | ||
| client = OneLogin::Api::Client.new( | ||
| client_id: client_id, | ||
| client_secret: client_secret, | ||
| region: region, | ||
| token_expiration_buffer: 30 | ||
| ) | ||
|
|
||
| # Set token to be expired | ||
| client.instance_variable_set(:@access_token, 'old_token') | ||
| client.instance_variable_set(:@refresh_token, 'old_refresh') | ||
| client.instance_variable_set(:@expiration, Time.now.utc + 25) | ||
|
|
||
| # First request: regenerate_token fails with 401 | ||
| stub_request(:post, token_url) | ||
| .with( | ||
| body: { | ||
| 'grant_type' => 'refresh_token', | ||
| 'access_token' => 'old_token', | ||
| 'refresh_token' => 'old_refresh' | ||
| }.to_json | ||
| ) | ||
| .to_return(status: 401, body: { error: 'invalid_token' }.to_json, headers: {}) | ||
|
|
||
| # Second request: get new token succeeds | ||
| stub_request(:post, token_url) | ||
| .with( | ||
| body: { 'grant_type' => 'client_credentials' }.to_json, | ||
| headers: { | ||
| 'Authorization' => "client_id:#{client_id},client_secret:#{client_secret}", | ||
| 'Content-Type' => 'application/json' | ||
| } | ||
| ) | ||
| .to_return(status: 200, body: valid_token_response, headers: {}) | ||
|
|
||
| client.send(:prepare_token) | ||
|
|
||
| # Should have new token, not the old one | ||
| expect(client.instance_variable_get(:@access_token)).to eq('test_access_token') | ||
| end | ||
| end | ||
|
|
||
| context 'configurable token expiration buffer' do | ||
| it 'allows custom expiration buffer' do | ||
| client = OneLogin::Api::Client.new( | ||
| client_id: client_id, | ||
| client_secret: client_secret, | ||
| region: region, | ||
| token_expiration_buffer: 60 | ||
| ) | ||
|
|
||
| # Set token to expire in 45 seconds (less than 60 second buffer) | ||
| client.instance_variable_set(:@access_token, 'test_token') | ||
| client.instance_variable_set(:@refresh_token, 'test_refresh') | ||
| client.instance_variable_set(:@expiration, Time.now.utc + 45) | ||
|
|
||
| expect(client.send(:expired?)).to be true | ||
| end | ||
|
|
||
| it 'uses default buffer of 30 seconds if not specified' do | ||
| client = OneLogin::Api::Client.new( | ||
| client_id: client_id, | ||
| client_secret: client_secret, | ||
| region: region | ||
| ) | ||
|
|
||
| expect(client.instance_variable_get(:@token_expiration_buffer)).to eq(30) | ||
| end | ||
| end | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #!/usr/bin/env ruby3.2 | ||
| # | ||
| # This file was generated by RubyGems. | ||
| # | ||
| # The application 'diff-lcs' is installed as part of a gem, and | ||
| # this file is here to facilitate running it. | ||
| # | ||
|
|
||
| require 'rubygems' | ||
|
|
||
| Gem.use_gemdeps | ||
|
|
||
| version = ">= 0.a" | ||
|
|
||
| str = ARGV.first | ||
| if str | ||
| str = str.b[/\A_(.*)_\z/, 1] | ||
| if str and Gem::Version.correct?(str) | ||
| version = str | ||
| ARGV.shift | ||
| end | ||
| end | ||
|
|
||
| if Gem.respond_to?(:activate_bin_path) | ||
| load Gem.activate_bin_path('diff-lcs', 'htmldiff', version) | ||
| else | ||
| gem "diff-lcs", version | ||
| load Gem.bin_path("diff-lcs", "htmldiff", version) | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #!/usr/bin/env ruby3.2 | ||
| # | ||
| # This file was generated by RubyGems. | ||
| # | ||
| # The application 'httparty' is installed as part of a gem, and | ||
| # this file is here to facilitate running it. | ||
| # | ||
|
|
||
| require 'rubygems' | ||
|
|
||
| Gem.use_gemdeps | ||
|
|
||
| version = ">= 0.a" | ||
|
|
||
| str = ARGV.first | ||
| if str | ||
| str = str.b[/\A_(.*)_\z/, 1] | ||
| if str and Gem::Version.correct?(str) | ||
| version = str | ||
| ARGV.shift | ||
| end | ||
| end | ||
|
|
||
| if Gem.respond_to?(:activate_bin_path) | ||
| load Gem.activate_bin_path('httparty', 'httparty', version) | ||
| else | ||
| gem "httparty", version | ||
| load Gem.bin_path("httparty", "httparty", version) | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #!/usr/bin/env ruby3.2 | ||
| # | ||
| # This file was generated by RubyGems. | ||
| # | ||
| # The application 'diff-lcs' is installed as part of a gem, and | ||
| # this file is here to facilitate running it. | ||
| # | ||
|
|
||
| require 'rubygems' | ||
|
|
||
| Gem.use_gemdeps | ||
|
|
||
| version = ">= 0.a" | ||
|
|
||
| str = ARGV.first | ||
| if str | ||
| str = str.b[/\A_(.*)_\z/, 1] | ||
| if str and Gem::Version.correct?(str) | ||
| version = str | ||
| ARGV.shift | ||
| end | ||
| end | ||
|
|
||
| if Gem.respond_to?(:activate_bin_path) | ||
| load Gem.activate_bin_path('diff-lcs', 'ldiff', version) | ||
| else | ||
| gem "diff-lcs", version | ||
| load Gem.bin_path("diff-lcs", "ldiff", version) | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #!/usr/bin/env ruby3.2 | ||
| # | ||
| # This file was generated by RubyGems. | ||
| # | ||
| # The application 'nokogiri' is installed as part of a gem, and | ||
| # this file is here to facilitate running it. | ||
| # | ||
|
|
||
| require 'rubygems' | ||
|
|
||
| Gem.use_gemdeps | ||
|
|
||
| version = ">= 0.a" | ||
|
|
||
| str = ARGV.first | ||
| if str | ||
| str = str.b[/\A_(.*)_\z/, 1] | ||
| if str and Gem::Version.correct?(str) | ||
| version = str | ||
| ARGV.shift | ||
| end | ||
| end | ||
|
|
||
| if Gem.respond_to?(:activate_bin_path) | ||
| load Gem.activate_bin_path('nokogiri', 'nokogiri', version) | ||
| else | ||
| gem "nokogiri", version | ||
| load Gem.bin_path("nokogiri", "nokogiri", version) | ||
| end |
Oops, something went wrong.
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.
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_tokenwithout adding any value. Consider directly callingaccess_tokeninprepare_tokeninstead of introducing this wrapper method, or add meaningful functionality if this separation is intended for future extensibility.