Skip to content

Ensure JWS serializers only throw InvalidArgumentException #513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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