如何替换cakephp密码哈希算法?

时间:2009-02-21 16:20:22

标签: php authentication cakephp crypt

我有一个现有的数据库我正试图将蛋糕应用程序放在上面。旧的应用程序在Perl中使用crypt()来散列密码。我需要在PHP应用程序中执行相同的操作。

在标准的cakephp应用程序中进行更改的正确位置在哪里?这样的改变会是什么样的?

3 个答案:

答案 0 :(得分:8)

我开始工作了......

这是我的AppController:

class AppController extends Controller {
    var $components = array('Auth');

    function beforeFilter() {
        // this is part of cake that serves up static pages, it should be authorized by default
        $this->Auth->allow('display');
        // tell cake to look on the user model itself for the password hashing function
        $this->Auth->authenticate = ClassRegistry::init('User');
        // tell cake where our credentials are on the User entity
        $this->Auth->fields = array(
           'username' => 'user',
           'password' => 'pass',
        );
        // this is where we want to go after a login... we'll want to make this dynamic at some point
        $this->Auth->loginRedirect = array('controller'=>'users', 'action'=>'index');
    }
}

然后是用户:

<?php
class User extends AppModel {
    var $name = 'User';

    // this is used by the auth component to turn the password into its hash before comparing with the DB
    function hashPasswords($data) {
         $data['User']['pass'] = crypt($data['User']['pass'], substr($data['User']['user'], 0, 2));
         return $data;
    }
}
?>

其他一切都很正常,我想。

这是一个很好的资源:http://teknoid.wordpress.com/2008/10/08/demystifying-auth-features-in-cakephp-12/

答案 1 :(得分:2)

实际上danb的上述方法在CakePHP 2.x中对我不起作用而是我最终创建了一个自定义auth组件来绕过标准的散列算法:

/app/Controller/Component/Auth/CustomFormAuthenticate.php

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

class CustomFormAuthenticate extends FormAuthenticate {

    protected function _password($password) {
        return self::hash($password);
    }

    public static function hash($password) {
        // Manipulate $password, hash, custom hash, whatever
        return $password;
    }
}

...然后在我的控制器中使用它......

public $components = array(
    'Session',
    'Auth' => array(
        'authenticate' => array(
            'CustomForm' => array(
                'userModel' => 'Admin'
            )
        )
    )
);

最后一个块也可以放在 AppController beforeFilter 方法中。在我的情况下,我只是选择将它专门放在一个控制器中,我将使用不同的用户模型进行自定义身份验证。

答案 2 :(得分:1)

为了在CakePHP 2.4.1中进行跟进,我正在构建一个旧数据库的前端,该数据库将现有用户密码存储为md5(accountnumber:statictext:password),并允许用户登录我们需要的也使用那个散列系统。

解决方案是:

使用以下命令创建文件app / Controller / Component / Auth / CustomAuthenticate.php:

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

class CustomAuthenticate extends FormAuthenticate {

    protected function _findUser($username, $password = null) {
        $userModel = $this->settings['userModel'];
        list(, $model) = pluginSplit($userModel);
        $fields = $this->settings['fields'];

        if (is_array($username)) {
            $conditions = $username;
        } else {
            $conditions = array(
                $model . '.' . $fields['username'] => $username
            );

        }

        if (!empty($this->settings['scope'])) {
            $conditions = array_merge($conditions, $this->settings['scope']);

        }

        $result = ClassRegistry::init($userModel)->find('first', array(
            'conditions' => $conditions,
            'recursive' => $this->settings['recursive'],
            'contain' => $this->settings['contain'],
        ));
        if (empty($result[$model])) {
            return false;
        }

        $user = $result[$model];
        if ($password) {
            if (!(md5($username.":statictext:".$password) === $user[$fields['password']])) {
                return false;
            }
            unset($user[$fields['password']]);
        }

        unset($result[$model]);
        return array_merge($user, $result);
    }

}

“extends FormAuthenticate”意味着此文件接管_findUser函数,但正常地遵循所有其他函数的FormAuthenticate。然后通过编辑AppController.php并添加到AppController类来激活它:

public $components = array(
    'Session',
    'Auth' => array(
        'loginAction' => array('controller' => 'accounts', 'action' => 'login'),
        'loginRedirect' => array('controller' => 'accounts', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
        'authenticate' => array (
            'Custom' => array(
                'userModel' => 'Account',
                'fields' => array('username' => 'number'),
            )
        ),
    )
);

特别注意使用关联数组键'Custom'。

最后在创建新用户时需要对密码进行哈希处理,因此对于模型文件(在我的案例中为Account.php),我添加了:

public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = md5($this->data[$this->alias]['number'].":statictext:".$this->data[$this->alias]['password']);
    }
    return true;
}