Class: Merb::Authentication::Strategy

Inherits:
Object
  • Object
show all
Defined in:
lib/merb-auth-core/strategy.rb

Overview

The Merb::Authentication::Strategy is where all the action happens in the merb-auth framework. Inherit from this class to setup your own strategy. The strategy will automatically be placed in the default_strategy_order array, and will be included in the strategy runs.

The strategy you implment should have a YourStrategy#run! method defined that returns

1. A user object if authenticated
2. nil if no authenticated user was found.

Example

class MyStrategy < Merb::Authentication::Strategy
  def run!
    u = User.get(params[:login])
    u if u.authentic?(params[:password])
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, params) ⇒ Strategy

End class << self



108
109
110
111
# File 'lib/merb-auth-core/strategy.rb', line 108

def initialize(request, params)
  @request = request
  @params  = params
end

Instance Attribute Details

#bodyObject

Allows you to provide a body of content to return when halting



173
174
175
# File 'lib/merb-auth-core/strategy.rb', line 173

def body
  @body || ""
end

#requestObject

Returns the value of attribute request.



65
66
67
# File 'lib/merb-auth-core/strategy.rb', line 65

def request
  @request
end

#statusObject

Provides a place to put the status of the response



153
154
155
# File 'lib/merb-auth-core/strategy.rb', line 153

def status
  @status
end

Class Method Details

.abstract!Object

Mark a strategy as abstract. This means that a strategy will not ever be run as part of the authentication. Instead this will be available to inherit from as a way to share code.

You could for example setup a strategy to check for a particular kind of login and then have a subclass for each class type of user in your system. i.e. Customer / Staff, Student / Staff etc



97
98
99
# File 'lib/merb-auth-core/strategy.rb', line 97

def abstract!
  @abstract = true
end

.abstract?Boolean

Asks is this strategy abstract. i.e. can it be run as part of the authentication

Returns:

  • (Boolean)


102
103
104
# File 'lib/merb-auth-core/strategy.rb', line 102

def abstract?
  !!@abstract
end

.after(strategy) ⇒ Object

Use this to declare the strategy should run after another strategy



83
84
85
86
87
88
# File 'lib/merb-auth-core/strategy.rb', line 83

def after(strategy)
  order = Merb::Authentication.default_strategy_order
  order.delete(self)
  index = order.index(strategy)
  index == order.size ? order << self : order.insert(index + 1, self)
end

.before(strategy) ⇒ Object

Use this to declare the strategy should run before another strategy



75
76
77
78
79
80
# File 'lib/merb-auth-core/strategy.rb', line 75

def before(strategy)
  order =  Merb::Authentication.default_strategy_order
  order.delete(self)
  index = order.index(strategy)
  order.insert(index,self)
end

.inherited(klass) ⇒ Object



69
70
71
72
# File 'lib/merb-auth-core/strategy.rb', line 69

def inherited(klass)
  Merb::Authentication.strategies << klass
  Merb::Authentication.default_strategy_order << klass
end

Instance Method Details

#cookiesObject

An alials to the request.cookies hash



121
122
123
# File 'lib/merb-auth-core/strategy.rb', line 121

def cookies
  request.cookies
end

#halt!Object

Mark this strategy as complete for this request. Will cause that no other strategies will be executed.



162
163
164
# File 'lib/merb-auth-core/strategy.rb', line 162

def halt!
  @halt = true
end

#halted?Boolean

Checks to see if this strategy has been halted

Returns:

  • (Boolean)


167
168
169
# File 'lib/merb-auth-core/strategy.rb', line 167

def halted?
  !!@halt
end

#headersObject

Provides a place to put headers



156
157
158
# File 'lib/merb-auth-core/strategy.rb', line 156

def headers
  @headers ||={}
end

#paramsObject

An alias to the request.params hash Only rely on this hash to find any router params you are looking for. If looking for paramteres use request.params



116
117
118
# File 'lib/merb-auth-core/strategy.rb', line 116

def params
  @params
end

#redirect!(url, opts = {}) ⇒ Object

Redirects causes the strategy to signal a redirect to the provided url.

Parameters

url<String>

The url to redirect to

options<Hash>

An options hash with the following keys:

+:permanent+ Set this to true to make the redirect permanent
+:status+ Set this to an integer for the status to return


138
139
140
141
142
143
144
145
# File 'lib/merb-auth-core/strategy.rb', line 138

def redirect!(url, opts = {})
  self.headers["Location"] = url
  self.status = opts[:permanent] ? 301 : 302
  self.status = opts[:status] if opts[:status]
  self.body   = opts[:message] || "<div>You are being redirected to <a href='#{url}'>#{url}</a></div>"
  halt!
  return true
end

#redirected?Boolean

Returns ture if the strategy redirected

Returns:

  • (Boolean)


148
149
150
# File 'lib/merb-auth-core/strategy.rb', line 148

def redirected?
  !!headers["Location"]
end

#run!Object

This is the method that is called as the test for authentication and is where you put your code.

You must overwrite this method in your strategy

Raises:



183
184
185
# File 'lib/merb-auth-core/strategy.rb', line 183

def run!
  raise NotImplemented
end

#sessionObject

An alias to the request.session hash



126
127
128
# File 'lib/merb-auth-core/strategy.rb', line 126

def session
  request.session
end

#user_classObject

Overwrite this method to scope a strategy to a particular user type you can use this with inheritance for example to try the same strategy on different user types

By default, Merb::Authentication.user_class is used. This method allows for particular strategies to deal with a different type of user class.

For example. If Merb::Authentication.user_class is Customer and you have a PasswordStrategy, you can subclass the PasswordStrategy and change this method to return Staff. Giving you a PasswordStrategy strategy for first Customer(s) and then Staff.



200
201
202
# File 'lib/merb-auth-core/strategy.rb', line 200

def user_class
  Merb::Authentication.user_class
end