From b9525908c9d4091885902c2793cd72e58cd1369c Mon Sep 17 00:00:00 2001 From: Awilum Date: Wed, 22 Sep 2021 13:53:47 +0300 Subject: [PATCH] feat(console): add VerifyTokenHashCommand, GenerateTokenHashCommand, GenerateTokenCommand #543 --- .../Commands/Utils/GenerateTokenCommand.php | 31 ++++++++++++++ .../Utils/GenerateTokenHashCommand.php | 31 ++++++++++++++ .../Commands/Utils/VerifyTokenHashCommand.php | 40 +++++++++++++++++++ .../Console/FlextypeConsoleApplication.php | 6 +++ 4 files changed, 108 insertions(+) create mode 100644 src/flextype/core/Console/Commands/Utils/GenerateTokenCommand.php create mode 100644 src/flextype/core/Console/Commands/Utils/GenerateTokenHashCommand.php create mode 100644 src/flextype/core/Console/Commands/Utils/VerifyTokenHashCommand.php diff --git a/src/flextype/core/Console/Commands/Utils/GenerateTokenCommand.php b/src/flextype/core/Console/Commands/Utils/GenerateTokenCommand.php new file mode 100644 index 00000000..b08bf8a1 --- /dev/null +++ b/src/flextype/core/Console/Commands/Utils/GenerateTokenCommand.php @@ -0,0 +1,31 @@ +setName('utils:generate-token'); + $this->setDescription('Generate token.'); + $this->addArgument('length', InputArgument::OPTIONAL, 'Token string length.'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $output->writeln(generateToken($input->getArgument('length') ?? 16)); + return Command::SUCCESS; + } +} \ No newline at end of file diff --git a/src/flextype/core/Console/Commands/Utils/GenerateTokenHashCommand.php b/src/flextype/core/Console/Commands/Utils/GenerateTokenHashCommand.php new file mode 100644 index 00000000..b5a60434 --- /dev/null +++ b/src/flextype/core/Console/Commands/Utils/GenerateTokenHashCommand.php @@ -0,0 +1,31 @@ +setName('utils:generate-token-hash'); + $this->setDescription('Generate token hash.'); + $this->addArgument('token', InputArgument::REQUIRED, 'Token string.'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $output->writeln(generateTokenHash($input->getArgument('token'))); + return Command::SUCCESS; + } +} \ No newline at end of file diff --git a/src/flextype/core/Console/Commands/Utils/VerifyTokenHashCommand.php b/src/flextype/core/Console/Commands/Utils/VerifyTokenHashCommand.php new file mode 100644 index 00000000..d0fbf04f --- /dev/null +++ b/src/flextype/core/Console/Commands/Utils/VerifyTokenHashCommand.php @@ -0,0 +1,40 @@ +setName('utils:verify-token-hash'); + $this->setDescription('Verify token hash.'); + $this->addArgument('token', InputArgument::REQUIRED, 'Token.'); + $this->addArgument('token-hash', InputArgument::REQUIRED, 'Token hash.'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + if (verrifyTokenHash($input->getArgument('token'), $input->getArgument('token-hash') )) { + $io->success('Token ' . $input->getArgument('token') . ' is verified'); + return Command::SUCCESS; + } else { + $io->error('Token ' . $input->getArgument('token') . ' isn\'t verified'); + return Command::FAILURE; + } + } +} \ No newline at end of file diff --git a/src/flextype/core/Console/FlextypeConsoleApplication.php b/src/flextype/core/Console/FlextypeConsoleApplication.php index b28ab6cd..28f8084f 100644 --- a/src/flextype/core/Console/FlextypeConsoleApplication.php +++ b/src/flextype/core/Console/FlextypeConsoleApplication.php @@ -25,6 +25,9 @@ use Flextype\Console\Commands\Cache\CacheSetMultipleCommand; use Flextype\Console\Commands\Cache\CacheDeleteMultipleCommand; use Flextype\Console\Commands\Cache\CacheClearCommand; use Flextype\Console\Commands\Cache\CacheHasCommand; +use Flextype\Console\Commands\Utils\GenerateTokenCommand; +use Flextype\Console\Commands\Utils\GenerateTokenHashCommand; +use Flextype\Console\Commands\Utils\VerifyTokenHashCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -48,6 +51,9 @@ class FlextypeConsoleApplication extends ConsoleApplication console()->add(new CacheDeleteCommand()); console()->add(new CacheClearCommand()); console()->add(new CacheHasCommand()); + console()->add(new GenerateTokenCommand()); + console()->add(new GenerateTokenHashCommand()); + console()->add(new VerifyTokenHashCommand()); parent::run(); }