Class: Warden::Proxy
- Inherits:
-
Object
- Object
- Warden::Proxy
- Extended by:
- Forwardable
- Includes:
- Mixins::Common
- Defined in:
- lib/warden/proxy.rb,
lib/warden/errors.rb
Defined Under Namespace
Classes: Errors
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
An accessor to the rack env hash :api: public.
-
#winning_strategy ⇒ Object
:api: private.
Instance Method Summary collapse
- #_session ⇒ Object
-
#authenticate(*args) ⇒ Object
Run the authentiation strategies for the given strategies.
-
#authenticate!(*args) ⇒ Object
The same as
authenticate
except on failure it will throw an :warden symbol causing the request to be halted and rendered through thefailure_app
. -
#authenticated?(scope = :default) ⇒ Boolean
Check to see if there is an authenticated user for the given scope.
-
#errors ⇒ Object
:api: public.
-
#initialize(env, config = {}) ⇒ Proxy
constructor
:nodoc:.
-
#logout(*scopes) ⇒ Object
Provides logout functionality.
-
#result ⇒ Object
proxy methods through to the winning strategy :api: private.
-
#session(scope = :default) ⇒ Object
Provides a scoped session data for authenticated users.
-
#set_user(user, opts = {}) ⇒ Object
Manually set the user into the session and auth proxy.
-
#user(scope = :default) ⇒ Object
Provides acccess to the user object in a given scope for a request.
Methods included from Mixins::Common
Constructor Details
#initialize(env, config = {}) ⇒ Proxy
:nodoc:
19 20 21 22 23 24 25 |
# File 'lib/warden/proxy.rb', line 19 def initialize(env, config = {}) # :nodoc: @env = env @config = config @strategies = @config.fetch(:default_strategies, []) @users = {} errors # setup the error object in the session end |
Instance Attribute Details
#env ⇒ Object (readonly)
An accessor to the rack env hash :api: public
10 11 12 |
# File 'lib/warden/proxy.rb', line 10 def env @env end |
#winning_strategy ⇒ Object
:api: private
6 7 8 |
# File 'lib/warden/proxy.rb', line 6 def winning_strategy @winning_strategy end |
Instance Method Details
#_session ⇒ Object
14 |
# File 'lib/warden/proxy.rb', line 14 alias_method :_session, :session |
#authenticate(*args) ⇒ Object
Run the authentiation strategies for the given strategies.
If there is already a user logged in for a given scope, the strategies are not run This does not halt the flow of control and is a passive attempt to authenticate only When scope is not specified, :default is assumed.
Parameters:
args - a list of symbols (labels) that name the strategies to attempt
opts - an options hash that contains the :scope of the user to check
Example:
env['auth'].authenticate(:password, :basic, :scope => :sudo)
:api: public
54 55 56 57 58 |
# File 'lib/warden/proxy.rb', line 54 def authenticate(*args) scope = scope_from_args(args) _perform_authentication(*args) user(scope) end |
#authenticate!(*args) ⇒ Object
The same as authenticate
except on failure it will throw an :warden symbol causing the request to be halted and rendered through the failure_app
Example
env['warden'].authenticate!(:password, :scope => :publisher) # throws if it cannot authenticate
:api: public
67 68 69 70 71 72 73 |
# File 'lib/warden/proxy.rb', line 67 def authenticate!(*args) opts = opts_from_args(args) scope = scope_from_args(args) _perform_authentication(*args) throw(:warden, opts.merge(:action => :unauthenticated)) if !user(scope) user(scope) end |
#authenticated?(scope = :default) ⇒ Boolean
Check to see if there is an authenticated user for the given scope. When scope is not specified, :default is assumed. This will not try to reconstitute the user from the session and will simply check for the existance of a session key
Parameters:
scope - the scope to check for authentication. Defaults to :default
Example:
env['warden'].authenticated?(:admin)
:api: public
38 39 40 |
# File 'lib/warden/proxy.rb', line 38 def authenticated?(scope = :default) !_session["warden.user.#{scope}.key"].nil? end |
#errors ⇒ Object
:api: public
4 5 6 |
# File 'lib/warden/errors.rb', line 4 def errors @env['warden.errors'] ||= Errors.new end |
#logout(*scopes) ⇒ Object
Provides logout functionality. The logout also manages any authenticated data storage and clears it when a user logs out.
Parameters:
scopes - a list of scopes to logout
Example:
# Logout everyone and clear the session
env['warden'].logout
# Logout the default user but leave the rest of the session alone
env['warden'].logout(:default)
# Logout the :publisher and :admin user
env['warden'].logout(:publisher, :admin)
:api: public
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/warden/proxy.rb', line 139 def logout(*scopes) if scopes.empty? _session.clear @users.clear else scopes.each do |s| _session["warden.user.#{s}.key"] = nil _session["warden.user.#{s}.session"] = nil @users.delete(s) end end end |
#result ⇒ Object
proxy methods through to the winning strategy :api: private
154 155 156 |
# File 'lib/warden/proxy.rb', line 154 def result # :nodoc: winning_strategy.nil? ? nil : winning_strategy.result end |
#session(scope = :default) ⇒ Object
Provides a scoped session data for authenticated users. Warden manages clearing out this data when a user logs out
Example
# default scope
env['warden'].data[:foo] = "bar"
# :sudo scope
env['warden'].data(:sudo)[:foo] = "bar"
:api: public
117 118 119 120 |
# File 'lib/warden/proxy.rb', line 117 def session(scope = :default) raise NotAuthenticated, "#{scope.inspect} user is not logged in" unless authenticated?(scope) _session["warden.user.#{scope}.session"] ||= {} end |
#set_user(user, opts = {}) ⇒ Object
Manually set the user into the session and auth proxy
Parameters:
user - An object that has been setup to serialize into and out of the session.
opts - An options hash. Use the :scope option to set the scope of the user
:api: public
81 82 83 84 85 86 87 88 89 |
# File 'lib/warden/proxy.rb', line 81 def set_user(user, opts = {}) scope = (opts[:scope] ||= :default) Warden::Manager._store_user(user, _session, scope) # Get the user into the session # Run the after hooks for setting the user Warden::Manager._after_set_user.each{|hook| hook.call(user, self, opts)} @users[scope] = user # Store the user in the proxy user object end |
#user(scope = :default) ⇒ Object
Provides acccess to the user object in a given scope for a request. will be nil if not logged in
Example:
# without scope (default user)
env['warden'].user
# with scope
env['warden'].user(:admin)
:api: public
102 103 104 |
# File 'lib/warden/proxy.rb', line 102 def user(scope = :default) @users[scope] ||= lookup_user_from_session(scope) end |