2014-09-04 130 views
0

我已經在Linux和Windows上使用Auth完成了項目,並且從未碰到過散列問題。它的基本要點是密碼不通過默認用戶設置的Hash :: check方法。我已經查看了大多數關於SO的答案,他們是不理解Hash的典型用戶每次都會產生不同的值,或者Hash :: check需要db中用戶的普通值和散列值。起初,這是Auth :: attempt的失敗,但看起來似乎是一個哈希驗證問題。我已經閱讀並重新閱讀Laravel Authentication以確保我沒有錯過確保db密碼字段太短(確保長度爲256)的內容。Hash :: check始終失敗

設置

用戶模型

class User extends Eloquent implements UserInterface, RemindableInterface { 
    protected $fillable = array(
    'email', 
    'password', 
); 

    protected $guarded = array(
    'id', 
); 

    //add the UserInterface/RemindableInterface methods here 
} 

種子

User::create(array(//could also do DB insert way.. 
    'id'=>1, 
    'email' => '[email protected]', 
    'password' => Hash::make('admin'), 
)); 

//User created with correct hash no problem. 

測試後的路線

//some other validation above this. Email/password correctly passed from form 
$email = Input::get('email'); 
$password = Input::get('password'); 

$user = User::where('email', $email)->first(); 
//gets user with hashed pass fine 
$sucess = Hash::check($password, $user->password); //could do $user->getAuthPassword() as well 
//always false.. 
//initially tried Auth::attempt getting the same results because of the internal Hash::check 

試過

  • 我已經通過驗證降壓到分貝散列與針對所插入的純通,鹽比較之後它未能鹽進行比較。
  • 我試圖創造路線(下圖)的get標誌一個新的用戶,並在表格上嘗試這些憑證,它仍然不能登錄(想通這可能是種子的問題。)

    $ newUser = new User(array( 'email'=>'[email protected]', 'password'=> Hash :: make('test') )); $ newUser-> save();

在這一點上,它似乎是一個奇怪的password_verify/password_hash問題,我不知道的引擎蓋下。

+0

如果你'Hash :: check('admin',Hash :: make('admin'))''會發生什麼? – Rob 2014-09-04 22:43:35

+0

工程很好(返回true)。其中一種情況是我期望在所有這些後都會失敗,但仍然令我難以置信。 – 2014-09-04 23:08:10

+0

然後可能'Input :: get('password')'不是你認爲的或者'$ user-> password'不是。你有一個唯一的電子郵件索引?沒有重複的電子郵件與不同的密碼?你是否在模型中創建了一個「創建」偵聽器,第二次散列密碼? – Rob 2014-09-04 23:13:23

回答

2

原來在用戶模型上設置了默認的setPasswordAttribute方法,該方法通過啓動填充程序爲任何創建或構建的用戶運行。

刪除:

public function setPasswordAttribute($value) 
{ 
    $this->attributes['password'] = Hash::make($value); 
} 

而且急,它工作正常。