class Warden::Strategies::Base

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)

Attributes

custom_response[RW]

:api: private

env[R]

:api: public

message[RW]

:api: public

result[RW]

:api: private

scope[R]

:api: public

status[R]

:api: public

user[RW]

:api: public

Public Instance Methods

clear!() click to toggle source

Marks this strategy as not performed. :api: private

# File lib/warden/strategies/base.rb, line 67
def clear!
  @performed = false
end
custom!(response) click to toggle source

Return a custom rack array. You must throw an :warden symbol to activate this :api: public

# File lib/warden/strategies/base.rb, line 174
def custom!(response)
  halt!
  @custom_response = response
  @result = :custom
end
errors() click to toggle source

Access to the errors object. :api: public

# File lib/warden/strategies/base.rb, line 87
def errors
  @env['warden'].errors
end
fail(message = "Failed to Login") click to toggle source

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

# File lib/warden/strategies/base.rb, line 145
def fail(message = "Failed to Login")
  @message = message
  @result = :failure
end
fail!(message = "Failed to Login") click to toggle source

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

# File lib/warden/strategies/base.rb, line 137
def fail!(message = "Failed to Login")
  halt!
  @message = message
  @result = :failure
end
halt!() click to toggle source

Cause the processing of the strategies to stop and cascade no further :api: public

# File lib/warden/strategies/base.rb, line 93
def halt!
  @halted = true
end
halted?() click to toggle source

Checks to see if a strategy was halted :api: public

# File lib/warden/strategies/base.rb, line 99
def halted?
  !!@halted
end
headers(header = {}) click to toggle source

Provides access to the headers hash for setting custom headers :api: public

# File lib/warden/strategies/base.rb, line 79
def headers(header = {})
  @headers ||= {}
  @headers.merge! header
  @headers
end
pass() click to toggle source

A simple method to return from authenticate! if you want to ignore this strategy :api: public

# File lib/warden/strategies/base.rb, line 111
def pass; end
redirect!(url, params = {}, opts = {}) click to toggle source

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

# File lib/warden/strategies/base.rb, line 159
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?() click to toggle source

Checks to see if a strategy should result in a permanent login :api: public

# File lib/warden/strategies/base.rb, line 105
def store?
  true
end
success!(user, message = nil) click to toggle source

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

# File lib/warden/strategies/base.rb, line 126
def success!(user, message = nil)
  halt!
  @user = user
  @message = message
  @result = :success
end
successful?() click to toggle source

Returns true only if the result is a success and a user was assigned.

# File lib/warden/strategies/base.rb, line 114
def successful?
  @result == :success && !user.nil?
end
valid?() click to toggle source

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

# File lib/warden/strategies/base.rb, line 75
def valid?; true; end