CakePHP - 如何实现河豚对密码的哈希处理



努力寻找有关在蛋糕 2.4 中使用河豚的一些基本问题的答案。

应用控制器.php

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

现在怎么办?如何登录?

用户控制器.php

public function login() {
    if (!empty($this->request->data)) {
        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirectUrl());
        }
    }
}

我需要添加什么?如果我尝试登录,我会收到以下错误:

警告 (512): 无效盐:用于河豚 请访问 http://www.php.net/crypt 并阅读有关构建河豚盐的相应部分。[核心/蛋糕/实用程序/安全.php,第 285 行]

在尝试登录之前,我是否需要加盐密码,如果是,我使用哪种方法以及使用盐的最佳方法是什么?Cake 是否会自动尝试为所有用户使用核心.php配置文件中的盐?

我很困惑,主要是因为我不知道以标准PHP方式使用河豚的哪些部分CakePHP试图自动为我完成。

如果您已经有一个充满使用其他方法散列的密码的数据库,则无法使用 Blowfish。如果是这样,它们将不是有效的河豚散列密码,您将收到上述错误。

在 CakePHP 应用程序中实现 Blowfish 进行密码哈希方面,Cookbook 有一个关于在身份验证中使用 bcrypt(Blowfish)的专门部分: 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'));
            }
        }
    }
}

仅此而已。

我为所有

有相同问题的人加了一个:我将河豚哈希保存为 VARCHAR(50),在某些情况下太短了。因此,我的登录不起作用,因为哈希错误。请确保使用足够长度的字段(对于河豚,至少为VARCHAR(123))。源

当我尝试将项目从 1.3.x 移植到 2.7.x 时,我遇到了类似的问题。我从完整的数据库开始(在同一过程中从MySQL移植到PostgreSQL),但是一个非常空的CakePHP应用程序。问题是我的用户表中的哈希密码来自CakePHP 1.x。因此,表中的哈希值不遵循河豚惯例。正是这一点触发了错误消息。河豚哈希遵循复杂的格式。顺便说一下,同一字符串的重复哈希值会有所不同。

溶液:

  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
  ...
}

非常粗糙,但有效。现在我可以登录以继续在同一项目中开发。登录后删除代码。

最新更新