From 74777c2234f1459d201a9d84aabf257e0b77204f Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 6 Aug 2016 18:07:48 +0200 Subject: [PATCH] Created command to generate a new api key --- module/CLI/config/cli.config.php | 1 + module/CLI/config/dependencies.config.php | 1 + .../CLI/src/Command/Api/DisableKeyCommand.php | 6 ++ .../src/Command/Api/GenerateKeyCommand.php | 56 +++++++++++++++++++ .../Command/Api/GenerateKeyCommandTest.php | 55 ++++++++++++++++++ module/Rest/config/dependencies.config.php | 1 + module/Rest/src/Entity/ApiKey.php | 10 ++++ 7 files changed, 130 insertions(+) create mode 100644 module/CLI/src/Command/Api/DisableKeyCommand.php create mode 100644 module/CLI/src/Command/Api/GenerateKeyCommand.php create mode 100644 module/CLI/test/Command/Api/GenerateKeyCommandTest.php diff --git a/module/CLI/config/cli.config.php b/module/CLI/config/cli.config.php index a9b13a72..3d9ff486 100644 --- a/module/CLI/config/cli.config.php +++ b/module/CLI/config/cli.config.php @@ -11,6 +11,7 @@ return [ Command\GetVisitsCommand::class, Command\ProcessVisitsCommand::class, Command\Config\GenerateCharsetCommand::class, + Command\Api\GenerateKeyCommand::class, ] ], diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index d99f68d9..ca671731 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -17,6 +17,7 @@ return [ Command\ProcessVisitsCommand::class => AnnotatedFactory::class, Command\ProcessVisitsCommand::class => AnnotatedFactory::class, Command\Config\GenerateCharsetCommand::class => AnnotatedFactory::class, + Command\Api\GenerateKeyCommand::class => AnnotatedFactory::class, ], ], diff --git a/module/CLI/src/Command/Api/DisableKeyCommand.php b/module/CLI/src/Command/Api/DisableKeyCommand.php new file mode 100644 index 00000000..3e52908e --- /dev/null +++ b/module/CLI/src/Command/Api/DisableKeyCommand.php @@ -0,0 +1,6 @@ +apiKeyService = $apiKeyService; + $this->translator = $translator; + parent::__construct(null); + } + + public function configure() + { + $this->setName('api-key:generate') + ->setDescription($this->translator->translate('Generates a new valid API key.')) + ->addOption( + 'expirationDate', + 'e', + InputOption::VALUE_OPTIONAL, + $this->translator->translate('The date in which the API key should expire. Use any valid PHP format.') + ); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $expirationDate = $input->getOption('expirationDate'); + $apiKey = $this->apiKeyService->create(isset($expirationDate) ? new \DateTime($expirationDate) : null); + $output->writeln($this->translator->translate('Generated API key') . sprintf(': %s', $apiKey)); + } +} diff --git a/module/CLI/test/Command/Api/GenerateKeyCommandTest.php b/module/CLI/test/Command/Api/GenerateKeyCommandTest.php new file mode 100644 index 00000000..b0d44a56 --- /dev/null +++ b/module/CLI/test/Command/Api/GenerateKeyCommandTest.php @@ -0,0 +1,55 @@ +apiKeyService = $this->prophesize(ApiKeyService::class); + $command = new GenerateKeyCommand($this->apiKeyService->reveal(), Translator::factory([])); + $app = new Application(); + $app->add($command); + $this->commandTester = new CommandTester($command); + } + + /** + * @test + */ + public function noExpirationDateIsDefinedIfNotProvided() + { + $this->apiKeyService->create(null)->shouldBeCalledTimes(1); + $this->commandTester->execute([ + 'command' => 'api-key:generate', + ]); + } + + /** + * @test + */ + public function expirationDateIsDefinedIfWhenProvided() + { + $this->apiKeyService->create(Argument::type(\DateTime::class))->shouldBeCalledTimes(1); + $this->commandTester->execute([ + 'command' => 'api-key:generate', + '--expirationDate' => '2016-01-01', + ]); + } +} diff --git a/module/Rest/config/dependencies.config.php b/module/Rest/config/dependencies.config.php index e04c8ba0..8f6dbbfb 100644 --- a/module/Rest/config/dependencies.config.php +++ b/module/Rest/config/dependencies.config.php @@ -10,6 +10,7 @@ return [ 'dependencies' => [ 'factories' => [ Service\RestTokenService::class => AnnotatedFactory::class, + Service\ApiKeyService::class => AnnotatedFactory::class, Action\AuthenticateAction::class => AnnotatedFactory::class, Action\CreateShortcodeAction::class => AnnotatedFactory::class, diff --git a/module/Rest/src/Entity/ApiKey.php b/module/Rest/src/Entity/ApiKey.php index e0600e88..0f458c11 100644 --- a/module/Rest/src/Entity/ApiKey.php +++ b/module/Rest/src/Entity/ApiKey.php @@ -124,4 +124,14 @@ class ApiKey extends AbstractEntity { return $this->isEnabled() && ! $this->isExpired(); } + + /** + * The string repesentation of an API key is the key itself + * + * @return string + */ + public function __toString() + { + return $this->getKey(); + } }