Uploaded From CV. Swandhana Server
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Encoding;
|
||||
|
||||
use JsonException;
|
||||
use Lcobucci\JWT\Exception;
|
||||
use RuntimeException;
|
||||
|
||||
final class CannotDecodeContent extends RuntimeException implements Exception
|
||||
{
|
||||
public static function jsonIssues(JsonException $previous): self
|
||||
{
|
||||
return new self(message: 'Error while decoding from JSON', previous: $previous);
|
||||
}
|
||||
|
||||
public static function invalidBase64String(): self
|
||||
{
|
||||
return new self('Error while decoding from Base64Url, invalid base64 characters detected');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Encoding;
|
||||
|
||||
use JsonException;
|
||||
use Lcobucci\JWT\Exception;
|
||||
use RuntimeException;
|
||||
|
||||
final class CannotEncodeContent extends RuntimeException implements Exception
|
||||
{
|
||||
public static function jsonIssues(JsonException $previous): self
|
||||
{
|
||||
return new self(message: 'Error while encoding to JSON', previous: $previous);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Encoding;
|
||||
|
||||
use Lcobucci\JWT\ClaimsFormatter;
|
||||
|
||||
final class ChainedFormatter implements ClaimsFormatter
|
||||
{
|
||||
/** @var array<ClaimsFormatter> */
|
||||
private array $formatters;
|
||||
|
||||
public function __construct(ClaimsFormatter ...$formatters)
|
||||
{
|
||||
$this->formatters = $formatters;
|
||||
}
|
||||
|
||||
public static function default(): self
|
||||
{
|
||||
return new self(new UnifyAudience(), new MicrosecondBasedDateConversion());
|
||||
}
|
||||
|
||||
public static function withUnixTimestampDates(): self
|
||||
{
|
||||
return new self(new UnifyAudience(), new UnixTimestampDates());
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function formatClaims(array $claims): array
|
||||
{
|
||||
foreach ($this->formatters as $formatter) {
|
||||
$claims = $formatter->formatClaims($claims);
|
||||
}
|
||||
|
||||
return $claims;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Encoding;
|
||||
|
||||
use JsonException;
|
||||
use Lcobucci\JWT\Decoder;
|
||||
use Lcobucci\JWT\Encoder;
|
||||
use Lcobucci\JWT\SodiumBase64Polyfill;
|
||||
|
||||
use function json_decode;
|
||||
use function json_encode;
|
||||
|
||||
use const JSON_THROW_ON_ERROR;
|
||||
use const JSON_UNESCAPED_SLASHES;
|
||||
use const JSON_UNESCAPED_UNICODE;
|
||||
|
||||
/**
|
||||
* A utilitarian class that encodes and decodes data according to JOSE specifications
|
||||
*/
|
||||
final class JoseEncoder implements Encoder, Decoder
|
||||
{
|
||||
public function jsonEncode(mixed $data): string
|
||||
{
|
||||
try {
|
||||
return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
|
||||
} catch (JsonException $exception) {
|
||||
throw CannotEncodeContent::jsonIssues($exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function jsonDecode(string $json): mixed
|
||||
{
|
||||
try {
|
||||
return json_decode(json: $json, associative: true, flags: JSON_THROW_ON_ERROR);
|
||||
} catch (JsonException $exception) {
|
||||
throw CannotDecodeContent::jsonIssues($exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function base64UrlEncode(string $data): string
|
||||
{
|
||||
return SodiumBase64Polyfill::bin2base64(
|
||||
$data,
|
||||
SodiumBase64Polyfill::SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING,
|
||||
);
|
||||
}
|
||||
|
||||
public function base64UrlDecode(string $data): string
|
||||
{
|
||||
return SodiumBase64Polyfill::base642bin(
|
||||
$data,
|
||||
SodiumBase64Polyfill::SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Encoding;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Lcobucci\JWT\ClaimsFormatter;
|
||||
use Lcobucci\JWT\Token\RegisteredClaims;
|
||||
|
||||
use function array_key_exists;
|
||||
|
||||
final class MicrosecondBasedDateConversion implements ClaimsFormatter
|
||||
{
|
||||
/** @inheritdoc */
|
||||
public function formatClaims(array $claims): array
|
||||
{
|
||||
foreach (RegisteredClaims::DATE_CLAIMS as $claim) {
|
||||
if (! array_key_exists($claim, $claims)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$claims[$claim] = $this->convertDate($claims[$claim]);
|
||||
}
|
||||
|
||||
return $claims;
|
||||
}
|
||||
|
||||
private function convertDate(DateTimeImmutable $date): int|float
|
||||
{
|
||||
if ($date->format('u') === '000000') {
|
||||
return (int) $date->format('U');
|
||||
}
|
||||
|
||||
return (float) $date->format('U.u');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Encoding;
|
||||
|
||||
use Lcobucci\JWT\ClaimsFormatter;
|
||||
use Lcobucci\JWT\Token\RegisteredClaims;
|
||||
|
||||
use function array_key_exists;
|
||||
use function count;
|
||||
use function current;
|
||||
|
||||
final class UnifyAudience implements ClaimsFormatter
|
||||
{
|
||||
/** @inheritdoc */
|
||||
public function formatClaims(array $claims): array
|
||||
{
|
||||
if (
|
||||
! array_key_exists(RegisteredClaims::AUDIENCE, $claims)
|
||||
|| count($claims[RegisteredClaims::AUDIENCE]) !== 1
|
||||
) {
|
||||
return $claims;
|
||||
}
|
||||
|
||||
$claims[RegisteredClaims::AUDIENCE] = current($claims[RegisteredClaims::AUDIENCE]);
|
||||
|
||||
return $claims;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Encoding;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Lcobucci\JWT\ClaimsFormatter;
|
||||
use Lcobucci\JWT\Token\RegisteredClaims;
|
||||
|
||||
use function array_key_exists;
|
||||
|
||||
final class UnixTimestampDates implements ClaimsFormatter
|
||||
{
|
||||
/** @inheritdoc */
|
||||
public function formatClaims(array $claims): array
|
||||
{
|
||||
foreach (RegisteredClaims::DATE_CLAIMS as $claim) {
|
||||
if (! array_key_exists($claim, $claims)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$claims[$claim] = $this->convertDate($claims[$claim]);
|
||||
}
|
||||
|
||||
return $claims;
|
||||
}
|
||||
|
||||
private function convertDate(DateTimeImmutable $date): int
|
||||
{
|
||||
return $date->getTimestamp();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user