Commit b1fd97bfd2e8cebd02c4fc7aa8b60d3d363bb518

Authored by Antonio Terceiro
1 parent e52a9b49

ActionItem305: supporting old cooperation.net passwords

Showing 2 changed files with 11 additions and 2 deletions   Show diff stats
app/models/user.rb
@@ -134,6 +134,10 @@ class User < ActiveRecord::Base @@ -134,6 +134,10 @@ class User < ActiveRecord::Base
134 password 134 password
135 end 135 end
136 136
  137 + add_encryption_method :crypt do |password, salt|
  138 + password.crypt(salt)
  139 + end
  140 +
137 def authenticated?(password) 141 def authenticated?(password)
138 result = (crypted_password == encrypt(password)) 142 result = (crypted_password == encrypt(password))
139 if (encryption_method != User.system_encryption_method) && result 143 if (encryption_method != User.system_encryption_method) && result
@@ -209,7 +213,7 @@ class User < ActiveRecord::Base @@ -209,7 +213,7 @@ class User < ActiveRecord::Base
209 # before filter 213 # before filter
210 def encrypt_password 214 def encrypt_password
211 return if password.blank? 215 return if password.blank?
212 - self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record? 216 + self.salt ||= Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
213 self.password_type ||= User.system_encryption_method.to_s 217 self.password_type ||= User.system_encryption_method.to_s
214 self.crypted_password = encrypt(password) 218 self.crypted_password = encrypt(password)
215 end 219 end
test/unit/user_test.rb
@@ -165,7 +165,7 @@ class UserTest < Test::Unit::TestCase @@ -165,7 +165,7 @@ class UserTest < Test::Unit::TestCase
165 def test_should_encrypt_password_with_salted_sha1 165 def test_should_encrypt_password_with_salted_sha1
166 user = User.new(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test') 166 user = User.new(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test')
167 user.build_person(person_data) 167 user.build_person(person_data)
168 - user.expects(:salt).returns('testsalt') 168 + user.stubs(:salt).returns('testsalt')
169 user.save! 169 user.save!
170 170
171 # SHA1+salt crypted form for password 'test', and salt 'testsalt', 171 # SHA1+salt crypted form for password 'test', and salt 'testsalt',
@@ -182,6 +182,11 @@ class UserTest < Test::Unit::TestCase @@ -182,6 +182,11 @@ class UserTest < Test::Unit::TestCase
182 assert_equal '098f6bcd4621d373cade4e832627b4f6', user.crypted_password 182 assert_equal '098f6bcd4621d373cade4e832627b4f6', user.crypted_password
183 end 183 end
184 184
  185 + def test_should_support_crypt_passwords
  186 + user = new_user(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test', :password_type => 'crypt', :salt => 'test')
  187 + assert_equal 'teH0wLIpW0gyQ', user.crypted_password
  188 + end
  189 +
185 def test_should_support_clear_passwords 190 def test_should_support_clear_passwords
186 assert_equal 'test', new_user(:password => 'test', :password_confirmation => 'test', :password_type => 'clear').crypted_password 191 assert_equal 'test', new_user(:password => 'test', :password_confirmation => 'test', :password_type => 'clear').crypted_password
187 end 192 end