|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace TinyAuth\Authenticator; |
| 5 | + |
| 6 | +use ArrayAccess; |
| 7 | +use Authentication\Authenticator\Result; |
| 8 | +use Authentication\Authenticator\ResultInterface; |
| 9 | +use Authentication\Authenticator\SessionAuthenticator as AuthenticationSessionAuthenticator; |
| 10 | +use Authentication\Identifier\IdentifierInterface; |
| 11 | +use Cake\Http\Exception\UnauthorizedException; |
| 12 | +use Psr\Http\Message\ResponseInterface; |
| 13 | +use Psr\Http\Message\ServerRequestInterface; |
| 14 | + |
| 15 | +/** |
| 16 | + * Session Authenticator with only ID |
| 17 | + */ |
| 18 | +class PrimaryKeySessionAuthenticator extends AuthenticationSessionAuthenticator { |
| 19 | + |
| 20 | + /** |
| 21 | + * @param \Authentication\Identifier\IdentifierInterface $identifier |
| 22 | + * @param array<string, mixed> $config |
| 23 | + */ |
| 24 | + public function __construct(IdentifierInterface $identifier, array $config = []) { |
| 25 | + $config += [ |
| 26 | + 'identifierKey' => 'key', |
| 27 | + 'idField' => 'id', |
| 28 | + ]; |
| 29 | + |
| 30 | + parent::__construct($identifier, $config); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Authenticate a user using session data. |
| 35 | + * |
| 36 | + * @param \Psr\Http\Message\ServerRequestInterface $request The request to authenticate with. |
| 37 | + * @return \Authentication\Authenticator\ResultInterface |
| 38 | + */ |
| 39 | + public function authenticate(ServerRequestInterface $request): ResultInterface { |
| 40 | + $sessionKey = $this->getConfig('sessionKey'); |
| 41 | + /** @var \Cake\Http\Session $session */ |
| 42 | + $session = $request->getAttribute('session'); |
| 43 | + |
| 44 | + $userId = $session->read($sessionKey); |
| 45 | + if (!$userId) { |
| 46 | + return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND); |
| 47 | + } |
| 48 | + |
| 49 | + $user = $this->_identifier->identify([$this->getConfig('identifierKey') => $userId]); |
| 50 | + if (!$user) { |
| 51 | + return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND); |
| 52 | + } |
| 53 | + |
| 54 | + return new Result($user, Result::SUCCESS); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @inheritDoc |
| 59 | + */ |
| 60 | + public function persistIdentity(ServerRequestInterface $request, ResponseInterface $response, $identity): array { |
| 61 | + $sessionKey = $this->getConfig('sessionKey'); |
| 62 | + /** @var \Cake\Http\Session $session */ |
| 63 | + $session = $request->getAttribute('session'); |
| 64 | + |
| 65 | + if (!$session->check($sessionKey)) { |
| 66 | + $session->renew(); |
| 67 | + $session->write($sessionKey, $identity[$this->getConfig('idField')]); |
| 68 | + } |
| 69 | + |
| 70 | + return [ |
| 71 | + 'request' => $request, |
| 72 | + 'response' => $response, |
| 73 | + ]; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Impersonates a user |
| 78 | + * |
| 79 | + * @param \Psr\Http\Message\ServerRequestInterface $request The request |
| 80 | + * @param \Psr\Http\Message\ResponseInterface $response The response |
| 81 | + * @param \ArrayAccess $impersonator User who impersonates |
| 82 | + * @param \ArrayAccess $impersonated User impersonated |
| 83 | + * @return array |
| 84 | + */ |
| 85 | + public function impersonate( |
| 86 | + ServerRequestInterface $request, |
| 87 | + ResponseInterface $response, |
| 88 | + ArrayAccess $impersonator, |
| 89 | + ArrayAccess $impersonated, |
| 90 | + ): array { |
| 91 | + $sessionKey = $this->getConfig('sessionKey'); |
| 92 | + $impersonateSessionKey = $this->getConfig('impersonateSessionKey'); |
| 93 | + /** @var \Cake\Http\Session $session */ |
| 94 | + $session = $request->getAttribute('session'); |
| 95 | + if ($session->check($impersonateSessionKey)) { |
| 96 | + throw new UnauthorizedException( |
| 97 | + 'You are impersonating a user already. ' . |
| 98 | + 'Stop the current impersonation before impersonating another user.', |
| 99 | + ); |
| 100 | + } |
| 101 | + $session->write($impersonateSessionKey, $impersonator[$this->getConfig('idField')]); |
| 102 | + $session->write($sessionKey, $impersonated[$this->getConfig('idField')]); |
| 103 | + $this->setConfig('identify', true); |
| 104 | + |
| 105 | + return [ |
| 106 | + 'request' => $request, |
| 107 | + 'response' => $response, |
| 108 | + ]; |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments