0

I have a weird mistake when log in. The field 'passwd' is hashed as usual, so for instance the password '12345' is '844b1054b7c94519b6565dd301b3dede4a07b2c1' in the database.

Apparently everything is correct...

...but when I try to log in, I can do it with '844b1054b7c94519b6565dd301b3dede4a07b2c1', but no with '12345', and I don't know why. It should be the opposite!

$default_settings = array(
    'Auth.enabled' => true,
    'Auth.authorize' => 'controller', //actions
    'Auth.actionPath' => $prefix.'/',
    'Auth.userModel' => 'Taller',
    'Auth.authenticate' => ClassRegistry::init('Taller'),
    'Auth.fields' => array('username' => 'usuario', 'password' => 'passwd'),
    'Auth.authError' => false,
    'Auth.loginAction' => array('plugin' => 'pro', 'controller' => 'users', 'action' => 'login'),                                       'Auth.loginRedirect' => '/webpro',                                  'Auth.logoutRedirect' => '/webpro',         
    'Acl.enabled' => true,
);

I'm using CakePHP 1.2.

Thanks in advance.

13
  • does it give any error ? Dec 31, 2013 at 12:48
  • No, just 'wrong password'. I guess is something in the configuration, but I don't know what. Dec 31, 2013 at 13:00
  • 2
    btw you are using very antique version of cake Dec 31, 2013 at 13:00
  • It should unhash the password before validate, but it doesn't. Dec 31, 2013 at 13:01
  • 2
    "It should unhash the password before validate, but it doesn't."... No - that's just not how it works. You don't unhash passwords.
    – Dave
    Dec 31, 2013 at 15:09

1 Answer 1

0

CakePHP probably doesn't understand that it needs to hash the password field before comparing because you have renamed your field... This is confirmed by the fact that you can enter the hashed password into the field and login successfully...

 'Auth.fields' => array('username' => 'usuario', 'password' => 'passwd'),

Try renaming your database column to password, and make sure you are using FormHelpers in your views to rule out that being your issue.. I apologise for the poor answer, but this is the best I can make from what you have posted. If you reply to my answer I'm happy to improve it.

Otherwise, Find a way to Hash the passwords before comparing, but this is something the new >2.0 definitely does. I am not sure how to do this in CakePHP 1.2, but I strongly suggest you upgrade to enjoy all the new features in 2.0, also try stick to CakePHP conventions, the more you customise the harder issues become to diagnose.

Update

Try adding the following to your User Model:

app/Model/User.php

    function hashPasswords($data) {
        return Security::hash($data,'md5',false);
    }

Ref: http://api.cakephp.org/1.2/class-Security.html

If the above doesn't work:

Alternative Controller Based Approach

app/Controller/UsersController.php

function login() {
    if(!empty($this->data)) {
        $this->data['User']['password'] = Security::hash($this->data['User']['password'],'md5',false);
        $this->Auth->login($this->data);
        $this->redirect("home");
    }
}

Ref: http://book.cakephp.org/1.2/en/The-Manual/Core-Components/Authentication.html

2
  • Thanks very much for your answer. I have tried to change the name of the column, but the problem is still there. I know that updating the vesrion of Cake should resolve lots of problems, but right now I can't do it. So, I will keep triying and I will inform you when the issue has been solved. Thanks Jan 2, 2014 at 15:18
  • I've updated my answer with how to hash, there is actually a number of ways to do the above, so it's up to you to find a more elegant way, but I am confident that based on what you have told us that my controller method will work. Jan 3, 2014 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.