Skip to content

Commit 78b56db

Browse files
authored
Merge pull request #812 from web-token/fix/generator-command-factory-setter
fix(console): inject the key factory into the generator commands through a setter
2 parents 85d5df0 + 5af8fc4 commit 78b56db

19 files changed

Lines changed: 162 additions & 27 deletions

‎src/Bundle/Resources/config/commands.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Jose\Component\Console\AddKeyIntoKeysetCommand;
66
use Jose\Component\Console\EcKeyGeneratorCommand;
77
use Jose\Component\Console\EcKeysetGeneratorCommand;
8+
use Jose\Component\Console\GeneratorCommand;
89
use Jose\Component\Console\GetThumbprintCommand;
910
use Jose\Component\Console\KeyAnalyzerCommand;
1011
use Jose\Component\Console\KeyFileLoaderCommand;
@@ -28,11 +29,17 @@
2829
use Jose\Component\Console\RsaKeysetGeneratorCommand;
2930
use Jose\Component\Console\SecretKeyGeneratorCommand;
3031
use Jose\Component\Console\X509CertificateLoaderCommand;
32+
use Jose\Component\KeyManagement\JWKFactoryInterface;
3133
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
34+
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
3235

3336
return function (ContainerConfigurator $container): void {
34-
$container = $container->services()
35-
->defaults()
37+
$services = $container->services();
38+
39+
$services->instanceof(GeneratorCommand::class)
40+
->call('setJwkFactory', [service(JWKFactoryInterface::class)]);
41+
42+
$container = $services->defaults()
3643
->private()
3744
->autoconfigure()
3845
->autowire();

‎src/Library/Console/EcKeyGeneratorCommand.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3131
}
3232
$args = $this->getOptions($input);
3333

34-
$jwk = $this->jwkFactory->ec($curve, $args);
34+
$jwk = $this->getJwkFactory()
35+
->ec($curve, $args);
3536
$this->prepareJsonOutput($input, $output, $jwk);
3637

3738
return self::SUCCESS;

‎src/Library/Console/EcKeysetGeneratorCommand.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3939
$keyset = new JWKSet([]);
4040
for ($i = 0; $i < $quantity; ++$i) {
4141
$args = $this->getOptions($input);
42-
$keyset = $keyset->with($this->jwkFactory->ec($curve, $args));
42+
$keyset = $keyset->with($this->getJwkFactory()->ec($curve, $args));
4343
}
4444
$this->prepareJsonOutput($input, $output, $keyset);
4545

‎src/Library/Console/GeneratorCommand.php‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@
1515

1616
abstract class GeneratorCommand extends ObjectOutputCommand
1717
{
18-
protected readonly JWKFactoryInterface $jwkFactory;
19-
2018
/**
21-
* The key factory is the second argument, and not the first as in the other commands of the library, so that the
22-
* name inherited from the Symfony command stays where it was: "new EcKeyGeneratorCommand('my:command')" keeps
23-
* working.
19+
* The factory is injected through a setter and held in a private property, so that nothing this abstract command
20+
* publishes changes: the constructor inherited from the Symfony command keeps the signature it always had, and a
21+
* subclass that already declares a member of that name is left alone.
2422
*/
25-
public function __construct(?string $name = null, ?JWKFactoryInterface $jwkFactory = null)
23+
private ?JWKFactoryInterface $jwkFactory = null;
24+
25+
public function setJwkFactory(JWKFactoryInterface $jwkFactory): void
2626
{
27-
parent::__construct($name);
28-
$this->jwkFactory = $jwkFactory ?? new JWKFactory();
27+
$this->jwkFactory = $jwkFactory;
2928
}
3029

3130
#[Override]
@@ -72,6 +71,14 @@ protected function getOptions(InputInterface $input): array
7271
return $args;
7372
}
7473

74+
/**
75+
* Falls back to a default factory, so that the commands stay usable outside of a container.
76+
*/
77+
protected function getJwkFactory(): JWKFactoryInterface
78+
{
79+
return $this->jwkFactory ??= new JWKFactory();
80+
}
81+
7582
private function generateKeyID(): string
7683
{
7784
return Base64UrlSafe::encode(random_bytes(32));

‎src/Library/Console/KeyFileLoaderCommand.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3737
}
3838
$args = $this->getOptions($input);
3939

40-
$jwk = $this->jwkFactory->fromKeyFile($file, $password, $args);
40+
$jwk = $this->getJwkFactory()
41+
->fromKeyFile($file, $password, $args);
4142
$this->prepareJsonOutput($input, $output, $jwk);
4243

4344
return self::SUCCESS;

‎src/Library/Console/MldsaKeyGeneratorCommand.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3131
}
3232
$args = $this->getOptions($input);
3333

34-
$jwk = $this->jwkFactory->mldsa($algorithm, $args);
34+
$jwk = $this->getJwkFactory()
35+
->mldsa($algorithm, $args);
3536
$this->prepareJsonOutput($input, $output, $jwk);
3637

3738
return self::SUCCESS;

‎src/Library/Console/MldsaKeysetGeneratorCommand.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4444
$keyset = new JWKSet([]);
4545
for ($i = 0; $i < $quantity; ++$i) {
4646
$args = $this->getOptions($input);
47-
$keyset = $keyset->with($this->jwkFactory->mldsa($algorithm, $args));
47+
$keyset = $keyset->with($this->getJwkFactory()->mldsa($algorithm, $args));
4848
}
4949
$this->prepareJsonOutput($input, $output, $keyset);
5050

‎src/Library/Console/NoneKeyGeneratorCommand.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
2020
{
2121
$args = $this->getOptions($input);
2222

23-
$jwk = $this->jwkFactory->none($args);
23+
$jwk = $this->getJwkFactory()
24+
->none($args);
2425
$this->prepareJsonOutput($input, $output, $jwk);
2526

2627
return self::SUCCESS;

‎src/Library/Console/OctKeyGeneratorCommand.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3030
}
3131
$args = $this->getOptions($input);
3232

33-
$jwk = $this->jwkFactory->oct($size, $args);
33+
$jwk = $this->getJwkFactory()
34+
->oct($size, $args);
3435
$this->prepareJsonOutput($input, $output, $jwk);
3536

3637
return self::SUCCESS;

‎src/Library/Console/OctKeysetGeneratorCommand.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3838
$keyset = new JWKSet([]);
3939
for ($i = 0; $i < $quantity; ++$i) {
4040
$args = $this->getOptions($input);
41-
$keyset = $keyset->with($this->jwkFactory->oct($size, $args));
41+
$keyset = $keyset->with($this->getJwkFactory()->oct($size, $args));
4242
}
4343
$this->prepareJsonOutput($input, $output, $keyset);
4444

0 commit comments

Comments
 (0)