2014-09-21 170 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