Skip to content

Commit 3e53169

Browse files
committed
feat(41755): update authentication configuration to use specific identifier keys for each authenticator
1 parent 4811032 commit 3e53169

File tree

6 files changed

+56
-34
lines changed

6 files changed

+56
-34
lines changed

config/users.php

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,34 @@
210210
'Form' => [
211211
'className' => 'CakeDC/Auth.Form',
212212
'urlChecker' => 'Authentication.CakeRouter',
213+
'identifier' => [
214+
'Authentication.Password' => [
215+
'fields' => [
216+
'username' => ['username', 'email'],
217+
'password' => 'password',
218+
],
219+
'resolver' => [
220+
'className' => 'Authentication.Orm',
221+
'finder' => 'active',
222+
],
223+
],
224+
],
213225
],
214226
'Token' => [
215227
'className' => 'Authentication.Token',
216228
'skipTwoFactorVerify' => true,
217229
'header' => null,
218230
'queryParam' => 'api_key',
219231
'tokenPrefix' => null,
232+
'identifier' => [
233+
'Authentication.Token' => [
234+
'tokenField' => 'api_token',
235+
'resolver' => [
236+
'className' => 'Authentication.Orm',
237+
'finder' => 'active',
238+
],
239+
],
240+
],
220241
],
221242
'Cookie' => [
222243
'className' => 'CakeDC/Auth.Cookie',
@@ -231,6 +252,11 @@
231252
'Social' => [
232253
'className' => 'CakeDC/Users.Social',
233254
'skipTwoFactorVerify' => true,
255+
'identifier' => [
256+
'CakeDC/Users.Social' => [
257+
'authFinder' => 'active',
258+
],
259+
],
234260
],
235261
'SocialPendingEmail' => [
236262
'className' => 'CakeDC/Users.SocialPendingEmail',
@@ -244,31 +270,6 @@
244270
],
245271
]
246272
],
247-
'Identifiers' => [
248-
'Password' => [
249-
'className' => 'Authentication.Password',
250-
'fields' => [
251-
'username' => ['username', 'email'],
252-
'password' => 'password',
253-
],
254-
'resolver' => [
255-
'className' => 'Authentication.Orm',
256-
'finder' => 'active',
257-
],
258-
],
259-
'Social' => [
260-
'className' => 'CakeDC/Users.Social',
261-
'authFinder' => 'active',
262-
],
263-
'Token' => [
264-
'className' => 'Authentication.Token',
265-
'tokenField' => 'api_token',
266-
'resolver' => [
267-
'className' => 'Authentication.Orm',
268-
'finder' => 'active',
269-
],
270-
],
271-
],
272273
'Authorization' => [
273274
'enable' => true,
274275
'serviceLoader' => \CakeDC\Users\Loader\AuthorizationServiceLoader::class,

src/Loader/AuthenticationServiceLoader.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ public function __invoke(ServerRequestInterface $request)
5252
protected function loadIdentifiers($service)
5353
{
5454
$identifiers = Configure::read('Auth.Identifiers');
55+
56+
if (empty($identifiers)) {
57+
return;
58+
}
59+
60+
deprecationWarning(
61+
'15.2.0',
62+
'Configuring identifiers globally via `Auth.Identifiers` is deprecated. ' .
63+
'Please move each identifier\'s configuration into the `identifier` key within its specific authenticator under `Auth.Authenticators`. ' .
64+
'For example, the `Auth.Identifiers.Password` configuration should now be placed inside `Auth.Authenticators.Form.identifier`.',
65+
);
66+
5567
foreach ($identifiers as $key => $item) {
5668
[$identifier, $options] = $this->_getItemLoadData($item, $key);
5769

src/Model/Behavior/RegisterBehavior.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ public function activateUser(EntityInterface $user)
146146
public function buildValidator(\Cake\Event\EventInterface $event, Validator $validator, $name)
147147
{
148148
if ($name === 'default') {
149-
return $this->_emailValidator($validator, $this->validateEmail);
149+
$validator = $this->_emailValidator($validator, $this->validateEmail);
150150
}
151151

152-
return $validator;
152+
$event->setResult($validator);
153153
}
154154

155155
/**

tests/TestCase/Controller/Traits/BaseTrait.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,11 @@ protected function _mockRequestGet($withSession = false)
187187
*/
188188
protected function _mockFlash()
189189
{
190-
$this->Trait->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
190+
$flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
191191
->addMethods(['error', 'success'])
192192
->disableOriginalConstructor()
193193
->getMock();
194+
$this->Trait->components()->set('Flash', $flash);
194195
}
195196

196197
/**
@@ -288,11 +289,14 @@ protected function _mockAuthentication($user = null, $failures = [], $identifier
288289

289290
$controller = new Controller($this->Trait->getRequest());
290291
$registry = new ComponentRegistry($controller);
291-
$this->Trait->Authentication = new AuthenticationComponent($registry, [
292+
293+
$authentication = new AuthenticationComponent($registry, [
292294
'loginRedirect' => $this->successLoginRedirect,
293295
'logoutRedirect' => $this->logoutRedirect,
294296
'loginAction' => $this->loginAction,
295297
]);
298+
299+
$this->Trait->components()->set('Authentication', $authentication);
296300
}
297301

298302
/**

tests/TestCase/Controller/Traits/LoginTraitTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,11 @@ public function testLoginGet()
235235
->method('is')
236236
->with('post')
237237
->will($this->returnValue(false));
238-
$this->Trait->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
238+
$flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
239239
->addMethods(['error'])
240240
->disableOriginalConstructor()
241241
->getMock();
242+
$this->Trait->components()->set('Flash', $flash);
242243

243244
$this->Trait->Flash->expects($this->never())
244245
->method('error');
@@ -297,10 +298,11 @@ public function testLogout()
297298
$this->Trait->expects($this->once())
298299
->method('redirect')
299300
->with($this->logoutRedirect);
300-
$this->Trait->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
301+
$flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
301302
->addMethods(['success'])
302303
->disableOriginalConstructor()
303304
->getMock();
305+
$this->Trait->components()->set('Flash', $flash);
304306
$this->Trait->Flash->expects($this->once())
305307
->method('success')
306308
->with('You\'ve successfully logged out');

tests/TestCase/Controller/Traits/OneTimePasswordVerifyTraitTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,11 @@ public function testVerifyNotEnabled()
111111
public function testVerifyGetShowQR()
112112
{
113113
Configure::write('OneTimePasswordAuthenticator.login', true);
114-
$this->Trait->OneTimePasswordAuthenticator = $this->getMockBuilder(OneTimePasswordAuthenticatorComponent::class)
114+
$oneTimePasswordAuthenticator = $this->getMockBuilder(OneTimePasswordAuthenticatorComponent::class)
115115
->disableOriginalConstructor()
116116
->onlyMethods(['createSecret', 'getQRCodeImageAsDataUri'])
117117
->getMock();
118+
$this->Trait->components()->set('OneTimePasswordAuthenticator', $oneTimePasswordAuthenticator);
118119

119120
$request = $this->getMockBuilder(ServerRequest::class)
120121
->onlyMethods(['is', 'getData', 'getSession'])
@@ -161,11 +162,12 @@ public function testVerifyGetGeneratesNewSecret()
161162
{
162163
Configure::write('OneTimePasswordAuthenticator.login', true);
163164

164-
$this->Trait->OneTimePasswordAuthenticator = $this
165+
$oneTimePasswordAuthenticator = $this
165166
->getMockBuilder(OneTimePasswordAuthenticatorComponent::class)
166167
->disableOriginalConstructor()
167168
->onlyMethods(['createSecret', 'getQRCodeImageAsDataUri'])
168169
->getMock();
170+
$this->Trait->components()->set('OneTimePasswordAuthenticator', $oneTimePasswordAuthenticator);
169171

170172
$request = $this->getMockBuilder(ServerRequest::class)
171173
->onlyMethods(['is', 'getData', 'getSession'])
@@ -222,11 +224,12 @@ public function testVerifyGetDoesNotGenerateNewSecret()
222224
{
223225
Configure::write('OneTimePasswordAuthenticator.login', true);
224226

225-
$this->Trait->OneTimePasswordAuthenticator = $this
227+
$oneTimePasswordAuthenticator = $this
226228
->getMockBuilder(OneTimePasswordAuthenticatorComponent::class)
227229
->disableOriginalConstructor()
228230
->onlyMethods(['createSecret', 'getQRCodeImageAsDataUri'])
229231
->getMock();
232+
$this->Trait->components()->set('OneTimePasswordAuthenticator', $oneTimePasswordAuthenticator);
230233

231234
$request = $this->getMockBuilder(ServerRequest::class)
232235
->onlyMethods(['is', 'getData', 'getSession'])

0 commit comments

Comments
 (0)