mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 17:16:18 +00:00
Starting Digest auth, using Basic auth as a starting base.
This commit is contained in:
parent
bcd8dcd0f7
commit
945e49ad09
2 changed files with 324 additions and 0 deletions
108
cake/libs/controller/components/auth/digest_authenticate.php
Normal file
108
cake/libs/controller/components/auth/digest_authenticate.php
Normal file
|
@ -0,0 +1,108 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* PHP 5
|
||||||
|
*
|
||||||
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||||
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||||
|
*
|
||||||
|
* Licensed under The MIT License
|
||||||
|
* Redistributions of files must retain the above copyright notice.
|
||||||
|
*
|
||||||
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||||
|
* @link http://cakephp.org CakePHP(tm) Project
|
||||||
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||||
|
*/
|
||||||
|
App::import('Component', 'auth/base_authenticate');
|
||||||
|
App::import('Core', 'String');
|
||||||
|
|
||||||
|
|
||||||
|
class DigestAuthenticate extends BaseAuthenticate {
|
||||||
|
/**
|
||||||
|
* Settings for this object.
|
||||||
|
*
|
||||||
|
* - `fields` The fields to use to identify a user by.
|
||||||
|
* - `userModel` The model name of the User, defaults to User.
|
||||||
|
* - `scope` Additional conditions to use when looking up and authenticating users,
|
||||||
|
* i.e. `array('User.is_active' => 1).`
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public $settings = array(
|
||||||
|
'fields' => array(
|
||||||
|
'username' => 'username',
|
||||||
|
'password' => 'password'
|
||||||
|
),
|
||||||
|
'userModel' => 'User',
|
||||||
|
'scope' => array(),
|
||||||
|
'realm' => '',
|
||||||
|
'qop' => 'auth',
|
||||||
|
'nonce' => '',
|
||||||
|
'opaque' => ''
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor, completes configuration for digest authentication.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct($settings) {
|
||||||
|
parent::__construct($settings);
|
||||||
|
if (empty($this->settings['realm'])) {
|
||||||
|
$this->settings['realm'] = env('SERVER_NAME');
|
||||||
|
}
|
||||||
|
if (empty($this->settings['nonce'])) {
|
||||||
|
$this->settings['realm'] = uniqid('');
|
||||||
|
}
|
||||||
|
if (empty($this->settings['opaque'])) {
|
||||||
|
$this->settings['opaque'] = md5($this->settings['realm']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Authenticate a user using Digest HTTP auth. Will use the configured User model and attempt a
|
||||||
|
* login using Digest HTTP auth.
|
||||||
|
*
|
||||||
|
* @param CakeRequest $request The request to authenticate with.
|
||||||
|
* @param CakeResponse $response The response to add headers to.
|
||||||
|
* @return mixed Either false on failure, or an array of user data on success.
|
||||||
|
*/
|
||||||
|
public function authenticate(CakeRequest $request, CakeResponse $response) {
|
||||||
|
$username = env('PHP_AUTH_USER');
|
||||||
|
$pass = env('PHP_AUTH_PW');
|
||||||
|
|
||||||
|
if (empty($username) || empty($pass)) {
|
||||||
|
$response->header($this->loginHeaders());
|
||||||
|
$response->send();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = $this->_findUser($username, $pass);
|
||||||
|
|
||||||
|
if (empty($result)) {
|
||||||
|
$response->header($this->loginHeaders());
|
||||||
|
$response->header('Location', Router::reverse($request));
|
||||||
|
$response->statusCode(401);
|
||||||
|
$response->send();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate the login headers
|
||||||
|
*
|
||||||
|
* @return string Headers for logging in.
|
||||||
|
*/
|
||||||
|
public function loginHeaders() {
|
||||||
|
$options = array(
|
||||||
|
'realm' => $this->settings['realm'],
|
||||||
|
'qop' => $this->settings['qop'],
|
||||||
|
'nonce' => $this->settings['nonce'],
|
||||||
|
'opaque' => $this->settings['opaque']
|
||||||
|
);
|
||||||
|
$opts = array();
|
||||||
|
foreach ($options as $k => $v) {
|
||||||
|
$opts[] = sprintf('%s="%s"', $k, $v);
|
||||||
|
}
|
||||||
|
return 'WWW-Authenticate: Digest ' . implode(',', $opts);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,216 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||||
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||||
|
*
|
||||||
|
* Licensed under The MIT License
|
||||||
|
* Redistributions of files must retain the above copyright notice.
|
||||||
|
*
|
||||||
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||||
|
* @link http://cakephp.org CakePHP(tm) Project
|
||||||
|
* @package cake.tests.cases.libs.controller.components.auth
|
||||||
|
* @since CakePHP(tm) v 2.0
|
||||||
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||||
|
*/
|
||||||
|
|
||||||
|
App::import('Component', 'auth/digest_authenticate');
|
||||||
|
App::import('Model', 'AppModel');
|
||||||
|
App::import('Core', 'CakeRequest');
|
||||||
|
App::import('Core', 'CakeResponse');
|
||||||
|
|
||||||
|
|
||||||
|
require_once CAKE_TESTS . 'cases' . DS . 'libs' . DS . 'model' . DS . 'models.php';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case for DigestAuthentication
|
||||||
|
*
|
||||||
|
* @package cake.test.cases.controller.components.auth
|
||||||
|
*/
|
||||||
|
class DigestAuthenticateTest extends CakeTestCase {
|
||||||
|
|
||||||
|
public $fixtures = array('core.user', 'core.auth_user');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* setup
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
$this->auth = new DigestAuthenticate(array(
|
||||||
|
'fields' => array('username' => 'user', 'password' => 'password'),
|
||||||
|
'userModel' => 'User',
|
||||||
|
'realm' => 'localhost',
|
||||||
|
'nonce' => 123,
|
||||||
|
'opaque' => '123abc'
|
||||||
|
));
|
||||||
|
|
||||||
|
$password = Security::hash('password', null, true);
|
||||||
|
ClassRegistry::init('User')->updateAll(array('password' => '"' . $password . '"'));
|
||||||
|
$this->server = $_SERVER;
|
||||||
|
$this->response = $this->getMock('CakeResponse');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* teardown
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function tearDown() {
|
||||||
|
parent::tearDown();
|
||||||
|
$_SERVER = $this->server;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test applying settings in the constructor
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testConstructor() {
|
||||||
|
$object = new DigestAuthenticate(array(
|
||||||
|
'userModel' => 'AuthUser',
|
||||||
|
'fields' => array('username' => 'user', 'password' => 'password'),
|
||||||
|
'nonce' => 123456
|
||||||
|
));
|
||||||
|
$this->assertEquals('AuthUser', $object->settings['userModel']);
|
||||||
|
$this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->settings['fields']);
|
||||||
|
$this->assertEquals(123456, $object->settings['nonce']);
|
||||||
|
$this->assertEquals(env('SERVER_NAME'), $object->settings['realm']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test the authenticate method
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testAuthenticateNoData() {
|
||||||
|
$request = new CakeRequest('posts/index', false);
|
||||||
|
|
||||||
|
$this->response->expects($this->once())
|
||||||
|
->method('header')
|
||||||
|
->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
|
||||||
|
|
||||||
|
$this->assertFalse($this->auth->authenticate($request, $this->response));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test the authenticate method
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testAuthenticateNoUsername() {
|
||||||
|
$request = new CakeRequest('posts/index', false);
|
||||||
|
$_SERVER['PHP_AUTH_PW'] = 'foobar';
|
||||||
|
|
||||||
|
$this->response->expects($this->once())
|
||||||
|
->method('header')
|
||||||
|
->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
|
||||||
|
|
||||||
|
$this->assertFalse($this->auth->authenticate($request, $this->response));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test the authenticate method
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testAuthenticateNoPassword() {
|
||||||
|
$request = new CakeRequest('posts/index', false);
|
||||||
|
$_SERVER['PHP_AUTH_USER'] = 'mariano';
|
||||||
|
|
||||||
|
$this->response->expects($this->once())
|
||||||
|
->method('header')
|
||||||
|
->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
|
||||||
|
|
||||||
|
$this->assertFalse($this->auth->authenticate($request, $this->response));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test the authenticate method
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testAuthenticateInjection() {
|
||||||
|
$request = new CakeRequest('posts/index', false);
|
||||||
|
$request->addParams(array('pass' => array(), 'named' => array()));
|
||||||
|
|
||||||
|
$_SERVER['PHP_AUTH_USER'] = '> 1';
|
||||||
|
$_SERVER['PHP_AUTH_PW'] = "' OR 1 = 1";
|
||||||
|
|
||||||
|
$this->assertFalse($this->auth->authenticate($request, $this->response));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test that challenge headers are sent when no credentials are found.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testAuthenticateChallenge() {
|
||||||
|
$request = new CakeRequest('posts/index', false);
|
||||||
|
$request->addParams(array('pass' => array(), 'named' => array()));
|
||||||
|
|
||||||
|
$this->response->expects($this->at(0))
|
||||||
|
->method('header')
|
||||||
|
->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
|
||||||
|
|
||||||
|
$this->response->expects($this->at(1))
|
||||||
|
->method('send');
|
||||||
|
|
||||||
|
$result = $this->auth->authenticate($request, $this->response);
|
||||||
|
$this->assertFalse($result);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* test authenticate sucesss
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testAuthenticateSuccess() {
|
||||||
|
$request = new CakeRequest('posts/index', false);
|
||||||
|
$request->addParams(array('pass' => array(), 'named' => array()));
|
||||||
|
|
||||||
|
$_SERVER['PHP_AUTH_USER'] = 'mariano';
|
||||||
|
$_SERVER['PHP_AUTH_PW'] = 'password';
|
||||||
|
|
||||||
|
$result = $this->auth->authenticate($request, $this->response);
|
||||||
|
$expected = array(
|
||||||
|
'id' => 1,
|
||||||
|
'user' => 'mariano',
|
||||||
|
'created' => '2007-03-17 01:16:23',
|
||||||
|
'updated' => '2007-03-17 01:18:31'
|
||||||
|
);
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test scope failure.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testAuthenticateFailReChallenge() {
|
||||||
|
$this->auth->settings['scope'] = array('user' => 'nate');
|
||||||
|
$request = new CakeRequest('posts/index', false);
|
||||||
|
$request->addParams(array('pass' => array(), 'named' => array()));
|
||||||
|
|
||||||
|
$_SERVER['PHP_AUTH_USER'] = 'mariano';
|
||||||
|
$_SERVER['PHP_AUTH_PW'] = 'password';
|
||||||
|
|
||||||
|
$this->response->expects($this->at(0))
|
||||||
|
->method('header')
|
||||||
|
->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
|
||||||
|
|
||||||
|
$this->response->expects($this->at(1))
|
||||||
|
->method('header')
|
||||||
|
->with('Location', Router::reverse($request));
|
||||||
|
|
||||||
|
$this->response->expects($this->at(2))
|
||||||
|
->method('statusCode')
|
||||||
|
->with(401);
|
||||||
|
|
||||||
|
$this->response->expects($this->at(3))
|
||||||
|
->method('send');
|
||||||
|
|
||||||
|
$this->assertFalse($this->auth->authenticate($request, $this->response));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue