From f9c0288cc25e6f106fc3a392f1a29b23d1c17c96 Mon Sep 17 00:00:00 2001 From: nov Date: Tue, 11 Jul 2023 13:40:28 +0900 Subject: [PATCH 1/3] rescue oauth2 timeout --- lib/omniauth/strategies/oauth2.rb | 2 +- omniauth-oauth2.gemspec | 2 +- spec/omniauth/strategies/oauth2_spec.rb | 27 +++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/omniauth/strategies/oauth2.rb b/lib/omniauth/strategies/oauth2.rb index e445214..468f2aa 100644 --- a/lib/omniauth/strategies/oauth2.rb +++ b/lib/omniauth/strategies/oauth2.rb @@ -94,7 +94,7 @@ def callback_phase # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexi end rescue ::OAuth2::Error, CallbackError => e fail!(:invalid_credentials, e) - rescue ::Timeout::Error, ::Errno::ETIMEDOUT => e + rescue ::Timeout::Error, ::Errno::ETIMEDOUT, ::OAuth2::TimeoutError, ::OAuth2::ConnectionError => e fail!(:timeout, e) rescue ::SocketError => e fail!(:failed_to_connect, e) diff --git a/omniauth-oauth2.gemspec b/omniauth-oauth2.gemspec index a3f7407..14513c3 100644 --- a/omniauth-oauth2.gemspec +++ b/omniauth-oauth2.gemspec @@ -4,7 +4,7 @@ require "omniauth-oauth2/version" Gem::Specification.new do |gem| gem.add_dependency "oauth2", [">= 1.4", "< 3"] - gem.add_dependency "omniauth", "~> 2.0" + gem.add_dependency "omniauth", [">= 2.0.2", "< 3"] gem.add_development_dependency "bundler", "~> 2.0" diff --git a/spec/omniauth/strategies/oauth2_spec.rb b/spec/omniauth/strategies/oauth2_spec.rb index 68b5d62..f8d3e86 100644 --- a/spec/omniauth/strategies/oauth2_spec.rb +++ b/spec/omniauth/strategies/oauth2_spec.rb @@ -140,6 +140,33 @@ def app expect(instance).to receive(:fail!).with(:csrf_detected, anything) instance.callback_phase end + + describe 'exception handlings' do + let(:params) do + {"code" => "code", "state" => state} + end + + before do + allow_any_instance_of(OmniAuth::Strategies::OAuth2).to receive(:build_access_token).and_raise(exception) + end + + { + invalid_credentials: [OAuth2::Error, OmniAuth::Strategies::OAuth2::CallbackError], + timeout: [Timeout::Error, Errno::ETIMEDOUT, OAuth2::TimeoutError, OAuth2::ConnectionError], + failed_to_connect: [SocketError] + }.each do |error_type, exceptions| + exceptions.each do |klass| + context "when #{klass}" do + let(:exception) { klass.new 'error' } + + it do + expect(instance).to receive(:fail!).with(error_type, exception) + instance.callback_phase + end + end + end + end + end end end From 948a9468cf80d2e115e4dab945c3966f29111d87 Mon Sep 17 00:00:00 2001 From: nov Date: Tue, 11 Jul 2023 18:15:31 +0900 Subject: [PATCH 2/3] ah, why requireing v2.0.2 of omniauth gem? it should be oauth2 gem --- omniauth-oauth2.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/omniauth-oauth2.gemspec b/omniauth-oauth2.gemspec index 14513c3..8a4cf11 100644 --- a/omniauth-oauth2.gemspec +++ b/omniauth-oauth2.gemspec @@ -3,8 +3,8 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require "omniauth-oauth2/version" Gem::Specification.new do |gem| - gem.add_dependency "oauth2", [">= 1.4", "< 3"] - gem.add_dependency "omniauth", [">= 2.0.2", "< 3"] + gem.add_dependency "oauth2", [">= 2.0.2", "< 3"] + gem.add_dependency "omniauth", "~> 2.0" gem.add_development_dependency "bundler", "~> 2.0" From dd60580c9bd5660ad6d179fa6a70ee681520b220 Mon Sep 17 00:00:00 2001 From: nov Date: Wed, 12 Jul 2023 09:45:56 +0900 Subject: [PATCH 3/3] use legacy hash syntax for legacy rubies --- spec/omniauth/strategies/oauth2_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/omniauth/strategies/oauth2_spec.rb b/spec/omniauth/strategies/oauth2_spec.rb index f8d3e86..ff6176c 100644 --- a/spec/omniauth/strategies/oauth2_spec.rb +++ b/spec/omniauth/strategies/oauth2_spec.rb @@ -151,9 +151,9 @@ def app end { - invalid_credentials: [OAuth2::Error, OmniAuth::Strategies::OAuth2::CallbackError], - timeout: [Timeout::Error, Errno::ETIMEDOUT, OAuth2::TimeoutError, OAuth2::ConnectionError], - failed_to_connect: [SocketError] + :invalid_credentials => [OAuth2::Error, OmniAuth::Strategies::OAuth2::CallbackError], + :timeout => [Timeout::Error, Errno::ETIMEDOUT, OAuth2::TimeoutError, OAuth2::ConnectionError], + :failed_to_connect => [SocketError] }.each do |error_type, exceptions| exceptions.each do |klass| context "when #{klass}" do