Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 240
xsquirrel Avatar asked Jul 15 '26 09:07

xsquirrel


1 Answers

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.

like image 90
Moz Morris Avatar answered Jul 19 '26 14:07

Moz Morris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!