Skip to content

Commit b82514a

Browse files
authored
feat: add support for encrypted signers ( passphrase ) (#65)
Signed-off-by: azjezz <[email protected]>
1 parent dd93513 commit b82514a

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"symfony/config": "^4.4|^5.0|^6.0",
2222
"symfony/dependency-injection": "^4.4|^5.0|^6.0",
2323
"symfony/http-kernel": "^4.4|^5.0|^6.0",
24-
"symfony/mercure": "^0.5.3|^0.6",
24+
"symfony/mercure": "^0.6.1",
2525
"symfony/web-link": "^4.4|^5.0|^6.0"
2626
},
2727
"autoload": {

src/DataCollector/MercureDataCollector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121

2222
final class MercureDataCollector extends DataCollector
2323
{
24+
/**
25+
* @var iterable<TraceablePublisher|TraceableHub>
26+
*/
2427
private $hubs;
2528

2629
/**
27-
* @var TraceablePublisher[]|TraceableHub[]
30+
* @param iterable<TraceablePublisher|TraceableHub> $hubs
2831
*/
2932
public function __construct(iterable $hubs)
3033
{

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function getConfigTreeBuilder(): TreeBuilder
6565
->info('A list of topics to allow subscribing to when using the given factory to generate the JWT.')
6666
->end()
6767
->scalarNode('secret')->info('The JWT Secret to use.')->example('!ChangeMe!')->end()
68+
->scalarNode('passphrase')->info('The JWT secret passphrase.')->defaultValue('')->end()
6869
->scalarNode('algorithm')->info('The algorithm to use to sign the JWT')->defaultValue('hmac.sha256')->end()
6970
->end()
7071
->end()

src/DependencyInjection/MercureExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public function load(array $configs, ContainerBuilder $container)
107107
$container->register($tokenFactory, LcobucciFactory::class)
108108
->addArgument($hub['jwt']['secret'])
109109
->addArgument($hub['jwt']['algorithm'])
110+
->addArgument(null)
111+
->addArgument($hub['jwt']['passphrase'])
110112
->addTag('mercure.jwt.factory')
111113
;
112114
}
@@ -286,6 +288,7 @@ private function deprecate($definition, string $message): void
286288
if (class_exists(AliasDeprecatedPublicServicesPass::class)) {
287289
$definition->setDeprecated('symfony/mercure-bundle', '0.2', $message);
288290
} else {
291+
/* @phpstan-ignore-next-line */
289292
$definition->setDeprecated(true, $message);
290293
}
291294
}

tests/DependencyInjection/MercureExtensionTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ public function testExtension(): void
8080
'subscribe' => 'https://example.com/book/1.jsonld',
8181
],
8282
],
83+
'managed2' => [
84+
'url' => 'https://demo.mercure.rocks/managed',
85+
'jwt' => [
86+
'secret' => '!ChangeMe!',
87+
'algorithm' => 'rsa.sha512',
88+
'passphrase' => 'test',
89+
'publish' => ['*'],
90+
'subscribe' => 'https://example.com/book/1.jsonld',
91+
],
92+
],
8393
],
8494
],
8595
];
@@ -110,6 +120,31 @@ public function testExtension(): void
110120
$this->assertArrayHasKey('Symfony\Component\Mercure\Jwt\TokenProviderInterface $managedTokenProvider', $container->getAliases());
111121
$this->assertArrayHasKey('Symfony\Component\Mercure\Jwt\TokenFactoryInterface $managedTokenFactory', $container->getAliases());
112122

123+
$this->assertTrue($container->hasDefinition('mercure.hub.managed2')); // Hub instance
124+
$this->assertTrue($container->hasDefinition('mercure.hub.managed2.publisher')); // Publisher
125+
$this->assertTrue($container->hasDefinition('mercure.hub.managed2.jwt.provider'));
126+
$this->assertTrue($container->hasDefinition('mercure.hub.managed2.jwt.factory'));
127+
$this->assertArrayHasKey('mercure.publisher', $container->getDefinition('mercure.hub.managed2.publisher')->getTags());
128+
$this->assertSame($config['mercure']['hubs']['managed2']['url'], $container->getDefinition('mercure.hub.managed2')->getArgument(0));
129+
$this->assertSame($config['mercure']['hubs']['managed2']['jwt']['secret'], $container->getDefinition('mercure.hub.managed2.jwt.factory')->getArgument(0));
130+
$this->assertSame($config['mercure']['hubs']['managed2']['jwt']['algorithm'], $container->getDefinition('mercure.hub.managed2.jwt.factory')->getArgument(1));
131+
$this->assertSame($config['mercure']['hubs']['managed2']['jwt']['passphrase'], $container->getDefinition('mercure.hub.managed2.jwt.factory')->getArgument(3));
132+
$this->assertSame([$config['mercure']['hubs']['managed2']['jwt']['subscribe']], $container->getDefinition('mercure.hub.managed2.jwt.provider')->getArgument(1));
133+
$this->assertSame($config['mercure']['hubs']['managed2']['jwt']['publish'], $container->getDefinition('mercure.hub.managed2.jwt.provider')->getArgument(2));
134+
135+
$this->assertArrayHasKey('Symfony\Component\Mercure\HubInterface $managed2', $container->getAliases());
136+
$this->assertArrayHasKey('Symfony\Component\Mercure\PublisherInterface $managed2', $container->getAliases());
137+
$this->assertArrayHasKey('Symfony\Component\Mercure\Jwt\TokenProviderInterface $managed2', $container->getAliases());
138+
$this->assertArrayHasKey('Symfony\Component\Mercure\Jwt\TokenFactoryInterface $managed2', $container->getAliases());
139+
140+
$this->assertArrayHasKey('Symfony\Component\Mercure\HubInterface $managed2Hub', $container->getAliases());
141+
$this->assertArrayHasKey('Symfony\Component\Mercure\PublisherInterface $managed2Publisher', $container->getAliases());
142+
$this->assertArrayHasKey('Symfony\Component\Mercure\Jwt\TokenProviderInterface $managed2Provider', $container->getAliases());
143+
$this->assertArrayHasKey('Symfony\Component\Mercure\Jwt\TokenFactoryInterface $managed2Factory', $container->getAliases());
144+
145+
$this->assertArrayHasKey('Symfony\Component\Mercure\Jwt\TokenProviderInterface $managed2TokenProvider', $container->getAliases());
146+
$this->assertArrayHasKey('Symfony\Component\Mercure\Jwt\TokenFactoryInterface $managed2TokenFactory', $container->getAliases());
147+
113148
$this->assertTrue($container->hasDefinition('mercure.hub.demo')); // Hub instance
114149
$this->assertTrue($container->hasDefinition('mercure.hub.demo.publisher')); // Publisher
115150
$this->assertTrue($container->hasDefinition('mercure.hub.demo.jwt.provider'));

0 commit comments

Comments
 (0)