Skip to content

Commit a3f120a

Browse files
committed
Clean up dormant code after identify option removal
Remove fields config and constructor from SessionAuthenticator since they were only used by the removed identify logic. Remove redundant constructor override from PrimaryKeySessionAuthenticator. Simplify tests.
1 parent 18afbf8 commit a3f120a

File tree

7 files changed

+12
-152
lines changed

7 files changed

+12
-152
lines changed

docs/en/authenticators.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ Configuration options:
1818

1919
- **sessionKey**: The session key for the user data, default is
2020
``Auth``
21-
- **fields**: Allows you to map the ``username`` field to the unique
22-
identifier in your user storage. Defaults to ``username``.
2321

2422
PrimaryKeySession
2523
=================

docs/es/authenticators.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ Opciones de configuración:
1818

1919
- **sessionKey**: Key para los datos de usuario, por defecto es
2020
``Auth``
21-
- **fields**: Permite mapear el campo ``username`` al identificador único
22-
en su almacenamiento de usuario. Por defecto es ``username``.
2321

2422
Form
2523
====

docs/fr/authenticators.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ Les options de configuration:
1919

2020
- **sessionKey**: La clé de session pour les données de l'utilisateur, par
2121
défaut ``Auth``.
22-
- **fields**: Vous permet de mapper le champ ``username`` à l'identifiant
23-
unique dans votre système de stockage des utilisateurs. Vaut ``username`` par
24-
défaut.
2522

2623
Form
2724
====

docs/ja/authenticators.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ Authenticatorは、リクエストを認証操作に変換する処理を行い
1414
設定オプション:
1515

1616
- **sessionKey**: ユーザーのセッションキー, デフォルトは ``Auth``
17-
- **fields**: ``username`` フィールドをユーザストレージ内の一意の識別しに写像することができます。
18-
デフォルトは ``username`` です。
1917

2018
`Form`
2119
=========

src/Authenticator/PrimaryKeySessionAuthenticator.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,6 @@ class PrimaryKeySessionAuthenticator extends SessionAuthenticator
5858
'idField' => 'id',
5959
];
6060

61-
/**
62-
* Constructor
63-
*
64-
* Bypasses SessionAuthenticator's default PasswordIdentifier creation
65-
* to allow lazy initialization of the TokenIdentifier in getIdentifier().
66-
*
67-
* @param \Authentication\Identifier\IdentifierInterface|null $identifier Identifier instance.
68-
* @param array<string, mixed> $config Configuration settings.
69-
*/
70-
public function __construct(?IdentifierInterface $identifier, array $config = [])
71-
{
72-
$this->_identifier = $identifier;
73-
$this->setConfig($config);
74-
}
75-
7661
/**
7762
* Gets the identifier.
7863
*

src/Authenticator/SessionAuthenticator.php

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
use ArrayAccess;
1919
use ArrayObject;
20-
use Authentication\Identifier\IdentifierFactory;
21-
use Authentication\Identifier\IdentifierInterface;
22-
use Authentication\Identifier\PasswordIdentifier;
2320
use Cake\Http\Exception\UnauthorizedException;
2421
use Psr\Http\Message\ResponseInterface;
2522
use Psr\Http\Message\ServerRequestInterface;
@@ -31,39 +28,19 @@ class SessionAuthenticator extends AbstractAuthenticator implements PersistenceI
3128
{
3229
/**
3330
* Default config for this object.
34-
* - `fields` The fields to use to verify a user by.
3531
* - `sessionKey` Session key.
32+
* - `impersonateSessionKey` Session key for impersonation.
33+
* - `identityAttribute` Request attribute for the identity.
3634
*
3735
* @var array
3836
*/
3937
protected array $_defaultConfig = [
40-
'fields' => [
41-
PasswordIdentifier::CREDENTIAL_USERNAME => 'username',
42-
],
38+
'fields' => [],
4339
'sessionKey' => 'Auth',
4440
'impersonateSessionKey' => 'AuthImpersonate',
4541
'identityAttribute' => 'identity',
4642
];
4743

