2012-03-05 20:27:08 -05:00
|
|
|
module OmniAuth
|
2012-03-05 23:11:35 -05: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-05 20:27:08 -05: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 14:29:57 -04:00
|
|
|
raise_out! if OmniAuth.config.failure_raise_out_environments.include?(ENV['RACK_ENV'].to_s)
|
2012-03-05 23:11:35 -05:00
|
|
|
redirect_to_failure
|
|
|
|
end
|
|
|
|
|
|
|
|
def raise_out!
|
2016-08-08 13:53:36 -04:00
|
|
|
raise(env['omniauth.error'] || OmniAuth::Error.new(env['omniauth.error.type']))
|
2012-03-05 23:11:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_to_failure
|
2012-03-05 20:27:08 -05:00
|
|
|
message_key = env['omniauth.error.type']
|
2020-12-10 10:10:02 -05: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-15 23:00:46 -05:00
|
|
|
Rack::Response.new(['302 Moved'], 302, 'Location' => new_path).finish
|
2012-03-05 20:27:08 -05:00
|
|
|
end
|
2012-03-20 21:47:03 -04:00
|
|
|
|
2018-03-15 07:14:50 -04: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 14:42:02 -04:00
|
|
|
def strategy_name_query_param
|
2014-01-15 23:00:46 -05:00
|
|
|
return '' unless env['omniauth.error.strategy']
|
2018-12-14 11:29:36 -05:00
|
|
|
|
2012-04-10 14:42:02 -04:00
|
|
|
"&strategy=#{env['omniauth.error.strategy'].name}"
|
|
|
|
end
|
|
|
|
|
2012-03-20 21:47:03 -04:00
|
|
|
def origin_query_param
|
2014-01-15 23:00:46 -05:00
|
|
|
return '' unless env['omniauth.origin']
|
2018-12-14 11:29:36 -05:00
|
|
|
|
2012-04-10 19:06:05 -04:00
|
|
|
"&origin=#{Rack::Utils.escape(env['omniauth.origin'])}"
|
2012-03-20 21:47:03 -04:00
|
|
|
end
|
2012-03-05 20:27:08 -05:00
|
|
|
end
|
2012-04-10 19:06:05 -04:00
|
|
|
end
|