2015-09-06 77 views
3

我有一個設計認證的rails 4應用程序。我從頭開始重建,並希望編寫自己的身份驗證系統,但我有用戶存儲在數據庫中,其密碼存儲爲encrypted_password,這是設計用於存儲散列密碼的用途。我明白,使用bcrypt我應該有一個password_digest列。我可以使用devise encrypted_pa​​ssword和自定義身份驗證嗎?

我的問題是雙重的:bcrypt能夠讀取我的設備encrypted_password列中存儲的內容嗎?如果是,我可以簡單地將該數據庫列重命名爲password_digest還是會導致問題?

回答

1

從我讀過的,是的,你應該能夠重命名列並將其用於您的自定義身份驗證。

參考文獻: https://github.com/plataformatec/devise/blob/master/lib/devise/models/database_authenticatable.rb#L149-L151

module Devise 
    def self.bcrypt(klass, password) 
    ActiveSupport::Deprecation.warn "Devise.bcrypt is deprecated; use Devise::Encryptor.digest instead" 
    Devise::Encryptor.digest(klass, password) 
    end 

    module Models 
    module DatabaseAuthenticatable 

     # Digests the password using bcrypt. Custom encryption should override 
     # this method to apply their own algorithm. 
     # 
     # See https://github.com/plataformatec/devise-encryptable for examples 
     # of other encryption engines. 
     def password_digest(password) 
     Devise::Encryptor.digest(self.class, password) 
     end 

和:

https://github.com/plataformatec/devise/blob/master/lib/devise/encryptor.rb#L5-L10

module Devise 
    module Encryptor 
    def self.digest(klass, password) 
     if klass.pepper.present? 
     password = "#{password}#{klass.pepper}" 
     end 
     ::BCrypt::Password.create(password, cost: klass.stretches).to_s 
    end