48-
/**
49-
* Constructor
50-
*
51-
* @param \Authentication\Identifier\IdentifierInterface|null $identifier Identifier instance.
52-
* @param array<string, mixed> $config Configuration settings.
53-
*/
54-
public function __construct(?IdentifierInterface $identifier, array $config = [])
55-
{
56-
if ($identifier === null) {
57-
$identifierConfig = [];
58-
if (isset($config['fields'])) {
59-
$identifierConfig['fields'] = $config['fields'];
60-
}
61-
$identifier = IdentifierFactory::create('Authentication.Password', $identifierConfig);
62-
}
63-
64-
parent::__construct($identifier, $config);
65-
}
66-
6744
/**
6845
* Authenticate a user using session data.
6946
*

tests/TestCase/Authenticator/SessionAuthenticatorTest.php

Lines changed: 9 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
use ArrayObject;
2020
use Authentication\Authenticator\Result;
2121
use Authentication\Authenticator\SessionAuthenticator;
22-
use Authentication\Identifier\IdentifierFactory;
23-
use Authentication\Identifier\PasswordIdentifier;
2422
use Authentication\Test\TestCase\AuthenticationTestCase as TestCase;
2523
use Cake\Http\Exception\UnauthorizedException;
2624
use Cake\Http\Response;
@@ -42,11 +40,6 @@ class SessionAuthenticatorTest extends TestCase
4240
'core.Users',
4341
];
4442

45-
/**
46-
* @var \Authentication\Identifier\IdentifierInterface
47-
*/
48-
protected $identifier;
49-
5043
protected $sessionMock;
5144

