2012-03-06 01:27:08 +00:00
|
|
|
module OmniAuth
|
2012-03-06 04:11:35 +00:00
|
|
|
# This simple Rack endpoint that serves as the default
|
|
|
|
# 'failure' mechanism for OmniAuth. If a strategy fails for
|
|
|
|
# any reason this endpoint will be invoked. The default behavior
|
|
|
|
# is to redirect to `/auth/failure` except in the case of
|
|
|
|
# a development `RACK_ENV`, in which case an exception will
|
|
|
|
# be raised.
|
2012-03-06 01:27:08 +00:00
|
|
|
class FailureEndpoint
|
|
|
|
attr_reader :env
|
|
|
|
|
|
|
|
def self.call(env)
|
|
|
|
new(env).call
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(env)
|
|
|
|
@env = env
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
2013-09-01 18:29:57 +00:00
|
|
|
raise_out! if OmniAuth.config.failure_raise_out_environments.include?(ENV['RACK_ENV'].to_s)
|
2012-03-06 04:11:35 +00:00
|
|
|
redirect_to_failure
|
|
|
|
end
|
|
|
|
|
|
|
|
def raise_out!
|
2016-08-08 17:53:36 +00:00
|
|
|
raise(env['omniauth.error'] || OmniAuth::Error.new(env['omniauth.error.type']))
|
2012-03-06 04:11:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_to_failure
|
2012-03-06 01:27:08 +00:00
|
|
|
message_key = env['omniauth.error.type']
|
2020-12-10 15:10:02 +00:00
|
|
|
|
|
|
|
new_path = "#{env['SCRIPT_NAME']}#{strategy_path_prefix}/failure?message=#{Rack::Utils.escape(message_key)}#{origin_query_param}#{strategy_name_query_param}"
|
2014-01-16 04:00:46 +00:00
|
|
|
Rack::Response.new(['302 Moved'], 302, 'Location' => new_path).finish
|
2012-03-06 01:27:08 +00:00
|
|
|
end
|
2012-03-21 01:47:03 +00:00
|
|
|
|
2018-03-15 11:14:50 +00:00
|
|
|
def strategy_path_prefix
|
|
|
|
if env['omniauth.error.strategy']
|
|
|
|
env['omniauth.error.strategy'].path_prefix
|
|
|
|
else
|
|
|
|
OmniAuth.config.path_prefix
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-10 18:42:02 +00:00
|
|
|
def strategy_name_query_param
|
2014-01-16 04:00:46 +00:00
|
|
|
return '' unless env['omniauth.error.strategy']
|
2018-12-14 16:29:36 +00:00
|
|
|
|
2012-04-10 18:42:02 +00:00
|
|
|
"&strategy=#{env['omniauth.error.strategy'].name}"
|
|
|
|
end
|
|
|
|
|
2012-03-21 01:47:03 +00:00
|
|
|
def origin_query_param
|
2014-01-16 04:00:46 +00:00
|
|
|
return '' unless env['omniauth.origin']
|
2018-12-14 16:29:36 +00:00
|
|
|
|
2012-04-10 23:06:05 +00:00
|
|
|
"&origin=#{Rack::Utils.escape(env['omniauth.origin'])}"
|
2012-03-21 01:47:03 +00:00
|
|
|
end
|
2012-03-06 01:27:08 +00:00
|
|
|
end
|
2012-04-10 23:06:05 +00:00
|
|
|
end
|