Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Bugs fixed and Add Key command added
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Oct 5, 2017
1 parent 2e98c51 commit 18cb207
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 9 deletions.
1 change: 1 addition & 0 deletions bin/jose
Expand Up @@ -68,6 +68,7 @@ $application->add(new Console\RsaKeysetGeneratorCommand($jsonConverter));
$application->add(new Console\MergeKeysetCommand($jsonConverter));
$application->add(new Console\PublicKeysetCommand($jsonConverter));
$application->add(new Console\RotateKeysetCommand($jsonConverter));
$application->add(new Console\AddKeyIntoKeysetCommand($jsonConverter));

$application->add(new Console\OptimizeRsaKeyCommand($jsonConverter));
$application->add(new Console\KeyAnalyzerCommand($jwkAnalyzerManager, $jsonConverter));
Expand Down
8 changes: 8 additions & 0 deletions src/Bundle/Console/Resources/config/commands.yml
Expand Up @@ -76,6 +76,14 @@ services:
tags:
- {name: 'console.command'}

Jose\Component\Console\RotateKeysetCommand:
tags:
- {name: 'console.command'}

Jose\Component\Console\AddKeyIntoKeysetCommand:
tags:
- {name: 'console.command'}

Jose\Component\Console\PemConverterCommand:
tags:
- {name: 'console.command'}
Expand Down
13 changes: 13 additions & 0 deletions src/Component/Console/AbstractGeneratorCommand.php
Expand Up @@ -13,6 +13,7 @@

namespace Jose\Component\Console;

use Base64Url\Base64Url;
use Jose\Component\KeyManagement\JWKFactory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -39,6 +40,7 @@ protected function configure()
$this
->addOption('use', 'u', InputOption::VALUE_OPTIONAL, 'Usage of the key. Must be either "sig" or "enc".')
->addOption('alg', 'a', InputOption::VALUE_OPTIONAL, 'Algorithm for the key.')
->addOption('random_id', null, InputOption::VALUE_NONE, 'If this option is set, a random key ID (kid) will be generated.')
;
}

Expand All @@ -50,6 +52,9 @@ protected function configure()
protected function getOptions(InputInterface $input): array
{
$args = [];
if ($input->getOption('random_id')) {
$args['kid'] = $this->generateKeyID();
}
foreach (['use', 'alg'] as $key) {
$value = $input->getOption($key);
if (null !== $value) {
Expand All @@ -59,4 +64,12 @@ protected function getOptions(InputInterface $input): array

return $args;
}

/**
* @return string
*/
private function generateKeyID(): string
{
return Base64Url::encode(random_bytes(64));
}
}
96 changes: 96 additions & 0 deletions src/Component/Console/AddKeyIntoKeysetCommand.php
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2017 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace Jose\Component\Console;

use Jose\Component\Core\Converter\JsonConverterInterface;
use Jose\Component\Core\JWK;
use Jose\Component\Core\JWKSet;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class AddKeyIntoKeysetCommand.
*/
final class AddKeyIntoKeysetCommand extends AbstractObjectOutputCommand
{
/**
* KeyAnalyzerCommand constructor.
*
* @param JsonConverterInterface $jsonConverter
* @param string|null $name
*/
public function __construct(JsonConverterInterface $jsonConverter, string $name = null)
{
parent::__construct($jsonConverter, $name);
}

/**
* {@inheritdoc}
*/
protected function configure()
{
parent::configure();
$this
->setName('keyset:add:key')
->setDescription('Add a key into a key set.')
->setHelp('This command adds a key at the end of a key set.')
->addArgument('jwkset', InputArgument::REQUIRED, 'The JWKSet object')
->addArgument('jwk', InputArgument::REQUIRED, 'The new JWK object')
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$jwkset = $this->getKeyset($input);
$jwk = $this->getKey($input);
$jwkset = $jwkset->with($jwk);
$this->prepareJsonOutput($input, $output,$jwkset);
}

/**
* @param InputInterface $input
*
* @return JWKSet
*/
private function getKeyset(InputInterface $input): JWKSet
{
$jwkset = $input->getArgument('jwkset');
$json = $this->jsonConverter->decode($jwkset);
if (is_array($json)) {
return JWKSet::createFromKeyData($json);
}

throw new \InvalidArgumentException('The argument must be a valid JWKSet.');
}

/**
* @param InputInterface $input
*
* @return JWK
*/
private function getKey(InputInterface $input): JWK
{
$jwkset = $input->getArgument('jwk');
$json = $this->jsonConverter->decode($jwkset);
if (is_array($json)) {
return JWK::create($json);
}

throw new \InvalidArgumentException('The argument must be a valid JWK.');
}
}
2 changes: 1 addition & 1 deletion src/Component/Console/EcKeysetGeneratorCommand.php
Expand Up @@ -44,10 +44,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$quantity = (int) $input->getArgument('quantity');
$curve = $input->getArgument('curve');
$args = $this->getOptions($input);

$keyset = JWKSet::createFromKeys([]);
for ($i = 0; $i < $quantity; ++$i) {
$args = $this->getOptions($input);
$keyset = $keyset->with(JWKFactory::createECKey($curve, $args));
}
$this->prepareJsonOutput($input, $output, $keyset);
Expand Down
4 changes: 2 additions & 2 deletions src/Component/Console/MergeKeysetCommand.php
Expand Up @@ -22,7 +22,7 @@
/**
* Class MergeKeysetCommand.
*/
final class MergeKeysetCommand extends AbstractGeneratorCommand
final class MergeKeysetCommand extends AbstractObjectOutputCommand
{
/**
* KeyAnalyzerCommand constructor.
Expand All @@ -42,7 +42,7 @@ protected function configure()
{
parent::configure();
$this
->setName('keyset:convert:merge')
->setName('keyset:merge')
->setDescription('Merge several key sets into one.')
->setHelp('This command merges several key sets into one. It is very useful when you generate e.g. RSA, EC and OKP keys and you want only one key set to rule them all.')
->addArgument('jwksets', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The JWKSet objects')
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Console/OctKeysetGeneratorCommand.php
Expand Up @@ -44,10 +44,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$quantity = (int) $input->getArgument('quantity');
$size = (int) $input->getArgument('size');
$args = $this->getOptions($input);

$keyset = JWKSet::createFromKeys([]);
for ($i = 0; $i < $quantity; ++$i) {
$args = $this->getOptions($input);
$keyset = $keyset->with(JWKFactory::createOctKey($size, $args));
}
$this->prepareJsonOutput($input, $output, $keyset);
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Console/OkpKeysetGeneratorCommand.php
Expand Up @@ -44,10 +44,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$quantity = (int) $input->getArgument('quantity');
$curve = $input->getArgument('curve');
$args = $this->getOptions($input);

$keyset = JWKSet::createFromKeys([]);
for ($i = 0; $i < $quantity; ++$i) {
$args = $this->getOptions($input);
$keyset = $keyset->with(JWKFactory::createOKPKey($curve, $args));
}
$this->prepareJsonOutput($input, $output, $keyset);
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Console/PublicKeysetCommand.php
Expand Up @@ -22,7 +22,7 @@
/**
* Class PublicKeysetCommand.
*/
final class PublicKeysetCommand extends AbstractGeneratorCommand
final class PublicKeysetCommand extends AbstractObjectOutputCommand
{
/**
* KeyAnalyzerCommand constructor.
Expand Down
4 changes: 2 additions & 2 deletions src/Component/Console/RotateKeysetCommand.php
Expand Up @@ -23,7 +23,7 @@
/**
* Class RotateKeysetCommand.
*/
final class RotateKeysetCommand extends AbstractGeneratorCommand
final class RotateKeysetCommand extends AbstractObjectOutputCommand
{
/**
* KeyAnalyzerCommand constructor.
Expand All @@ -43,7 +43,7 @@ protected function configure()
{
parent::configure();
$this
->setName('keyset:convert:rotate')
->setName('keyset:rotate')
->setDescription('Rotate a key set.')
->setHelp('This command removes the last key in a key set a place a new one at the beginning.')
->addArgument('jwkset', InputArgument::REQUIRED, 'The JWKSet object')
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Console/RsaKeysetGeneratorCommand.php
Expand Up @@ -44,10 +44,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$quantity = (int) $input->getArgument('quantity');
$size = (int) $input->getArgument('size');
$args = $this->getOptions($input);

$keyset = JWKSet::createFromKeys([]);
for ($i = 0; $i < $quantity; ++$i) {
$args = $this->getOptions($input);
$keyset = $keyset->with(JWKFactory::createRSAKey($size, $args));
}
$this->prepareJsonOutput($input, $output, $keyset);
Expand Down

0 comments on commit 18cb207

Please sign in to comment.