Skip to content

Commit 6e6c06a

Browse files
committed
Add tests covering sessionauthenticator refresing the identity
Refs #596
1 parent 52a330d commit 6e6c06a

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/Authenticator/SessionAuthenticator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ class SessionAuthenticator extends AbstractAuthenticator implements PersistenceI
3030
* Default config for this object.
3131
* - `fields` The fields to use to verify a user by.
3232
* - `sessionKey` Session key.
33-
* - `identify` Whether or not to identify user data stored in a session.
33+
* - `identify` Whether or not to identify user data stored in a session. This is
34+
* useful if you want to remotely end sessions that have a different password stored,
35+
* or if your identification logic needs additional conditions before a user can login.
3436
*
3537
* @var array
3638
*/

tests/TestCase/AuthenticationServiceTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Cake\Http\ServerRequest;
3232
use Cake\Http\ServerRequestFactory;
3333
use Cake\Http\Uri;
34+
use Cake\I18n\FrozenTime;
3435
use Psr\Http\Message\RequestInterface;
3536
use Psr\Http\Message\ResponseInterface;
3637
use Psr\Http\Message\ServerRequestInterface;
@@ -134,6 +135,58 @@ public function testAuthenticateWithChallengeDisabled()
134135
$this->assertFalse($result->isValid());
135136
}
136137

138+
/**
139+
* Integration test for session auth + identify always getting a fresh user record.
140+
*
141+
* @return void
142+
*/
143+
public function testAuthenticationWithSessionIdentify()
144+
{
145+
$users = $this->fetchTable('Users');
146+
$user = $users->get(1);
147+
148+
$request = ServerRequestFactory::fromGlobals([
149+
'SERVER_NAME' => 'example.com',
150+
'REQUEST_URI' => '/testpath',
151+
]);
152+
$request->getSession()->write('Auth', [
153+
'username' => $user->username,
154+
'password' => $user->password,
155+
]);
156+
157+
$factory = function () {
158+
return new AuthenticationService([
159+
'identifiers' => [
160+
'Authentication.Password',
161+
],
162+
'authenticators' => [
163+
'Authentication.Session' => [
164+
'identify' => true,
165+
],
166+
],
167+
]);
168+
};
169+
$service = $factory();
170+
$result = $service->authenticate($request);
171+
$this->assertTrue($result->isValid());
172+
173+
$dateValue = new FrozenTime('2022-01-01 10:11:12');
174+
$identity = $result->getData();
175+
$this->assertEquals($identity->username, $user->username);
176+
$this->assertNotEquals($identity->created, $dateValue);
177+
178+
// Update the user so that we can ensure session is reading from the db.
179+
$user->created = $dateValue;
180+
$users->saveOrFail($user);
181+
182+
$service = $factory();
183+
$result = $service->authenticate($request);
184+
$this->assertTrue($result->isValid());
185+
$identity = $result->getData();
186+
$this->assertEquals($identity->username, $user->username);
187+
$this->assertEquals($identity->created, $dateValue);
188+
}
189+
137190
/**
138191
* testLoadAuthenticatorException
139192
*/

0 commit comments

Comments
 (0)