first commit
This commit is contained in:
88
libraries/vendor/web-token/jwt-signature/Serializer/CompactSerializer.php
vendored
Normal file
88
libraries/vendor/web-token/jwt-signature/Serializer/CompactSerializer.php
vendored
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jose\Component\Signature\Serializer;
|
||||
|
||||
use function count;
|
||||
use InvalidArgumentException;
|
||||
use function is_array;
|
||||
use Jose\Component\Core\Util\JsonConverter;
|
||||
use Jose\Component\Signature\JWS;
|
||||
use LogicException;
|
||||
use ParagonIE\ConstantTime\Base64UrlSafe;
|
||||
use Throwable;
|
||||
|
||||
final class CompactSerializer extends Serializer
|
||||
{
|
||||
public const NAME = 'jws_compact';
|
||||
|
||||
public function displayName(): string
|
||||
{
|
||||
return 'JWS Compact';
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return self::NAME;
|
||||
}
|
||||
|
||||
public function serialize(JWS $jws, ?int $signatureIndex = null): string
|
||||
{
|
||||
if ($signatureIndex === null) {
|
||||
$signatureIndex = 0;
|
||||
}
|
||||
$signature = $jws->getSignature($signatureIndex);
|
||||
if (count($signature->getHeader()) !== 0) {
|
||||
throw new LogicException(
|
||||
'The signature contains unprotected header parameters and cannot be converted into compact JSON.'
|
||||
);
|
||||
}
|
||||
$isEmptyPayload = $jws->getEncodedPayload() === null || $jws->getEncodedPayload() === '';
|
||||
if (! $isEmptyPayload && ! $this->isPayloadEncoded($signature->getProtectedHeader())) {
|
||||
if (preg_match('/^[\x{20}-\x{2d}|\x{2f}-\x{7e}]*$/u', $jws->getPayload() ?? '') !== 1) {
|
||||
throw new LogicException('Unable to convert the JWS with non-encoded payload.');
|
||||
}
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
'%s.%s.%s',
|
||||
$signature->getEncodedProtectedHeader(),
|
||||
$jws->getEncodedPayload(),
|
||||
Base64UrlSafe::encodeUnpadded($signature->getSignature())
|
||||
);
|
||||
}
|
||||
|
||||
public function unserialize(string $input): JWS
|
||||
{
|
||||
$parts = explode('.', $input);
|
||||
if (count($parts) !== 3) {
|
||||
throw new InvalidArgumentException('Unsupported input');
|
||||
}
|
||||
|
||||
try {
|
||||
$encodedProtectedHeader = $parts[0];
|
||||
$protectedHeader = JsonConverter::decode(Base64UrlSafe::decode($parts[0]));
|
||||
if (! is_array($protectedHeader)) {
|
||||
throw new InvalidArgumentException('Bad protected header.');
|
||||
}
|
||||
$hasPayload = $parts[1] !== '';
|
||||
if (! $hasPayload) {
|
||||
$payload = null;
|
||||
$encodedPayload = null;
|
||||
} else {
|
||||
$encodedPayload = $parts[1];
|
||||
$payload = $this->isPayloadEncoded($protectedHeader) ? Base64UrlSafe::decode(
|
||||
$encodedPayload
|
||||
) : $encodedPayload;
|
||||
}
|
||||
$signature = Base64UrlSafe::decode($parts[2]);
|
||||
|
||||
$jws = new JWS($payload, $encodedPayload, ! $hasPayload);
|
||||
|
||||
return $jws->addSignature($signature, $protectedHeader, $encodedProtectedHeader);
|
||||
} catch (Throwable $throwable) {
|
||||
throw new InvalidArgumentException('Unsupported input', $throwable->getCode(), $throwable);
|
||||
}
|
||||
}
|
||||
}
|
||||
97
libraries/vendor/web-token/jwt-signature/Serializer/JSONFlattenedSerializer.php
vendored
Normal file
97
libraries/vendor/web-token/jwt-signature/Serializer/JSONFlattenedSerializer.php
vendored
Normal file
@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jose\Component\Signature\Serializer;
|
||||
|
||||
use function count;
|
||||
use InvalidArgumentException;
|
||||
use function is_array;
|
||||
use Jose\Component\Core\Util\JsonConverter;
|
||||
use Jose\Component\Signature\JWS;
|
||||
use ParagonIE\ConstantTime\Base64UrlSafe;
|
||||
|
||||
final class JSONFlattenedSerializer extends Serializer
|
||||
{
|
||||
public const NAME = 'jws_json_flattened';
|
||||
|
||||
public function displayName(): string
|
||||
{
|
||||
return 'JWS JSON Flattened';
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return self::NAME;
|
||||
}
|
||||
|
||||
public function serialize(JWS $jws, ?int $signatureIndex = null): string
|
||||
{
|
||||
if ($signatureIndex === null) {
|
||||
$signatureIndex = 0;
|
||||
}
|
||||
$signature = $jws->getSignature($signatureIndex);
|
||||
|
||||
$data = [];
|
||||
$encodedPayload = $jws->getEncodedPayload();
|
||||
if ($encodedPayload !== null && $encodedPayload !== '') {
|
||||
$data['payload'] = $encodedPayload;
|
||||
}
|
||||
$encodedProtectedHeader = $signature->getEncodedProtectedHeader();
|
||||
if ($encodedProtectedHeader !== null && $encodedProtectedHeader !== '') {
|
||||
$data['protected'] = $encodedProtectedHeader;
|
||||
}
|
||||
$header = $signature->getHeader();
|
||||
if (count($header) !== 0) {
|
||||
$data['header'] = $header;
|
||||
}
|
||||
$data['signature'] = Base64UrlSafe::encodeUnpadded($signature->getSignature());
|
||||
|
||||
return JsonConverter::encode($data);
|
||||
}
|
||||
|
||||
public function unserialize(string $input): JWS
|
||||
{
|
||||
$data = JsonConverter::decode($input);
|
||||
if (! is_array($data)) {
|
||||
throw new InvalidArgumentException('Unsupported input.');
|
||||
}
|
||||
if (! isset($data['signature'])) {
|
||||
throw new InvalidArgumentException('Unsupported input.');
|
||||
}
|
||||
$signature = Base64UrlSafe::decode($data['signature']);
|
||||
|
||||
if (isset($data['protected'])) {
|
||||
$encodedProtectedHeader = $data['protected'];
|
||||
$protectedHeader = JsonConverter::decode(Base64UrlSafe::decode($data['protected']));
|
||||
if (! is_array($protectedHeader)) {
|
||||
throw new InvalidArgumentException('Bad protected header.');
|
||||
}
|
||||
} else {
|
||||
$encodedProtectedHeader = null;
|
||||
$protectedHeader = [];
|
||||
}
|
||||
if (isset($data['header'])) {
|
||||
if (! is_array($data['header'])) {
|
||||
throw new InvalidArgumentException('Bad header.');
|
||||
}
|
||||
$header = $data['header'];
|
||||
} else {
|
||||
$header = [];
|
||||
}
|
||||
|
||||
if (isset($data['payload'])) {
|
||||
$encodedPayload = $data['payload'];
|
||||
$payload = $this->isPayloadEncoded($protectedHeader) ? Base64UrlSafe::decode(
|
||||
$encodedPayload
|
||||
) : $encodedPayload;
|
||||
} else {
|
||||
$payload = null;
|
||||
$encodedPayload = null;
|
||||
}
|
||||
|
||||
$jws = new JWS($payload, $encodedPayload, $encodedPayload === null);
|
||||
|
||||
return $jws->addSignature($signature, $protectedHeader, $encodedProtectedHeader, $header);
|
||||
}
|
||||
}
|
||||
160
libraries/vendor/web-token/jwt-signature/Serializer/JSONGeneralSerializer.php
vendored
Normal file
160
libraries/vendor/web-token/jwt-signature/Serializer/JSONGeneralSerializer.php
vendored
Normal file
@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jose\Component\Signature\Serializer;
|
||||
|
||||
use function array_key_exists;
|
||||
use function count;
|
||||
use InvalidArgumentException;
|
||||
use function is_array;
|
||||
use function is_string;
|
||||
use Jose\Component\Core\Util\JsonConverter;
|
||||
use Jose\Component\Signature\JWS;
|
||||
use LogicException;
|
||||
use ParagonIE\ConstantTime\Base64UrlSafe;
|
||||
|
||||
final class JSONGeneralSerializer extends Serializer
|
||||
{
|
||||
public const NAME = 'jws_json_general';
|
||||
|
||||
public function displayName(): string
|
||||
{
|
||||
return 'JWS JSON General';
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return self::NAME;
|
||||
}
|
||||
|
||||
public function serialize(JWS $jws, ?int $signatureIndex = null): string
|
||||
{
|
||||
if ($jws->countSignatures() === 0) {
|
||||
throw new LogicException('No signature.');
|
||||
}
|
||||
|
||||
$data = [];
|
||||
$this->checkPayloadEncoding($jws);
|
||||
|
||||
if ($jws->isPayloadDetached() === false) {
|
||||
$data['payload'] = $jws->getEncodedPayload();
|
||||
}
|
||||
|
||||
$data['signatures'] = [];
|
||||
foreach ($jws->getSignatures() as $signature) {
|
||||
$tmp = [
|
||||
'signature' => Base64UrlSafe::encodeUnpadded($signature->getSignature()),
|
||||
];
|
||||
$values = [
|
||||
'protected' => $signature->getEncodedProtectedHeader(),
|
||||
'header' => $signature->getHeader(),
|
||||
];
|
||||
|
||||
foreach ($values as $key => $value) {
|
||||
if ((is_string($value) && $value !== '') || (is_array($value) && count($value) !== 0)) {
|
||||
$tmp[$key] = $value;
|
||||
}
|
||||
}
|
||||
$data['signatures'][] = $tmp;
|
||||
}
|
||||
|
||||
return JsonConverter::encode($data);
|
||||
}
|
||||
|
||||
public function unserialize(string $input): JWS
|
||||
{
|
||||
$data = JsonConverter::decode($input);
|
||||
if (! is_array($data)) {
|
||||
throw new InvalidArgumentException('Unsupported input.');
|
||||
}
|
||||
if (! isset($data['signatures'])) {
|
||||
throw new InvalidArgumentException('Unsupported input.');
|
||||
}
|
||||
|
||||
$isPayloadEncoded = null;
|
||||
$rawPayload = $data['payload'] ?? null;
|
||||
$signatures = [];
|
||||
foreach ($data['signatures'] as $signature) {
|
||||
if (! isset($signature['signature'])) {
|
||||
throw new InvalidArgumentException('Unsupported input.');
|
||||
}
|
||||
[$encodedProtectedHeader, $protectedHeader, $header] = $this->processHeaders($signature);
|
||||
$signatures[] = [
|
||||
'signature' => Base64UrlSafe::decode($signature['signature']),
|
||||
'protected' => $protectedHeader,
|
||||
'encoded_protected' => $encodedProtectedHeader,
|
||||
'header' => $header,
|
||||
];
|
||||
$isPayloadEncoded = $this->processIsPayloadEncoded($isPayloadEncoded, $protectedHeader);
|
||||
}
|
||||
|
||||
$payload = $this->processPayload($rawPayload, $isPayloadEncoded);
|
||||
$jws = new JWS($payload, $rawPayload);
|
||||
foreach ($signatures as $signature) {
|
||||
$jws = $jws->addSignature(
|
||||
$signature['signature'],
|
||||
$signature['protected'],
|
||||
$signature['encoded_protected'],
|
||||
$signature['header']
|
||||
);
|
||||
}
|
||||
|
||||
return $jws;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $protectedHeader
|
||||
*/
|
||||
private function processIsPayloadEncoded(?bool $isPayloadEncoded, array $protectedHeader): bool
|
||||
{
|
||||
if ($isPayloadEncoded === null) {
|
||||
return $this->isPayloadEncoded($protectedHeader);
|
||||
}
|
||||
if ($this->isPayloadEncoded($protectedHeader) !== $isPayloadEncoded) {
|
||||
throw new InvalidArgumentException('Foreign payload encoding detected.');
|
||||
}
|
||||
|
||||
return $isPayloadEncoded;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{protected?: string, header?: array<string, mixed>} $signature
|
||||
* @return array<mixed>
|
||||
*/
|
||||
private function processHeaders(array $signature): array
|
||||
{
|
||||
$encodedProtectedHeader = $signature['protected'] ?? null;
|
||||
$protectedHeader = $encodedProtectedHeader === null ? [] : JsonConverter::decode(
|
||||
Base64UrlSafe::decode($encodedProtectedHeader)
|
||||
);
|
||||
$header = array_key_exists('header', $signature) ? $signature['header'] : [];
|
||||
|
||||
return [$encodedProtectedHeader, $protectedHeader, $header];
|
||||
}
|
||||
|
||||
private function processPayload(?string $rawPayload, ?bool $isPayloadEncoded): ?string
|
||||
{
|
||||
if ($rawPayload === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $isPayloadEncoded === false ? $rawPayload : Base64UrlSafe::decode($rawPayload);
|
||||
}
|
||||
|
||||
private function checkPayloadEncoding(JWS $jws): void
|
||||
{
|
||||
if ($jws->isPayloadDetached()) {
|
||||
return;
|
||||
}
|
||||
$is_encoded = null;
|
||||
foreach ($jws->getSignatures() as $signature) {
|
||||
if ($is_encoded === null) {
|
||||
$is_encoded = $this->isPayloadEncoded($signature->getProtectedHeader());
|
||||
}
|
||||
if ($is_encoded !== $this->isPayloadEncoded($signature->getProtectedHeader())) {
|
||||
throw new LogicException('Foreign payload encoding detected.');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
29
libraries/vendor/web-token/jwt-signature/Serializer/JWSSerializer.php
vendored
Normal file
29
libraries/vendor/web-token/jwt-signature/Serializer/JWSSerializer.php
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jose\Component\Signature\Serializer;
|
||||
|
||||
use Jose\Component\Signature\JWS;
|
||||
|
||||
interface JWSSerializer
|
||||
{
|
||||
/**
|
||||
* The name of the serialization.
|
||||
*/
|
||||
public function name(): string;
|
||||
|
||||
public function displayName(): string;
|
||||
|
||||
/**
|
||||
* Converts a JWS into a string.
|
||||
*/
|
||||
public function serialize(JWS $jws, ?int $signatureIndex = null): string;
|
||||
|
||||
/**
|
||||
* Loads data and return a JWS object.
|
||||
*
|
||||
* @param string $input A string that represents a JWS
|
||||
*/
|
||||
public function unserialize(string $input): JWS;
|
||||
}
|
||||
73
libraries/vendor/web-token/jwt-signature/Serializer/JWSSerializerManager.php
vendored
Normal file
73
libraries/vendor/web-token/jwt-signature/Serializer/JWSSerializerManager.php
vendored
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jose\Component\Signature\Serializer;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Jose\Component\Signature\JWS;
|
||||
|
||||
class JWSSerializerManager
|
||||
{
|
||||
/**
|
||||
* @var JWSSerializer[]
|
||||
*/
|
||||
private array $serializers = [];
|
||||
|
||||
/**
|
||||
* @param JWSSerializer[] $serializers
|
||||
*/
|
||||
public function __construct(array $serializers)
|
||||
{
|
||||
foreach ($serializers as $serializer) {
|
||||
$this->add($serializer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function list(): array
|
||||
{
|
||||
return array_keys($this->serializers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a JWS into a string.
|
||||
*/
|
||||
public function serialize(string $name, JWS $jws, ?int $signatureIndex = null): string
|
||||
{
|
||||
if (! isset($this->serializers[$name])) {
|
||||
throw new InvalidArgumentException(sprintf('Unsupported serializer "%s".', $name));
|
||||
}
|
||||
|
||||
return $this->serializers[$name]->serialize($jws, $signatureIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads data and return a JWS object.
|
||||
*
|
||||
* @param string $input A string that represents a JWS
|
||||
* @param string|null $name the name of the serializer if the input is unserialized
|
||||
*/
|
||||
public function unserialize(string $input, ?string &$name = null): JWS
|
||||
{
|
||||
foreach ($this->serializers as $serializer) {
|
||||
try {
|
||||
$jws = $serializer->unserialize($input);
|
||||
$name = $serializer->name();
|
||||
|
||||
return $jws;
|
||||
} catch (InvalidArgumentException) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('Unsupported input.');
|
||||
}
|
||||
|
||||
private function add(JWSSerializer $serializer): void
|
||||
{
|
||||
$this->serializers[$serializer->name()] = $serializer;
|
||||
}
|
||||
}
|
||||
52
libraries/vendor/web-token/jwt-signature/Serializer/JWSSerializerManagerFactory.php
vendored
Normal file
52
libraries/vendor/web-token/jwt-signature/Serializer/JWSSerializerManagerFactory.php
vendored
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jose\Component\Signature\Serializer;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class JWSSerializerManagerFactory
|
||||
{
|
||||
/**
|
||||
* @var JWSSerializer[]
|
||||
*/
|
||||
private array $serializers = [];
|
||||
|
||||
/**
|
||||
* @param string[] $names
|
||||
*/
|
||||
public function create(array $names): JWSSerializerManager
|
||||
{
|
||||
$serializers = [];
|
||||
foreach ($names as $name) {
|
||||
if (! isset($this->serializers[$name])) {
|
||||
throw new InvalidArgumentException(sprintf('Unsupported serializer "%s".', $name));
|
||||
}
|
||||
$serializers[] = $this->serializers[$name];
|
||||
}
|
||||
|
||||
return new JWSSerializerManager($serializers);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function names(): array
|
||||
{
|
||||
return array_keys($this->serializers);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return JWSSerializer[]
|
||||
*/
|
||||
public function all(): array
|
||||
{
|
||||
return $this->serializers;
|
||||
}
|
||||
|
||||
public function add(JWSSerializer $serializer): void
|
||||
{
|
||||
$this->serializers[$serializer->name()] = $serializer;
|
||||
}
|
||||
}
|
||||
18
libraries/vendor/web-token/jwt-signature/Serializer/Serializer.php
vendored
Normal file
18
libraries/vendor/web-token/jwt-signature/Serializer/Serializer.php
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jose\Component\Signature\Serializer;
|
||||
|
||||
use function array_key_exists;
|
||||
|
||||
abstract class Serializer implements JWSSerializer
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $protectedHeader
|
||||
*/
|
||||
protected function isPayloadEncoded(array $protectedHeader): bool
|
||||
{
|
||||
return ! array_key_exists('b64', $protectedHeader) || $protectedHeader['b64'] === true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user