Class: Warden::Strategies::Base

Inherits:
Object
  • Object
show all
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 is:

<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 stragtegy 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 convinient 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

Instance Method Summary collapse

Methods included from Mixins::Common

#params, #request, #reset_session!, #response, #session

Constructor Details

#initialize(env, scope = nil) ⇒ Base

:api: private



47
48
49
50
51
# File 'lib/warden/strategies/base.rb', line 47

def initialize(env, scope=nil) # :nodoc:
  @env, @scope = env, scope
  @_status, @headers = nil, {}
  @halted = false
end

Instance Attribute Details

#_statusObject (readonly)

Setup for redirection :api: private



39
40
41
# File 'lib/warden/strategies/base.rb', line 39

def _status
  @_status
end

#custom_responseObject

:api: private



35
36
37
# File 'lib/warden/strategies/base.rb', line 35

def custom_response
  @custom_response
end

#envObject (readonly)

Accessor for the rack env :api: public



43
44
45
# File 'lib/warden/strategies/base.rb', line 43

def env
  @env
end

#messageObject

:api: public



32
33
34
# File 'lib/warden/strategies/base.rb', line 32

def message
  @message
end

#resultObject

:api: private



35
36
37
# File 'lib/warden/strategies/base.rb', line 35

def result
  @result
end

#scopeObject (readonly)

Accessor for the rack env :api: public



43
44
45
# File 'lib/warden/strategies/base.rb', line 43

def scope
  @scope
end

#userObject

:api: public



32
33
34
# File 'lib/warden/strategies/base.rb', line 32

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



55
56
57
58
# File 'lib/warden/strategies/base.rb', line 55

def _run! # :nodoc:
  result = authenticate!
  self
end

#custom!(response) ⇒ Object

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



143
144
145
146
147
# File 'lib/warden/strategies/base.rb', line 143

def custom!(response)
  halt!
  @custom_response = response
  @result = :custom
end

#errorsObject

Access to the errors object. :api: public



76
77
78
# File 'lib/warden/strategies/base.rb', line 76

def errors
  @env['warden.errors']
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 :api: public



113
114
115
116
117
# File 'lib/warden/strategies/base.rb', line 113

def fail!(message = "Failed to Login")
  halt!
  @message = message
  @result = :failure
end

#halt!Object

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



82
83
84
# File 'lib/warden/strategies/base.rb', line 82

def halt!
  @halted = true
end

#halted?Boolean

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

Returns:

  • (Boolean)


88
89
90
# File 'lib/warden/strategies/base.rb', line 88

def halted?
  !!@halted
end

#headers(header = {}) ⇒ Object

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



68
69
70
71
72
# File 'lib/warden/strategies/base.rb', line 68

def headers(header = {})
  @headers ||= {}
  @headers.merge! header
  @headers
end

#passObject

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



94
# File 'lib/warden/strategies/base.rb', line 94

def pass; 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
pararms <Hash> - Any parameters to encode into the URL
opts <Hash> - Any options to recirect with.
  available options: permanent => (true || false)

:api: public



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/warden/strategies/base.rb', line 128

def redirect!(url, params = {}, opts = {})
  halt!
  @_status = opts[:permanent] ? 301 : 302
  headers["Location"] = url
  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

#success!(user) ⇒ 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 approprieate 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



104
105
106
107
108
# File 'lib/warden/strategies/base.rb', line 104

def success!(user)
  halt!
  @user   = user
  @result = :success
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

Returns:

  • (Boolean)


64
# File 'lib/warden/strategies/base.rb', line 64

def valid?; true; end