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

Commit

Permalink
Rotate Key Set Command
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Oct 5, 2017
1 parent 61f7e2c commit 2e98c51
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 3 deletions.
1 change: 1 addition & 0 deletions bin/jose
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ $application->add(new Console\OctKeysetGeneratorCommand($jsonConverter));
$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\OptimizeRsaKeyCommand($jsonConverter));
$application->add(new Console\KeyAnalyzerCommand($jwkAnalyzerManager, $jsonConverter));
Expand Down
4 changes: 2 additions & 2 deletions src/Component/Console/MergeKeysetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class MergeKeysetCommand
* Class MergeKeysetCommand.
*/
final class MergeKeysetCommand extends AbstractGeneratorCommand
{
Expand All @@ -44,7 +44,7 @@ protected function configure()
$this
->setName('keyset:convert:merge')
->setDescription('Merge several key sets into one.')
->setHelp('This command merge 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.')
->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/PublicKeysetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class PublicKeysetCommand
* Class PublicKeysetCommand.
*/
final class PublicKeysetCommand extends AbstractGeneratorCommand
{
Expand Down
101 changes: 101 additions & 0 deletions src/Component/Console/RotateKeysetCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?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 RotateKeysetCommand.
*/
final class RotateKeysetCommand extends AbstractGeneratorCommand
{
/**
* 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:convert: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')
->addArgument('jwk', InputArgument::REQUIRED, 'The new JWK object')
;
}

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

if (0 !== count($jwkset)) {
array_pop($jwkset);
}
array_unshift($jwkset, $jwk);

$this->prepareJsonOutput($input, $output, JWKSet::createFromKeys($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.');
}
}

0 comments on commit 2e98c51

Please sign in to comment.