diff options
author | Anton Luka Šijanec <anton@sijanec.eu> | 2022-01-11 12:35:47 +0100 |
---|---|---|
committer | Anton Luka Šijanec <anton@sijanec.eu> | 2022-01-11 12:35:47 +0100 |
commit | 19985dbb8c0aa66dc4bf7905abc1148de909097d (patch) | |
tree | 2cd5a5d20d7e80fc2a51adf60d838d8a2c40999e /vendor/web-token/jwt-signature/Serializer/CompactSerializer.php | |
download | 1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar 1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.gz 1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.bz2 1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.lz 1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.xz 1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.zst 1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.zip |
Diffstat (limited to '')
-rw-r--r-- | vendor/web-token/jwt-signature/Serializer/CompactSerializer.php | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/vendor/web-token/jwt-signature/Serializer/CompactSerializer.php b/vendor/web-token/jwt-signature/Serializer/CompactSerializer.php new file mode 100644 index 0000000..7e26d79 --- /dev/null +++ b/vendor/web-token/jwt-signature/Serializer/CompactSerializer.php @@ -0,0 +1,97 @@ +<?php + +declare(strict_types=1); + +/* + * The MIT License (MIT) + * + * Copyright (c) 2014-2018 Spomky-Labs + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ + +namespace Jose\Component\Signature\Serializer; + +use Base64Url\Base64Url; +use Jose\Component\Core\Converter\JsonConverter; +use Jose\Component\Signature\JWS; + +final class CompactSerializer extends Serializer +{ + public const NAME = 'jws_compact'; + + /** + * @var JsonConverter + */ + private $jsonConverter; + + /** + * JSONFlattenedSerializer constructor. + */ + public function __construct(?JsonConverter $jsonConverter = null) + { + $this->jsonConverter = $jsonConverter ?? new \Jose\Component\Core\Util\JsonConverter(); + } + + public function displayName(): string + { + return 'JWS Compact'; + } + + public function name(): string + { + return self::NAME; + } + + public function serialize(JWS $jws, ?int $signatureIndex = null): string + { + if (null === $signatureIndex) { + $signatureIndex = 0; + } + $signature = $jws->getSignature($signatureIndex); + if (!empty($signature->getHeader())) { + throw new \LogicException('The signature contains unprotected header parameters and cannot be converted into compact JSON.'); + } + if (!$this->isPayloadEncoded($signature->getProtectedHeader()) && !empty($jws->getEncodedPayload())) { + if (1 !== \preg_match('/^[\x{20}-\x{2d}|\x{2f}-\x{7e}]*$/u', $jws->getPayload())) { + throw new \LogicException('Unable to convert the JWS with non-encoded payload.'); + } + } + + return \sprintf( + '%s.%s.%s', + $signature->getEncodedProtectedHeader(), + $jws->getEncodedPayload(), + Base64Url::encode($signature->getSignature()) + ); + } + + public function unserialize(string $input): JWS + { + $parts = \explode('.', $input); + if (3 !== \count($parts)) { + throw new \InvalidArgumentException('Unsupported input'); + } + + try { + $encodedProtectedHeader = $parts[0]; + $protectedHeader = $this->jsonConverter->decode(Base64Url::decode($parts[0])); + if (empty($parts[1])) { + $payload = null; + $encodedPayload = null; + } else { + $encodedPayload = $parts[1]; + $payload = $this->isPayloadEncoded($protectedHeader) ? Base64Url::decode($encodedPayload) : $encodedPayload; + } + $signature = Base64Url::decode($parts[2]); + + $jws = JWS::create($payload, $encodedPayload, empty($parts[1])); + $jws = $jws->addSignature($signature, $protectedHeader, $encodedProtectedHeader); + + return $jws; + } catch (\Error | \Exception $e) { + throw new \InvalidArgumentException('Unsupported input'); + } + } +} |