| 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 LogicException; |
| 12 |
use Throwable; |
| 13 |
use function count; |
| 14 |
use function is_array; |
| 15 |
|
| 16 |
final class CompactSerializer extends Serializer |
| 17 |
{ |
| 18 |
public const NAME = 'jws_compact'; |
| 19 |
|
| 20 |
public function displayName(): string |
| 21 |
{ |
| 22 |
return 'JWS Compact'; |
| 23 |
} |
| 24 |
|
| 25 |
public function name(): string |
| 26 |
{ |
| 27 |
return self::NAME; |
| 28 |
} |
| 29 |
|
| 30 |
public function serialize(JWS $jws, ?int $signatureIndex = null): string |
| 31 |
{ |
| 32 |
if ($signatureIndex === null) { |
| 33 |
$signatureIndex = 0; |
| 34 |
} |
| 35 |
$signature = $jws->getSignature($signatureIndex); |
| 36 |
if (count($signature->getHeader()) !== 0) { |
| 37 |
throw new LogicException( |
| 38 |
'The signature contains unprotected header parameters and cannot be converted into compact JSON.' |
| 39 |
); |
| 40 |
} |
| 41 |
$isEmptyPayload = $jws->getEncodedPayload() === null || $jws->getEncodedPayload() === ''; |
| 42 |
if (! $isEmptyPayload && ! $this->isPayloadEncoded($signature->getProtectedHeader())) { |
| 43 |
if (preg_match('/^[\x{20}-\x{2d}|\x{2f}-\x{7e}]*$/u', $jws->getPayload() ?? '') !== 1) { |
| 44 |
throw new LogicException('Unable to convert the JWS with non-encoded payload.'); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return sprintf( |
| 49 |
'%s.%s.%s', |
| 50 |
$signature->getEncodedProtectedHeader(), |
| 51 |
$jws->getEncodedPayload(), |
| 52 |
Base64UrlSafe::encodeUnpadded($signature->getSignature()) |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
public function unserialize(string $input): JWS |
| 57 |
{ |
| 58 |
$parts = explode('.', $input); |
| 59 |
if (count($parts) !== 3) { |
| 60 |
throw new InvalidArgumentException('Unsupported input'); |
| 61 |
} |
| 62 |
|
| 63 |
try { |
| 64 |
$encodedProtectedHeader = $parts[0]; |
| 65 |
$protectedHeader = JsonConverter::decode(Base64UrlSafe::decodeNoPadding($parts[0])); |
| 66 |
if (! is_array($protectedHeader)) { |
| 67 |
throw new InvalidArgumentException('Bad protected header.'); |
| 68 |
} |
| 69 |
$hasPayload = $parts[1] !== ''; |
| 70 |
if (! $hasPayload) { |
| 71 |
$payload = null; |
| 72 |
$encodedPayload = null; |
| 73 |
} else { |
| 74 |
$encodedPayload = $parts[1]; |
| 75 |
$payload = $this->isPayloadEncoded($protectedHeader) ? Base64UrlSafe::decodeNoPadding( |
| 76 |
$encodedPayload |
| 77 |
) : $encodedPayload; |
| 78 |
} |
| 79 |
$signature = Base64UrlSafe::decodeNoPadding($parts[2]); |
| 80 |
|
| 81 |
$jws = new JWS($payload, $encodedPayload, ! $hasPayload); |
| 82 |
|
| 83 |
return $jws->addSignature($signature, $protectedHeader, $encodedProtectedHeader); |
| 84 |
} catch (Throwable $throwable) { |
| 85 |
throw new InvalidArgumentException('Unsupported input', $throwable->getCode(), $throwable); |
| 86 |
} |
| 87 |
} |
| 88 |
} |