Class: Warden::Strategies::Base

Inherits:
Object
  • Object
show all
Includes:
Mixins::Common
Defined in:
lib/warden/authentication/strategy_base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Common

#params, #request, #session

Constructor Details

#initialize(env, config = {}) ⇒ Base

:api: private



20
21
22
23
24
# File 'lib/warden/authentication/strategy_base.rb', line 20

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

Instance Attribute Details

#_statusObject (readonly)

Setup for redirection :api: private



12
13
14
# File 'lib/warden/authentication/strategy_base.rb', line 12

def _status
  @_status
end

#custom_responseObject

:api: private



8
9
10
# File 'lib/warden/authentication/strategy_base.rb', line 8

def custom_response
  @custom_response
end

#envObject (readonly)

Accessor for the rack env :api: public



16
17
18
# File 'lib/warden/authentication/strategy_base.rb', line 16

def env
  @env
end

#messageObject

:api: public



5
6
7
# File 'lib/warden/authentication/strategy_base.rb', line 5

def message
  @message
end

#resultObject

:api: private



8
9
10
# File 'lib/warden/authentication/strategy_base.rb', line 8

def result
  @result
end

#userObject

:api: public



5
6
7
# File 'lib/warden/authentication/strategy_base.rb', line 5

def user
  @user
end

Instance Method Details

#_run!Object

The method that is called from above. This method calls the underlying authetniate! method :api: private



28
29
30
31
# File 'lib/warden/authentication/strategy_base.rb', line 28

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



116
117
118
119
120
# File 'lib/warden/authentication/strategy_base.rb', line 116

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

#errorsObject

Access to the errors object. :api: public



49
50
51
# File 'lib/warden/authentication/strategy_base.rb', line 49

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



86
87
88
89
90
# File 'lib/warden/authentication/strategy_base.rb', line 86

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



55
56
57
# File 'lib/warden/authentication/strategy_base.rb', line 55

def halt!
  @halted = true
end

#halted?Boolean

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

Returns:

  • (Boolean)


61
62
63
# File 'lib/warden/authentication/strategy_base.rb', line 61

def halted?
  !!@halted
end

#headers(header = {}) ⇒ Object

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



41
42
43
44
45
# File 'lib/warden/authentication/strategy_base.rb', line 41

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



67
# File 'lib/warden/authentication/strategy_base.rb', line 67

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



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/warden/authentication/strategy_base.rb', line 101

def redirect!(url, params = {}, opts = {})
  halt!
  @_status = opts[:permanent] ? 301 : 302
  headers["Location"] = url
  headers["Location"] << "?" << Rack::Utils.build_query(params) unless params.empty?
    
  @message = opts[:message].nil? ? "You are being redirected to #{headers["Location"]}" : opts[:message]
  
  @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



77
78
79
80
81
# File 'lib/warden/authentication/strategy_base.rb', line 77

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)


37
# File 'lib/warden/authentication/strategy_base.rb', line 37

def valid?; true; end