@@ -10,7 +10,7 @@ def self.included(klass)
10
10
add_acts_as_authentic_module ( Methods )
11
11
end
12
12
end
13
-
13
+
14
14
# All configuration for the password aspect of acts_as_authentic.
15
15
module Config
16
16
# The name of the crypted_password field in the database.
@@ -21,7 +21,7 @@ def crypted_password_field(value = nil)
21
21
rw_config ( :crypted_password_field , value , first_column_to_exist ( nil , :crypted_password , :encrypted_password , :password_hash , :pw_hash ) )
22
22
end
23
23
alias_method :crypted_password_field= , :crypted_password_field
24
-
24
+
25
25
# The name of the password_salt field in the database.
26
26
#
27
27
# * <tt>Default:</tt> :password_salt, :pw_salt, :salt, nil if none exist
@@ -30,7 +30,7 @@ def password_salt_field(value = nil)
30
30
rw_config ( :password_salt_field , value , first_column_to_exist ( nil , :password_salt , :pw_salt , :salt ) )
31
31
end
32
32
alias_method :password_salt_field= , :password_salt_field
33
-
33
+
34
34
# Whether or not to require a password confirmation. If you don't want your users to confirm their password
35
35
# just set this to false.
36
36
#
@@ -40,7 +40,7 @@ def require_password_confirmation(value = nil)
40
40
rw_config ( :require_password_confirmation , value , true )
41
41
end
42
42
alias_method :require_password_confirmation= , :require_password_confirmation
43
-
43
+
44
44
# By default passwords are required when a record is new or the crypted_password is blank, but if both of these things
45
45
# are met a password is not required. In this case, blank passwords are ignored.
46
46
#
@@ -56,7 +56,7 @@ def ignore_blank_passwords(value = nil)
56
56
rw_config ( :ignore_blank_passwords , value , true )
57
57
end
58
58
alias_method :ignore_blank_passwords= , :ignore_blank_passwords
59
-
59
+
60
60
# When calling valid_password?("some pass") do you want to check that password against what's in that object or whats in
61
61
# the database. Take this example:
62
62
#
@@ -73,7 +73,7 @@ def check_passwords_against_database(value = nil)
73
73
rw_config ( :check_passwords_against_database , value , true )
74
74
end
75
75
alias_method :check_passwords_against_database= , :check_passwords_against_database
76
-
76
+
77
77
# Whether or not to validate the password field.
78
78
#
79
79
# * <tt>Default:</tt> true
@@ -82,7 +82,7 @@ def validate_password_field(value = nil)
82
82
rw_config ( :validate_password_field , value , true )
83
83
end
84
84
alias_method :validate_password_field= , :validate_password_field
85
-
85
+
86
86
# A hash of options for the validates_length_of call for the password field. Allows you to change this however you want.
87
87
#
88
88
# <b>Keep in mind this is ruby. I wanted to keep this as flexible as possible, so you can completely replace the hash or
@@ -95,7 +95,7 @@ def validates_length_of_password_field_options(value = nil)
95
95
rw_config ( :validates_length_of_password_field_options , value , { :minimum => 4 , :if => :require_password? } )
96
96
end
97
97
alias_method :validates_length_of_password_field_options= , :validates_length_of_password_field_options
98
-
98
+
99
99
# A convenience function to merge options into the validates_length_of_login_field_options. So intead of:
100
100
#
101
101
# self.validates_length_of_password_field_options = validates_length_of_password_field_options.merge(:my_option => my_value)
@@ -106,7 +106,7 @@ def validates_length_of_password_field_options(value = nil)
106
106
def merge_validates_length_of_password_field_options ( options = { } )
107
107
self . validates_length_of_password_field_options = validates_length_of_password_field_options . merge ( options )
108
108
end
109
-
109
+
110
110
# A hash of options for the validates_confirmation_of call for the password field. Allows you to change this however you want.
111
111
#
112
112
# <b>Keep in mind this is ruby. I wanted to keep this as flexible as possible, so you can completely replace the hash or
@@ -119,12 +119,12 @@ def validates_confirmation_of_password_field_options(value = nil)
119
119
rw_config ( :validates_confirmation_of_password_field_options , value , { :if => :require_password? } )
120
120
end
121
121
alias_method :validates_confirmation_of_password_field_options= , :validates_confirmation_of_password_field_options
122
-
122
+
123
123
# See merge_validates_length_of_password_field_options. The same thing, except for validates_confirmation_of_password_field_options
124
124
def merge_validates_confirmation_of_password_field_options ( options = { } )
125
125
self . validates_confirmation_of_password_field_options = validates_confirmation_of_password_field_options . merge ( options )
126
126
end
127
-
127
+
128
128
# A hash of options for the validates_length_of call for the password_confirmation field. Allows you to change this however you want.
129
129
#
130
130
# <b>Keep in mind this is ruby. I wanted to keep this as flexible as possible, so you can completely replace the hash or
@@ -137,22 +137,23 @@ def validates_length_of_password_confirmation_field_options(value = nil)
137
137
rw_config ( :validates_length_of_password_confirmation_field_options , value , validates_length_of_password_field_options )
138
138
end
139
139
alias_method :validates_length_of_password_confirmation_field_options= , :validates_length_of_password_confirmation_field_options
140
-
140
+
141
141
# See merge_validates_length_of_password_field_options. The same thing, except for validates_length_of_password_confirmation_field_options
142
142
def merge_validates_length_of_password_confirmation_field_options ( options = { } )
143
143
self . validates_length_of_password_confirmation_field_options = validates_length_of_password_confirmation_field_options . merge ( options )
144
144
end
145
-
145
+
146
146
# The class you want to use to encrypt and verify your encrypted passwords. See the Authlogic::CryptoProviders module for more info
147
- # on the available methods and how to create your own.
147
+ # on the available methods and how to create your own. It is strongly recommended that you use SCrpyt or BCrypt. The default is Sah512 to
148
+ # support backwards compatibility.
148
149
#
149
150
# * <tt>Default:</tt> CryptoProviders::Sha512
150
151
# * <tt>Accepts:</tt> Class
151
152
def crypto_provider ( value = nil )
152
- rw_config ( :crypto_provider , value , CryptoProviders ::SCrypt )
153
+ rw_config ( :crypto_provider , value , CryptoProviders ::Sha512 )
153
154
end
154
155
alias_method :crypto_provider= , :crypto_provider
155
-
156
+
156
157
# Let's say you originally encrypted your passwords with Sha1. Sha1 is starting to join the party with MD5 and you want to switch
157
158
# to something stronger. No problem, just specify your new and improved algorithm with the crypt_provider option and then let
158
159
# Authlogic know you are transitioning from Sha1 using this option. Authlogic will take care of everything, including transitioning
@@ -169,18 +170,18 @@ def transition_from_crypto_providers(value = nil)
169
170
end
170
171
alias_method :transition_from_crypto_providers= , :transition_from_crypto_providers
171
172
end
172
-
173
+
173
174
# Callbacks / hooks to allow other modules to modify the behavior of this module.
174
175
module Callbacks
175
176
METHODS = [
176
177
"before_password_set" , "after_password_set" ,
177
178
"before_password_verification" , "after_password_verification"
178
179
]
179
-
180
+
180
181
def self . included ( klass )
181
182
return if klass . crypted_password_field . nil?
182
183
klass . define_callbacks *METHODS
183
-
184
+
184
185
# If Rails 3, support the new callback syntax
185
186
if klass . send ( klass . respond_to? ( :singleton_class ) ? :singleton_class : :metaclass ) . method_defined? ( :set_callback )
186
187
METHODS . each do |method |
@@ -192,7 +193,7 @@ def self.#{method}(*methods, &block)
192
193
end
193
194
end
194
195
end
195
-
196
+
196
197
private
197
198
METHODS . each do |method |
198
199
class_eval <<-"end_eval" , __FILE__ , __LINE__
@@ -202,34 +203,34 @@ def #{method}
202
203
end_eval
203
204
end
204
205
end
205
-
206
+
206
207
# The methods related to the password field.
207
208
module Methods
208
209
def self . included ( klass )
209
210
return if klass . crypted_password_field . nil?
210
-
211
+
211
212
klass . class_eval do
212
213
include InstanceMethods
213
-
214
+
214
215
if validate_password_field
215
216
validates_length_of :password , validates_length_of_password_field_options
216
-
217
+
217
218
if require_password_confirmation
218
219
validates_confirmation_of :password , validates_confirmation_of_password_field_options
219
220
validates_length_of :password_confirmation , validates_length_of_password_confirmation_field_options
220
221
end
221
222
end
222
-
223
+
223
224
after_save :reset_password_changed
224
225
end
225
226
end
226
-
227
+
227
228
module InstanceMethods
228
229
# The password
229
230
def password
230
231
@password
231
232
end
232
-
233
+
233
234
# This is a virtual method. Once a password is passed to it, it will create new password salt as well as encrypt
234
235
# the password.
235
236
def password = ( pass )
@@ -241,67 +242,67 @@ def password=(pass)
241
242
@password_changed = true
242
243
after_password_set
243
244
end
244
-
245
+
245
246
# Accepts a raw password to determine if it is the correct password or not. Notice the second argument. That defaults to the value of
246
247
# check_passwords_against_database. See that method for more information, but basically it just tells Authlogic to check the password
247
248
# against the value in the database or the value in the object.
248
249
def valid_password? ( attempted_password , check_against_database = check_passwords_against_database? )
249
250
crypted = check_against_database && send ( "#{ crypted_password_field } _changed?" ) ? send ( "#{ crypted_password_field } _was" ) : send ( crypted_password_field )
250
251
return false if attempted_password . blank? || crypted . blank?
251
252
before_password_verification
252
-
253
+
253
254
crypto_providers . each_with_index do |encryptor , index |
254
255
# The arguments_type of for the transitioning from restful_authentication
255
256
arguments_type = ( act_like_restful_authentication? && index == 0 ) ||
256
257
( transition_from_restful_authentication? && index > 0 && encryptor == Authlogic ::CryptoProviders ::Sha1 ) ?
257
258
:restful_authentication : nil
258
-
259
+
259
260
if encryptor . matches? ( crypted , *encrypt_arguments ( attempted_password , check_against_database , arguments_type ) )
260
261
transition_password ( attempted_password ) if transition_password? ( index , encryptor , crypted , check_against_database )
261
262
after_password_verification
262
263
return true
263
264
end
264
265
end
265
-
266
+
266
267
false
267
268
end
268
-
269
+
269
270
# Resets the password to a random friendly token.
270
271
def reset_password
271
272
friendly_token = Authlogic ::Random . friendly_token
272
273
self . password = friendly_token
273
274
self . password_confirmation = friendly_token
274
275
end
275
276
alias_method :randomize_password , :reset_password
276
-
277
+
277
278
# Resets the password to a random friendly token and then saves the record.
278
279
def reset_password!
279
280
reset_password
280
281
save_without_session_maintenance ( :validate => false )
281
282
end
282
283
alias_method :randomize_password! , :reset_password!
283
-
284
+
284
285
private
285
286
def check_passwords_against_database?
286
287
self . class . check_passwords_against_database == true
287
288
end
288
-
289
+
289
290
def crypto_providers
290
291
[ crypto_provider ] + transition_from_crypto_providers
291
292
end
292
-
293
+
293
294
def encrypt_arguments ( raw_password , check_against_database , arguments_type = nil )
294
295
salt = nil
295
296
salt = ( check_against_database && send ( "#{ password_salt_field } _changed?" ) ? send ( "#{ password_salt_field } _was" ) : send ( password_salt_field ) ) if password_salt_field
296
-
297
+
297
298
case arguments_type
298
299
when :restful_authentication
299
300
[ REST_AUTH_SITE_KEY , salt , raw_password , REST_AUTH_SITE_KEY ] . compact
300
301
else
301
302
[ raw_password , salt ] . compact
302
303
end
303
304
end
304
-
305
+
305
306
# Determines if we need to tranisiton the password.
306
307
# If the index > 0 then we are using an "transition from" crypto provider.
307
308
# If the encryptor has a cost and the cost it outdated.
@@ -311,40 +312,40 @@ def transition_password?(index, encryptor, crypted, check_against_database)
311
312
( index > 0 || ( encryptor . respond_to? ( :cost_matches? ) && !encryptor . cost_matches? ( send ( crypted_password_field ) ) ) ) &&
312
313
( !check_against_database || !send ( "#{ crypted_password_field } _changed?" ) )
313
314
end
314
-
315
+
315
316
def transition_password ( attempted_password )
316
317
self . password = attempted_password
317
318
save ( :validate => false )
318
319
end
319
-
320
+
320
321
def require_password?
321
322
new_record? || password_changed? || send ( crypted_password_field ) . blank?
322
323
end
323
-
324
+
324
325
def ignore_blank_passwords?
325
326
self . class . ignore_blank_passwords == true
326
327
end
327
-
328
+
328
329
def password_changed?
329
330
@password_changed == true
330
331
end
331
-
332
+
332
333
def reset_password_changed
333
334
@password_changed = nil
334
335
end
335
-
336
+
336
337
def crypted_password_field
337
338
self . class . crypted_password_field
338
339
end
339
-
340
+
340
341
def password_salt_field
341
342
self . class . password_salt_field
342
343
end
343
-
344
+
344
345
def crypto_provider
345
346
self . class . crypto_provider
346
347
end
347
-
348
+
348
349
def transition_from_crypto_providers
349
350
self . class . transition_from_crypto_providers
350
351
end
0 commit comments