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\JWTInterface; |
17
|
|
|
use Jose\Component\Encryption\JWE; |
18
|
|
|
use Jose\Component\Signature\JWS; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class HeaderCheckerManager. |
22
|
|
|
*/ |
23
|
|
|
final class HeaderCheckerManager |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var HeaderCheckerInterface[] |
27
|
|
|
*/ |
28
|
|
|
private $checkers = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param HeaderCheckerInterface $checker |
32
|
|
|
*/ |
33
|
|
|
public function add(HeaderCheckerInterface $checker) |
34
|
|
|
{ |
35
|
|
|
$this->checkers[$checker->supportedHeader()] = $checker; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param JWTInterface $jwt |
40
|
|
|
* @param int $component |
41
|
|
|
*/ |
42
|
|
|
public function check(JWTInterface $jwt, int $component) |
43
|
|
|
{ |
44
|
|
|
switch (true) { |
45
|
|
|
case $jwt instanceof JWS: |
46
|
|
|
$this->checkJWS($jwt, $component); |
47
|
|
|
|
48
|
|
|
break; |
49
|
|
|
case $jwt instanceof JWE: |
50
|
|
|
$this->checkJWE($jwt, $component); |
51
|
|
|
|
52
|
|
|
break; |
53
|
|
|
default: |
54
|
|
|
throw new \InvalidArgumentException('Unsupported argument'); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param JWS $jws |
60
|
|
|
* @param int $signature |
61
|
|
|
*/ |
62
|
|
|
public function checkJWS(JWS $jws, int $signature) |
63
|
|
|
{ |
64
|
|
|
if ($signature > $jws->countSignatures()) { |
65
|
|
|
throw new \InvalidArgumentException('Unknown signature index.'); |
66
|
|
|
} |
67
|
|
|
$protected = $jws->getSignature($signature)->getProtectedHeaders(); |
68
|
|
|
$headers = $jws->getSignature($signature)->getHeaders(); |
69
|
|
|
$this->checkDuplicatedHeaderParameters($protected, $headers); |
70
|
|
|
$this->checkHeaders($protected, $headers); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param JWE $jwe |
75
|
|
|
* @param int $recipient |
76
|
|
|
*/ |
77
|
|
|
public function checkJWE(JWE $jwe, int $recipient) |
78
|
|
|
{ |
79
|
|
|
if ($recipient > $jwe->countRecipients()) { |
80
|
|
|
throw new \InvalidArgumentException('Unknown recipient index.'); |
81
|
|
|
} |
82
|
|
|
$protected = $jwe->getSharedProtectedHeaders(); |
83
|
|
|
$headers = $jwe->getSharedHeaders(); |
84
|
|
|
$recipient = $jwe->getRecipient($recipient)->getHeaders(); |
85
|
|
|
$this->checkDuplicatedHeaderParameters($protected, $headers); |
86
|
|
|
$this->checkDuplicatedHeaderParameters($protected, $recipient); |
87
|
|
|
$this->checkDuplicatedHeaderParameters($headers, $recipient); |
88
|
|
|
$unprotected = array_merge( |
89
|
|
|
$headers, |
90
|
|
|
$recipient |
91
|
|
|
); |
92
|
|
|
$this->checkHeaders($protected, $unprotected); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param array $header1 |
97
|
|
|
* @param array $header2 |
98
|
|
|
*/ |
99
|
|
|
private function checkDuplicatedHeaderParameters(array $header1, array $header2) |
100
|
|
|
{ |
101
|
|
|
$inter = array_intersect_key($header1, $header2); |
102
|
|
|
if (!empty($inter)) { |
103
|
|
|
throw new \InvalidArgumentException(sprintf('The header contains duplicated entries: %s.', json_encode(array_keys($inter)))); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param array $protected |
109
|
|
|
* @param array $headers |
110
|
|
|
*/ |
111
|
|
|
private function checkHeaders(array $protected, array $headers) |
112
|
|
|
{ |
113
|
|
|
$checkedHeaders = []; |
114
|
|
|
foreach ($this->checkers as $header => $checker) { |
115
|
|
|
if ($checker->protectedHeaderOnly()) { |
116
|
|
|
if (array_key_exists($header, $protected)) { |
117
|
|
|
$checker->checkHeader($protected[$header]); |
118
|
|
|
$checkedHeaders[] = $header; |
119
|
|
|
} else { |
120
|
|
|
throw new \InvalidArgumentException(sprintf('The header "%s" must be protected.', $header)); |
121
|
|
|
} |
122
|
|
|
} else { |
123
|
|
|
if (array_key_exists($header, $protected)) { |
124
|
|
|
$checker->checkHeader($protected[$header]); |
125
|
|
|
$checkedHeaders[] = $header; |
126
|
|
|
} elseif (array_key_exists($header, $headers)) { |
127
|
|
|
$checker->checkHeader($headers[$header]); |
128
|
|
|
$checkedHeaders[] = $header; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (array_key_exists('crit', $protected)) { |
134
|
|
|
if (!is_array($protected['crit'])) { |
135
|
|
|
throw new \InvalidArgumentException('The header "crit" mus be a list of header parameters.'); |
136
|
|
|
} |
137
|
|
|
$diff = array_diff($protected['crit'], $checkedHeaders); |
138
|
|
|
if (!empty($diff)) { |
139
|
|
|
throw new \InvalidArgumentException(sprintf('One or more headers are marked as critical, but they are missing or have not been checked: %s.', json_encode(array_values($diff)))); |
140
|
|
|
} |
141
|
|
|
} elseif (array_key_exists('crit', $headers)) { |
142
|
|
|
throw new \InvalidArgumentException('The header parameter "crit" must be protected.'); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|