1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-06 05:07:41 +02:00

feat(console): add new command utils:create-token

This commit is contained in:
Awilum
2022-04-27 14:41:06 +03:00
parent e47f65639a
commit db8f6ec382
2 changed files with 63 additions and 1 deletions

View File

@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
/**
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
* and with the full functionality of a traditional CMS!
*
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
*
* Licensed under The MIT License.
*
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*/
namespace Flextype\Console\Commands\Utils;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use function Thermage\div;
use function Thermage\renderToString;
class CreateTokenCommand extends Command
{
protected function configure(): void
{
$this->setName('utils:create-token');
$this->setDescription('Create a new unique token.');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$token = generateToken();
$access_token = generateToken();
$hashed_access_token = generateTokenHash($access_token);
filesystem()->directory(PATH['project'] . '/entries/tokens')->ensureExists();
if (entries()->create('tokens/' . $token, ['hashed_access_token' => $hashed_access_token])) {
$output->write(
renderToString(
div('Success: Token [b]' . $token . '[/b] with secret access token [b]' . $access_token . '[/b] created.',
'bg-success px-2 py-1')
)
);
return Command::SUCCESS;
} else {
$output->write(
renderToString(
div('Failure: Token wasn\'t created.',
'bg-danger px-2 py-1')
)
);
return Command::FAILURE;
}
}
}

View File

@@ -35,6 +35,7 @@ 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 Flextype\Console\Commands\Utils\CreateTokenCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
@@ -61,7 +62,8 @@ class FlextypeConsole extends ConsoleApplication
console()->add(new GenerateTokenCommand());
console()->add(new GenerateTokenHashCommand());
console()->add(new VerifyTokenHashCommand());
console()->add(new CreateTokenCommand());
parent::run();
}
}