| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Jose\Component\Console; |
| 6 | |
| 7 | use InvalidArgumentException; |
| 8 | use Jose\Component\KeyManagement\JWKFactory; |
| 9 | use Symfony\Component\Console\Attribute\AsCommand; |
| 10 | use Symfony\Component\Console\Input\InputArgument; |
| 11 | use Symfony\Component\Console\Input\InputInterface; |
| 12 | use Symfony\Component\Console\Output\OutputInterface; |
| 13 | |
| 14 | #[AsCommand(name: 'key:generate:rsa', description: 'Generate a RSA key (JWK format)',)] |
| 15 | final class RsaKeyGeneratorCommand extends GeneratorCommand |
| 16 | { |
| 17 | protected static $defaultName = 'key:generate:rsa'; |
| 18 | |
| 19 | protected static $defaultDescription = 'Generate a RSA key (JWK format)'; |
| 20 | |
| 21 | protected function configure(): void |
| 22 | { |
| 23 | parent::configure(); |
| 24 | $this->addArgument('size', InputArgument::REQUIRED, 'Key size.'); |
| 25 | } |
| 26 | |
| 27 | protected function execute(InputInterface $input, OutputInterface $output): int |
| 28 | { |
| 29 | $size = (int) $input->getArgument('size'); |
| 30 | $args = $this->getOptions($input); |
| 31 | if ($size < 1) { |
| 32 | throw new InvalidArgumentException('Invalid size'); |
| 33 | } |
| 34 | |
| 35 | $jwk = JWKFactory::createRSAKey($size, $args); |
| 36 | $this->prepareJsonOutput($input, $output, $jwk); |
| 37 | |
| 38 | return self::SUCCESS; |
| 39 | } |
| 40 | } |