1 | <?php |
||
21 | final class JWE implements JWTInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var Recipient[] |
||
25 | */ |
||
26 | private $recipients = []; |
||
27 | |||
28 | /** |
||
29 | * @var string|null |
||
30 | */ |
||
31 | private $ciphertext = null; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $iv; |
||
37 | |||
38 | /** |
||
39 | * @var string|null |
||
40 | */ |
||
41 | private $aad = null; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | private $tag; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | private $sharedHeaders = []; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | private $sharedProtectedHeaders = []; |
||
57 | |||
58 | /** |
||
59 | * @var string|null |
||
60 | */ |
||
61 | private $encodedSharedProtectedHeaders = null; |
||
62 | |||
63 | /** |
||
64 | * @var string|null |
||
65 | */ |
||
66 | private $payload = null; |
||
67 | |||
68 | /** |
||
69 | * JWE constructor. |
||
70 | * |
||
71 | * @param string $ciphertext |
||
72 | * @param string $iv |
||
73 | * @param string $tag |
||
74 | * @param null|string $aad |
||
75 | * @param array $sharedHeaders |
||
76 | * @param array $sharedProtectedHeaders |
||
77 | * @param null|string $encodedSharedProtectedHeaders |
||
78 | * @param array $recipients |
||
79 | */ |
||
80 | private function __construct(string $ciphertext, string $iv, string $tag, ?string $aad = null, array $sharedHeaders = [], array $sharedProtectedHeaders = [], ?string $encodedSharedProtectedHeaders = null, array $recipients = []) |
||
81 | { |
||
82 | $this->ciphertext = $ciphertext; |
||
83 | $this->iv = $iv; |
||
84 | $this->aad = $aad; |
||
85 | $this->tag = $tag; |
||
86 | $this->sharedHeaders = $sharedHeaders; |
||
87 | $this->sharedProtectedHeaders = $sharedProtectedHeaders; |
||
88 | $this->encodedSharedProtectedHeaders = $encodedSharedProtectedHeaders; |
||
89 | $this->recipients = $recipients; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param string $ciphertext |
||
94 | * @param string $iv |
||
95 | * @param string $tag |
||
96 | * @param null|string $aad |
||
97 | * @param array $sharedHeaders |
||
98 | * @param array $sharedProtectedHeaders |
||
99 | * @param null|string $encodedSharedProtectedHeaders |
||
100 | * @param array $recipients |
||
101 | * |
||
102 | * @return JWE |
||
103 | */ |
||
104 | public static function create(string $ciphertext, string $iv, string $tag, ?string $aad = null, array $sharedHeaders = [], array $sharedProtectedHeaders = [], ?string $encodedSharedProtectedHeaders = null, array $recipients = []): JWE |
||
105 | { |
||
106 | return new self($ciphertext, $iv, $tag, $aad, $sharedHeaders, $sharedProtectedHeaders, $encodedSharedProtectedHeaders, $recipients); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function getPayload(): ?string |
||
113 | { |
||
114 | return $this->payload; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param string $payload |
||
119 | * |
||
120 | * @return JWE |
||
121 | */ |
||
122 | public function withPayload(string $payload): JWE |
||
123 | { |
||
124 | $clone = clone $this; |
||
125 | $clone->payload = $payload; |
||
126 | |||
127 | return $clone; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Returns the number of recipients associated with the JWS. |
||
132 | * |
||
133 | * @return int |
||
134 | */ |
||
135 | public function countRecipients(): int |
||
136 | { |
||
137 | return count($this->recipients); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function isEncrypted(): bool |
||
144 | { |
||
145 | return null !== $this->getCiphertext(); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Returns the recipients associated with the JWS. |
||
150 | * |
||
151 | * @return Recipient[] |
||
152 | */ |
||
153 | public function getRecipients(): array |
||
154 | { |
||
155 | return $this->recipients; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param int $id |
||
160 | * |
||
161 | * @return Recipient |
||
162 | */ |
||
163 | public function getRecipient(int $id): Recipient |
||
164 | { |
||
165 | if (!array_key_exists($id, $this->recipients)) { |
||
166 | throw new \InvalidArgumentException('The recipient does not exist.'); |
||
167 | } |
||
168 | |||
169 | return $this->recipients[$id]; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return string|null The cyphertext |
||
174 | */ |
||
175 | public function getCiphertext(): ?string |
||
179 | |||
180 | /** |
||
181 | * @return string|null |
||
182 | */ |
||
183 | public function getAAD(): ?string |
||
187 | |||
188 | /** |
||
189 | * @return string|null |
||
190 | */ |
||
191 | public function getIV(): ?string |
||
195 | |||
196 | /** |
||
197 | * @return string|null |
||
198 | */ |
||
199 | public function getTag(): ?string |
||
203 | |||
204 | /** |
||
205 | * @return string |
||
206 | */ |
||
207 | public function getEncodedSharedProtectedHeaders(): string |
||
211 | |||
212 | /** |
||
213 | * @return array |
||
214 | */ |
||
215 | public function getSharedProtectedHeaders(): array |
||
219 | |||
220 | /** |
||
221 | * @param string $key The key |
||
222 | * |
||
223 | * @return mixed|null Header value |
||
224 | */ |
||
225 | public function getSharedProtectedHeader(string $key) |
||
226 | { |
||
227 | if ($this->hasSharedProtectedHeader($key)) { |
||
228 | return $this->sharedProtectedHeaders[$key]; |
||
229 | } |
||
230 | |||
231 | throw new \InvalidArgumentException(sprintf('The shared protected header "%s" does not exist.', $key)); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @param string $key The key |
||
236 | * |
||
237 | * @return bool |
||
238 | */ |
||
239 | public function hasSharedProtectedHeader(string $key): bool |
||
243 | |||
244 | /** |
||
245 | * @return array |
||
246 | */ |
||
247 | public function getSharedHeaders(): array |
||
251 | |||
252 | /** |
||
253 | * @param string $key The key |
||
254 | * |
||
255 | * @return mixed|null Header value |
||
256 | */ |
||
257 | public function getSharedHeader(string $key) |
||
258 | { |
||
259 | if ($this->hasSharedHeader($key)) { |
||
260 | return $this->sharedHeaders[$key]; |
||
265 | |||
266 | /** |
||
267 | * @param string $key The key |
||
268 | * |
||
269 | * @return bool |
||
270 | */ |
||
271 | public function hasSharedHeader(string $key): bool |
||
275 | } |
||
276 |