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