Skip to content

Commit 3825a48

Browse files
committed
Docs.
1 parent e450914 commit 3825a48

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed

docs/en/authenticators.rst

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,24 @@ It also helps to avoid session invalidation.
3737
Session itself stores the entity object including nested objects like DateTime or enums.
3838
With only the ID stored, the invalidation due to objects being modified will also dissolve.
3939

40-
Make sure to match this with a Token identifier with ``id`` keys:
40+
Make sure to match this with a Token identifier with ``key``/``id`` keys::
4141

4242
$service->loadIdentifier('Authentication.Token', [
43-
'tokenField' => 'id',
44-
'dataField' => 'id',
43+
'tokenField' => 'id', // lookup for DB table
44+
'dataField' => 'key', // incoming data
4545
'resolver' => 'Authentication.Orm',
4646
]);
4747

48+
$service->loadAuthenticator('Authentication.PrimaryKeySession', [
49+
'urlChecker' => 'Authentication.CakeRouter',
50+
'loginUrl' => [
51+
'prefix' => false,
52+
'plugin' => false,
53+
'controller' => 'Users',
54+
'action' => 'login',
55+
],
56+
]);
57+
4858
Form
4959
====
5060

docs/en/url-checkers.rst

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ URL Checkers
22
############
33

44
To provide an abstract and framework agnostic solution there are URL
5-
checkers implemented that allow you to customize the comparision of the
5+
checkers implemented that allow you to customize the comparison of the
66
current URL if needed. For example to another frameworks routing.
77

88
Included Checkers
@@ -24,11 +24,24 @@ Options:
2424
CakeRouterUrlChecker
2525
--------------------
2626

27-
Options:
28-
2927
Use this checker if you want to use the array notation of CakePHPs
3028
routing system. The checker also works with named routes.
3129

30+
$service->loadAuthenticator('Authentication.Form', [
31+
'urlChecker' => 'Authentication.CakeRouter',
32+
'fields' => [
33+
AbstractIdentifier::CREDENTIAL_USERNAME => 'email',
34+
AbstractIdentifier::CREDENTIAL_PASSWORD => 'password',
35+
],
36+
'loginUrl' => [
37+
'prefix' => false,
38+
'plugin' => false,
39+
'controller' => 'Users',
40+
'action' => 'login',
41+
],
42+
]);
43+
44+
Options:
3245
- **checkFullUrl**: To compare the full URL, including protocol, host
3346
and port or not. Default is ``false``
3447

src/Authenticator/PrimaryKeySessionAuthenticator.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Authentication\Authenticator;
55

66
use ArrayAccess;
7+
use Authentication\Identifier\IdentifierInterface;
78
use Cake\Http\Exception\UnauthorizedException;
89
use Psr\Http\Message\ResponseInterface;
910
use Psr\Http\Message\ServerRequestInterface;
@@ -13,6 +14,20 @@
1314
*/
1415
class PrimaryKeySessionAuthenticator extends SessionAuthenticator
1516
{
17+
/**
18+
* @param \Authentication\Identifier\IdentifierInterface $identifier
19+
* @param array<string, mixed> $config
20+
*/
21+
public function __construct(IdentifierInterface $identifier, array $config = [])
22+
{
23+
$config += [
24+
'identifierKey' => 'key',
25+
'idField' => 'id',
26+
];
27+
28+
parent::__construct($identifier, $config);
29+
}
30+
1631
/**
1732
* Authenticate a user using session data.
1833
*
@@ -30,7 +45,7 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
3045
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND);
3146
}
3247

33-
$user = $this->_identifier->identify(['id' => $userId]);
48+
$user = $this->_identifier->identify([$this->getConfig('identifierKey') => $userId]);
3449
if (!$user) {
3550
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND);
3651
}
@@ -49,7 +64,7 @@ public function persistIdentity(ServerRequestInterface $request, ResponseInterfa
4964

5065
if (!$session->check($sessionKey)) {
5166
$session->renew();
52-
$session->write($sessionKey, $identity['id']);
67+
$session->write($sessionKey, $identity[$this->getConfig('idField')]);
5368
}
5469

5570
return [
@@ -83,8 +98,8 @@ public function impersonate(
8398
'Stop the current impersonation before impersonating another user.',
8499
);
85100
}
86-
$session->write($impersonateSessionKey, $impersonator['id']);
87-
$session->write($sessionKey, $impersonated['id']);
101+
$session->write($impersonateSessionKey, $impersonator[$this->getConfig('idField')]);
102+
$session->write($sessionKey, $impersonated[$this->getConfig('idField')]);
88103
$this->setConfig('identify', true);
89104

90105
return [

tests/TestCase/Authenticator/PrimaryKeySessionAuthenticatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function testAuthenticateSuccess()
8484
$this->identifiers = new IdentifierCollection([
8585
'Authentication.Token' => [
8686
'tokenField' => 'id',
87-
'dataField' => 'id',
87+
'dataField' => 'key',
8888
'resolver' => [
8989
'className' => 'Authentication.Orm',
9090
'userModel' => 'AuthUsers',
@@ -121,7 +121,7 @@ public function testAuthenticateSuccessCustomFinder()
121121
$this->identifiers = new IdentifierCollection([
122122
'Authentication.Token' => [
123123
'tokenField' => 'id',
124-
'dataField' => 'id',
124+
'dataField' => 'key',
125125
'resolver' => [
126126
'className' => 'Authentication.Orm',
127127
'userModel' => 'AuthUsers',

0 commit comments

Comments
 (0)