2011-09-03 14:08:07 -04:00
|
|
|
require 'rack'
|
|
|
|
require 'singleton'
|
|
|
|
|
|
|
|
module OmniAuth
|
2011-10-16 19:27:26 -04:00
|
|
|
module Strategies
|
|
|
|
autoload :Developer, 'omniauth/strategies/developer'
|
|
|
|
end
|
2011-09-03 14:08:07 -04:00
|
|
|
|
|
|
|
autoload :Builder, 'omniauth/builder'
|
|
|
|
autoload :Strategy, 'omniauth/strategy'
|
|
|
|
autoload :Test, 'omniauth/test'
|
|
|
|
autoload :Form, 'omniauth/form'
|
2011-09-03 20:26:57 -04:00
|
|
|
autoload :AuthHash, 'omniauth/auth_hash'
|
2011-09-03 14:08:07 -04:00
|
|
|
|
|
|
|
def self.strategies
|
|
|
|
@@strategies ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
class Configuration
|
|
|
|
include Singleton
|
|
|
|
|
|
|
|
@@defaults = {
|
2011-09-22 19:49:22 -04:00
|
|
|
:camelizations => {},
|
2011-09-03 14:08:07 -04:00
|
|
|
:path_prefix => '/auth',
|
|
|
|
:on_failure => Proc.new do |env|
|
|
|
|
message_key = env['omniauth.error.type']
|
|
|
|
new_path = "#{OmniAuth.config.path_prefix}/failure?message=#{message_key}"
|
|
|
|
[302, {'Location' => new_path, 'Content-Type'=> 'text/html'}, []]
|
|
|
|
end,
|
|
|
|
:form_css => Form::DEFAULT_CSS,
|
|
|
|
:test_mode => false,
|
|
|
|
:allowed_request_methods => [:get, :post],
|
|
|
|
:mock_auth => {
|
|
|
|
:default => {
|
|
|
|
'provider' => 'default',
|
|
|
|
'uid' => '1234',
|
2011-11-12 19:02:49 -05:00
|
|
|
'info' => {
|
|
|
|
'name' => 'Bob Example'
|
|
|
|
}
|
2011-09-03 14:08:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def self.defaults
|
|
|
|
@@defaults
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@@defaults.each_pair{|k,v| self.send("#{k}=",v)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_failure(&block)
|
|
|
|
if block_given?
|
|
|
|
@on_failure = block
|
|
|
|
else
|
|
|
|
@on_failure
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_mock(provider, mock={})
|
|
|
|
# Stringify keys recursively one level.
|
|
|
|
mock.keys.each do |key|
|
|
|
|
mock[key.to_s] = mock.delete(key)
|
|
|
|
end
|
|
|
|
mock.each_pair do |key, val|
|
|
|
|
if val.is_a? Hash
|
|
|
|
val.keys.each do |subkey|
|
|
|
|
val[subkey.to_s] = val.delete(subkey)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Merge with the default mock and ensure provider is correct.
|
|
|
|
mock = self.mock_auth[:default].dup.merge(mock)
|
|
|
|
mock["provider"] = provider.to_s
|
|
|
|
|
|
|
|
# Add it to the mocks.
|
|
|
|
self.mock_auth[provider.to_sym] = mock
|
|
|
|
end
|
|
|
|
|
2011-09-26 12:44:17 -04:00
|
|
|
# This is a convenience method to be used by strategy authors
|
|
|
|
# so that they can add special cases to the camelization utility
|
|
|
|
# method that allows OmniAuth::Builder to work.
|
|
|
|
#
|
|
|
|
# @param name [String] The underscored name, e.g. `oauth`
|
|
|
|
# @param camelized [String] The properly camelized name, e.g. 'OAuth'
|
2011-09-22 19:49:22 -04:00
|
|
|
def add_camelization(name, camelized)
|
|
|
|
self.camelizations[name.to_s] = camelized.to_s
|
|
|
|
end
|
|
|
|
|
2011-09-03 14:08:07 -04:00
|
|
|
attr_writer :on_failure
|
2011-09-22 19:49:22 -04:00
|
|
|
attr_accessor :path_prefix, :allowed_request_methods, :form_css, :test_mode, :mock_auth, :full_host, :camelizations
|
2011-09-03 14:08:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.config
|
|
|
|
Configuration.instance
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.configure
|
|
|
|
yield config
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.mock_auth_for(provider)
|
|
|
|
config.mock_auth[provider.to_sym] || config.mock_auth[:default]
|
|
|
|
end
|
|
|
|
|
|
|
|
module Utils
|
|
|
|
module_function
|
|
|
|
|
|
|
|
def form_css
|
|
|
|
"<style type='text/css'>#{OmniAuth.config.form_css}</style>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def deep_merge(hash, other_hash)
|
|
|
|
target = hash.dup
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
target[key] = other_hash[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
target
|
|
|
|
end
|
|
|
|
|
|
|
|
def camelize(word, first_letter_in_uppercase = true)
|
2011-09-22 19:49:22 -04:00
|
|
|
return OmniAuth.config.camelizations[word.to_s] if OmniAuth.config.camelizations[word.to_s]
|
2011-09-03 14:08:07 -04:00
|
|
|
|
|
|
|
if first_letter_in_uppercase
|
|
|
|
word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
|
|
|
|
else
|
|
|
|
word.first + camelize(word)[1..-1]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|