added predis and eseye back in.
This commit is contained in:
122
vendor/web-token/jwt-encryption/Serializer/CompactSerializer.php
vendored
Normal file
122
vendor/web-token/jwt-encryption/Serializer/CompactSerializer.php
vendored
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2020 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\Encryption\Serializer;
|
||||
|
||||
use Base64Url\Base64Url;
|
||||
use function count;
|
||||
use InvalidArgumentException;
|
||||
use Jose\Component\Core\Util\JsonConverter;
|
||||
use Jose\Component\Encryption\JWE;
|
||||
use Jose\Component\Encryption\Recipient;
|
||||
use LogicException;
|
||||
use Throwable;
|
||||
|
||||
final class CompactSerializer implements JWESerializer
|
||||
{
|
||||
public const NAME = 'jwe_compact';
|
||||
|
||||
public function displayName(): string
|
||||
{
|
||||
return 'JWE Compact';
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return self::NAME;
|
||||
}
|
||||
|
||||
public function serialize(JWE $jwe, ?int $recipientIndex = null): string
|
||||
{
|
||||
if (null === $recipientIndex) {
|
||||
$recipientIndex = 0;
|
||||
}
|
||||
$recipient = $jwe->getRecipient($recipientIndex);
|
||||
|
||||
$this->checkHasNoAAD($jwe);
|
||||
$this->checkHasSharedProtectedHeader($jwe);
|
||||
$this->checkRecipientHasNoHeader($jwe, $recipientIndex);
|
||||
|
||||
return sprintf(
|
||||
'%s.%s.%s.%s.%s',
|
||||
$jwe->getEncodedSharedProtectedHeader(),
|
||||
Base64Url::encode(null === $recipient->getEncryptedKey() ? '' : $recipient->getEncryptedKey()),
|
||||
Base64Url::encode(null === $jwe->getIV() ? '' : $jwe->getIV()),
|
||||
Base64Url::encode($jwe->getCiphertext()),
|
||||
Base64Url::encode(null === $jwe->getTag() ? '' : $jwe->getTag())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidArgumentException if the input is not supported
|
||||
*/
|
||||
public function unserialize(string $input): JWE
|
||||
{
|
||||
$parts = explode('.', $input);
|
||||
if (5 !== count($parts)) {
|
||||
throw new InvalidArgumentException('Unsupported input');
|
||||
}
|
||||
|
||||
try {
|
||||
$encodedSharedProtectedHeader = $parts[0];
|
||||
$sharedProtectedHeader = JsonConverter::decode(Base64Url::decode($encodedSharedProtectedHeader));
|
||||
$encryptedKey = '' === $parts[1] ? null : Base64Url::decode($parts[1]);
|
||||
$iv = Base64Url::decode($parts[2]);
|
||||
$ciphertext = Base64Url::decode($parts[3]);
|
||||
$tag = Base64Url::decode($parts[4]);
|
||||
|
||||
return new JWE(
|
||||
$ciphertext,
|
||||
$iv,
|
||||
$tag,
|
||||
null,
|
||||
[],
|
||||
$sharedProtectedHeader,
|
||||
$encodedSharedProtectedHeader,
|
||||
[new Recipient([], $encryptedKey)]
|
||||
);
|
||||
} catch (Throwable $throwable) {
|
||||
throw new InvalidArgumentException('Unsupported input', $throwable->getCode(), $throwable);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws LogicException if the AAD is invalid
|
||||
*/
|
||||
private function checkHasNoAAD(JWE $jwe): void
|
||||
{
|
||||
if (null !== $jwe->getAAD()) {
|
||||
throw new LogicException('This JWE has AAD and cannot be converted into Compact JSON.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws LogicException if the JWE has a shared header or recipient header (invalid for compact JSON)
|
||||
*/
|
||||
private function checkRecipientHasNoHeader(JWE $jwe, int $id): void
|
||||
{
|
||||
if (0 !== count($jwe->getSharedHeader()) || 0 !== count($jwe->getRecipient($id)->getHeader())) {
|
||||
throw new LogicException('This JWE has shared header parameters or recipient header parameters and cannot be converted into Compact JSON.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws LogicException if the JWE has no shared protected header (invalid for compact JSON)
|
||||
*/
|
||||
private function checkHasSharedProtectedHeader(JWE $jwe): void
|
||||
{
|
||||
if (0 === count($jwe->getSharedProtectedHeader())) {
|
||||
throw new LogicException('This JWE does not have shared protected header parameters and cannot be converted into Compact JSON.');
|
||||
}
|
||||
}
|
||||
}
|
||||
111
vendor/web-token/jwt-encryption/Serializer/JSONFlattenedSerializer.php
vendored
Normal file
111
vendor/web-token/jwt-encryption/Serializer/JSONFlattenedSerializer.php
vendored
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2020 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\Encryption\Serializer;
|
||||
|
||||
use function array_key_exists;
|
||||
use Base64Url\Base64Url;
|
||||
use function count;
|
||||
use InvalidArgumentException;
|
||||
use Jose\Component\Core\Util\JsonConverter;
|
||||
use Jose\Component\Encryption\JWE;
|
||||
use Jose\Component\Encryption\Recipient;
|
||||
|
||||
final class JSONFlattenedSerializer implements JWESerializer
|
||||
{
|
||||
public const NAME = 'jwe_json_flattened';
|
||||
|
||||
public function displayName(): string
|
||||
{
|
||||
return 'JWE JSON Flattened';
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return self::NAME;
|
||||
}
|
||||
|
||||
public function serialize(JWE $jwe, ?int $recipientIndex = null): string
|
||||
{
|
||||
if (null === $recipientIndex) {
|
||||
$recipientIndex = 0;
|
||||
}
|
||||
$recipient = $jwe->getRecipient($recipientIndex);
|
||||
$data = [
|
||||
'ciphertext' => Base64Url::encode($jwe->getCiphertext()),
|
||||
'iv' => Base64Url::encode($jwe->getIV()),
|
||||
'tag' => Base64Url::encode($jwe->getTag()),
|
||||
];
|
||||
if (null !== $jwe->getAAD()) {
|
||||
$data['aad'] = Base64Url::encode($jwe->getAAD());
|
||||
}
|
||||
if (0 !== count($jwe->getSharedProtectedHeader())) {
|
||||
$data['protected'] = $jwe->getEncodedSharedProtectedHeader();
|
||||
}
|
||||
if (0 !== count($jwe->getSharedHeader())) {
|
||||
$data['unprotected'] = $jwe->getSharedHeader();
|
||||
}
|
||||
if (0 !== count($recipient->getHeader())) {
|
||||
$data['header'] = $recipient->getHeader();
|
||||
}
|
||||
if (null !== $recipient->getEncryptedKey()) {
|
||||
$data['encrypted_key'] = Base64Url::encode($recipient->getEncryptedKey());
|
||||
}
|
||||
|
||||
return JsonConverter::encode($data);
|
||||
}
|
||||
|
||||
public function unserialize(string $input): JWE
|
||||
{
|
||||
$data = JsonConverter::decode($input);
|
||||
$this->checkData($data);
|
||||
|
||||
$ciphertext = Base64Url::decode($data['ciphertext']);
|
||||
$iv = Base64Url::decode($data['iv']);
|
||||
$tag = Base64Url::decode($data['tag']);
|
||||
$aad = array_key_exists('aad', $data) ? Base64Url::decode($data['aad']) : null;
|
||||
list($encodedSharedProtectedHeader, $sharedProtectedHeader, $sharedHeader) = $this->processHeaders($data);
|
||||
$encryptedKey = array_key_exists('encrypted_key', $data) ? Base64Url::decode($data['encrypted_key']) : null;
|
||||
$header = array_key_exists('header', $data) ? $data['header'] : [];
|
||||
|
||||
return new JWE(
|
||||
$ciphertext,
|
||||
$iv,
|
||||
$tag,
|
||||
$aad,
|
||||
$sharedHeader,
|
||||
$sharedProtectedHeader,
|
||||
$encodedSharedProtectedHeader,
|
||||
[new Recipient($header, $encryptedKey)]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidArgumentException if the payload cannot be encoded
|
||||
*/
|
||||
private function checkData(?array $data): void
|
||||
{
|
||||
if (null === $data || !isset($data['ciphertext']) || isset($data['recipients'])) {
|
||||
throw new InvalidArgumentException('Unsupported input.');
|
||||
}
|
||||
}
|
||||
|
||||
private function processHeaders(array $data): array
|
||||
{
|
||||
$encodedSharedProtectedHeader = array_key_exists('protected', $data) ? $data['protected'] : null;
|
||||
$sharedProtectedHeader = $encodedSharedProtectedHeader ? JsonConverter::decode(Base64Url::decode($encodedSharedProtectedHeader)) : [];
|
||||
$sharedHeader = $data['unprotected'] ?? [];
|
||||
|
||||
return [$encodedSharedProtectedHeader, $sharedProtectedHeader, $sharedHeader];
|
||||
}
|
||||
}
|
||||
131
vendor/web-token/jwt-encryption/Serializer/JSONGeneralSerializer.php
vendored
Normal file
131
vendor/web-token/jwt-encryption/Serializer/JSONGeneralSerializer.php
vendored
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2020 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\Encryption\Serializer;
|
||||
|
||||
use function array_key_exists;
|
||||
use Base64Url\Base64Url;
|
||||
use function count;
|
||||
use InvalidArgumentException;
|
||||
use Jose\Component\Core\Util\JsonConverter;
|
||||
use Jose\Component\Encryption\JWE;
|
||||
use Jose\Component\Encryption\Recipient;
|
||||
use LogicException;
|
||||
|
||||
final class JSONGeneralSerializer implements JWESerializer
|
||||
{
|
||||
public const NAME = 'jwe_json_general';
|
||||
|
||||
public function displayName(): string
|
||||
{
|
||||
return 'JWE JSON General';
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return self::NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws LogicException if there is no recipient
|
||||
*/
|
||||
public function serialize(JWE $jwe, ?int $recipientIndex = null): string
|
||||
{
|
||||
if (0 === $jwe->countRecipients()) {
|
||||
throw new LogicException('No recipient.');
|
||||
}
|
||||
|
||||
$data = [
|
||||
'ciphertext' => Base64Url::encode($jwe->getCiphertext()),
|
||||
'iv' => Base64Url::encode($jwe->getIV()),
|
||||
'tag' => Base64Url::encode($jwe->getTag()),
|
||||
];
|
||||
if (null !== $jwe->getAAD()) {
|
||||
$data['aad'] = Base64Url::encode($jwe->getAAD());
|
||||
}
|
||||
if (0 !== count($jwe->getSharedProtectedHeader())) {
|
||||
$data['protected'] = $jwe->getEncodedSharedProtectedHeader();
|
||||
}
|
||||
if (0 !== count($jwe->getSharedHeader())) {
|
||||
$data['unprotected'] = $jwe->getSharedHeader();
|
||||
}
|
||||
$data['recipients'] = [];
|
||||
foreach ($jwe->getRecipients() as $recipient) {
|
||||
$temp = [];
|
||||
if (0 !== count($recipient->getHeader())) {
|
||||
$temp['header'] = $recipient->getHeader();
|
||||
}
|
||||
if (null !== $recipient->getEncryptedKey()) {
|
||||
$temp['encrypted_key'] = Base64Url::encode($recipient->getEncryptedKey());
|
||||
}
|
||||
$data['recipients'][] = $temp;
|
||||
}
|
||||
|
||||
return JsonConverter::encode($data);
|
||||
}
|
||||
|
||||
public function unserialize(string $input): JWE
|
||||
{
|
||||
$data = JsonConverter::decode($input);
|
||||
$this->checkData($data);
|
||||
|
||||
$ciphertext = Base64Url::decode($data['ciphertext']);
|
||||
$iv = Base64Url::decode($data['iv']);
|
||||
$tag = Base64Url::decode($data['tag']);
|
||||
$aad = array_key_exists('aad', $data) ? Base64Url::decode($data['aad']) : null;
|
||||
list($encodedSharedProtectedHeader, $sharedProtectedHeader, $sharedHeader) = $this->processHeaders($data);
|
||||
$recipients = [];
|
||||
foreach ($data['recipients'] as $recipient) {
|
||||
list($encryptedKey, $header) = $this->processRecipient($recipient);
|
||||
$recipients[] = new Recipient($header, $encryptedKey);
|
||||
}
|
||||
|
||||
return new JWE(
|
||||
$ciphertext,
|
||||
$iv,
|
||||
$tag,
|
||||
$aad,
|
||||
$sharedHeader,
|
||||
$sharedProtectedHeader,
|
||||
$encodedSharedProtectedHeader,
|
||||
$recipients
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidArgumentException if the input is not supported
|
||||
*/
|
||||
private function checkData(?array $data): void
|
||||
{
|
||||
if (null === $data || !isset($data['ciphertext']) || !isset($data['recipients'])) {
|
||||
throw new InvalidArgumentException('Unsupported input.');
|
||||
}
|
||||
}
|
||||
|
||||
private function processRecipient(array $recipient): array
|
||||
{
|
||||
$encryptedKey = array_key_exists('encrypted_key', $recipient) ? Base64Url::decode($recipient['encrypted_key']) : null;
|
||||
$header = array_key_exists('header', $recipient) ? $recipient['header'] : [];
|
||||
|
||||
return [$encryptedKey, $header];
|
||||
}
|
||||
|
||||
private function processHeaders(array $data): array
|
||||
{
|
||||
$encodedSharedProtectedHeader = array_key_exists('protected', $data) ? $data['protected'] : null;
|
||||
$sharedProtectedHeader = $encodedSharedProtectedHeader ? JsonConverter::decode(Base64Url::decode($encodedSharedProtectedHeader)) : [];
|
||||
$sharedHeader = array_key_exists('unprotected', $data) ? $data['unprotected'] : [];
|
||||
|
||||
return [$encodedSharedProtectedHeader, $sharedProtectedHeader, $sharedHeader];
|
||||
}
|
||||
}
|
||||
44
vendor/web-token/jwt-encryption/Serializer/JWESerializer.php
vendored
Normal file
44
vendor/web-token/jwt-encryption/Serializer/JWESerializer.php
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2020 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\Encryption\Serializer;
|
||||
|
||||
use Jose\Component\Encryption\JWE;
|
||||
|
||||
interface JWESerializer
|
||||
{
|
||||
/**
|
||||
* The name of the serialization method.
|
||||
*/
|
||||
public function name(): string;
|
||||
|
||||
/**
|
||||
* Display name of the serialization method.
|
||||
*/
|
||||
public function displayName(): string;
|
||||
|
||||
/**
|
||||
* Converts a JWE into a string.
|
||||
* If the JWE is designed for multiple recipients and the serializer only supports one recipient,
|
||||
* the recipient index has to be set.
|
||||
*/
|
||||
public function serialize(JWE $jws, ?int $recipientIndex = null): string;
|
||||
|
||||
/**
|
||||
* Loads data and return a JWE object.
|
||||
* Throws an exception in case of failure.
|
||||
*
|
||||
* @param string $input A string that represents a JWE
|
||||
*/
|
||||
public function unserialize(string $input): JWE;
|
||||
}
|
||||
93
vendor/web-token/jwt-encryption/Serializer/JWESerializerManager.php
vendored
Normal file
93
vendor/web-token/jwt-encryption/Serializer/JWESerializerManager.php
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2020 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\Encryption\Serializer;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Jose\Component\Encryption\JWE;
|
||||
|
||||
class JWESerializerManager
|
||||
{
|
||||
/**
|
||||
* @var JWESerializer[]
|
||||
*/
|
||||
private $serializers = [];
|
||||
|
||||
/**
|
||||
* @param JWESerializer[] $serializers
|
||||
*/
|
||||
public function __construct(array $serializers)
|
||||
{
|
||||
foreach ($serializers as $serializer) {
|
||||
$this->add($serializer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the serializer names supported by the manager.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function names(): array
|
||||
{
|
||||
return array_keys($this->serializers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a JWE into a string.
|
||||
* Throws an exception if none of the serializer was able to convert the input.
|
||||
*
|
||||
* @throws InvalidArgumentException if the serializer is not supported
|
||||
*/
|
||||
public function serialize(string $name, JWE $jws, ?int $recipientIndex = null): string
|
||||
{
|
||||
if (!isset($this->serializers[$name])) {
|
||||
throw new InvalidArgumentException(sprintf('Unsupported serializer "%s".', $name));
|
||||
}
|
||||
|
||||
return $this->serializers[$name]->serialize($jws, $recipientIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads data and return a JWE object.
|
||||
* Throws an exception if none of the serializer was able to convert the input.
|
||||
*
|
||||
* @param string $input A string that represents a JWE
|
||||
* @param null|string $name the name of the serializer if the input is unserialized
|
||||
*
|
||||
* @throws InvalidArgumentException if the input cannot be loaded
|
||||
*/
|
||||
public function unserialize(string $input, ?string &$name = null): JWE
|
||||
{
|
||||
foreach ($this->serializers as $serializer) {
|
||||
try {
|
||||
$jws = $serializer->unserialize($input);
|
||||
$name = $serializer->name();
|
||||
|
||||
return $jws;
|
||||
} catch (InvalidArgumentException $e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('Unsupported input.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a serializer to the manager.
|
||||
*/
|
||||
private function add(JWESerializer $serializer): void
|
||||
{
|
||||
$this->serializers[$serializer->name()] = $serializer;
|
||||
}
|
||||
}
|
||||
72
vendor/web-token/jwt-encryption/Serializer/JWESerializerManagerFactory.php
vendored
Normal file
72
vendor/web-token/jwt-encryption/Serializer/JWESerializerManagerFactory.php
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2020 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\Encryption\Serializer;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class JWESerializerManagerFactory
|
||||
{
|
||||
/**
|
||||
* @var JWESerializer[]
|
||||
*/
|
||||
private $serializers = [];
|
||||
|
||||
/**
|
||||
* Creates a serializer manager factory using the given serializers.
|
||||
*
|
||||
* @param string[] $names
|
||||
*
|
||||
* @throws InvalidArgumentException if the serializer is not supported
|
||||
*/
|
||||
public function create(array $names): JWESerializerManager
|
||||
{
|
||||
$serializers = [];
|
||||
foreach ($names as $name) {
|
||||
if (!isset($this->serializers[$name])) {
|
||||
throw new InvalidArgumentException(sprintf('Unsupported serializer "%s".', $name));
|
||||
}
|
||||
$serializers[] = $this->serializers[$name];
|
||||
}
|
||||
|
||||
return new JWESerializerManager($serializers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the serializer names supported by the manager.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function names(): array
|
||||
{
|
||||
return array_keys($this->serializers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all serializers supported by this factory.
|
||||
*
|
||||
* @return JWESerializer[]
|
||||
*/
|
||||
public function all(): array
|
||||
{
|
||||
return $this->serializers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a serializer to the manager.
|
||||
*/
|
||||
public function add(JWESerializer $serializer): void
|
||||
{
|
||||
$this->serializers[$serializer->name()] = $serializer;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user