| 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 |
|
| 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 |
|
| 46 |
|
| 47 |
public function countRecipients(): int |
| 48 |
{ |
| 49 |
return count($this->recipients); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function isEncrypted(): bool |
| 56 |
{ |
| 57 |
return $this->getCiphertext() !== null; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function getRecipients(): array |
| 66 |
{ |
| 67 |
return $this->recipients; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 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 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function getCiphertext(): ?string |
| 88 |
{ |
| 89 |
return $this->ciphertext; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function getAAD(): ?string |
| 96 |
{ |
| 97 |
return $this->aad; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function getIV(): ?string |
| 104 |
{ |
| 105 |
return $this->iv; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function getTag(): ?string |
| 112 |
{ |
| 113 |
return $this->tag; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function getEncodedSharedProtectedHeader(): string |
| 120 |
{ |
| 121 |
return $this->encodedSharedProtectedHeader ?? ''; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function getSharedProtectedHeader(): array |
| 128 |
{ |
| 129 |
return $this->sharedProtectedHeader; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 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 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public function hasSharedProtectedHeaderParameter(string $key): bool |
| 155 |
{ |
| 156 |
return array_key_exists($key, $this->sharedProtectedHeader); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public function getSharedHeader(): array |
| 163 |
{ |
| 164 |
return $this->sharedHeader; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 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 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function hasSharedHeaderParameter(string $key): bool |
| 190 |
{ |
| 191 |
return array_key_exists($key, $this->sharedHeader); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 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 |
} |