1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author SignpostMarv |
4
|
|
|
*/ |
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace SignpostMarv\DaftFramework\Http; |
8
|
|
|
|
9
|
|
|
use SignpostMarv\DaftFramework\Framework; |
10
|
|
|
use SignpostMarv\DaftRouter\DaftMiddleware; |
11
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
|
15
|
|
|
class CookieMiddleware implements DaftMiddleware |
16
|
|
|
{ |
17
|
18 |
|
public static function DaftRouterMiddlewareHandler( |
18
|
|
|
Request $request, |
19
|
|
|
? Response $response |
20
|
|
|
) : ? Response { |
21
|
18 |
|
$config = Framework::ObtainFrameworkForRequest($request)->ObtainConfig(); |
22
|
18 |
|
if (isset($response, $config[self::class])) { |
23
|
16 |
|
$config = (array) $config[self::class]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string|null $sameSite |
27
|
|
|
*/ |
28
|
16 |
|
$sameSite = $config['sameSite'] ?? null; |
29
|
16 |
|
$sameSite = is_string($sameSite) ? $sameSite : null; |
30
|
16 |
|
$isSecure = (bool) ($config['secure'] ?? null); |
31
|
16 |
|
$isHttpOnly = (bool) ($config['httpOnly'] ?? null); |
32
|
|
|
|
33
|
16 |
|
self::PerhapsReconfigureResponseCookies($response, $isSecure, $isHttpOnly, $sameSite); |
34
|
|
|
} |
35
|
|
|
|
36
|
18 |
|
return $response; |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
public static function DaftRouterRoutePrefixExceptions() : array |
40
|
|
|
{ |
41
|
2 |
|
return []; |
42
|
|
|
} |
43
|
|
|
|
44
|
16 |
|
public static function PerhapsReconfigureResponseCookies( |
45
|
|
|
Response $response, |
46
|
|
|
bool $configSecure, |
47
|
|
|
bool $configHttpOnly, |
48
|
|
|
? string $configSameSite |
49
|
|
|
) : void { |
50
|
|
|
/** |
51
|
|
|
* @var Cookie $cookie |
52
|
|
|
*/ |
53
|
16 |
|
foreach ($response->headers->getCookies() as $cookie) { |
54
|
16 |
|
self::PerhapsReconfigureCookie( |
55
|
16 |
|
$response, |
56
|
16 |
|
$cookie, |
57
|
16 |
|
$configSecure, |
58
|
16 |
|
$configHttpOnly, |
59
|
16 |
|
$configSameSite |
60
|
|
|
); |
61
|
|
|
} |
62
|
16 |
|
} |
63
|
|
|
|
64
|
16 |
|
public static function PerhapsReconfigureCookie( |
65
|
|
|
Response $response, |
66
|
|
|
Cookie $cookie, |
67
|
|
|
bool $isSecure, |
68
|
|
|
bool $isHttpOnly, |
69
|
|
|
? string $sameSite |
70
|
|
|
) : void { |
71
|
16 |
|
$updateSecure = $cookie->isSecure() !== $isSecure; |
72
|
16 |
|
$updateHttpOnly = $cookie->isHttpOnly() !== $isHttpOnly; |
73
|
16 |
|
$updateSameSite = $cookie->getSameSite() !== $sameSite; |
74
|
|
|
|
75
|
16 |
|
if ($updateSecure || $updateHttpOnly || $updateSameSite) { |
76
|
16 |
|
static::ReconfigureCookie($response, $cookie, $isSecure, $isHttpOnly, $sameSite); |
77
|
|
|
} |
78
|
16 |
|
} |
79
|
|
|
|
80
|
16 |
|
public static function ReconfigureCookie( |
81
|
|
|
Response $response, |
82
|
|
|
Cookie $cookie, |
83
|
|
|
bool $configSecure, |
84
|
|
|
bool $configHttpOnly, |
85
|
|
|
? string $configSameSite |
86
|
|
|
) : void { |
87
|
16 |
|
$cookieName = $cookie->getName(); |
88
|
16 |
|
$cookiePath = $cookie->getPath(); |
89
|
16 |
|
$cookieDomain = $cookie->getDomain(); |
90
|
16 |
|
$response->headers->removeCookie($cookieName, $cookiePath, $cookieDomain); |
91
|
16 |
|
$response->headers->setCookie(new Cookie( |
92
|
16 |
|
$cookieName, |
93
|
16 |
|
$cookie->getValue(), |
94
|
16 |
|
$cookie->getExpiresTime(), |
95
|
16 |
|
$cookiePath, |
96
|
16 |
|
$cookieDomain, |
97
|
16 |
|
$configSecure, |
98
|
16 |
|
$configHttpOnly, |
99
|
16 |
|
$cookie->isRaw(), |
100
|
16 |
|
$configSameSite |
101
|
|
|
)); |
102
|
16 |
|
} |
103
|
|
|
} |
104
|
|
|
|