Class: Auth::Configuration

Inherits:
Object
  • Object
show all
Extended by:
BehaviorLookup
Includes:
BehaviorLookup, Keys
Defined in:
lib/auth/configuration.rb,
lib/auth/configuration/keys.rb

Defined Under Namespace

Modules: Keys

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BehaviorLookup

lookup_behavior

Methods included from Keys

#configuration_keys, included, #to_hash

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/auth/configuration.rb', line 283

def initialize
  @password_format = /(?=[-_a-zA-Z0-9]*?[A-Z])(?=[-_a-zA-Z0-9]*?[a-z])(?=[-_a-zA-Z0-9]*?[0-9])[-_a-zA-Z0-9]{7,}/
  @password_format_message = "must contain at least 1 uppercase, 1 lowercase and 1 number"
  @minimum_password_length = 7
  @path = File.expand_path(File.join(File.dirname(__FILE__), '..'))
  @authenticated_models = Auth::TargetList.new
  @behaviors = [ :core ]
  @password_update_frequency = 30.days
  @encryptor = Auth::Encryptors::Sha512
  @password_uniqueness_message = "must not be the same as any of your recent passwords"
  @password_history_length = 4
  @default_accounts_controller_name = "sparkly_accounts"
  @default_sessions_controller_name = "sparkly_sessions"
  @login_required_message = "You must be signed in to view this page."
  @logout_required_message = "You must be signed out to view this page."
  @invalid_credentials_message = "Credentials were not valid."
  @login_successful_message = "Signed in successfully."
  @default_destination = "/"
  @base_controller_name = 'application'
  @session_duration = 30.minutes
  @logout_message = "You have been signed out."
  @session_timeout_message = "You have been signed out due to inactivity. Please sign in again."
  @default_login_path = :new_user_session_path
  @account_deleted_message = "Your account has been deleted."
  @account_created_message = "Your account has been created."
  @account_updated_message = "Your changes have been saved."
  @account_locked_message = "Account is locked due to too many invalid attempts."
  @account_lock_duration = 30.minutes
  @max_login_failures = 5
  @generate_routes = true
  @login_after_signup = false
  
  self.class.behavior_configs.each do |accessor_name, config_klass|
    instance_variable_set("@#{accessor_name}", config_klass.constantize.new(self))
    singleton = (class << self; self; end)
    singleton.send(:define_method, accessor_name) { instance_variable_get("@#{accessor_name}") }
  end
end

Instance Attribute Details

#account_created_messageObject

The message to display when the user creates an account.

Default:

"Your account has been created."


70
71
72
# File 'lib/auth/configuration.rb', line 70

def 
  @account_created_message
end

#account_deleted_messageObject

The message to display when the user deletes his or her account.

Default:

"Your account has been deleted."


76
77
78
# File 'lib/auth/configuration.rb', line 76

def 
  @account_deleted_message
end

#account_lock_durationObject

The length of time an account is locked for, if it is locked.

Default:

30.minutes


88
89
90
# File 'lib/auth/configuration.rb', line 88

def 
  @account_lock_duration
end

#account_locked_messageObject

The message to display if an account has been locked.

Default:

"Account is locked due to too many invalid attempts."


94
95
96
# File 'lib/auth/configuration.rb', line 94

def 
  @account_locked_message
end

#account_updated_messageObject

The message to display when user profile has been updated or the password has been changed.

Default:

"Your changes have been saved."


82
83
84
# File 'lib/auth/configuration.rb', line 82

def 
  @account_updated_message
end

#authenticated_modelsObject

The array of Auth::Model instances which represent the models which will be authenticated. See also #authenticate



8
9
10
# File 'lib/auth/configuration.rb', line 8

def authenticated_models
  @authenticated_models
end

#base_controller_nameObject

The NAME of the controller to use as a base controller. All Sparkly controllers will subclass this, and methods such as current_user will be added to it. Defaults to ‘application’.

