| 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 | |
| 15 | #[AsCommand(name: 'keyset:generate:oct', description: 'Generate a key set with octet keys (JWK format)',)] |
| 16 | final class OctKeysetGeneratorCommand extends GeneratorCommand |
| 17 | { |
| 18 | protected static $defaultName = 'keyset:generate:oct'; |
| 19 | |
| 20 | protected static $defaultDescription = 'Generate a key set with octet keys (JWK format)'; |
| 21 | |
| 22 | protected function configure(): void |
| 23 | { |
| 24 | parent::configure(); |
| 25 | $this->addArgument('quantity', InputArgument::REQUIRED, 'Quantity of keys in the key set.') |
| 26 | ->addArgument('size', InputArgument::REQUIRED, 'Key size.'); |
| 27 | } |
| 28 | |
| 29 | protected function execute(InputInterface $input, OutputInterface $output): int |
| 30 | { |
| 31 | $quantity = (int) $input->getArgument('quantity'); |
| 32 | $size = (int) $input->getArgument('size'); |
| 33 | if ($quantity < 1) { |
| 34 | throw new InvalidArgumentException('Invalid quantity'); |
| 35 | } |
| 36 | if ($size < 1) { |
| 37 | throw new InvalidArgumentException('Invalid size'); |
| 38 | } |
| 39 | |
| 40 | $keyset = new JWKSet([]); |
| 41 | for ($i = 0; $i < $quantity; ++$i) { |
| 42 | $args = $this->getOptions($input); |
| 43 | $keyset = $keyset->with(JWKFactory::createOctKey($size, $args)); |
| 44 | } |
| 45 | $this->prepareJsonOutput($input, $output, $keyset); |
| 46 | |
| 47 | return self::SUCCESS; |
| 48 | } |
| 49 | } |