2011-01-21 23:10:45 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-01-21 23:10:45 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2011-01-21 23:10:45 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-01-21 23:10:45 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2011-01-21 23:10:45 +00:00
|
|
|
*/
|
2011-03-05 22:10:42 +00:00
|
|
|
|
2011-03-06 00:07:56 +00:00
|
|
|
App::uses('BaseAuthenticate', 'Controller/Component/Auth');
|
2011-01-21 23:10:45 +00:00
|
|
|
|
2011-01-22 21:27:07 +00:00
|
|
|
/**
|
|
|
|
* Basic Authentication adapter for AuthComponent.
|
|
|
|
*
|
2014-06-07 12:53:49 +00:00
|
|
|
* Provides Basic HTTP authentication support for AuthComponent. Basic Auth will
|
|
|
|
* authenticate users against the configured userModel and verify the username
|
|
|
|
* and passwords match.
|
2011-01-22 21:27:07 +00:00
|
|
|
*
|
|
|
|
* ### Using Basic auth
|
2011-08-16 03:55:08 +00:00
|
|
|
*
|
2011-01-22 21:27:07 +00:00
|
|
|
* In your controller's components array, add auth + the required settings.
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-08-19 02:16:16 +00:00
|
|
|
* public $components = array(
|
2011-01-22 21:27:07 +00:00
|
|
|
* 'Auth' => array(
|
|
|
|
* 'authenticate' => array('Basic')
|
|
|
|
* )
|
|
|
|
* );
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-01-22 21:27:07 +00:00
|
|
|
*
|
2014-06-07 12:53:49 +00:00
|
|
|
* You should also set `AuthComponent::$sessionKey = false;` in your AppController's
|
|
|
|
* beforeFilter() to prevent CakePHP from sending a session cookie to the client.
|
|
|
|
*
|
|
|
|
* Since HTTP Basic Authentication is stateless you don't need a login() action
|
|
|
|
* in your controller. The user credentials will be checked on each request. If
|
|
|
|
* valid credentials are not provided, required authentication headers will be sent
|
|
|
|
* by this authentication provider which triggers the login dialog in the browser/client.
|
|
|
|
*
|
|
|
|
* You may also want to use `$this->Auth->unauthorizedRedirect = false;`.
|
2014-06-17 01:48:35 +00:00
|
|
|
* By default, unauthorized users are redirected to the referrer URL,
|
|
|
|
* `AuthComponent::$loginAction`, or '/'. If unauthorizedRedirect is set to
|
|
|
|
* false, a ForbiddenException exception is thrown instead of redirecting.
|
2011-01-22 21:27:07 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component.Auth
|
2011-01-22 21:27:07 +00:00
|
|
|
* @since 2.0
|
|
|
|
*/
|
2011-01-21 23:10:45 +00:00
|
|
|
class BasicAuthenticate extends BaseAuthenticate {
|
2012-03-04 00:27:46 +00:00
|
|
|
|
2011-01-22 01:28:24 +00:00
|
|
|
/**
|
|
|
|
* Constructor, completes configuration for basic authentication.
|
|
|
|
*
|
2011-02-18 04:17:07 +00:00
|
|
|
* @param ComponentCollection $collection The Component collection used on this request.
|
2011-01-22 21:27:07 +00:00
|
|
|
* @param array $settings An array of settings.
|
2011-01-22 01:28:24 +00:00
|
|
|
*/
|
2011-02-18 04:17:07 +00:00
|
|
|
public function __construct(ComponentCollection $collection, $settings) {
|
|
|
|
parent::__construct($collection, $settings);
|
2011-01-22 01:28:24 +00:00
|
|
|
if (empty($this->settings['realm'])) {
|
|
|
|
$this->settings['realm'] = env('SERVER_NAME');
|
|
|
|
}
|
|
|
|
}
|
2011-01-22 21:27:07 +00:00
|
|
|
|
2011-01-21 23:10:45 +00:00
|
|
|
/**
|
2013-03-03 20:42:52 +00:00
|
|
|
* Authenticate a user using HTTP auth. Will use the configured User model and attempt a
|
|
|
|
* login using HTTP auth.
|
2011-01-21 23:10:45 +00:00
|
|
|
*
|
2011-01-22 01:28:24 +00:00
|
|
|
* @param CakeRequest $request The request to authenticate with.
|
|
|
|
* @param CakeResponse $response The response to add headers to.
|
2011-01-21 23:10:45 +00:00
|
|
|
* @return mixed Either false on failure, or an array of user data on success.
|
|
|
|
*/
|
2011-01-22 01:28:24 +00:00
|
|
|
public function authenticate(CakeRequest $request, CakeResponse $response) {
|
2013-03-03 20:42:52 +00:00
|
|
|
return $this->getUser($request);
|
2011-01-22 01:28:24 +00:00
|
|
|
}
|
|
|
|
|
2011-02-05 23:41:00 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Get a user based on information in the request. Used by cookie-less auth for stateless clients.
|
2011-02-05 23:41:00 +00:00
|
|
|
*
|
|
|
|
* @param CakeRequest $request Request object.
|
|
|
|
* @return mixed Either false or an array of user information
|
|
|
|
*/
|
2013-02-09 12:39:11 +00:00
|
|
|
public function getUser(CakeRequest $request) {
|
2017-03-15 16:32:47 +00:00
|
|
|
if (!isset($_SERVER['PHP_AUTH_USER'])) {
|
2017-03-15 15:27:27 +00:00
|
|
|
$httpAuthorization = $request->header('Authorization');
|
2017-03-15 16:32:47 +00:00
|
|
|
if ($httpAuthorization !== false && strlen($httpAuthorization) > 0 && strpos($httpAuthorization, 'basic') !== false) {
|
2017-03-15 16:22:31 +00:00
|
|
|
list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', base64_decode(substr($httpAuthorization, 6)));
|
2017-03-15 16:32:47 +00:00
|
|
|
if (strlen($_SERVER['PHP_AUTH_USER']) === 0 || strlen($_SERVER['PHP_AUTH_PW']) === 0) {
|
2017-03-15 16:22:31 +00:00
|
|
|
unset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
|
|
|
|
}
|
|
|
|
}
|
2016-08-28 17:16:59 +00:00
|
|
|
}
|
2011-02-05 23:41:00 +00:00
|
|
|
$username = env('PHP_AUTH_USER');
|
|
|
|
$pass = env('PHP_AUTH_PW');
|
2011-08-16 03:55:08 +00:00
|
|
|
|
2014-06-03 02:02:28 +00:00
|
|
|
if (!is_string($username) || $username === '' || !is_string($pass) || $pass === '') {
|
2011-02-05 23:41:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $this->_findUser($username, $pass);
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:42:52 +00:00
|
|
|
/**
|
|
|
|
* Handles an unauthenticated access attempt by sending appropriate login headers
|
|
|
|
*
|
|
|
|
* @param CakeRequest $request A request object.
|
|
|
|
* @param CakeResponse $response A response object.
|
2013-03-18 14:58:12 +00:00
|
|
|
* @return void
|
|
|
|
* @throws UnauthorizedException
|
2013-03-03 20:42:52 +00:00
|
|
|
*/
|
|
|
|
public function unauthenticated(CakeRequest $request, CakeResponse $response) {
|
2013-03-18 09:43:35 +00:00
|
|
|
$Exception = new UnauthorizedException();
|
2013-03-18 14:58:12 +00:00
|
|
|
$Exception->responseHeader(array($this->loginHeaders()));
|
2013-03-18 09:43:35 +00:00
|
|
|
throw $Exception;
|
2013-03-03 20:42:52 +00:00
|
|
|
}
|
|
|
|
|
2011-01-22 01:28:24 +00:00
|
|
|
/**
|
|
|
|
* Generate the login headers
|
|
|
|
*
|
|
|
|
* @return string Headers for logging in.
|
|
|
|
*/
|
|
|
|
public function loginHeaders() {
|
|
|
|
return sprintf('WWW-Authenticate: Basic realm="%s"', $this->settings['realm']);
|
2011-01-21 23:10:45 +00:00
|
|
|
}
|
2012-03-04 00:27:46 +00:00
|
|
|
|
|
|
|
}
|