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 |
|
if ( ! is_null($response)) { |
22
|
18 |
|
$framework = Framework::ObtainFrameworkForRequest($request); |
23
|
|
|
|
24
|
18 |
|
$config = $framework->ObtainConfig(); |
25
|
|
|
|
26
|
18 |
|
if ( ! isset($config[self::class])) { |
27
|
2 |
|
return $response; |
28
|
|
|
} |
29
|
|
|
|
30
|
16 |
|
$config = $config[self::class]; |
31
|
|
|
|
32
|
16 |
|
$configSecure = (bool) ($config['secure'] ?? null); |
33
|
16 |
|
$configHttpOnly = (bool) ($config['httpOnly'] ?? null); |
34
|
16 |
|
$configSameSite = is_string($config['sameSite'] ?? null) ? $config['sameSite'] : null; |
35
|
|
|
|
36
|
16 |
|
self::PerhapsReconfigureResponseCookies( |
37
|
16 |
|
$response, |
38
|
16 |
|
$configSecure, |
39
|
16 |
|
$configHttpOnly, |
40
|
16 |
|
$configSameSite |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
18 |
|
return $response; |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
public static function DaftRouterRoutePrefixExceptions() : array |
48
|
|
|
{ |
49
|
2 |
|
return []; |
50
|
|
|
} |
51
|
|
|
|
52
|
16 |
|
public static function PerhapsReconfigureResponseCookies( |
53
|
|
|
Response $response, |
54
|
|
|
bool $configSecure, |
55
|
|
|
bool $configHttpOnly, |
56
|
|
|
? string $configSameSite |
57
|
|
|
) : void { |
58
|
16 |
|
foreach ($response->headers->getCookies() as $cookie) { |
59
|
16 |
|
self::PerhapsReconfigureCookie( |
60
|
16 |
|
$response, |
61
|
16 |
|
$cookie, |
62
|
16 |
|
$configSecure, |
63
|
16 |
|
$configHttpOnly, |
64
|
16 |
|
$configSameSite |
65
|
|
|
); |
66
|
|
|
} |
67
|
16 |
|
} |
68
|
|
|
|
69
|
16 |
|
public static function PerhapsReconfigureCookie( |
70
|
|
|
Response $response, |
71
|
|
|
Cookie $cookie, |
72
|
|
|
bool $configSecure, |
73
|
|
|
bool $configHttpOnly, |
74
|
|
|
? string $configSameSite |
75
|
|
|
) : void { |
76
|
16 |
|
$updateSecure = $cookie->isSecure() !== $configSecure; |
77
|
16 |
|
$updateHttpOnly = $cookie->isHttpOnly() !== $configHttpOnly; |
78
|
16 |
|
$updateSameSite = $cookie->getSameSite() !== $configSameSite; |
79
|
|
|
|
80
|
16 |
|
if ($updateSecure || $updateHttpOnly || $updateSameSite) { |
81
|
16 |
|
$response->headers->removeCookie( |
82
|
16 |
|
$cookie->getName(), |
83
|
16 |
|
$cookie->getPath(), |
84
|
16 |
|
$cookie->getDomain() |
85
|
|
|
); |
86
|
16 |
|
$response->headers->setCookie(new Cookie( |
87
|
16 |
|
$cookie->getName(), |
88
|
16 |
|
$cookie->getValue(), |
89
|
16 |
|
$cookie->getExpiresTime(), |
90
|
16 |
|
$cookie->getPath(), |
91
|
16 |
|
$cookie->getDomain(), |
92
|
16 |
|
$configSecure, |
93
|
16 |
|
$configHttpOnly, |
94
|
16 |
|
$cookie->isRaw(), |
95
|
16 |
|
$configSameSite |
96
|
|
|
)); |
97
|
|
|
} |
98
|
16 |
|
} |
99
|
|
|
} |
100
|
|
|
|