Skip to content
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

Symfony 7 support #21

Merged
merged 2 commits into from
Nov 15, 2023
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
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
"phpstan/phpstan": "^1.4",
"phpunit/phpunit": "^9.5",
"rize/uri-template": "^0.3.5",
"symfony/browser-kit": "^5.4|^6.0",
"symfony/framework-bundle": "^5.4|^6.0",
"symfony/phpunit-bridge": "^6.1",
"symfony/twig-bundle": "^5.4|^6.0",
"symfony/web-link": "^5.4|^6.0"
"symfony/browser-kit": "^5.4|^6.0|^7.0",
"symfony/framework-bundle": "^5.4|^6.0|^7.0",
"symfony/phpunit-bridge": "^6.1|^7.0",
"symfony/twig-bundle": "^5.4|^6.0|^7.0",
"symfony/web-link": "^5.4|^6.0|^7.0"
},
"provide": {
"psr/link-implementation": "1.0|2.0"
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/bin/.phpunit/phpunit.xsd"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
Expand All @@ -11,7 +11,7 @@
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_CLASS" value="Zenstruck\Uri\Tests\Bridge\Symfony\Fixture\TestKernel" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;max[direct]=0"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;max[direct]=0&amp;quiet[]=indirect&amp;quiet[]=other"/>
<env name="SHELL_VERBOSITY" value="-1"/>
</php>

Expand Down
33 changes: 22 additions & 11 deletions src/Uri/Signed/SymfonySigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

namespace Zenstruck\Uri\Signed;

use Symfony\Component\HttpKernel\UriSigner;
use Symfony\Component\HttpFoundation\UriSigner;
use Symfony\Component\HttpKernel\UriSigner as LegacyUriSigner;
use Zenstruck\Uri;
use Zenstruck\Uri\ParsedUri;
use Zenstruck\Uri\Signed\Exception\AlreadyUsed;
Expand All @@ -27,15 +28,12 @@ final class SymfonySigner
private const SINGLE_USE_TOKEN_KEY = '_token';
private const HASH_KEY = '_hash';

private UriSigner $signer;
/** @var UriSigner|LegacyUriSigner */
private $signer; // @phpstan-ignore-line

public function __construct(string $secret)
{
if (!\class_exists(UriSigner::class)) {
throw new \LogicException('symfony/http-kernel is required to sign URIs. Install with "composer require symfony/http-kernel".');
}

$this->signer = new UriSigner($secret, self::HASH_KEY);
$this->signer = self::createSigner($secret, self::HASH_KEY);
}

public static function create(self|string $secret): self
Expand All @@ -57,10 +55,10 @@ public function sign(Uri|string $uri, ?\DateTimeImmutable $expiresAt, ?string $s
}

if ($singleUseToken) {
$uri = (new UriSigner($singleUseToken, self::SINGLE_USE_TOKEN_KEY))->sign($uri);
$uri = self::createSigner($singleUseToken, self::SINGLE_USE_TOKEN_KEY)->sign($uri); // @phpstan-ignore-line
}

return [new ParsedUri($this->signer->sign($uri)), $expiresAt, (bool) $singleUseToken];
return [new ParsedUri($this->signer->sign($uri)), $expiresAt, (bool) $singleUseToken]; // @phpstan-ignore-line
}

/**
Expand All @@ -73,7 +71,7 @@ public function verify(Uri|string $uri, ?string $singleUseToken): array
$uri = ParsedUri::wrap($uri);
$expiresAt = null;

if (!$this->signer->check($uri)) {
if (!$this->signer->check($uri)) { // @phpstan-ignore-line
throw new InvalidSignature($uri);
}

Expand Down Expand Up @@ -101,10 +99,23 @@ public function verify(Uri|string $uri, ?string $singleUseToken): array

$withoutHash = $uri->withoutQueryParams(self::HASH_KEY); // @phpstan-ignore-line

if (!(new UriSigner($singleUseToken, self::SINGLE_USE_TOKEN_KEY))->check($withoutHash)) {
if (!self::createSigner($singleUseToken, self::SINGLE_USE_TOKEN_KEY)->check($withoutHash)) {
throw new AlreadyUsed($uri);
}

return [$uri, $expiresAt, true];
}

private static function createSigner(#[\SensitiveParameter] string $secret, string $parameter = '_hash'): UriSigner|LegacyUriSigner // @phpstan-ignore-line
{
if (\class_exists(UriSigner::class)) {
return new UriSigner($secret, $parameter);
}

if (\class_exists(LegacyUriSigner::class)) {
return new LegacyUriSigner($secret, $parameter);
}

throw new \LogicException('symfony/http-foundation is required to sign URIs. Install with "composer require symfony/http-foundation".');
}
}
2 changes: 1 addition & 1 deletion tests/Bridge/Symfony/Fixture/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load

protected function configureRoutes(RoutingConfigurator $routes): void
{
$routes->import(__DIR__, 'annotation');
$routes->import(__DIR__, self::MAJOR_VERSION < 6 ? 'annotation' : 'attribute');
}
}