| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Jose\Component\Console; |
| 6 | |
| 7 | use InvalidArgumentException; |
| 8 | use Jose\Component\Core\JWKSet; |
| 9 | use Jose\Component\KeyManagement\JWKFactory; |
| 10 | use Symfony\Component\Console\Attribute\AsCommand; |
| 11 | use Symfony\Component\Console\Input\InputArgument; |
| 12 | use Symfony\Component\Console\Input\InputInterface; |
| 13 | use Symfony\Component\Console\Output\OutputInterface; |
| 14 | use function is_string; |
| 15 | |
| 16 | #[AsCommand(name: 'keyset:generate:ec', description: 'Generate an EC key set (JWKSet format)',)] |
| 17 | final class EcKeysetGeneratorCommand extends GeneratorCommand |
| 18 | { |
| 19 | protected static $defaultName = 'keyset:generate:ec'; |
| 20 | |
| 21 | protected static $defaultDescription = 'Generate an EC key set (JWKSet format)'; |
| 22 | |
| 23 | protected function configure(): void |
| 24 | { |
| 25 | parent::configure(); |
| 26 | $this->addArgument('quantity', InputArgument::REQUIRED, 'Quantity of keys in the key set.') |
| 27 | ->addArgument('curve', InputArgument::REQUIRED, 'Curve of the keys.'); |
| 28 | } |
| 29 | |
| 30 | protected function execute(InputInterface $input, OutputInterface $output): int |
| 31 | { |
| 32 | $quantity = (int) $input->getArgument('quantity'); |
| 33 | if ($quantity < 1) { |
| 34 | throw new InvalidArgumentException('Invalid quantity'); |
| 35 | } |
| 36 | $curve = $input->getArgument('curve'); |
| 37 | if (! is_string($curve)) { |
| 38 | throw new InvalidArgumentException('Invalid curve'); |
| 39 | } |
| 40 | |
| 41 | $keyset = new JWKSet([]); |
| 42 | for ($i = 0; $i < $quantity; ++$i) { |
| 43 | $args = $this->getOptions($input); |
| 44 | $keyset = $keyset->with(JWKFactory::createECKey($curve, $args)); |
| 45 | } |
| 46 | $this->prepareJsonOutput($input, $output, $keyset); |
| 47 | |
| 48 | return self::SUCCESS; |
| 49 | } |
| 50 | } |