How to configure CakePHP's $this->Auth->login() to use custom password hasher

Viewed 3553

CakePHP v.2.4...

I'm following this documentation trying to set up the Auth component to use my custom password hashing class:

App::uses('PHPassPasswordHasher', 'Controller/Component/Auth');

class AppController extends Controller {

    // auth needed stuff
    public $components = array(
        'Session',
        'Cookie',
        'Auth'      => array(
            'authenticate'      => array(
                'Form'  => array(
                    'fields' => array('username'=>'email', 'password'=>'password'),
                    'passwordHasher'    => 'PHPass' 

                )
            ),

Inside my UsersController::login() I debug the return from $this->Auth->login(); and it always returns false, even when I submit the correct email / password.

(NOTE: It looks strange to me that the login() takes no parameters, but the docs seem to imply that it looks into the the request data automatically. And this would make sense if my configurations aren't correctly causing it to check the User.email field instead username.)

The post data from the submitted login form looks like this:

array(
    'User' => array(
        'password' => '*****',
        'email' => 'whatever@example.com'
    )
)

What am I missing?

Update2

I'm starting to suspect that the default hashing algorithm is getting used instead of my custom class. I tried to match the examples in the docs but they're quite vague on how to do this.

Here's the contents of app/Controller/Component/Auth/PHPassPasswordHasher.php

<?php
App::import('Vendor', 'PHPass/class-phpass'); //<--this exists and defines PasswordHash class
class PHPassPasswordHasher extends AbstractPasswordHasher {

    public function hash($password) {
        $hasher = new new PasswordHash( 8, true );
        return $hasher->HashPassword($password);
    }

    public function check($password, $hashedPassword) {
        debug('PHPassHasher'); die('Using custom hasher'); //<--THIS NEVER HAPPENS!
        $hasher = new new PasswordHash( 8, true );
        return $hasher->CheckPassword($password, $hashedPassword);
    }

}

AHA! The debug() never appears... so I'm pretty sure the problem is with my custom hasher configuration(s).

Update3

Additional clue: I experimented by setting various default hashing algorithms (Ex: "Simple", "Blowfish") and creating users. The hashes which show up in the DB are all the same which tells me that my config settings are getting ignored completely.

Update4

I debugged $this->settings inside the constructor of /lib/Cake/Controller/Component/Auth/BaseAuthenticate.php and my custom hasher settings are in there:

array(
    'fields' => array(
        'password' => 'password',
        'username' => 'email'
    ),
    'userModel' => 'User',
    'scope' => array(),
    'recursive' => (int) 0,
    'contain' => null,
    'passwordHasher' => 'PHPass'
)
3 Answers
Related