どのように私は、CakePHPパスワードハッシュアルゴリズムを置き換えるのですか?

StackOverflow https://stackoverflow.com/questions/573307

質問

私はの上にケーキのアプリを入れしようとしている既存のデータベースを持っています。古いアプリはパスワードをハッシュするPerlでのcrypt()を使用しました。私は、PHPのアプリで同じことを行う必要があります。

ここで、標準CakePHPのアプリでその変更を行うための正しい場所はありますか?そして、何そのような変更は次のようになりますか?

役に立ちましたか?

解決

私それが働いてしまった...

ここに私の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/する

他のヒント

実際にdanbすることにより、上記の方法は、代わりに私は、標準のハッシュアルゴリズムをバイパスするためのカスタム認証コンポーネントを作成することになったのCakePHP 2.xの中で私のために動作しませんでした

/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内に置くことができます。私の場合、私はちょうど私が別のユーザモデルとカスタム認証を使用するつもりだったのコントローラで特異的にそれを置くことを選択します。

ただ、CakePHPの2.4.1でこれをフォローアップするために、私は、MD5(:スタティック:ACCOUNTNUMBERパスワード)として保存されたユーザーのパスワードを既存していた従来のデータベースのフロントエンドを構築し、ユーザーは、我々がするために必要なログインを許可します同様にそのハッシュ方式を使用します。

ソリューションでした。

でファイルアプリ/コントローラ/コンポーネント/認証/ 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);
    }

}

「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'),
            )
        ),
    )
);

特に、連想配列のキー「カスタム」の使用を注意します。

最後に、それは私が追加(私の場合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;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top