/
BaseAuthenticate.php
223 lines (197 loc) · 6.55 KB
/
BaseAuthenticate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Auth;
use Cake\Auth\PasswordHasherFactory;
use Cake\Controller\ComponentRegistry;
use Cake\Core\InstanceConfigTrait;
use Cake\Event\EventListenerInterface;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\ORM\TableRegistry;
/**
* Base Authentication class with common methods and properties.
*
*/
abstract class BaseAuthenticate implements EventListenerInterface {
use InstanceConfigTrait;
/**
* Default config for this object.
*
* - `fields` The fields to use to identify a user by.
* - `userModel` The alias for users table, defaults to Users.
* - `scope` Additional conditions to use when looking up and authenticating users,
* i.e. `['Users.is_active' => 1].`
* - `contain` Extra models to contain and store in session.
* - `passwordHasher` Password hasher class. Can be a string specifying class name
* or an array containing `className` key, any other keys will be passed as
* config to the class. Defaults to 'Default'.
*
* @var array
*/
protected $_defaultConfig = [
'fields' => [
'username' => 'username',
'password' => 'password'
],
'userModel' => 'Users',
'scope' => [],
'contain' => null,
'passwordHasher' => 'Default'
];
/**
* A Component registry, used to get more components.
*
* @var ComponentRegistry
*/
protected $_registry;
/**
* Password hasher instance.
*
* @var AbstractPasswordHasher
*/
protected $_passwordHasher;
/**
* Whether or not the user authenticated by this class
* requires their password to be rehashed with another algorithm.
*
* @var bool
*/
protected $_needsPasswordRehash = false;
/**
* Constructor
*
* @param \Cake\Controller\ComponentRegistry $registry The Component registry used on this request.
* @param array $config Array of config to use.
*/
public function __construct(ComponentRegistry $registry, array $config = []) {
$this->_registry = $registry;
$this->config($config);
}
/**
* Find a user record using the username and password provided.
*
* Input passwords will be hashed even when a user doesn't exist. This
* helps mitigate timing attacks that are attempting to find valid usernames.
*
* @param string $username The username/identifier.
* @param string|null $password The password, if not provide password checking is skipped
* and result of find is returned.
* @return bool|array Either false on failure, or an array of user data.
*/
protected function _findUser($username, $password = null) {
$userModel = $this->_config['userModel'];
list(, $model) = pluginSplit($userModel);
$fields = $this->_config['fields'];
$conditions = [$model . '.' . $fields['username'] => $username];
$scope = $this->_config['scope'];
if ($scope) {
$conditions = array_merge($conditions, $scope);
}
$table = TableRegistry::get($userModel)->find('all');
$contain = $this->_config['contain'];
if ($contain) {
$table = $table->contain($contain);
}
$result = $table
->where($conditions)
->first();
if (empty($result)) {
return false;
}
if ($password !== null) {
$hasher = $this->passwordHasher();
$hashedPassword = $result->get($fields['password']);
if (!$hasher->check($password, $hashedPassword)) {
return false;
}
$this->_needsPasswordRehash = $hasher->needsRehash($hashedPassword);
$result->unsetProperty($fields['password']);
}
return $result->toArray();
}
/**
* Return password hasher object
*
* @return AbstractPasswordHasher Password hasher instance
* @throws \RuntimeException If password hasher class not found or
* it does not extend AbstractPasswordHasher
*/
public function passwordHasher() {
if ($this->_passwordHasher) {
return $this->_passwordHasher;
}
$passwordHasher = $this->_config['passwordHasher'];
return $this->_passwordHasher = PasswordHasherFactory::build($passwordHasher);
}
/**
* Returns whether or not the password stored in the repository for the logged in user
* requires to be rehashed with another algorithm
*
* @return bool
*/
public function needsPasswordRehash() {
return $this->_needsPasswordRehash;
}
/**
* Authenticate a user based on the request information.
*
* @param \Cake\Network\Request $request Request to get authentication information from.
* @param \Cake\Network\Response $response A response object that can have headers added.
* @return mixed Either false on failure, or an array of user data on success.
*/
abstract public function authenticate(Request $request, Response $response);
/**
* Get a user based on information in the request. Primarily used by stateless authentication
* systems like basic and digest auth.
*
* @param \Cake\Network\Request $request Request object.
* @return mixed Either false or an array of user information
*/
public function getUser(Request $request) {
return false;
}
/**
* Handle unauthenticated access attempt. In implementation valid return values
* can be:
*
* - Null - No action taken, AuthComponent should return appropriate response.
* - Cake\Network\Response - A response object, which will cause AuthComponent to
* simply return that response.
*
* @param \Cake\Network\Request $request A request object.
* @param \Cake\Network\Response $response A response object.
* @return void
*/
public function unauthenticated(Request $request, Response $response) {
}
/**
* Returns a list of all events that this authenticate class will listen to.
*
* An authenticate class can listen to following events fired by AuthComponent:
*
* - `Auth.afterIdentify` - Fired after a user has been identified using one of
* configured authenticate class. The callback function should have signature
* like `afteIndentify(Event $event, array $user)` when `$user` is the
* identified user record.
*
* - `Auth.logout` - Fired when AuthComponent::logout() is called. The callback
* function should have signature like `logout(Event $event, array $user)`
* where `$user` is the user about to be logged out.
*
* @return array List of events this class listens to. Defaults to `[]`.
*/
public function implementedEvents() {
return [];
}
}