Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/Encryption/JWE.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jose\Component\Encryption;
6
7 use InvalidArgumentException;
8 use Jose\Component\Core\JWT;
9 use function array_key_exists;
10 use function count;
11
12 class JWE implements JWT
13 {
14 private ?string $payload = null;
15
16 public function __construct(
17 private readonly ?string $ciphertext,
18 private readonly string $iv,
19 private readonly string $tag,
20 private readonly ?string $aad = null,
21 private readonly array $sharedHeader = [],
22 private readonly array $sharedProtectedHeader = [],
23 private readonly ?string $encodedSharedProtectedHeader = null,
24 private readonly array $recipients = []
25 ) {
26 }
27
28 public function getPayload(): ?string
29 {
30 return $this->payload;
31 }
32
33 /**
34 * Set the payload. This method is immutable and a new object will be returned.
35 */
36 public function withPayload(string $payload): self
37 {
38 $clone = clone $this;
39 $clone->payload = $payload;
40
41 return $clone;
42 }
43
44 /**
45 * Returns the number of recipients associated with the JWS.
46 */
47 public function countRecipients(): int
48 {
49 return count($this->recipients);
50 }
51
52 /**
53 * Returns true is the JWE has already been encrypted.
54 */
55 public function isEncrypted(): bool
56 {
57 return $this->getCiphertext() !== null;
58 }
59
60 /**
61 * Returns the recipients associated with the JWS.
62 *
63 * @return Recipient[]
64 */
65 public function getRecipients(): array
66 {
67 return $this->recipients;
68 }
69
70 /**
71 * Returns the recipient object at the given index.
72 */
73 public function getRecipient(int $id): Recipient
74 {
75 if (! isset($this->recipients[$id])) {
76 throw new InvalidArgumentException('The recipient does not exist.');
77 }
78
79 return $this->recipients[$id];
80 }
81
82 /**
83 * Returns the ciphertext. This method will return null is the JWE has not yet been encrypted.
84 *
85 * @return string|null The ciphertext
86 */
87 public function getCiphertext(): ?string
88 {
89 return $this->ciphertext;
90 }
91
92 /**
93 * Returns the Additional Authentication Data if available.
94 */
95 public function getAAD(): ?string
96 {
97 return $this->aad;
98 }
99
100 /**
101 * Returns the Initialization Vector if available.
102 */
103 public function getIV(): ?string
104 {
105 return $this->iv;
106 }
107
108 /**
109 * Returns the tag if available.
110 */
111 public function getTag(): ?string
112 {
113 return $this->tag;
114 }
115
116 /**
117 * Returns the encoded shared protected header.
118 */
119 public function getEncodedSharedProtectedHeader(): string
120 {
121 return $this->encodedSharedProtectedHeader ?? '';
122 }
123
124 /**
125 * Returns the shared protected header.
126 */
127 public function getSharedProtectedHeader(): array
128 {
129 return $this->sharedProtectedHeader;
130 }
131
132 /**
133 * Returns the shared protected header parameter identified by the given key. Throws an exception is the the
134 * parameter is not available.
135 *
136 * @param string $key The key
137 *
138 * @return mixed|null
139 */
140 public function getSharedProtectedHeaderParameter(string $key)
141 {
142 if (! $this->hasSharedProtectedHeaderParameter($key)) {
143 throw new InvalidArgumentException(sprintf('The shared protected header "%s" does not exist.', $key));
144 }
145
146 return $this->sharedProtectedHeader[$key];
147 }
148
149 /**
150 * Returns true if the shared protected header has the parameter identified by the given key.
151 *
152 * @param string $key The key
153 */
154 public function hasSharedProtectedHeaderParameter(string $key): bool
155 {
156 return array_key_exists($key, $this->sharedProtectedHeader);
157 }
158
159 /**
160 * Returns the shared header.
161 */
162 public function getSharedHeader(): array
163 {
164 return $this->sharedHeader;
165 }
166
167 /**
168 * Returns the shared header parameter identified by the given key. Throws an exception is the the parameter is not
169 * available.
170 *
171 * @param string $key The key
172 *
173 * @return mixed|null
174 */
175 public function getSharedHeaderParameter(string $key)
176 {
177 if (! $this->hasSharedHeaderParameter($key)) {
178 throw new InvalidArgumentException(sprintf('The shared header "%s" does not exist.', $key));
179 }
180
181 return $this->sharedHeader[$key];
182 }
183
184 /**
185 * Returns true if the shared header has the parameter identified by the given key.
186 *
187 * @param string $key The key
188 */
189 public function hasSharedHeaderParameter(string $key): bool
190 {
191 return array_key_exists($key, $this->sharedHeader);
192 }
193
194 /**
195 * This method splits the JWE into a list of JWEs. It is only useful when the JWE contains more than one recipient
196 * (JSON General Serialization).
197 *
198 * @return JWE[]
199 */
200 public function split(): array
201 {
202 $result = [];
203 foreach ($this->recipients as $recipient) {
204 $result[] = new self(
205 $this->ciphertext,
206 $this->iv,
207 $this->tag,
208 $this->aad,
209 $this->sharedHeader,
210 $this->sharedProtectedHeader,
211 $this->encodedSharedProtectedHeader,
212 [$recipient]
213 );
214 }
215
216 return $result;
217 }
218 }