Default:

'application'


101
102
103
# File 'lib/auth/configuration.rb', line 101

def base_controller_name
  @base_controller_name
end

#behaviorsObject

The array of behaviors which will be applied by default to every authenticated model. If a behavior set is specified for a given model, it will be used instead of (not in addition to) this array.

Default:

[ :core ]


109
110
111
# File 'lib/auth/configuration.rb', line 109

def behaviors
  @behaviors
end

#default_accounts_controller_nameObject

The name of the controller to route to for creating users, editing them, etc.

"sparkly_accounts"


114
115
116
# File 'lib/auth/configuration.rb', line 114

def default_accounts_controller_name
  @default_accounts_controller_name
end

#default_destinationObject

If an issue would prevent the user from viewing the current page, Auth will redirect the user to the value stored in session. If this value is not set, then Auth will default to this path.

Default:

"/"


122
123
124
# File 'lib/auth/configuration.rb', line 122

def default_destination
  @default_destination
end

#default_login_pathObject

The method to call in order to determine which resource to use when implicitly logging in.

If set to nil, the #default_destination will be used instead.

Default:

:new_user_session_path


130
131
132
# File 'lib/auth/configuration.rb', line 130

def 
  @default_login_path
end

#default_sessions_controller_nameObject

The name of the controller to route to for logging in, logging out, etc.

Default:

"sparkly_sessions"


136
137
138
# File 'lib/auth/configuration.rb', line 136

def default_sessions_controller_name
  @default_sessions_controller_name
end

#encryptorObject

The class to use for encryption of passwords. This can be any class, as long as it responds to #encrypt and #matches?

Default:

Auth::Encryptors::Sha512


143
144
145
# File 'lib/auth/configuration.rb', line 143

def encryptor
  @encryptor
end

#invalid_credentials_messageObject

Message to display if username and/or password were incorrect.

Default:

"Credentials were not valid."


149
150
151
# File 'lib/auth/configuration.rb', line 149

def invalid_credentials_message
  @invalid_credentials_message
end

#login_after_signupObject

If true, the user will be automatically logged in after registering a new account. Note that this can be modified by some behaviors.

Default:

true


156
157
158
# File 'lib/auth/configuration.rb', line 156

def 
  @login_after_signup
end

#login_required_messageObject

The message to display when the user is not allowed to view a page because s/he must log in.

Default:

"You must be signed in to view this page."


162
163
164
# File 'lib/auth/configuration.rb', line 162

def 
  @login_required_message
end

#login_successful_messageObject

Message to display if login was successful.

Default:

"Signed in successfully."


168
169
170
# File 'lib/auth/configuration.rb', line 168

def 
  @login_successful_message
end

#logout_messageObject

Message to display when user logs out.

Default:

"You have been signed out."


174
175
176
# File 'lib/auth/configuration.rb', line 174

def logout_message
  @logout_message
end

#logout_required_messageObject

The message to display when the user is not allowed to view a page because s/he must log out.

"You must be signed out to view this page."


179
180
181
# File 'lib/auth/configuration.rb', line 179

def logout_required_message
  @logout_required_message
end

#max_login_failuresObject

The maximum login attempts permitted before an account is locked. Set to nil to disable locking.

Default:

5


185
186
187
# File 'lib/auth/configuration.rb', line 185

def 
  @max_login_failures
end

#minimum_password_lengthObject

Minimum length for passwords.

Default:

7


191
192
193
# File 'lib/auth/configuration.rb', line 191

def minimum_password_length
  @minimum_password_length
end

#password_formatObject

Regular expression which passwords must match. The default forces at least 1 uppercase, lowercase and numeric character.

Default:

/(^(?=.*\d)(?=.*[a-zA-Z]).{7,}$)/


198
199
200
# File 'lib/auth/configuration.rb', line 198

def password_format
  @password_format
end

#password_format_messageObject

When the password to be created does not conform to the above format, this error message will be shown.