5245
/**
@@ -56,8 +49,6 @@ public function setUp(): void
5649
{
5750
parent::setUp();
5851

59-
$this->identifier = IdentifierFactory::create('Authentication.Password');
60-
6152
$this->sessionMock = $this->getMockBuilder(Session::class)
6253
->disableOriginalConstructor()
6354
->onlyMethods(['read', 'write', 'delete', 'renew', 'check'])
@@ -83,91 +74,7 @@ public function testAuthenticateSuccess()
8374

8475
$request = $request->withAttribute('session', $this->sessionMock);
8576

86-
$authenticator = new SessionAuthenticator($this->identifier);
87-
$result = $authenticator->authenticate($request);
88-
89-
$this->assertInstanceOf(Result::class, $result);
90-
$this->assertSame(Result::SUCCESS, $result->getStatus());
91-
}
92-
93-
/**
94-
* Test authentication
95-
*
96-
* @return void
97-
*/
98-
public function testAuthenticateSuccessWithoutCollection()
99-
{
100-
$request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/']);
101-
102-
$this->sessionMock->expects($this->once())
103-
->method('read')
104-
->with('Auth')
105-
->willReturn([
106-
'username' => 'mariano',
107-
'password' => 'password',
108-
]);
109-
110-
$request = $request->withAttribute('session', $this->sessionMock);
111-
112-
$authenticator = new SessionAuthenticator(null, [
113-
'identifier' => 'Authentication.Password',
114-
]);
115-
$result = $authenticator->authenticate($request);
116-
117-
$this->assertInstanceOf(Result::class, $result);
118-
$this->assertSame(Result::SUCCESS, $result->getStatus());
119-
}
120-
121-
/**
122-
* Test authentication
123-
*
124-
* @return void
125-
*/
126-
public function testAuthenticateSuccessWithoutCollectionButObject()
127-
{
128-
$request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/']);
129-
130-
$this->sessionMock->expects($this->once())
131-
->method('read')
132-
->with('Auth')
133-
->willReturn([
134-
'username' => 'mariano',
135-
'password' => 'password',
136-
]);
137-
138-
$request = $request->withAttribute('session', $this->sessionMock);
139-
140-
$authenticator = new SessionAuthenticator(null, [
141-
'identifier' => new PasswordIdentifier(),
142-
]);
143-
$result = $authenticator->authenticate($request);
144-
145-
$this->assertInstanceOf(Result::class, $result);
146-
$this->assertSame(Result::SUCCESS, $result->getStatus());
147-
}
148-
149-
/**
150-
* Test authentication
151-
*
152-
* @return void
153-
*/
154-
public function testAuthenticateSuccessWithDirectCollection()
155-
{
156-
$request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/']);
157-
158-
$this->sessionMock->expects($this->once())
159-
->method('read')
160-
->with('Auth')
161-
->willReturn([
162-
'username' => 'mariano',
163-
'password' => 'password',
164-
]);
165-
166-
$request = $request->withAttribute('session', $this->sessionMock);
167-
168-
$authenticator = new SessionAuthenticator(null, [
169-
'identifier' => IdentifierFactory::create('Authentication.Password'),
170-
]);
77+
$authenticator = new SessionAuthenticator(null);
17178
$result = $authenticator->authenticate($request);
17279

17380
$this->assertInstanceOf(Result::class, $result);
@@ -190,7 +97,7 @@ public function testAuthenticateFailure()
19097

19198
$request = $request->withAttribute('session', $this->sessionMock);
19299

193-
$authenticator = new SessionAuthenticator($this->identifier);
100+
$authenticator = new SessionAuthenticator(null);
194101
$result = $authenticator->authenticate($request);
195102

196103
$this->assertInstanceOf(Result::class, $result);
@@ -207,7 +114,7 @@ public function testPersistIdentity()
207114
$request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/']);
208115
$request = $request->withAttribute('session', $this->sessionMock);
209116
$response = new Response();
210-
$authenticator = new SessionAuthenticator($this->identifier);
117+
$authenticator = new SessionAuthenticator(null);
211118

212119
$data = new ArrayObject(['username' => 'florian']);
213120

@@ -250,7 +157,7 @@ public function testClearIdentity()
250157
$request = $request->withAttribute('session', $this->sessionMock);
251158
$response = new Response();
252159

253-
$authenticator = new SessionAuthenticator($this->identifier);
160+
$authenticator = new SessionAuthenticator(null);
254161

255162
$this->sessionMock->expects($this->once())
256163
->method('delete')
@@ -279,7 +186,7 @@ public function testImpersonate()
279186
$request = $request->withAttribute('session', $this->sessionMock);
280187
$response = new Response();
281188

282-
$authenticator = new SessionAuthenticator($this->identifier);
189+
$authenticator = new SessionAuthenticator(null);
283190
$AuthUsers = TableRegistry::getTableLocator()->get('AuthUsers');
284191
$impersonator = $AuthUsers->newEntity([
285192
'username' => 'mariano',
@@ -318,7 +225,7 @@ public function testImpersonateAlreadyImpersonating()
318225
$request = $request->withAttribute('session', $this->sessionMock);
319226
$response = new Response();
320227

321-
$authenticator = new SessionAuthenticator($this->identifier);
228+
$authenticator = new SessionAuthenticator(null);
322229
$impersonator = new ArrayObject([
323230
'username' => 'mariano',
324231
'password' => 'password',
@@ -352,7 +259,7 @@ public function testStopImpersonating()
352259
$request = $request->withAttribute('session', $this->sessionMock);
353260
$response = new Response();
354261

355-
$authenticator = new SessionAuthenticator($this->identifier);
262+
$authenticator = new SessionAuthenticator(null);
356263

357264
$impersonator = new ArrayObject([
358265
'username' => 'mariano',
@@ -399,7 +306,7 @@ public function testStopImpersonatingNotImpersonating()
399306
$request = $request->withAttribute('session', $this->sessionMock);
400307
$response = new Response();
401308

402-
$authenticator = new SessionAuthenticator($this->identifier);
309+
$authenticator = new SessionAuthenticator(null);
403310

404311
$this->sessionMock->expects($this->once())
405312
->method('check')
@@ -436,7 +343,7 @@ public function testIsImpersonating()
436343
$request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/']);
437344
$request = $request->withAttribute('session', $this->sessionMock);
438345

439-
$authenticator = new SessionAuthenticator($this->identifier);
346+
$authenticator = new SessionAuthenticator(null);
440347

441348
$this->sessionMock->expects($this->once())
442349
->method('check')

0 commit comments

Comments
 (0)