1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2017 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Jose\Component\Checker; |
15
|
|
|
|
16
|
|
|
use Jose\Component\Core\JWT; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class HeaderCheckerManager. |
20
|
|
|
*/ |
21
|
|
|
final class HeaderCheckerManager |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var HeaderChecker[] |
25
|
|
|
*/ |
26
|
|
|
private $checkers = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var TokenTypeSupport[] |
30
|
|
|
*/ |
31
|
|
|
private $tokenTypes = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* HeaderCheckerManager constructor. |
35
|
|
|
* |
36
|
|
|
* @param HeaderChecker[] $checkers |
37
|
|
|
* @param TokenTypeSupport[] $tokenTypes |
38
|
|
|
*/ |
39
|
|
|
private function __construct(array $checkers, array $tokenTypes) |
40
|
|
|
{ |
41
|
|
|
foreach ($checkers as $checker) { |
42
|
|
|
$this->add($checker); |
43
|
|
|
} |
44
|
|
|
foreach ($tokenTypes as $tokenType) { |
45
|
|
|
$this->addTokenTypeSupport($tokenType); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param HeaderChecker[] $checkers |
51
|
|
|
* @param TokenTypeSupport[] $tokenTypes |
52
|
|
|
* |
53
|
|
|
* @return HeaderCheckerManager |
54
|
|
|
*/ |
55
|
|
|
public static function create(array $checkers, array $tokenTypes): self |
56
|
|
|
{ |
57
|
|
|
return new self($checkers, $tokenTypes); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return HeaderChecker[] |
62
|
|
|
*/ |
63
|
|
|
public function getCheckers(): array |
64
|
|
|
{ |
65
|
|
|
return $this->checkers; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param TokenTypeSupport $tokenType |
70
|
|
|
* |
71
|
|
|
* @return HeaderCheckerManager |
72
|
|
|
*/ |
73
|
|
|
private function addTokenTypeSupport(TokenTypeSupport $tokenType): self |
74
|
|
|
{ |
75
|
|
|
$this->tokenTypes[] = $tokenType; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param HeaderChecker $checker |
82
|
|
|
* |
83
|
|
|
* @return HeaderCheckerManager |
84
|
|
|
*/ |
85
|
|
|
private function add(HeaderChecker $checker): self |
86
|
|
|
{ |
87
|
|
|
$header = $checker->supportedHeader(); |
88
|
|
|
$this->checkers[$header] = $checker; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param JWT $jwt |
95
|
|
|
* @param int $component |
96
|
|
|
*/ |
97
|
|
|
public function check(JWT $jwt, int $component) |
98
|
|
|
{ |
99
|
|
|
foreach ($this->tokenTypes as $tokenType) { |
100
|
|
|
if ($tokenType->supports($jwt)) { |
101
|
|
|
$protected = []; |
102
|
|
|
$unprotected = []; |
103
|
|
|
$tokenType->retrieveTokenHeaders($jwt, $component, $protected, $unprotected); |
104
|
|
|
$this->checkDuplicatedHeaderParameters($protected, $unprotected); |
105
|
|
|
$this->checkHeaders($protected, $unprotected); |
106
|
|
|
|
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
throw new \InvalidArgumentException('Unsupported token type.'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param array $header1 |
116
|
|
|
* @param array $header2 |
117
|
|
|
*/ |
118
|
|
|
private function checkDuplicatedHeaderParameters(array $header1, array $header2) |
119
|
|
|
{ |
120
|
|
|
$inter = array_intersect_key($header1, $header2); |
121
|
|
|
if (!empty($inter)) { |
122
|
|
|
throw new \InvalidArgumentException(sprintf('The header contains duplicated entries: %s.', implode(', ', array_keys($inter)))); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param array $protected |
128
|
|
|
* @param array $headers |
129
|
|
|
*/ |
130
|
|
|
private function checkHeaders(array $protected, array $headers) |
131
|
|
|
{ |
132
|
|
|
$checkedHeaders = []; |
133
|
|
|
foreach ($this->checkers as $header => $checker) { |
134
|
|
|
if ($checker->protectedHeaderOnly()) { |
135
|
|
|
if (array_key_exists($header, $protected)) { |
136
|
|
|
$checker->checkHeader($protected[$header]); |
137
|
|
|
$checkedHeaders[] = $header; |
138
|
|
|
} elseif (array_key_exists($header, $headers)) { |
139
|
|
|
throw new \InvalidArgumentException(sprintf('The header "%s" must be protected.', $header)); |
140
|
|
|
} |
141
|
|
|
} else { |
142
|
|
|
if (array_key_exists($header, $protected)) { |
143
|
|
|
$checker->checkHeader($protected[$header]); |
144
|
|
|
$checkedHeaders[] = $header; |
145
|
|
|
} elseif (array_key_exists($header, $headers)) { |
146
|
|
|
$checker->checkHeader($headers[$header]); |
147
|
|
|
$checkedHeaders[] = $header; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
$this->checkCriticalHeader($protected, $headers, $checkedHeaders); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param array $protected |
156
|
|
|
* @param array $headers |
157
|
|
|
* @param array $checkedHeaders |
158
|
|
|
*/ |
159
|
|
|
private function checkCriticalHeader(array $protected, array $headers, array $checkedHeaders) |
160
|
|
|
{ |
161
|
|
|
if (array_key_exists('crit', $protected)) { |
162
|
|
|
if (!is_array($protected['crit'])) { |
163
|
|
|
throw new \InvalidArgumentException('The header "crit" mus be a list of header parameters.'); |
164
|
|
|
} |
165
|
|
|
$diff = array_diff($protected['crit'], $checkedHeaders); |
166
|
|
|
if (!empty($diff)) { |
167
|
|
|
throw new \InvalidArgumentException(sprintf('One or more headers are marked as critical, but they are missing or have not been checked: %s.', implode(', ', array_values($diff)))); |
168
|
|
|
} |
169
|
|
|
} elseif (array_key_exists('crit', $headers)) { |
170
|
|
|
throw new \InvalidArgumentException('The header parameter "crit" must be protected.'); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|