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, #reset_session!, #session

Constructor Details

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

:api: private



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

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

Instance Attribute Details

#_statusObject (readonly)

Setup for redirection :api: private



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

def _status
  @_status
end

#custom_responseObject

:api: private



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

def custom_response
  @custom_response
end

#envObject (readonly)

Accessor for the rack env :api: public



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

def env
  @env
end

#messageObject

:api: public



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

def message
  @message
end

#resultObject

:api: private



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

def result
  @result
end

#scopeObject (readonly)

Accessor for the rack env :api: public



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

def scope
  @scope
end

#userObject

:api: public



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

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



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

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



118
119
120
121
122
# File 'lib/warden/authentication/strategy_base.rb', line 118

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

#errorsObject

Access to the errors object. :api: public



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

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



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

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



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

def halt!
  @halted = true
end

#halted?Boolean

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

Returns:

  • (Boolean)


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

def halted?
  !!@halted
end

#headers(header = {}) ⇒ Object

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



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

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



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

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



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

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].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



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

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)


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

def valid?; true; end