|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Damax\Bundle\ApiAuthBundle\Command\Storage; |
|
6
|
|
|
|
|
7
|
|
|
use Damax\Bundle\ApiAuthBundle\Key\Factory; |
|
8
|
|
|
use Damax\Bundle\ApiAuthBundle\Key\Storage\Writer as Storage; |
|
9
|
|
|
use Symfony\Component\Console\Command\Command; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
15
|
|
|
|
|
16
|
|
|
class AddKeyCommand extends Command |
|
17
|
|
|
{ |
|
18
|
|
|
protected static $defaultName = 'damax:api-auth:storage:add-key'; |
|
19
|
|
|
|
|
20
|
|
|
private $factory; |
|
21
|
|
|
private $storage; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(Factory $factory, Storage $storage) |
|
24
|
|
|
{ |
|
25
|
|
|
parent::__construct(); |
|
26
|
|
|
|
|
27
|
|
|
$this->factory = $factory; |
|
28
|
|
|
$this->storage = $storage; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function configure() |
|
32
|
|
|
{ |
|
33
|
|
|
// 10 years. |
|
34
|
|
|
$ttl = 3600 * 24 * 365 * 10; |
|
35
|
|
|
|
|
36
|
|
|
$this |
|
37
|
|
|
->setDescription('Add api key to storage.') |
|
38
|
|
|
->addArgument('username', InputArgument::REQUIRED, 'Username for the key.') |
|
39
|
|
|
->addOption('ttl', null, InputOption::VALUE_REQUIRED, 'Time to live in seconds.', $ttl) |
|
40
|
|
|
; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
44
|
|
|
{ |
|
45
|
|
|
$key = $this->factory->createKey($input->getArgument('username'), (int) $input->getOption('ttl')); |
|
46
|
|
|
|
|
47
|
|
|
$this->storage->add($key); |
|
48
|
|
|
|
|
49
|
|
|
(new SymfonyStyle($input, $output))->success('Key: ' . (string) $key); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|