1: <?php
2: /**
3: * PHP 5
4: *
5: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7: *
8: * Licensed under The MIT License
9: * For full copyright and license information, please see the LICENSE.txt
10: * Redistributions of files must retain the above copyright notice.
11: *
12: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13: * @link http://cakephp.org CakePHP(tm) Project
14: * @license http://www.opensource.org/licenses/mit-license.php MIT License
15: */
16:
17: App::uses('BaseAuthenticate', 'Controller/Component/Auth');
18:
19: /**
20: * Basic Authentication adapter for AuthComponent.
21: *
22: * Provides Basic HTTP authentication support for AuthComponent. Basic Auth will authenticate users
23: * against the configured userModel and verify the username and passwords match. Clients using Basic Authentication
24: * must support cookies. Since AuthComponent identifies users based on Session contents, clients using Basic
25: * Auth must support cookies.
26: *
27: * ### Using Basic auth
28: *
29: * In your controller's components array, add auth + the required settings.
30: * {{{
31: * public $components = array(
32: * 'Auth' => array(
33: * 'authenticate' => array('Basic')
34: * )
35: * );
36: * }}}
37: *
38: * In your login function just call `$this->Auth->login()` without any checks for POST data. This
39: * will send the authentication headers, and trigger the login dialog in the browser/client.
40: *
41: * @package Cake.Controller.Component.Auth
42: * @since 2.0
43: */
44: class BasicAuthenticate extends BaseAuthenticate {
45:
46: /**
47: * Settings for this object.
48: *
49: * - `fields` The fields to use to identify a user by.
50: * - `userModel` The model name of the User, defaults to User.
51: * - `scope` Additional conditions to use when looking up and authenticating users,
52: * i.e. `array('User.is_active' => 1).`
53: * - `recursive` The value of the recursive key passed to find(). Defaults to 0.
54: * - `contain` Extra models to contain and store in session.
55: * - `realm` The realm authentication is for. Defaults the server name.
56: *
57: * @var array
58: */
59: public $settings = array(
60: 'fields' => array(
61: 'username' => 'username',
62: 'password' => 'password'
63: ),
64: 'userModel' => 'User',
65: 'scope' => array(),
66: 'recursive' => 0,
67: 'contain' => null,
68: 'realm' => '',
69: );
70:
71: /**
72: * Constructor, completes configuration for basic authentication.
73: *
74: * @param ComponentCollection $collection The Component collection used on this request.
75: * @param array $settings An array of settings.
76: */
77: public function __construct(ComponentCollection $collection, $settings) {
78: parent::__construct($collection, $settings);
79: if (empty($this->settings['realm'])) {
80: $this->settings['realm'] = env('SERVER_NAME');
81: }
82: }
83:
84: /**
85: * Authenticate a user using basic HTTP auth. Will use the configured User model and attempt a
86: * login using basic HTTP auth.
87: *
88: * @param CakeRequest $request The request to authenticate with.
89: * @param CakeResponse $response The response to add headers to.
90: * @return mixed Either false on failure, or an array of user data on success.
91: */
92: public function authenticate(CakeRequest $request, CakeResponse $response) {
93: $result = $this->getUser($request);
94:
95: if (empty($result)) {
96: $response->header($this->loginHeaders());
97: $response->statusCode(401);
98: $response->send();
99: return false;
100: }
101: return $result;
102: }
103:
104: /**
105: * Get a user based on information in the request. Used by cookie-less auth for stateless clients.
106: *
107: * @param CakeRequest $request Request object.
108: * @return mixed Either false or an array of user information
109: */
110: public function getUser(CakeRequest $request) {
111: $username = env('PHP_AUTH_USER');
112: $pass = env('PHP_AUTH_PW');
113:
114: if (empty($username) || empty($pass)) {
115: return false;
116: }
117: return $this->_findUser($username, $pass);
118: }
119:
120: /**
121: * Generate the login headers
122: *
123: * @return string Headers for logging in.
124: */
125: public function loginHeaders() {
126: return sprintf('WWW-Authenticate: Basic realm="%s"', $this->settings['realm']);
127: }
128:
129: }
130: