| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Jose\Component\Console; |
| 6 | |
| 7 | use InvalidArgumentException; |
| 8 | use Jose\Component\Core\JWK; |
| 9 | use Jose\Component\Core\Util\JsonConverter; |
| 10 | use Jose\Component\KeyManagement\KeyConverter\RSAKey; |
| 11 | use Symfony\Component\Console\Attribute\AsCommand; |
| 12 | use Symfony\Component\Console\Input\InputArgument; |
| 13 | use Symfony\Component\Console\Input\InputInterface; |
| 14 | use Symfony\Component\Console\Output\OutputInterface; |
| 15 | use function is_array; |
| 16 | use function is_string; |
| 17 | |
| 18 | #[AsCommand(name: 'key:optimize', description: 'Optimize a RSA key by calculating additional primes (CRT).',)] |
| 19 | final class OptimizeRsaKeyCommand extends ObjectOutputCommand |
| 20 | { |
| 21 | protected static $defaultName = 'key:optimize'; |
| 22 | |
| 23 | protected static $defaultDescription = 'Optimize a RSA key by calculating additional primes (CRT).'; |
| 24 | |
| 25 | protected function configure(): void |
| 26 | { |
| 27 | parent::configure(); |
| 28 | $this->addArgument('jwk', InputArgument::REQUIRED, 'The RSA key.'); |
| 29 | } |
| 30 | |
| 31 | protected function execute(InputInterface $input, OutputInterface $output): int |
| 32 | { |
| 33 | $jwk = $input->getArgument('jwk'); |
| 34 | if (! is_string($jwk)) { |
| 35 | throw new InvalidArgumentException('Invalid JWK'); |
| 36 | } |
| 37 | $json = JsonConverter::decode($jwk); |
| 38 | if (! is_array($json)) { |
| 39 | throw new InvalidArgumentException('Invalid JWK'); |
| 40 | } |
| 41 | $key = RSAKey::createFromJWK(new JWK($json)); |
| 42 | $key->optimize(); |
| 43 | $this->prepareJsonOutput($input, $output, $key->toJwk()); |
| 44 | |
| 45 | return self::SUCCESS; |
| 46 | } |
| 47 | } |