Default:

"must contain at least 1 uppercase, 1 lowercase and 1 number"


205
206
207
# File 'lib/auth/configuration.rb', line 205

def password_format_message
  @password_format_message
end

#password_history_lengthObject

The number of passwords to keep in the password change history for each user. Any given user may not use the same password twice for at least this duration. For instance, if set to 4, then a user must change his password 4 times before s/he can reuse one of his/her previous passwords.

Default:

4


214
215
216
# File 'lib/auth/configuration.rb', line 214

def password_history_length
  @password_history_length
end

#password_uniqueness_messageObject

The message to display when password change matches one of the previous passwords

Default:

"must not be the same as any of your recent passwords"


220
221
222
# File 'lib/auth/configuration.rb', line 220

def password_uniqueness_message
  @password_uniqueness_message
end

#password_update_frequencyObject

How frequently should passwords be forced to change? Nil for never.

Default:

30.days


226
227
228
# File 'lib/auth/configuration.rb', line 226

def password_update_frequency
  @password_update_frequency
end

#pathObject (readonly)

The path to the Sparkly Auth libraries.



229
230
231
# File 'lib/auth/configuration.rb', line 229

def path
  @path
end

#session_durationObject

The maximum session duration. Users will be logged out automatically after this period expires.

Default:

30.minutes


235
236
237
# File 'lib/auth/configuration.rb', line 235

def session_duration
  @session_duration
end

#session_timeout_messageObject

Message to display when the user’s session times out due to inactivity.

Default:

"You have been signed out due to inactivity. Please sign in again."


241
242
243
# File 'lib/auth/configuration.rb', line 241

def session_timeout_message
  @session_timeout_message
end

Class Method Details

.add_configuration_key_with_delegation(*keys) ⇒ Object



25
26
27
28
29
30
# File 'lib/auth/configuration.rb', line 25

def add_configuration_key_with_delegation(*keys)
  keys = keys.flatten
  eig = class << Auth; self; end
  eig.instance_eval { delegate *[keys, {:to => :configuration}].flatten }
  add_configuration_key_without_delegation(*keys)
end

.add_option_delegator_for(key) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/auth/configuration.rb', line 15

def add_option_delegator_for(key)
  define_method :"#{key}_with_option_delegation=" do |value|
    res = send("#{key}_without_option_delegation=", value)
    authenticated_models.each { |model| model.set_default_option(key, value) }
    res
  end

  alias_method_chain :"#{key}=", :option_delegation
end

.attr_accessor_with_delegator(*keys) ⇒ Object



32
33
34
35
36
# File 'lib/auth/configuration.rb', line 32

def attr_accessor_with_delegator(*keys)
  result = attr_accessor_without_delegator(*keys)
  keys.flatten.each { |key| add_option_delegator_for(key) }
  result
end

.attr_writer_with_delegator(*keys) ⇒ Object



38
39
40
41
42
# File 'lib/auth/configuration.rb', line 38

def attr_writer_with_delegator(*keys)
  result = attr_writer_without_delegator(*keys)
  keys.flatten.each { |key| add_option_delegator_for(key) }
  result
end

.behavior_configsObject



48
49
50
51
# File 'lib/auth/configuration.rb', line 48

def behavior_configs
  @behavior_configs ||= []
  @behavior_configs
end

.register_behavior(name, behavior_class = lookup_behavior(name)) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/auth/configuration.rb', line 53

def register_behavior(name, behavior_class = lookup_behavior(name))
  # If the behavior has a configuration, add it to self.
  accessor_name = name
  name = "#{behavior_class.name}::Configuration"
  # we do this so that we can raise NameError now, not later.
  behavior_configs << [ accessor_name, name.constantize.name ]
  # eg Auth.remember_me.something = 5
  Auth.class.delegate accessor_name, :to => :configuration
rescue NameError
  # Presumably, the behavior does not have a configuration.
end

Instance Method Details

