2009-02-21 9 views
6

私は既存のデータベースを持っています。私はケーキアプリを上に置こうとしています。古いアプリはPerlでcrypt()を使ってパスワードをハッシュしました。私はPHPアプリケーションで同じことをする必要があります。cakephpパスワードハッシュアルゴリズムを置き換えるにはどうしたらいいですか?

標準のcakephpアプリでその変更を行う正しい場所はどこですか?そして、そのような変化はどのように見えるでしょうか?

答えて

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/

2

実際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方法の内側に置くことができます。私の場合は、別のユーザーモデルを使用してカスタム認証を使用するコントローラーに具体的に指定するだけです。

+1

をファイル名はCustomFormAuthenticate.phpにする必要がある - ないCustomFormAuthentication .php –

+0

ありがとう、良いキャッチ – jesal

+0

@jesalこれは5ヶ月のスレッドですが、あなたが電子メールでsaltとして使用する必要がある場合、CustomFormAuthenticateクラスを構造化する方法を知っていますか?明らかに、電子メールはこの時点でアクセスできません。助けてください! – rizalp1

1

これをCakePHP 2.4.1で追ってみると、既存のユーザパスワードがmd5(accountnumber:statictext:password)として保存されていたレガシーデータベースのフロントエンドを構築していました。そのハッシングシステムを使用することもできます。

解決した

でファイルアプリ/コントローラ/コンポーネント/認証/ 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を通常どおりにします。連想配列のキー「カスタム」の使用を注意してください。特に

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

:これは、その後AppController.phpを編集してのAppControllerクラスにこのような何かを追加することによって活性化されます。

私は追加(私の場合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; 
} 
+0

ありがとう@sverreg!ご回答いただきありがとうございます。私はパスワード処理を動作させることができました! – azerto00

+0

さらに、このソリューションでは、認証プロセスを処理するモデルを選択できます。 – azerto00

+0

助けてくれてうれしいです:-) – sverreg

関連する問題