2014-01-14 19 views
5

努力尋找關於在Cake 2.4中使用Blowfish的幾個基本問​​題的答案。CakePHP - 如何爲密碼實現河豚散列?

AppController.php

public $components = array(
    'Auth' => array(
     'authenticate' => array(
      'Form' => array(
       'fields' => array(
        'username' => 'email' 
       ), 
       'passwordHasher' => 'Blowfish' 
      ) 
     ) 
    ), 
    'Cookie', 
    'Session' 
); 

現在怎麼辦?我如何登錄?

UsersController.php

public function login() { 

    if (!empty($this->request->data)) { 

     if ($this->Auth->login()) { 
      $this->redirect($this->Auth->redirectUrl()); 
     } 

    } 
} 

我需要什麼添加到這個?我收到以下錯誤,如果我試圖登錄:

警告(512):無效鹽:用於河豚請訪問http://www.php.net/crypt和讀取建立河豚鹽的相應部分。在嘗試登錄之前是否需要提供密碼?如果是,我將使用哪種方法以及鹽的最佳用途是什麼?[CORE/Cake/Utility/Security.php,line 285]

? Cake是否會自動嘗試使用core.php config文件中的所有用戶的鹽?

我很困惑,主要是因爲我不知道在一個標準的PHP方式CakePHP會嘗試自動爲我做河豚使用的哪些部分。

+0

我也有這個問題。你有沒有解決它? – Katelyn

回答

9

不能使用河豚,如果你已經擁有一個充滿密碼數據庫使用另一種方法散列。如果是這樣,他們將不會有效的Blowfish哈希密碼,你會得到上述錯誤。

在一個CakePHP的應用程序中實現的密碼哈希河豚而言,食譜對身份驗證使用bcrypt(河豚)專用部分:http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#using-bcrypt-for-passwords

你設置組件陣列,你這樣做:

<?php 
class AppController { 

    public $components = array(
     'Auth' => array(
      'authenticate' => array(
       'Form' => array(
        'passwordHasher' => 'Blowfish' 
       ) 
      ) 
     ) 
    ); 
} 

然後生成你可以使用密碼散列器類模型中的密碼。例如,User型號:

<?php 
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth'); 

class User extends AppModel { 

    public function beforeSave($options = array()) { 
     // if ID is not set, we're inserting a new user as opposed to updating 
     if (!$this->id) { 
      $passwordHasher = new BlowfishPasswordHasher(); 
      $this->data[$this->alias]['password'] = $passwordHasher->hash($this->data[$this->alias]['password']); 
     } 
     return true; 
    } 
} 

然後來驗證你真的不需要做任何事情,因爲CakePHP的認證處理程序將執行密碼爲您比較:

<?php 
class UsersController extends AppController { 

    public function login() { 
     if ($this->request->is('post')) { 
      if ($this->Auth->login()) { 
       return $this->redirect($this->Auth->redirectUrl()); 
      } else { 
       $this->Session->setFlash(__('Username or password incorrect')); 
      } 
     } 
    } 
} 

而且這是所有有就是這樣。

+0

'if(!$ this-> id){}'是非常規的。我不會推薦這種方法。特別是如果您還想爲用戶提供能夠更改其密碼的功能。在這裏使用!empty()更適合。見[這裏](http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/)。 – mark

+0

您的示例並沒有真正添加任何內容,因爲用戶無法註冊自己,因此沒有用戶創建它們。我應該如何將散列的密碼存儲在數據庫中?哪種數據類型/長度? – BadHorsie

+0

@mark事實上,但我有一個問題,如果我提取'用戶'記錄,然後保存它們,如果密碼進入結果集(即用一個'find('all')'調用),那麼密碼是重新哈希,只是因爲它存在,因此'!empty()'不會削減它。 –

1

我已經爲所有問題添加了相同的問題: 我將blowfish-hash保存爲VARCHAR(50),它在某些情況下太短。由此導致,我的登錄無效,因爲哈希是錯誤的。請確保使用足夠長度的字段(對於Blowfish至少VARCHAR(123))。 Source

0

我在嘗試將項目從1.3.x移植到2.7.x時遇到了類似的問題。我從完整的數據庫開始(在同一進程中從MySQL移植到PostgreSQL),但是卻是一個非常空的CakePHP應用程序。 問題是我的用戶表中的哈希密碼來自CakePHP 1.x.所以表中的哈希值並不遵循Blowfish慣例。這是觸發錯誤信息。河豚哈希遵循複雜的格式。順便說一句,同一個字符串的重複哈希將會不同。

解決方案:

  1. 變化的用戶表中的密碼列VARCHAR(128) (或更多)。
  2. 清空該列。
  3. 爲第一個 登錄生成有效散列。

爲了做到後者,我插入UsersController.php下列行:

public function login() { 
    if ... 
    // code for getting a start password: 
    $passwordHasher = new BlowfishPasswordHasher(); 
    $mypasswd = $passwordHasher->hash('MyPasswordinClearText'); 
    debug($mypasswd); 
    // end inserted code 
    ... 
} 

非常粗的,但有效的。現在我可以登錄繼續在同一個項目中開發。刪除代碼後,您可以登錄。