Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ test/tmp
test/version_tmp
tmp
*.swp
vendor/bundle
10 changes: 7 additions & 3 deletions lib/omniauth/strategies/oauth2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,16 @@ def callback_phase # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexi
fail!(error, CallbackError.new(request.params["error"], request.params["error_description"] || request.params["error_reason"], request.params["error_uri"]))
else
self.access_token = build_access_token
self.access_token = access_token.refresh! if access_token.expired?
super
if access_token
self.access_token = access_token.refresh! if access_token.expired?
super
else
fail!(:invalid_credentials, CallbackError.new(:invalid_credentials, "Failed to build access token"))
end
end
rescue ::OAuth2::Error, CallbackError => e
fail!(:invalid_credentials, e)
rescue ::Timeout::Error, ::Errno::ETIMEDOUT, OAuth2::TimeoutError, OAuth2::ConnectionError => e
rescue ::Timeout::Error, ::Errno::ETIMEDOUT, ::OAuth2::TimeoutError, ::OAuth2::ConnectionError => e
fail!(:timeout, e)
rescue ::SocketError => e
fail!(:failed_to_connect, e)
Expand Down
9 changes: 9 additions & 0 deletions spec/omniauth/strategies/oauth2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ def app
expect(instance).to receive(:fail!).with(:csrf_detected, anything)
instance.callback_phase
end

it "handles the case when build_access_token returns nil" do
params.delete("error")
params.delete("error_reason")
allow(instance).to receive(:build_access_token).and_return(nil)

expect(instance).to receive(:fail!).with(:invalid_credentials, anything)
instance.callback_phase
end
end
end

Expand Down