Class: Warden::Strategies::Base
- Inherits:
-
Object
- Object
- Warden::Strategies::Base
- Includes:
- Mixins::Common
- Defined in:
- lib/warden/strategies/base.rb
Overview
A strategy is a place where you can put logic related to authentication. Any strategy inherits from Warden::Strategies::Base.
The Warden::Strategies.add method is a simple way to provide custom strategies. You must declare an @authenticate!@ method. You may provide a @valid?@ method. The valid method should return true or false depending on if the strategy is a valid one for the request.
The parameters for Warden::Strategies.add method are:
<label: Symbol> The label is the name given to a strategy. Use the label to refer to the strategy when authenticating
<strategy: Class|nil> The optional strategy argument if set _must_ be a class that inherits from Warden::Strategies::Base and _must_
implement an @authenticate!@ method
<block> The block acts as a convenient way to declare your strategy. Inside is the class definition of a strategy.
Examples:
Block Declared Strategy:
Warden::Strategies.add(:foo) do
def authenticate!
# authentication logic
end
end
Class Declared Strategy:
Warden::Strategies.add(:foo, MyStrategy)
Instance Attribute Summary collapse
-
#custom_response ⇒ Object
:api: private.
-
#env ⇒ Object
readonly
:api: public.
-
#message ⇒ Object
:api: public.
-
#result ⇒ Object
:api: private.
-
#scope ⇒ Object
readonly
:api: public.
-
#status ⇒ Object
readonly
:api: public.
-
#user ⇒ Object
:api: public.
Instance Method Summary collapse
-
#_run! ⇒ Object
The method that is called from above.
-
#clear! ⇒ Object
Marks this strategy as not performed.
-
#custom!(response) ⇒ Object
Return a custom rack array.
-
#errors ⇒ Object
Access to the errors object.
-
#fail(message = "Failed to Login") ⇒ Object
Causes the strategy to fail, but not halt.
-
#fail!(message = "Failed to Login") ⇒ Object
This causes the strategy to fail.
-
#halt! ⇒ Object
Cause the processing of the strategies to stop and cascade no further :api: public.
-
#halted? ⇒ Boolean
Checks to see if a strategy was halted :api: public.
-
#headers(header = {}) ⇒ Object
Provides access to the headers hash for setting custom headers :api: public.
-
#initialize(env, scope = nil) ⇒ Base
constructor
:api: private.
-
#pass ⇒ Object
A simple method to return from authenticate! if you want to ignore this strategy :api: public.
-
#performed? ⇒ Boolean
Returns if this strategy was already performed.
-
#redirect!(url, params = {}, opts = {}) ⇒ Object
Causes the authentication to redirect.
-
#store? ⇒ Boolean
Checks to see if a strategy should result in a permanent login :api: public.
-
#success!(user, message = nil) ⇒ Object
Whenever you want to provide a user object as “authenticated” use the
success!
method. -
#successful? ⇒ Boolean
Returns true only if the result is a success and a user was assigned.
-
#valid? ⇒ Boolean
Acts as a guarding method for the strategy.
Methods included from Mixins::Common
#params, #request, #reset_session!, #session, #warden_cookies
Constructor Details
#initialize(env, scope = nil) ⇒ Base
:api: private
44 45 46 47 48 |
# File 'lib/warden/strategies/base.rb', line 44 def initialize(env, scope=nil) # :nodoc: @env, @scope = env, scope @status, @headers = nil, {} @halted, @performed = false, false end |
Instance Attribute Details
#custom_response ⇒ Object
:api: private
36 37 38 |
# File 'lib/warden/strategies/base.rb', line 36 def custom_response @custom_response end |
#env ⇒ Object (readonly)
:api: public
39 40 41 |
# File 'lib/warden/strategies/base.rb', line 39 def env @env end |
#message ⇒ Object
:api: public
33 34 35 |
# File 'lib/warden/strategies/base.rb', line 33 def @message end |
#result ⇒ Object
:api: private
36 37 38 |
# File 'lib/warden/strategies/base.rb', line 36 def result @result end |
#scope ⇒ Object (readonly)
:api: public
39 40 41 |
# File 'lib/warden/strategies/base.rb', line 39 def scope @scope end |
#status ⇒ Object (readonly)
:api: public
39 40 41 |
# File 'lib/warden/strategies/base.rb', line 39 def status @status end |
#user ⇒ Object
:api: public
33 34 35 |
# File 'lib/warden/strategies/base.rb', line 33 def user @user end |
Instance Method Details
#_run! ⇒ Object
The method that is called from above. This method calls the underlying authenticate! method :api: private
52 53 54 55 56 |
# File 'lib/warden/strategies/base.rb', line 52 def _run! # :nodoc: @performed = true authenticate! self end |
#clear! ⇒ Object
Marks this strategy as not performed. :api: private
66 67 68 |
# File 'lib/warden/strategies/base.rb', line 66 def clear! @performed = false end |
#custom!(response) ⇒ Object
Return a custom rack array. You must throw an :warden symbol to activate this :api: public
173 174 175 176 177 |
# File 'lib/warden/strategies/base.rb', line 173 def custom!(response) halt! @custom_response = response @result = :custom end |
#errors ⇒ Object
Access to the errors object. :api: public
86 87 88 |
# File 'lib/warden/strategies/base.rb', line 86 def errors @env['warden'].errors end |
#fail(message = "Failed to Login") ⇒ Object
Causes the strategy to fail, but not halt. The strategies will cascade after this failure and warden will check the next strategy. The last strategy to fail will have it’s message displayed. :api: public
144 145 146 147 |
# File 'lib/warden/strategies/base.rb', line 144 def fail( = "Failed to Login") @message = @result = :failure end |
#fail!(message = "Failed to Login") ⇒ Object
This causes the strategy to fail. It does not throw an :warden symbol to drop the request out to the failure application You must throw an :warden symbol somewhere in the application to enforce this Halts the strategies so that this is the last strategy checked :api: public
136 137 138 139 140 |
# File 'lib/warden/strategies/base.rb', line 136 def fail!( = "Failed to Login") halt! @message = @result = :failure end |
#halt! ⇒ Object
Cause the processing of the strategies to stop and cascade no further :api: public
92 93 94 |
# File 'lib/warden/strategies/base.rb', line 92 def halt! @halted = true end |
#halted? ⇒ Boolean
Checks to see if a strategy was halted :api: public
98 99 100 |
# File 'lib/warden/strategies/base.rb', line 98 def halted? !!@halted end |
#headers(header = {}) ⇒ Object
Provides access to the headers hash for setting custom headers :api: public
78 79 80 81 82 |
# File 'lib/warden/strategies/base.rb', line 78 def headers(header = {}) @headers ||= {} @headers.merge! header @headers end |
#pass ⇒ Object
A simple method to return from authenticate! if you want to ignore this strategy :api: public
110 |
# File 'lib/warden/strategies/base.rb', line 110 def pass; end |
#performed? ⇒ Boolean
Returns if this strategy was already performed. :api: private
60 61 62 |
# File 'lib/warden/strategies/base.rb', line 60 def performed? #:nodoc: @performed end |
#redirect!(url, params = {}, opts = {}) ⇒ Object
Causes the authentication to redirect. An :warden symbol must be thrown to actually execute this redirect
Parameters:
url <String> - The string representing the URL to be redirected to
params <Hash> - Any parameters to encode into the URL
opts <Hash> - Any options to redirect with.
available options: permanent => (true || false)
:api: public
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/warden/strategies/base.rb', line 158 def redirect!(url, params = {}, opts = {}) halt! @status = opts[:permanent] ? 301 : 302 headers["Location"] = url.dup headers["Location"] << "?" << Rack::Utils.build_query(params) unless params.empty? headers["Content-Type"] = opts[:content_type] || 'text/plain' @message = opts[:message] || "You are being redirected to #{headers["Location"]}" @result = :redirect headers["Location"] end |
#store? ⇒ Boolean
Checks to see if a strategy should result in a permanent login :api: public
104 105 106 |
# File 'lib/warden/strategies/base.rb', line 104 def store? true end |
#success!(user, message = nil) ⇒ Object
Whenever you want to provide a user object as “authenticated” use the success!
method. This will halt the strategy, and set the user in the appropriate scope. It is the “login” method
Parameters:
user - The user object to login. This object can be anything you have setup to serialize in and out of the session
:api: public
125 126 127 128 129 130 |
# File 'lib/warden/strategies/base.rb', line 125 def success!(user, = nil) halt! @user = user @message = @result = :success end |
#successful? ⇒ Boolean
Returns true only if the result is a success and a user was assigned.
113 114 115 |
# File 'lib/warden/strategies/base.rb', line 113 def successful? @result == :success && !user.nil? end |
#valid? ⇒ Boolean
Acts as a guarding method for the strategy. If #valid? responds false, the strategy will not be executed Overwrite with your own logic :api: overwritable
74 |
# File 'lib/warden/strategies/base.rb', line 74 def valid?; true; end |