| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jose\Component\Signature\Serializer; |
| 6 |
|
| 7 |
use InvalidArgumentException; |
| 8 |
use Jose\Component\Core\Util\Base64UrlSafe; |
| 9 |
use Jose\Component\Core\Util\JsonConverter; |
| 10 |
use Jose\Component\Signature\JWS; |
| 11 |
use function count; |
| 12 |
use function is_array; |
| 13 |
|
| 14 |
final class JSONFlattenedSerializer extends Serializer |
| 15 |
{ |
| 16 |
public const NAME = 'jws_json_flattened'; |
| 17 |
|
| 18 |
public function displayName(): string |
| 19 |
{ |
| 20 |
return 'JWS JSON Flattened'; |
| 21 |
} |
| 22 |
|
| 23 |
public function name(): string |
| 24 |
{ |
| 25 |
return self::NAME; |
| 26 |
} |
| 27 |
|
| 28 |
public function serialize(JWS $jws, ?int $signatureIndex = null): string |
| 29 |
{ |
| 30 |
if ($signatureIndex === null) { |
| 31 |
$signatureIndex = 0; |
| 32 |
} |
| 33 |
$signature = $jws->getSignature($signatureIndex); |
| 34 |
|
| 35 |
$data = []; |
| 36 |
$encodedPayload = $jws->getEncodedPayload(); |
| 37 |
if ($encodedPayload !== null && $encodedPayload !== '') { |
| 38 |
$data['payload'] = $encodedPayload; |
| 39 |
} |
| 40 |
$encodedProtectedHeader = $signature->getEncodedProtectedHeader(); |
| 41 |
if ($encodedProtectedHeader !== null && $encodedProtectedHeader !== '') { |
| 42 |
$data['protected'] = $encodedProtectedHeader; |
| 43 |
} |
| 44 |
$header = $signature->getHeader(); |
| 45 |
if (count($header) !== 0) { |
| 46 |
$data['header'] = $header; |
| 47 |
} |
| 48 |
$data['signature'] = Base64UrlSafe::encodeUnpadded($signature->getSignature()); |
| 49 |
|
| 50 |
return JsonConverter::encode($data); |
| 51 |
} |
| 52 |
|
| 53 |
public function unserialize(string $input): JWS |
| 54 |
{ |
| 55 |
$data = JsonConverter::decode($input); |
| 56 |
if (! is_array($data)) { |
| 57 |
throw new InvalidArgumentException('Unsupported input.'); |
| 58 |
} |
| 59 |
if (! isset($data['signature'])) { |
| 60 |
throw new InvalidArgumentException('Unsupported input.'); |
| 61 |
} |
| 62 |
$signature = Base64UrlSafe::decodeNoPadding($data['signature']); |
| 63 |
|
| 64 |
if (isset($data['protected'])) { |
| 65 |
$encodedProtectedHeader = $data['protected']; |
| 66 |
$protectedHeader = JsonConverter::decode(Base64UrlSafe::decodeNoPadding($data['protected'])); |
| 67 |
if (! is_array($protectedHeader)) { |
| 68 |
throw new InvalidArgumentException('Bad protected header.'); |
| 69 |
} |
| 70 |
} else { |
| 71 |
$encodedProtectedHeader = null; |
| 72 |
$protectedHeader = []; |
| 73 |
} |
| 74 |
if (isset($data['header'])) { |
| 75 |
if (! is_array($data['header'])) { |
| 76 |
throw new InvalidArgumentException('Bad header.'); |
| 77 |
} |
| 78 |
$header = $data['header']; |
| 79 |
} else { |
| 80 |
$header = []; |
| 81 |
} |
| 82 |
|
| 83 |
if (isset($data['payload'])) { |
| 84 |
$encodedPayload = $data['payload']; |
| 85 |
$payload = $this->isPayloadEncoded($protectedHeader) ? Base64UrlSafe::decodeNoPadding( |
| 86 |
$encodedPayload |
| 87 |
) : $encodedPayload; |
| 88 |
} else { |
| 89 |
$payload = null; |
| 90 |
$encodedPayload = null; |
| 91 |
} |
| 92 |
|
| 93 |
$jws = new JWS($payload, $encodedPayload, $encodedPayload === null); |
| 94 |
|
| 95 |
return $jws->addSignature($signature, $protectedHeader, $encodedProtectedHeader, $header); |
| 96 |
} |
| 97 |
} |