2012-01-15 31 views
2

我最近得到了Devise的工作。新用戶登錄,註冊,註銷等等就好了。舊用戶然而有問題。我已經得到了它,我得到了一個401未經授權的,在我看來,哈希正好在登錄時被錯誤地創建,當然不正確匹配。Rails從CakePHP設計傳統用戶

我的用戶模型:

class User < ActiveRecord::Base 
    require "digest/sha1" 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :encryptable, :encryptor => :old_cakephp_auth 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me 
    has_many :events 
end 

CakePHP的使用SHA1,但我不知道它是如何做事情的具體細節。這顯然不起作用,這就是爲什麼我在這裏:

require "digest/sha1" 

module Devise 
    module Encryptors 
    class OldCakephpAuth < Base 
     def self.digest(password, stretches, salt, pepper) 
     Digest::SHA1.hexdigest("#{salt}#{password}") 
     end 
    end 
    end 
end 

我從如何添加自定義加密器的例子。他們有這個:

Digest::SHA1.hexdigest("--#{salt}--#{password}--") 

這也沒有工作。有人有主意嗎?

+0

So Digest :: SHA1.hexdigest(「#{salt}#{password}」)實際上會返回數據庫中相同的加密密碼,但我仍然收到401未經授權的消息。我試過返回Digest :: SHA1.hexdigest(「#{salt}#{password}」),但那也不行。 – Parris 2012-01-16 00:36:12

回答

2

我在創建自己的自定義加密器wiki上看到了這種變化。我不知道我以前沒有看到它。也許有人最近更新了它。

將以下內容放入用戶模型中。它應該覆蓋從設計的有效密碼:

def valid_password?(password) 
    return false if encrypted_password.blank? 
    Devise.secure_compare(Digest::SHA1.hexdigest(self.password_salt+password), self.encrypted_password) 
    end 

您需要確保填寫您在蛋糕中使用的密碼salt到所有舊用戶的行中。您還需要根據設計說明將密碼更改爲加密密碼。

我覺得我可能需要添加用戶模型的加密方式,以及新用戶。或者也許我創建的自定義加密器處理這個方面。

+0

感謝您的提示!也許你對這個問題有所瞭解? http://stackoverflow.com/questions/17322911/migrating-passwords-to-devise – Gediminas 2013-06-26 14:35:35