xsquirrel
xsquirrel

Reputation: 139

Cakephp 2.0 and basic auth

I've upgraded my app from CakePHP 1.3 to 2.0.4.

Previously, I was able to use the Security component to emulate Basic HTTP authentication only in one controller.

I used to do something like this:

$this->Auth->allow(array('*'));
$this->Security->loginOptions = array('type'=>'basic','realm'=>'api');
$this->Security->loginUsers = array("api"=>"123");
$this->Security->requireLogin();

Now SecurityComponent no longer handles Basic and Digest Authentication and I need to do something like this:

public $components = array(
    'Auth' => array(
        'authenticate' => array('Basic')
    )
);

But when I use this on my ApiController it redirects to my login form at /users/login. Am I missing something?

Upvotes: 3

Views: 3780

Answers (1)

Moz Morris
Moz Morris

Reputation: 6761

You need to configure the AuthComponent with your login action. You should check out the section on Configuring Authentication handlers in the Cake book.

Your setup will probably look something similar to this:

public $components = array(
  'Auth'=> array(
    'loginAction' => array(
      'controller' => 'api',
      'action'     => 'login'
    ),
    'loginRedirect' => array(
      'controller' => 'api',
      'action'     => 'logged_on'
    ),
    'authenticate' => array(
      'Basic' => array(
        'realm' => 'api'
      )
    )
  )
);

Also, it should be noted that Cake no longer supports defining users using the loginUsers property. You would probably have to extend the BasicAuthenticate class and override it's getUser() method.

Upvotes: 2

Related Questions