Changeset 857

Show
Ignore:
Timestamp:
05/06/08 10:01:14 (4 months ago)
Author:
seancribbs
Message:

Add per-user salting to password encryption.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/radiant/app/models/user.rb

    r813 r857  
    2626   
    2727  validates_numericality_of :id, :only_integer => true, :allow_nil => true, :message => 'must be a number' 
    28    
    29   cattr_accessor :salt 
    30   @@salt = 'sweet harmonious biscuits' # historic value 
    31    
     28     
    3229  attr_writer :confirm_password 
    3330   
    34   def self.sha1(phrase) 
    35     Digest::SHA1.hexdigest("--#{@@salt}--#{phrase}--") 
     31  def sha1(phrase) 
     32    Digest::SHA1.hexdigest("--#{salt}--#{phrase}--") 
    3633  end 
    3734   
    3835  def self.authenticate(login, password) 
    39     find_by_login_and_password(login, sha1(password)) 
     36    user = find_by_login(login) 
     37    user if user && user.password == user.sha1(password) 
    4038  end 
    4139   
     
    5654    before_create :encrypt_password 
    5755    def encrypt_password 
    58       self.password = self.class.sha1(password) 
     56      self.salt = Digest::SHA1.hexdigest("--#{Time.now}--#{login}--sweet harmonious biscuits--") 
     57      self.password = sha1(password) 
    5958    end 
    6059   
  • trunk/radiant/db/schema.rb

    r833 r857  
    1010# It's strongly recommended to check this file into your version control system. 
    1111 
    12 ActiveRecord::Schema.define(:version => 18) do 
     12ActiveRecord::Schema.define(:version => 19) do 
    1313 
    1414  create_table "config", :force => true do |t| 
     
    9797    t.text     "notes" 
    9898    t.integer  "lock_version",                 :default => 0 
     99    t.string   "salt" 
    99100  end 
    100101 
  • trunk/radiant/spec/models/user_spec.rb

    r647 r857  
    9494    @user.password_confirmation = @user.password = 'test_password' 
    9595    @user.save! 
    96     @user.password.should == User.sha1('test_password') 
     96    @user.password.should == @user.sha1('test_password') 
    9797  end 
    9898   
     
    101101    @user.password_confirmation = @user.password = '' 
    102102    @user.save! 
    103     @user.password.should == User.sha1('password') 
     103    @user.password.should == @user.sha1('password') 
    104104  end 
    105105   
     
    108108    @user.password_confirmation = @user.password = 'cool beans' 
    109109    @user.save! 
    110     @user.password.should == User.sha1('cool beans') 
     110    @user.password.should == @user.sha1('cool beans') 
    111111  end 
    112112   
    113113  it 'should save existing but same password' do 
    114114    @user.save! && @user.save! 
    115     @user.password.should == User.sha1('password') 
     115    @user.password.should == @user.sha1('password') 
     116  end 
     117   
     118  it "should create a salt when encrypting the password" do 
     119    @user.salt.should be_nil 
     120    @user.send(:encrypt_password) 
     121    @user.salt.should_not be_nil 
     122    @user.password.should == @user.sha1('password') 
    116123  end 
    117124end 
  • trunk/radiant/spec/scenarios/users_scenario.rb

    r764 r857  
    1111  helpers do 
    1212    def create_user(name, attributes={}) 
    13       create_record :user, name.symbolize, user_attributes(attributes.update(:name => name)) 
     13      create_model :user, name.symbolize, user_attributes(attributes.update(:name => name)) 
    1414    end 
    1515    def user_attributes(attributes={}) 
     
    2222        :password => "password" 
    2323      }.merge(attributes) 
    24       attributes[:password] = User.sha1(attributes[:password]) 
     24      attributes[:password_confirmation] = attributes[:password] 
    2525      attributes 
    2626    end