Skip to content

Commit

Permalink
Ensure JWS serializers only throw InvalidArgumentException (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky authored Feb 1, 2024
1 parent bc76770 commit a19ebf7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
15 changes: 12 additions & 3 deletions src/Library/Core/Util/JsonConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Jose\Component\Core\Util;

use RuntimeException;
use InvalidArgumentException;
use Throwable;
use const JSON_THROW_ON_ERROR;
use const JSON_UNESCAPED_SLASHES;
Expand All @@ -17,12 +17,21 @@ public static function encode(mixed $payload): string
try {
return json_encode($payload, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
} catch (Throwable $throwable) {
throw new RuntimeException('Invalid content.', $throwable->getCode(), $throwable);
throw new InvalidArgumentException('Invalid content.', $throwable->getCode(), $throwable);
}
}

public static function decode(string $payload): mixed
{
return json_decode($payload, true, 512, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
try {
return json_decode(
$payload,
true,
512,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
);
} catch (Throwable $throwable) {
throw new InvalidArgumentException('Unsupported input.', $throwable->getCode(), $throwable);
}
}
}
10 changes: 5 additions & 5 deletions tests/Component/KeyManagement/UrlKeySetFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Jose\Tests\Component\KeyManagement;

use Http\Mock\Client;
use InvalidArgumentException;
use Jose\Component\KeyManagement\JKUFactory;
use Jose\Component\KeyManagement\X5UFactory;
use JsonException;
use Nyholm\Psr7\Factory\Psr17Factory;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -52,8 +52,8 @@ public function iCanGetAKeySetFromAJWKUrl(): void
#[Test]
public function theJWKUrlIsValidButDoesNotContainAKeySet(): void
{
$this->expectException(JsonException::class);
$this->expectExceptionMessage('Syntax error');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unsupported input.');

$response = $this->messageFactory->createResponse(200);
$response->getBody()
Expand Down Expand Up @@ -104,8 +104,8 @@ public function iCanGetAKeySetFromAX509Url(): void
#[Test]
public function theX509UrlIsValidButDoesNotContainAKeySet(): void
{
$this->expectException(JsonException::class);
$this->expectExceptionMessage('Syntax error');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unsupported input.');

$response = $this->messageFactory->createResponse(200);
$response->getBody()
Expand Down

0 comments on commit a19ebf7

Please sign in to comment.