Skip to content

Commit 86b7278

Browse files
committed
Allow to set a default expiration value on the generated token
1 parent 5f17b7f commit 86b7278

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

src/Authorization.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ final class Authorization
2323
private const MERCURE_AUTHORIZATION_COOKIE_NAME = 'mercureAuthorization';
2424

2525
private $registry;
26+
private $jwtLifetime;
2627

27-
public function __construct(HubRegistry $registry)
28+
/**
29+
* @param int|null $jwtLifetime If not null, an "exp" claim is always set to now + $jwtLifetime (in seconds)
30+
*/
31+
public function __construct(HubRegistry $registry, int $jwtLifetime = null)
2832
{
2933
$this->registry = $registry;
34+
$this->jwtLifetime = $jwtLifetime;
3035
}
3136

3237
/**
@@ -45,6 +50,10 @@ public function createCookie(Request $request, array $subscribe = [], array $pub
4550
throw new InvalidArgumentException(sprintf('The %s hub does not contain a token factory.', $hub ? '"'.$hub.'"' : 'default'));
4651
}
4752

53+
if (null !== $this->jwtLifetime && !isset($additionalClaims['exp'])) {
54+
$additionalClaims['exp'] = new \DateTimeImmutable("+{$this->jwtLifetime} seconds");
55+
}
56+
4857
$token = $tokenFactory->create($subscribe, $publish, $additionalClaims);
4958
$url = $hubInstance->getPublicUrl();
5059
/** @var array $urlComponents */

src/HubRegistry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class HubRegistry
2323
/**
2424
* @param array<string, HubInterface> $hubs An array of hub instances, where the keys are the names
2525
*/
26-
public function __construct(HubInterface $defaultHub, array $hubs)
26+
public function __construct(HubInterface $defaultHub, array $hubs = [])
2727
{
2828
$this->defaultHub = $defaultHub;
2929
$this->hubs = $hubs;

src/Publisher.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ final class Publisher implements PublisherInterface
3737

3838
/**
3939
* @param TokenProviderInterface|callable(Update $update):string $jwtProvider
40+
* @param mixed $jwtProvider
4041
*/
4142
public function __construct(string $hubUrl, $jwtProvider, HttpClientInterface $httpClient = null)
4243
{

tests/AuthorizationTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Mercure Component project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Mercure\Tests;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\Mercure\Authorization;
19+
use Symfony\Component\Mercure\HubRegistry;
20+
use Symfony\Component\Mercure\Jwt\LcobucciFactory;
21+
use Symfony\Component\Mercure\Jwt\StaticTokenProvider;
22+
use Symfony\Component\Mercure\MockHub;
23+
use Symfony\Component\Mercure\Update;
24+
25+
/**
26+
* @author Kévin Dunglas <[email protected]>
27+
*/
28+
class AuthorizationTest extends TestCase
29+
{
30+
public function testJwtLifetime(): void
31+
{
32+
$registry = new HubRegistry(new MockHub(
33+
'https://example.com/.well-known/mercure',
34+
new StaticTokenProvider('foo.bar.baz'),
35+
function (Update $u): string { return 'dummy'; },
36+
new LcobucciFactory('secret')
37+
), []);
38+
39+
$authorization = new Authorization($registry, 3600);
40+
$cookie = $authorization->createCookie(Request::create('https://example.com'));
41+
42+
$payload = json_decode(base64_decode(explode('.', $cookie->getValue())[1], true), true);
43+
$this->assertArrayHasKey('exp', $payload);
44+
$this->assertIsFloat($payload['exp']);
45+
}
46+
}

0 commit comments

Comments
 (0)