|
4 | 4 | namespace Authentication\Authenticator; |
5 | 5 |
|
6 | 6 | use ArrayAccess; |
| 7 | +use Authentication\Identifier\IdentifierFactory; |
7 | 8 | use Authentication\Identifier\IdentifierInterface; |
8 | 9 | use Cake\Http\Exception\UnauthorizedException; |
9 | 10 | use Psr\Http\Message\ResponseInterface; |
10 | 11 | use Psr\Http\Message\ServerRequestInterface; |
11 | 12 |
|
12 | 13 | /** |
13 | 14 | * Session Authenticator with only ID |
| 15 | + * |
| 16 | + * This authenticator stores only the user's primary key in the session, |
| 17 | + * and looks up the full user record from the database on each request. |
| 18 | + * |
| 19 | + * By default, it uses a TokenIdentifier configured to look up users by |
| 20 | + * their `id` field. This works out of the box for most applications: |
| 21 | + * |
| 22 | + * ```php |
| 23 | + * $service->loadAuthenticator('Authentication.PrimaryKeySession'); |
| 24 | + * ``` |
| 25 | + * |
| 26 | + * You can customize the identifier configuration if needed: |
| 27 | + * |
| 28 | + * ```php |
| 29 | + * $service->loadAuthenticator('Authentication.PrimaryKeySession', [ |
| 30 | + * 'identifier' => [ |
| 31 | + * 'className' => 'Authentication.Token', |
| 32 | + * 'tokenField' => 'uuid', |
| 33 | + * 'dataField' => 'key', |
| 34 | + * 'resolver' => [ |
| 35 | + * 'className' => 'Authentication.Orm', |
| 36 | + * 'userModel' => 'Members', |
| 37 | + * ], |
| 38 | + * ], |
| 39 | + * ]); |
| 40 | + * ``` |
14 | 41 | */ |
15 | 42 | class PrimaryKeySessionAuthenticator extends SessionAuthenticator |
16 | 43 | { |
17 | 44 | /** |
18 | | - * @param \Authentication\Identifier\IdentifierInterface|null $identifier |
19 | | - * @param array<string, mixed> $config |
| 45 | + * Default config for this object. |
| 46 | + * |
| 47 | + * - `identifierKey` The key used when passing the ID to the identifier. |
| 48 | + * - `idField` The field on the user entity that contains the primary key. |
| 49 | + * |
| 50 | + * @var array<string, mixed> |
| 51 | + */ |
| 52 | + protected array $_defaultConfig = [ |
| 53 | + 'fields' => [], |
| 54 | + 'sessionKey' => 'Auth', |
| 55 | + 'impersonateSessionKey' => 'AuthImpersonate', |
| 56 | + 'identify' => false, |
| 57 | + 'identityAttribute' => 'identity', |
| 58 | + 'identifierKey' => 'key', |
| 59 | + 'idField' => 'id', |
| 60 | + ]; |
| 61 | + |
| 62 | + /** |
| 63 | + * Constructor |
| 64 | + * |
| 65 | + * Bypasses SessionAuthenticator's default PasswordIdentifier creation |
| 66 | + * to allow lazy initialization of the TokenIdentifier in getIdentifier(). |
| 67 | + * |
| 68 | + * @param \Authentication\Identifier\IdentifierInterface|null $identifier Identifier instance. |
| 69 | + * @param array<string, mixed> $config Configuration settings. |
20 | 70 | */ |
21 | 71 | public function __construct(?IdentifierInterface $identifier, array $config = []) |
22 | 72 | { |
23 | | - $config += [ |
24 | | - 'identifierKey' => 'key', |
25 | | - 'idField' => 'id', |
26 | | - ]; |
| 73 | + $this->_identifier = $identifier; |
| 74 | + $this->setConfig($config); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Gets the identifier. |
| 79 | + * |
| 80 | + * If no identifier was explicitly configured, creates a default TokenIdentifier |
| 81 | + * configured to look up users by their primary key (`id` field). |
| 82 | + * |
| 83 | + * @return \Authentication\Identifier\IdentifierInterface |
| 84 | + */ |
| 85 | + public function getIdentifier(): IdentifierInterface |
| 86 | + { |
| 87 | + if ($this->_identifier === null) { |
| 88 | + $this->_identifier = IdentifierFactory::create([ |
| 89 | + 'className' => 'Authentication.Token', |
| 90 | + 'tokenField' => $this->getConfig('idField'), |
| 91 | + 'dataField' => $this->getConfig('identifierKey'), |
| 92 | + ]); |
| 93 | + } |
27 | 94 |
|
28 | | - parent::__construct($identifier, $config); |
| 95 | + return $this->_identifier; |
29 | 96 | } |
30 | 97 |
|
31 | 98 | /** |
|
0 commit comments