| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Facile\OpenIDClient\Middleware; |
| 6 |
|
| 7 |
use Dflydev\FigCookies\Cookies; |
| 8 |
use Dflydev\FigCookies\FigResponseCookies; |
| 9 |
use Dflydev\FigCookies\Modifier\SameSite; |
| 10 |
use Dflydev\FigCookies\SetCookie; |
| 11 |
use Facile\OpenIDClient\Exception\LogicException; |
| 12 |
use Facile\OpenIDClient\Exception\RuntimeException; |
| 13 |
use Facile\OpenIDClient\Session\AuthSession; |
| 14 |
use Facile\OpenIDClient\Session\AuthSessionInterface; |
| 15 |
use Psr\Http\Message\ResponseInterface; |
| 16 |
use Psr\Http\Message\ServerRequestInterface; |
| 17 |
use Psr\Http\Server\MiddlewareInterface; |
| 18 |
use Psr\Http\Server\RequestHandlerInterface; |
| 19 |
use Psr\SimpleCache\CacheInterface; |
| 20 |
use Override; |
| 21 |
|
| 22 |
use function bin2hex; |
| 23 |
use function class_exists; |
| 24 |
use function is_array; |
| 25 |
use function json_decode; |
| 26 |
use function json_encode; |
| 27 |
use function random_bytes; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
final class SessionCookieMiddleware implements MiddlewareInterface |
| 35 |
{ |
| 36 |
public const SESSION_ATTRIBUTE = AuthSessionInterface::class; |
| 37 |
|
| 38 |
private string $cookieName; |
| 39 |
|
| 40 |
private int $ttl; |
| 41 |
|
| 42 |
private CacheInterface $cache; |
| 43 |
|
| 44 |
public function __construct(CacheInterface $cache, string $cookieName = 'openid', int $ttl = 300) |
| 45 |
{ |
| 46 |
$this->cache = $cache; |
| 47 |
$this->cookieName = $cookieName; |
| 48 |
$this->ttl = $ttl; |
| 49 |
} |
| 50 |
|
| 51 |
#[Override] |
| 52 |
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 53 |
{ |
| 54 |
if (! class_exists(Cookies::class)) { |
| 55 |
throw new LogicException('To use the SessionCookieMiddleware you should install dflydev/fig-cookies package'); |
| 56 |
} |
| 57 |
|
| 58 |
$cookies = Cookies::fromRequest($request); |
| 59 |
$sessionCookie = $cookies->get($this->cookieName); |
| 60 |
|
| 61 |
$sessionId = null !== $sessionCookie ? $sessionCookie->getValue() : null; |
| 62 |
|
| 63 |
$sessionValue = null !== $sessionId ? $this->cache->get($sessionId) : null; |
| 64 |
|
| 65 |
$data = null !== $sessionValue ? json_decode($sessionValue, true, 512, JSON_THROW_ON_ERROR) : []; |
| 66 |
|
| 67 |
if (! is_array($data)) { |
| 68 |
$data = []; |
| 69 |
} |
| 70 |
|
| 71 |
$authSession = AuthSession::fromArray($data); |
| 72 |
|
| 73 |
$response = $handler->handle($request->withAttribute(self::SESSION_ATTRIBUTE, $authSession)); |
| 74 |
|
| 75 |
$sessionId ??= bin2hex(random_bytes(32)); |
| 76 |
|
| 77 |
$sessionValue = json_encode($authSession, JSON_THROW_ON_ERROR); |
| 78 |
|
| 79 |
if (false === $this->cache->set($sessionId, $sessionValue, $this->ttl)) { |
| 80 |
throw new RuntimeException('Unable to save session'); |
| 81 |
} |
| 82 |
|
| 83 |
$sessionCookie = SetCookie::create($this->cookieName) |
| 84 |
->withValue($sessionId) |
| 85 |
->withMaxAge($this->ttl) |
| 86 |
->withHttpOnly() |
| 87 |
->withPath('/') |
| 88 |
->withSameSite(SameSite::strict()); |
| 89 |
|
| 90 |
$response = FigResponseCookies::set($response, $sessionCookie); |
| 91 |
|
| 92 |
return $response; |
| 93 |
} |
| 94 |
} |