#apply!Object



322
323
324
325
326
327
328
329
330
# File 'lib/auth/configuration.rb', line 322

def apply!
  # all configurations are now applied through Auth::Model. If no models are being authenticated,
  # then no authentication should be possible -- so what is there to apply?
  
  # Apply options to authenticated models
  authenticated_models.each do |model|
    model.apply_options!
  end
end

#authenticate(*model_names) ⇒ Object

Accepts a list of model names (or the models themselves) and an optional set of options which govern how the models will be authenticated.

Examples:

Auth.configure do |config|
  config.authenticate :user
  config.authenticate :admin, :key => :login
  config.authenticate :user, :admin, :with => /a password validating regexp/
end

Note that if an item is specified more than once, the options will be merged together for the entry. For instance, in the above example, the :user model will be authenticated with :password, while the :admin model will be authenticated with :password on key :login.



354
355
356
357
358
359
360
361
362
363
# File 'lib/auth/configuration.rb', line 354

def authenticate(*model_names)
  options = model_names.extract_options!
  model_names.flatten.each do |name|
    if model = authenticated_models.find(name)
      model.merge_options! options
    else
      authenticated_models << Auth::Model.new(name, options, to_hash)
    end
  end
end

#base_controllerObject

Finds the controller with the same name as #base_controller_name and returns it.



248
249
250
251
252
253
254
255
256
257
258
# File 'lib/auth/configuration.rb', line 248

def base_controller
  "#{base_controller_name.to_s.camelize}Controller".constantize
rescue NameError => err
  begin
    base_controller_name.to_s.camelize.constantize
  rescue NameError
    # reraise the original error because '_controller' should have been omitted by convention. Also,
    # the backtrace will be more useful.
    raise err
  end
end

#behavior=(*args, &block) ⇒ Object

:nodoc:



243
244
245
# File 'lib/auth/configuration.rb', line 243

def behavior=(*args, &block) #:nodoc:
  send(:behaviors=, *args, &block)
end

#behavior_classesObject

Returns the classes which represent each behavior listed in #behaviors



261
262
263
# File 'lib/auth/configuration.rb', line 261

def behavior_classes
  behaviors.collect { |behavior| lookup_behavior(behavior) }
end

#behaviors_with_conversion_to_array=(*args) ⇒ Object

this was documented as an accessor, but is expected to always be an Array.



278
279
280
# File 'lib/auth/configuration.rb', line 278

def behaviors_with_conversion_to_array=(*args) #:nodoc:
  self.behaviors_without_conversion_to_array = args.flatten
end

#disable_route_generation!Object

Causes Sparkly Auth to not generate routes by default. You’ll have to map them yourself if you disable route generation.



267
268
269
# File 'lib/auth/configuration.rb', line 267

def disable_route_generation!
  @generate_routes = false
end

#for_model(name_or_class_or_instance) ⇒ Object

Returns the configuration for the given authenticated model.



366
367
368
369
370
# File 'lib/auth/configuration.rb', line 366

def for_model(name_or_class_or_instance)
  name_or_class = name_or_class_or_instance
  name_or_class = name_or_class.class if name_or_class.kind_of?(ActiveRecord::Base)
  authenticated_models.find(name_or_class)
end

#generate_routes?Boolean

Returns true if Sparkly Auth is expected to generate routes for this application. This is true by default, and can be disabled with #disable_route_generation!

Returns:

  • (Boolean)


273
274
275
# File 'lib/auth/configuration.rb', line 273

def generate_routes?
  @generate_routes
end

#to_hash_with_subconfigsObject

:nodoc:



332
333
334
335
336
337
# File 'lib/auth/configuration.rb', line 332

def to_hash_with_subconfigs #:nodoc:
  self.class.behavior_configs.inject(to_hash_without_subconfigs) do |hash, (accessor_name, constant_name)|
    hash[accessor_name.to_sym] = send(accessor_name)
    hash
  end
end