0
votes

I'm trying to set up bcrypt for my cakephp app. I've set it up before, on another app, and that worked. But, after basically copy / pasting the encryption code from one app to the other, it's saving the password as blank.

The database is setup correctly with the password field being a varchar(225).

I've come to the conclusion that the following line of code is what's causing the problem;

public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
        $hash = Security::hash($this->data['User']['password'], 'blowfish');
        $this->data['User']['password'] = $hash;
    }
    return true;
}

If I were to take out this beforeSave function, my password would save correctly, as plaintext. If I were to replace

$this->data['User']['password'] = $hash;

with

$this->data['User']['password'] = 'testpassword';

It would correctly save the password as testpassword.

My AppController:

public $components = array(
    'Session',
    'Auth' => array(
        'authenticate' =>'Blowfish',
        'logoutRedirect' => array('controller'=>'fronts', 'action'=>'index'),
        'authorize' => array('Controller')
    )
);

My Form:

<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User'); ?>
<fieldset>
    <?php 
        echo $this->form->input('username', array('placeholder' => 'Username', 'label' => false));
        echo $this->form->input('password', array('placeholder' => 'Password', 'label' => false));
        echo $this->form->submit('CREATE', array('class' => 'button')); 
    ?>
</fieldset>
<?php echo $this->Form->end(); ?>

When trying to login, though I know it won't work, I get this error

Authentication adapter Blowfish was not found.
1
Can you post or look into the form you have setup? Maybe a name of a field is wrong - Sixthpoint
Good idea, just edited OP - user2444539

1 Answers

1
votes

The beforesave appears to be correct. I am using the same thing essentially. Here is my code.

I also setup blowfish in my beforefilter, had problems with the other way

$this->Auth->authorize = array('Controller');
$this->Auth->authenticate = array(
   'Blowfish' => array( 
      'userModel' => 'Account',
      'fields' => array('username' => 'act_username', 'password' => 'act_password')
   )
);

The only other difference is my form button has this for submit

echo $this->Form->submit('Login', array('id' => 'submit', 'type' => 'submit'));

Note this as of 2.4 the blowfish encryption is changing, bcrypt only in 2.3: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#using-bcrypt-for-passwords