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