|
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
|
16 |
|
public static function DaftRouterMiddlewareHandler( |
|
18
|
|
|
Request $request, |
|
19
|
|
|
? Response $response |
|
20
|
|
|
) : ? Response { |
|
21
|
16 |
|
if ( ! is_null($response)) { |
|
22
|
16 |
|
$framework = Framework::ObtainFrameworkForRequest($request); |
|
23
|
|
|
|
|
24
|
16 |
|
$config = $framework->ObtainConfig(); |
|
25
|
|
|
|
|
26
|
16 |
|
if ( ! isset($config[self::class])) { |
|
27
|
|
|
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 |
|
foreach ($response->headers->getCookies() as $cookie) { |
|
37
|
16 |
|
self::PerhapsReconfigureCookie( |
|
38
|
16 |
|
$response, |
|
39
|
16 |
|
$cookie, |
|
40
|
16 |
|
$configSecure, |
|
41
|
16 |
|
$configHttpOnly, |
|
42
|
16 |
|
$configSameSite |
|
|
|
|
|
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
16 |
|
return $response; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
public static function DaftRouterRoutePrefixExceptions() : array |
|
51
|
|
|
{ |
|
52
|
2 |
|
return []; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
16 |
|
public static function PerhapsReconfigureCookie( |
|
56
|
|
|
Response $response, |
|
57
|
|
|
Cookie $cookie, |
|
58
|
|
|
bool $configSecure, |
|
59
|
|
|
bool $configHttpOnly, |
|
60
|
|
|
string $configSameSite |
|
61
|
|
|
) : void { |
|
62
|
16 |
|
$updateSecure = $cookie->isSecure() !== $configSecure; |
|
63
|
16 |
|
$updateHttpOnly = $cookie->isHttpOnly() !== $configHttpOnly; |
|
64
|
16 |
|
$updateSameSite = $cookie->getSameSite() !== $configSameSite; |
|
65
|
|
|
|
|
66
|
16 |
|
if ($updateSecure || $updateHttpOnly || $updateSameSite) { |
|
67
|
16 |
|
$response->headers->removeCookie( |
|
68
|
16 |
|
$cookie->getName(), |
|
69
|
16 |
|
$cookie->getPath(), |
|
70
|
16 |
|
$cookie->getDomain() |
|
71
|
|
|
); |
|
72
|
16 |
|
$response->headers->setCookie(new Cookie( |
|
73
|
16 |
|
$cookie->getName(), |
|
74
|
16 |
|
$cookie->getValue(), |
|
75
|
16 |
|
$cookie->getExpiresTime(), |
|
76
|
16 |
|
$cookie->getPath(), |
|
77
|
16 |
|
$cookie->getDomain(), |
|
78
|
16 |
|
$configSecure, |
|
79
|
16 |
|
$configHttpOnly, |
|
80
|
16 |
|
$cookie->isRaw(), |
|
81
|
16 |
|
$configSameSite |
|
82
|
|
|
)); |
|
83
|
|
|
} |
|
84
|
16 |
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|