Class: WardenOmniAuth
- Inherits:
-
Object
- Object
- WardenOmniAuth
- Defined in:
- lib/warden_omniauth.rb
Defined Under Namespace
Classes: Strategy
Constant Summary collapse
- DEFAULT_CALLBACK =
lambda do |user| u = {} u[:info] = user['info'] u[:uid] = user['uid'] u[:credentials] = user['credentials'] u[:provider] = user['provider'] u end
- SCOPE_KEY =
'warden_omni_auth.scope'
- SESSION_KEY =
'rack.session'
Class Method Summary collapse
-
.on_callback(&blk) ⇒ Object
Setup a callback to transform the user from the omni user hash to what you want warden to store as the user object.
-
.setup_strategies(*names) ⇒ Object
Create a warden strategy to wrap an OmniAuth strategy.
-
.snake_case(string) ⇒ String
Pulled from extlib Convert to snake case.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) {|_self| ... } ⇒ WardenOmniAuth
constructor
A new instance of WardenOmniAuth.
- #redirect_after_callback(&block) ⇒ Object
-
#redirect_after_callback=(path) ⇒ Object
redirect after a callback.
- #redirect_after_callback_path(env = {}) ⇒ Object
Constructor Details
#initialize(app) {|_self| ... } ⇒ WardenOmniAuth
Returns a new instance of WardenOmniAuth.
87 88 89 90 91 92 93 94 95 |
# File 'lib/warden_omniauth.rb', line 87 def initialize(app) # setup the warden strategies to wrap the omniauth ones names = OmniAuth::Strategies.constants.map do |konstant| name = WardenOmniAuth.snake_case(konstant.to_s) end WardenOmniAuth.setup_strategies(*names) yield self if block_given? @app = app end |
Class Method Details
.on_callback(&blk) ⇒ Object
Setup a callback to transform the user from the omni user hash to what you want warden to store as the user object
23 24 25 26 |
# File 'lib/warden_omniauth.rb', line 23 def self.on_callback(&blk) @on_callback = blk if blk @on_callback || DEFAULT_CALLBACK end |
.setup_strategies(*names) ⇒ Object
Create a warden strategy to wrap an OmniAuth strategy
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/warden_omniauth.rb', line 32 def self.setup_strategies(*names) names.map do |name| full_name = :"omni_#{name}" unless Warden::Strategies[full_name] klass = Class.new(WardenOmniAuth::Strategy) klass.omni_name = name Warden::Strategies.add(full_name, klass) end Warden::Strategies[full_name] end end |
.snake_case(string) ⇒ String
Pulled from extlib Convert to snake case.
"FooBar".snake_case #=> "foo_bar"
"HeadlineCNNNews".snake_case #=> "headline_cnn_news"
"CNN".snake_case #=> "cnn"
80 81 82 83 84 85 |
# File 'lib/warden_omniauth.rb', line 80 def self.snake_case(string) return string.downcase if string.match(/\A[A-Z]+\z/) string.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2'). gsub(/([a-z])([A-Z])/, '\1_\2'). downcase end |
Instance Method Details
#call(env) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/warden_omniauth.rb', line 109 def call(env) request = Rack::Request.new(env) prefix = OmniAuth::Configuration.instance.path_prefix if request.path =~ /^#{prefix}\/(.+?)\/callback$/i strategy_name = $1 strategy = Warden::Strategies._strategies.keys.detect{|k| k.to_s == "omni_#{strategy_name}"} if !strategy Rack::Response.new("Unknown Handler", 401).finish else # Warden needs to use a hashie for looking up scope # and strategy names session = env[SESSION_KEY] scope = session[SCOPE_KEY] if scope.nil? || scope.to_s.length < 100 # have to protect against symbols :(. need a hashie args = [strategy] args << {:scope => scope.to_sym} if scope response = Rack::Response.new if env['warden'].authenticate? *args response.redirect(redirect_after_callback_path(env)) response.finish else auth_path = request.path.gsub(/\/callback$/, "") response.redirect(auth_path) response.finish end else Rack::Response.new("Bad Session", 400).finish end end else @app.call(env) end end |
#redirect_after_callback(&block) ⇒ Object
101 102 103 |
# File 'lib/warden_omniauth.rb', line 101 def redirect_after_callback(&block) @generate_path_to_redirect_after_callback = block end |
#redirect_after_callback=(path) ⇒ Object
redirect after a callback
98 99 100 |
# File 'lib/warden_omniauth.rb', line 98 def redirect_after_callback=(path) @generate_path_to_redirect_after_callback = proc { path } end |
#redirect_after_callback_path(env = {}) ⇒ Object
105 106 107 |
# File 'lib/warden_omniauth.rb', line 105 def redirect_after_callback_path(env={}) (@generate_path_to_redirect_after_callback || proc { "/" } ).call(env) end |