2010-06-12 22:43:07 -04:00
|
|
|
require 'omniauth/core'
|
|
|
|
|
2010-03-30 16:26:26 -04:00
|
|
|
module OmniAuth
|
2010-06-12 22:43:07 -04:00
|
|
|
|
2010-03-30 16:26:26 -04:00
|
|
|
module Strategy
|
2010-06-12 22:43:07 -04:00
|
|
|
|
2010-03-30 16:26:26 -04:00
|
|
|
def self.included(base)
|
|
|
|
base.class_eval do
|
|
|
|
attr_reader :app, :name, :env
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(app, name, *args)
|
|
|
|
@app = app
|
|
|
|
@name = name.to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2010-04-09 21:30:16 -04:00
|
|
|
dup.call!(env)
|
2010-03-30 16:26:26 -04:00
|
|
|
end
|
|
|
|
|
2010-04-09 21:30:16 -04:00
|
|
|
def call!(env)
|
2010-03-30 16:26:26 -04:00
|
|
|
@env = env
|
|
|
|
if request.path == "#{OmniAuth.config.path_prefix}/#{name}"
|
|
|
|
request_phase
|
|
|
|
elsif request.path == "#{OmniAuth.config.path_prefix}/#{name}/callback"
|
|
|
|
callback_phase
|
|
|
|
else
|
2010-04-09 21:30:16 -04:00
|
|
|
if respond_to?(:other_phase)
|
|
|
|
other_phase
|
|
|
|
else
|
2010-10-09 22:20:15 -05:00
|
|
|
call_app!
|
2010-04-09 21:30:16 -04:00
|
|
|
end
|
2010-03-30 16:26:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def request_phase
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def callback_phase
|
2010-10-09 22:20:15 -05:00
|
|
|
env['omniauth.auth'] = auth_hash
|
|
|
|
call_app!
|
|
|
|
end
|
|
|
|
|
|
|
|
def call_app!
|
|
|
|
@env['rack.auth'] = env['omniauth.auth'] if env.key?('omniauth.auth')
|
|
|
|
@env['rack.auth.error'] = env['omniauth.error'] if env.key?('omniauth.error')
|
|
|
|
|
|
|
|
@app.call(@env)
|
2010-03-30 16:26:26 -04:00
|
|
|
end
|
|
|
|
|
2010-04-04 13:56:26 -04:00
|
|
|
def auth_hash
|
|
|
|
{
|
|
|
|
'provider' => name.to_s,
|
|
|
|
'uid' => nil
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_host
|
|
|
|
uri = URI.parse(request.url)
|
|
|
|
uri.path = ''
|
2010-04-29 16:54:07 -04:00
|
|
|
uri.query = nil
|
2010-04-04 13:56:26 -04:00
|
|
|
uri.to_s
|
|
|
|
end
|
|
|
|
|
2010-04-29 16:54:07 -04:00
|
|
|
def callback_url
|
|
|
|
full_host + "#{OmniAuth.config.path_prefix}/#{name}/callback"
|
|
|
|
end
|
|
|
|
|
2010-03-30 16:26:26 -04:00
|
|
|
def session
|
|
|
|
@env['rack.session']
|
|
|
|
end
|
|
|
|
|
|
|
|
def request
|
|
|
|
@request ||= Rack::Request.new(@env)
|
|
|
|
end
|
|
|
|
|
2010-04-29 16:54:07 -04:00
|
|
|
def redirect(uri)
|
|
|
|
r = Rack::Response.new("Redirecting to #{uri}...")
|
|
|
|
r.redirect(uri)
|
|
|
|
r.finish
|
|
|
|
end
|
|
|
|
|
2010-03-30 16:26:26 -04:00
|
|
|
def user_info; {} end
|
|
|
|
|
2010-10-08 12:39:02 -05:00
|
|
|
def fail!(message_key, exception = nil)
|
2010-10-09 22:20:15 -05:00
|
|
|
self.env['omniauth.error'] = exception
|
2010-03-30 16:26:26 -04:00
|
|
|
OmniAuth.config.on_failure.call(self.env, message_key.to_sym)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|