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