2014-09-21 168 views
0

我是新來php和cakephp,我是從cakephp(http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html)下面的簡單身份驗證和授權應用程序教程。所有人似乎都很好。Cakephp&用戶電子郵件確認

我添加了電子郵件確認以在用戶訂閱時激活帳戶。在教程中,密碼使用blowfishpassword hasher。我將它用作確認鏈接中的一個標記。

,但我似乎無法能夠比較與數據庫中的密碼的鏈接令牌......

$passwordHasher = new BlowfishPasswordHasher(); 
      $motdepasse = $this->data['Utilisateur']['mot_passe'] = $passwordHasher->hash(
       $this->data['Utilisateur']['mot_passe'] 
      ); 
      $link = array('controller'=>'utilisateurs','action'=>'activate',$this->Utilisateur->id 
        .'-'. $motdepasse); 


public function activate($token) { 
    $token = explode('-',$token); 
    $user = $this->Utilisateur->find('first',array(
    'conditions' => array('id' => $token[0],'Utilisateur.mot_passe' => Security::hash($token[1], 'blowfish', 'Utilisateur.mot_passe')) 
)); 
    debug($user); 
    debug($token[1]); 
    die(); 

} 

你能幫助我嗎?多謝你們!

+0

Wince你正在做手動,你能檢查每個哈希版本是什麼,並看看它們是否真的不同? – Dave 2014-09-21 15:14:19

回答

1

首先,你不應該發送密碼散列,無論散列可能有多安全,確認令牌應該單獨生成!只需將其存儲在一個額外的列或一個單獨的表中。

也就是說,在您的activate()方法中,您再次散列散列,如果實際上會生成散列,則會導致比較失敗。然而,腳本不會genereate哈希,因爲你使用的是無效的鹽值這將導致以下警告:

無效鹽:Utilisateur.mot_passe對河豚請訪問http://www.php.net/crypt讀建築的相應章節河豚鹽。

and Security::hash()將返回一個空字符串。如果你沒有收到這樣的消息,那麼你需要enable the debug mode

我建議在嘗試實現安全相關功能之前先熟悉PHP,CakePHP,哈希和東西!

您可能想查看https://github.com/CakeDC/users,它支持電子郵件驗證和更多的開箱即用。

+0

@PatriceB可能也想閱讀[如何在CakePHP中使用令牌等](http://www.dereuromark.de/2010/06/25/tools-plugin-part1-codekey/)而不是輕量級的方法。 – mark 2014-09-21 22:07:44