|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under The MIT License |
|
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
|
14
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
|
15
|
|
|
* |
|
16
|
|
|
* Licensed under The MIT License |
|
17
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
18
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
19
|
|
|
* |
|
20
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
|
21
|
|
|
* @link http://cakephp.org CakePHP(tm) Project |
|
22
|
|
|
* @since 0.10.0 |
|
23
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace CakeDC\Api\Service\Auth; |
|
27
|
|
|
|
|
28
|
|
|
use Cake\Controller\Component\AuthComponent; |
|
29
|
|
|
use Cake\Core\App; |
|
30
|
|
|
use Cake\Core\Exception\Exception; |
|
31
|
|
|
use Cake\Utility\Hash; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Class AuthenticateTrait |
|
35
|
|
|
* |
|
36
|
|
|
* @package CakeDC\Api\Service\Auth |
|
37
|
|
|
*/ |
|
38
|
|
|
trait AuthenticateTrait |
|
39
|
|
|
{ |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Objects that will be used for authentication checks. |
|
43
|
|
|
* |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $_authenticateObjects = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The instance of the Authenticate provider that was used for |
|
50
|
|
|
* successfully logging in the current user after calling `login()` |
|
51
|
|
|
* in the same request |
|
52
|
|
|
* |
|
53
|
|
|
* @var \Cake\Auth\BaseAuthenticate |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $_authenticationProvider; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get the current user from storage. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string|null $key Field to retrieve. Leave null to get entire User record. |
|
61
|
|
|
* @return mixed|null Either User record or null if no user is logged in, or retrieved field if key is specified. |
|
62
|
|
|
*/ |
|
63
|
48 |
|
public function user($key = null) |
|
64
|
|
|
{ |
|
65
|
48 |
|
$user = $this->storage()->read(); |
|
|
|
|
|
|
66
|
48 |
|
if (!$user) { |
|
67
|
48 |
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
47 |
|
if ($key === null) { |
|
71
|
47 |
|
return $user; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return Hash::get($user, $key); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Set provided user info to storage as logged in user. |
|
79
|
|
|
* |
|
80
|
|
|
* The storage class is configured using `storage` config key or passing |
|
81
|
|
|
* instance to AuthComponent::storage(). |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $user Array of user data. |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
3 |
|
public function setUser(array $user) |
|
87
|
|
|
{ |
|
88
|
3 |
|
$this->storage()->write($user); |
|
|
|
|
|
|
89
|
3 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* connected authentication objects will have their |
|
93
|
|
|
* getUser() methods called. |
|
94
|
|
|
* |
|
95
|
|
|
* This lets stateless authentication methods function correctly. |
|
96
|
|
|
* |
|
97
|
|
|
* @return bool true If a user can be found, false if one cannot. |
|
98
|
|
|
*/ |
|
99
|
48 |
|
protected function _getUser() |
|
100
|
|
|
{ |
|
101
|
48 |
|
$user = $this->user(); |
|
102
|
48 |
|
if ($user) { |
|
103
|
|
|
$this->storage()->redirectUrl(false); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
48 |
|
if (empty($this->_authenticateObjects)) { |
|
109
|
48 |
|
$this->constructAuthenticate(); |
|
110
|
48 |
|
} |
|
111
|
48 |
|
foreach ($this->_authenticateObjects as $auth) { |
|
112
|
48 |
|
$result = $auth->getUser($this->request); |
|
|
|
|
|
|
113
|
48 |
|
if (!empty($result) && is_array($result)) { |
|
114
|
47 |
|
$this->_authenticationProvider = $auth; |
|
115
|
47 |
|
$event = $this->dispatchEvent('Auth.afterIdentify', [$result, $auth]); |
|
|
|
|
|
|
116
|
47 |
|
if ($event->result !== null) { |
|
117
|
|
|
$result = $event->result; |
|
118
|
|
|
} |
|
119
|
47 |
|
$this->storage()->write($result); |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
47 |
|
return true; |
|
122
|
|
|
} |
|
123
|
1 |
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Use the configured authentication adapters, and attempt to identify the user |
|
130
|
|
|
* by credentials contained in $request. |
|
131
|
|
|
* |
|
132
|
|
|
* Triggers `Auth.afterIdentify` event which the authenticate classes can listen |
|
133
|
|
|
* to. |
|
134
|
|
|
* |
|
135
|
|
|
* @return array|bool User record data, or false, if the user could not be identified. |
|
136
|
|
|
*/ |
|
137
|
4 |
|
public function identify() |
|
138
|
|
|
{ |
|
139
|
|
|
// $this->_setDefaults(); |
|
140
|
|
|
|
|
141
|
4 |
|
if (empty($this->_authenticateObjects)) { |
|
142
|
4 |
|
$this->constructAuthenticate(); |
|
143
|
4 |
|
} |
|
144
|
4 |
|
foreach ($this->_authenticateObjects as $auth) { |
|
145
|
4 |
|
$result = $auth->authenticate($this->request, $this->response); |
|
|
|
|
|
|
146
|
4 |
|
if (!empty($result) && is_array($result)) { |
|
147
|
3 |
|
$this->_authenticationProvider = $auth; |
|
148
|
3 |
|
$event = $this->dispatchEvent('Auth.afterIdentify', [$result, $auth]); |
|
|
|
|
|
|
149
|
3 |
|
if ($event->result !== null) { |
|
150
|
|
|
return $event->result; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
3 |
|
return $result; |
|
154
|
|
|
} |
|
155
|
2 |
|
} |
|
156
|
|
|
|
|
157
|
2 |
|
return false; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Loads the configured authentication objects. |
|
162
|
|
|
* |
|
163
|
|
|
* @return array|null The loaded authorization objects, or null on empty authenticate value. |
|
164
|
|
|
* @throws \Cake\Core\Exception\Exception |
|
165
|
|
|
*/ |
|
166
|
52 |
|
public function constructAuthenticate() |
|
167
|
|
|
{ |
|
168
|
52 |
|
if (empty($this->_config['authenticate'])) { |
|
|
|
|
|
|
169
|
|
|
return null; |
|
170
|
|
|
} |
|
171
|
52 |
|
$this->_authenticateObjects = []; |
|
172
|
52 |
|
$authenticate = Hash::normalize((array)$this->_config['authenticate']); |
|
173
|
52 |
|
$global = []; |
|
174
|
52 |
|
if (isset($authenticate[AuthComponent::ALL])) { |
|
175
|
51 |
|
$global = $authenticate[AuthComponent::ALL]; |
|
176
|
51 |
|
unset($authenticate[AuthComponent::ALL]); |
|
177
|
51 |
|
} |
|
178
|
52 |
|
foreach ($authenticate as $alias => $config) { |
|
179
|
52 |
|
if (!empty($config['className'])) { |
|
180
|
|
|
$class = $config['className']; |
|
181
|
|
|
unset($config['className']); |
|
182
|
|
|
} else { |
|
183
|
52 |
|
$class = $alias; |
|
184
|
|
|
} |
|
185
|
52 |
|
$className = App::className($class, 'Service/Auth/Authenticate', 'Authenticate'); |
|
186
|
52 |
|
if (!class_exists($className)) { |
|
187
|
|
|
throw new Exception(sprintf('Authentication adapter "%s" was not found.', $class)); |
|
188
|
|
|
} |
|
189
|
52 |
|
if (!method_exists($className, 'authenticate')) { |
|
190
|
|
|
throw new Exception('Authentication objects must implement an authenticate() method.'); |
|
191
|
|
|
} |
|
192
|
52 |
|
$config = array_merge($global, (array)$config); |
|
193
|
52 |
|
$this->_authenticateObjects[$alias] = new $className($this->_action, $config); |
|
|
|
|
|
|
194
|
52 |
|
$this->getEventManager()->on($this->_authenticateObjects[$alias]); |
|
|
|
|
|
|
195
|
52 |
|
} |
|
196
|
|
|
|
|
197
|
52 |
|
return $this->_authenticateObjects; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Getter for authenticate objects. Will return a particular authenticate object. |
|
202
|
|
|
* |
|
203
|
|
|
* @param string $alias Alias for the authenticate object |
|
204
|
|
|
* |
|
205
|
|
|
* @return \Cake\Auth\BaseAuthenticate|null |
|
206
|
|
|
*/ |
|
207
|
|
|
public function getAuthenticate($alias) |
|
208
|
|
|
{ |
|
209
|
|
|
if (empty($this->_authenticateObjects)) { |
|
210
|
|
|
$this->constructAuthenticate(); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return isset($this->_authenticateObjects[$alias]) ? $this->_authenticateObjects[$alias] : null; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* If login was called during this request and the user was successfully |
|
218
|
|
|
* authenticated, this function will return the instance of the authentication |
|
219
|
|
|
* object that was used for logging the user in. |
|
220
|
|
|
* |
|
221
|
|
|
* @return \Cake\Auth\BaseAuthenticate|null |
|
222
|
|
|
*/ |
|
223
|
|
|
public function authenticationProvider() |
|
224
|
|
|
{ |
|
225
|
|
|
return $this->_authenticationProvider; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.