1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT |
5
|
|
|
* @link https://github.com/nicoSWD |
6
|
|
|
* @author Nicolas Oelgart <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
namespace nicoSWD\SecHeaderCheck\Domain\Validator\Header; |
9
|
|
|
|
10
|
|
|
use nicoSWD\SecHeaderCheck\Domain\Result\Result\SetCookieHeaderResult; |
11
|
|
|
use nicoSWD\SecHeaderCheck\Domain\Validator\AbstractHeaderParser; |
12
|
|
|
|
13
|
|
|
final class SetCookieHeader extends AbstractHeaderParser |
14
|
|
|
{ |
15
|
|
|
private const FLAG_SECURE = 'secure'; |
16
|
|
|
private const FLAG_HTTP_ONLY = 'httponly'; |
17
|
|
|
private const FLAG_SAME_SITE_STRICT = 'samesite=strict'; |
18
|
|
|
private const FLAG_SAME_SITE_LAX = 'samesite=lax'; |
19
|
|
|
|
20
|
8 |
|
public function parse(): SetCookieHeaderResult |
21
|
|
|
{ |
22
|
8 |
|
$flags = $this->getCookieFlags(); |
23
|
|
|
|
24
|
8 |
|
return (new SetCookieHeaderResult($this->getName(), $this->getValue())) |
25
|
8 |
|
->setCookieName($this->getCookieName()) |
26
|
8 |
|
->setHasFlagHttpOnly($this->hasHttpOnlyFlag($flags)) |
27
|
8 |
|
->setHasFlagSecure($this->hasSecureFlag($flags)) |
28
|
8 |
|
->setHasFlagSameSite($this->hasSameSiteFlag($flags)); |
29
|
|
|
} |
30
|
|
|
|
31
|
8 |
|
private function hasSecureFlag(array $options): bool |
32
|
|
|
{ |
33
|
8 |
|
return in_array(self::FLAG_SECURE, $options, true); |
34
|
|
|
} |
35
|
|
|
|
36
|
8 |
|
private function hasHttpOnlyFlag(array $options): bool |
37
|
|
|
{ |
38
|
8 |
|
return in_array(self::FLAG_HTTP_ONLY, $options, true); |
39
|
|
|
} |
40
|
|
|
|
41
|
8 |
|
private function hasSameSiteFlag(array $options): bool |
42
|
|
|
{ |
43
|
|
|
return |
44
|
8 |
|
in_array(self::FLAG_SAME_SITE_STRICT, $options, true) || |
45
|
8 |
|
in_array(self::FLAG_SAME_SITE_LAX, $options, true); |
46
|
|
|
} |
47
|
|
|
|
48
|
8 |
|
private function getCookieFlags(): array |
49
|
|
|
{ |
50
|
|
|
$callback = function (string $value): string { |
51
|
8 |
|
return strtolower(trim($value)); |
52
|
8 |
|
}; |
53
|
|
|
|
54
|
8 |
|
return array_map($callback, explode(';', $this->getValue())); |
55
|
|
|
} |
56
|
|
|
|
57
|
8 |
|
private function getCookieName(): string |
58
|
|
|
{ |
59
|
8 |
|
parse_str(explode(';', $this->getValue(), 2)[0], $components); |
60
|
|
|
|
61
|
8 |
|
return key($components) ?? ''; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|