class Warden::Strategies::Base
Warden::Strategies.add(:foo, MyStrategy)
Class Declared Strategy:
end
end
# authentication logic
def authenticate!
Warden::Strategies.add(:foo) do
Block Declared Strategy:
Examples:
<block> The block acts as a convenient way to declare your strategy. Inside is the class definition of a strategy.
implement an @authenticate!@ method
<strategy: Class|nil> The optional strategy argument if set must be a class that inherits from Warden::Strategies::Base and must
<label: Symbol> The label is the name given to a strategy. Use the label to refer to the strategy when authenticating
The parameters for Warden::Strategies.add method are:
The valid method should return true or false depending on if the strategy is a valid one for the request.
You may provide a @valid?@ method.
You must declare an @authenticate!@ method.
The Warden::Strategies.add method is a simple way to provide custom strategies.
from Warden::Strategies::Base.
A strategy is a place where you can put logic related to authentication. Any strategy inherits
def _run! # :nodoc:
:api: private
The method that is called from above. This method calls the underlying authenticate! method
def _run! # :nodoc: @performed = true authenticate! self end
def clear!
Marks this strategy as not performed.
def clear! @performed = false end
def custom!(response)
Return a custom rack array. You must throw an :warden symbol to activate this
def custom!(response) halt! @custom_response = response @result = :custom end
def errors
Access to the errors object.
def errors @env['warden'].errors end
def fail(message = "Failed to Login")
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.
def fail(message = "Failed to Login") @message = message @result = :failure end
def fail!(message = "Failed to Login")
Halts the strategies so that this is the last strategy checked
You must throw an :warden symbol somewhere in the application to enforce this
This causes the strategy to fail. It does not throw an :warden symbol to drop the request out to the failure application
def fail!(message = "Failed to Login") halt! @message = message @result = :failure end
def halt!
Cause the processing of the strategies to stop and cascade no further
def halt! @halted = true end
def halted?
Checks to see if a strategy was halted
def halted? !!@halted end
def headers(header = {})
Provides access to the headers hash for setting custom headers
def headers(header = {}) @headers ||= {} @headers.merge! header @headers end
def initialize(env, scope=nil) # :nodoc:
:api: private
def initialize(env, scope=nil) # :nodoc: @env, @scope = env, scope @status, @headers = nil, {} @halted, @performed = false, false @result = nil end
def pass; end
A simple method to return from authenticate! if you want to ignore this strategy
def pass; end
def performed? #:nodoc:
:api: private
Returns if this strategy was already performed.
def performed? #:nodoc: @performed end
def redirect!(url, params = {}, opts = {})
available options: permanent => (true || false)
opts
params
url
Parameters:
Causes the authentication to redirect. An :warden symbol must be thrown to actually execute this redirect
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
def store?
Checks to see if a strategy should result in a permanent login
def store? true end
def success!(user, message = nil)
user - The user object to login. This object can be anything you have setup to serialize in and out of the session
Parameters:
It is the "login" method
This will halt the strategy, and set the user in the appropriate scope.
Whenever you want to provide a user object as "authenticated" use the +success!+ method.
def success!(user, message = nil) halt! @user = user @message = message @result = :success end
def successful?
def successful? @result == :success && !user.nil? end
def valid?; true; end
Overwrite with your own logic
If #valid? responds false, the strategy will not be executed
Acts as a guarding method for the strategy.
def valid?; true; end