2010-03-30 16:26:26 -04:00
|
|
|
require 'rack'
|
|
|
|
require 'singleton'
|
2010-05-01 14:14:46 -04:00
|
|
|
require 'omniauth/form'
|
|
|
|
|
2010-03-30 16:26:26 -04:00
|
|
|
module OmniAuth
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-06-12 22:43:07 -04:00
|
|
|
autoload :Builder, 'omniauth/builder'
|
|
|
|
autoload :Strategy, 'omniauth/strategy'
|
2010-06-18 20:54:46 -04:00
|
|
|
autoload :Test, 'omniauth/test'
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-06-12 22:43:07 -04:00
|
|
|
module Strategies
|
|
|
|
autoload :Password, 'omniauth/strategies/password'
|
|
|
|
end
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-03-30 16:26:26 -04:00
|
|
|
class Configuration
|
|
|
|
include Singleton
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-05-01 14:14:46 -04:00
|
|
|
@@defaults = {
|
|
|
|
:path_prefix => '/auth',
|
|
|
|
:on_failure => Proc.new do |env, message_key|
|
2010-03-30 16:26:26 -04:00
|
|
|
new_path = "#{OmniAuth.config.path_prefix}/failure?message=#{message_key}"
|
2010-10-11 21:20:30 -04:00
|
|
|
[302, {'Location' => "#{new_path}", 'Content-Type'=> 'text/html'}, []]
|
2010-05-01 14:14:46 -04:00
|
|
|
end,
|
|
|
|
:form_css => Form::DEFAULT_CSS
|
|
|
|
}
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-05-01 14:14:46 -04:00
|
|
|
def self.defaults
|
|
|
|
@@defaults
|
|
|
|
end
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-05-01 14:14:46 -04:00
|
|
|
def initialize
|
|
|
|
@@defaults.each_pair{|k,v| self.send("#{k}=",v)}
|
2010-03-30 16:26:26 -04:00
|
|
|
end
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-03-30 16:26:26 -04:00
|
|
|
def on_failure(&block)
|
|
|
|
if block_given?
|
|
|
|
@on_failure = block
|
|
|
|
else
|
|
|
|
@on_failure
|
|
|
|
end
|
|
|
|
end
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-05-01 14:14:46 -04:00
|
|
|
attr_writer :on_failure
|
|
|
|
attr_accessor :path_prefix, :form_css
|
2010-03-30 16:26:26 -04:00
|
|
|
end
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-03-30 16:26:26 -04:00
|
|
|
def self.config
|
|
|
|
Configuration.instance
|
|
|
|
end
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-03-30 16:26:26 -04:00
|
|
|
def self.configure
|
|
|
|
yield config
|
|
|
|
end
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-04-04 13:56:26 -04:00
|
|
|
module Utils
|
2010-05-01 14:14:46 -04:00
|
|
|
CAMELIZE_SPECIAL = {
|
|
|
|
'oauth' => 'OAuth',
|
|
|
|
'oauth2' => 'OAuth2',
|
|
|
|
'openid' => 'OpenID',
|
|
|
|
'open_id' => 'OpenID',
|
|
|
|
'github' => 'GitHub'
|
|
|
|
}
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-05-01 14:14:46 -04:00
|
|
|
module_function
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-05-01 14:14:46 -04:00
|
|
|
def form_css
|
|
|
|
"<style type='text/css'>#{OmniAuth.config.form_css}</style>"
|
|
|
|
end
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-04-04 13:56:26 -04:00
|
|
|
def deep_merge(hash, other_hash)
|
|
|
|
target = hash.dup
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-04-04 13:56:26 -04:00
|
|
|
other_hash.keys.each do |key|
|
|
|
|
if other_hash[key].is_a? ::Hash and hash[key].is_a? ::Hash
|
|
|
|
target[key] = deep_merge(target[key],other_hash[key])
|
|
|
|
next
|
|
|
|
end
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-04-04 13:56:26 -04:00
|
|
|
target[key] = other_hash[key]
|
|
|
|
end
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-04-04 13:56:26 -04:00
|
|
|
target
|
|
|
|
end
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-04-29 16:54:07 -04:00
|
|
|
def camelize(word, first_letter_in_uppercase = true)
|
|
|
|
return CAMELIZE_SPECIAL[word.to_s] if CAMELIZE_SPECIAL[word.to_s]
|
2010-10-11 21:20:30 -04:00
|
|
|
|
2010-04-04 13:56:26 -04:00
|
|
|
if first_letter_in_uppercase
|
2010-04-29 16:54:07 -04:00
|
|
|
word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
|
2010-04-04 13:56:26 -04:00
|
|
|
else
|
2010-04-29 16:54:07 -04:00
|
|
|
word.first + camelize(word)[1..-1]
|
2010-04-04 13:56:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-03-30 16:26:26 -04:00
|
|
|
end
|