diff --git a/lib/devise/models/database_authenticatable.rb b/lib/devise/models/database_authenticatable.rb index 3e1da40c..1f3ebaab 100644 --- a/lib/devise/models/database_authenticatable.rb +++ b/lib/devise/models/database_authenticatable.rb @@ -2,6 +2,11 @@ require 'devise/strategies/database_authenticatable' require 'bcrypt' module Devise + # Digests the password using bcrypt. + def self.bcrypt(klass, password) + ::BCrypt::Password.create("#{password}#{klass.pepper}", :cost => klass.stretches).to_s + end + module Models # Authenticatable Module, responsible for encrypting password and validating # authenticity of a user while signing in. @@ -34,7 +39,7 @@ module Devise # Generates password encryption based on the given value. def password=(new_password) @password = new_password - self.encrypted_password = password_digest(@password) if @password.present? + self.encrypted_password = Devise.bcrypt(self.class, @password) if @password.present? end # Verifies whether an password (ie from sign in) is the user password. @@ -120,11 +125,6 @@ module Devise protected - # Digests the password using bcrypt. - def password_digest(password) - ::BCrypt::Password.create("#{password}#{self.class.pepper}", :cost => self.class.stretches).to_s - end - module ClassMethods Devise::Models.config(self, :pepper, :stretches) diff --git a/test/devise_test.rb b/test/devise_test.rb index 9352e164..1a5f7149 100644 --- a/test/devise_test.rb +++ b/test/devise_test.rb @@ -11,6 +11,16 @@ module Devise end class DeviseTest < ActiveSupport::TestCase + test 'bcrypt on the class' do + password = "super secret" + klass = Struct.new(:pepper, :stretches).new("blahblah", 2) + hash = Devise.bcrypt(klass, password) + assert_equal hash, Devise.bcrypt(klass, password) + + klass = Struct.new(:pepper, :stretches).new("bla", 2) + assert_not_equal hash, Devise.bcrypt(klass, password) + end + test 'model options can be configured through Devise' do swap Devise, :allow_unconfirmed_access_for => 113, :pepper => "foo" do assert_equal 113, Devise.allow_unconfirmed_access_for