1
votes

I baked a cakephp application with a users table, and I'm trying to get authentication to work using the Blowfish hash. My password field is a varchar(255), so it should be long enough to store the hash. Everything in the app is the default baked output, expect for what follows.

This issue is that I can't log in after creating a user; I always get "Access Denied". What's the best way of troubleshooting this?

AppController.php

App::uses('Controller', 'Controller');

class AppController extends Controller {
    public function beforeFilter(){
        $this->Auth->allow('index', 'view');
    }

    public $components = array(
        'Session',
        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'fields' => array('username' => 'email'),
                    'passwordHasher' => 'Blowfish'
                    )
                ),
            'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'index'),
            'authError' => "Access Denied",
            'authorize' => array('Controller'),
        )
    );

    public function isAuthorized($user){
        return true;
    }
}

User.php (model)

App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

public function beforeSave($options = array()) {
    if (!empty($this->data['User']['password'])) {
        $passwordHasher = new BlowfishPasswordHasher();
        $this->data['User']['password'] = $passwordHasher->hash($this->data['User']['password']);
    }
    return true;
}

UsersController.php

public function login(){
    if ($this->request->is('post')) {
        if($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        }
        else {
            $this->Session->setFlash('Access Denied');
        }
    }
}

login.ctp

echo $this->Form->create('user');
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->button('Log In', array('type' => 'submit');
echo $this->Form->end();

'debug($this->request); die;' in login function gives the following output. should password be * or should it be the hashed version of the input?

data => array(
    'user' => array(
        'password' => '*****',
        'email' => '[email protected]'
    )
)
2

2 Answers

2
votes

1)listen to @waspinator echo $this->Form->create('User');

2)

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

remove it ad put it in AppController and it should be

App::uses('AuthComponent', 'Controller/Component');

3)comment this lines

//public function beforeFilter(){
//    $this->Auth->allow('index', 'view');
//}

//public function isAuthorized($user){
//        return true;
//}

4) for first time put this on top of user controller so you can save your password

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('edit', 'index', 'view);
}
1
votes
echo $this->Form->create('user');

should be

echo $this->Form->create('User');