1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ParcelValue\ApiClient\Domain\AuthenticationToken; |
6
|
|
|
|
7
|
|
|
use ParcelValue\Api\JWT\Helper; |
8
|
|
|
use WebServCo\Framework\Cli\Ansi; |
9
|
|
|
use WebServCo\Framework\Cli\Response; |
10
|
|
|
use WebServCo\Framework\Cli\Sgr; |
11
|
|
|
use WebServCo\Framework\Environment\Config; |
12
|
|
|
use WebServCo\Framework\Interfaces\ResponseInterface; |
13
|
|
|
|
14
|
|
|
final class Command extends \ParcelValue\ApiClient\AbstractController |
15
|
|
|
{ |
16
|
|
|
use \ParcelValue\ApiClient\Traits\ControllerApiTrait; |
17
|
|
|
|
18
|
|
|
protected \WebServCo\Framework\Interfaces\OutputLoggerInterface $outputLogger; |
19
|
|
|
|
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
parent::__construct(); |
23
|
|
|
|
24
|
|
|
$this->outputLogger = new \WebServCo\Framework\Log\CliOutputLogger(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function generate(): ResponseInterface |
28
|
|
|
{ |
29
|
|
|
$this->init(); |
30
|
|
|
|
31
|
|
|
$this->outputLogger->output(Ansi::clear(), true); |
32
|
|
|
$this->outputLogger->output(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true); |
33
|
|
|
$this->outputLogger->output(''); |
34
|
|
|
|
35
|
|
|
$jwt = Helper::generate( |
36
|
|
|
Config::string('APP_API_CLIENT_ID'), |
37
|
|
|
Config::string('APP_API_CLIENT_KEY'), |
38
|
|
|
Config::string('APP_API_SERVER_KEY'), |
39
|
|
|
); |
40
|
|
|
$this->outputLogger->output(Ansi::sgr('Success!', [Sgr::GREEN]), false); |
41
|
|
|
$this->outputLogger->output(' Your token is:', true); |
42
|
|
|
$this->outputLogger->output(''); |
43
|
|
|
$this->outputLogger->output($jwt, true); |
44
|
|
|
$this->outputLogger->output(''); |
45
|
|
|
return new Response(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function validate(?string $token = null): ResponseInterface |
49
|
|
|
{ |
50
|
|
|
$this->init(); |
51
|
|
|
|
52
|
|
|
$this->outputLogger->output(Ansi::clear(), true); |
53
|
|
|
$this->outputLogger->output(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true); |
54
|
|
|
$this->outputLogger->output(''); |
55
|
|
|
$this->outputLogger->output(\sprintf('Input: %s', $token), true); |
56
|
|
|
$this->outputLogger->output(''); |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
if (!$token) { |
60
|
|
|
throw new \InvalidArgumentException('Token is missing.'); |
61
|
|
|
} |
62
|
|
|
// \ParcelValue\Api\JWT\Payload |
63
|
|
|
$result = Helper::decode($token, Config::string('APP_API_SERVER_KEY')); |
64
|
|
|
$this->outputLogger->output(Ansi::sgr('Success!', [Sgr::GREEN]), true); |
65
|
|
|
$this->outputLogger->output(\var_export($result, true), true); |
66
|
|
|
} catch (\Throwable $e) { |
67
|
|
|
$this->outputLogger->output(Ansi::sgr(\sprintf('Error: %s', $e->getMessage()), [Sgr::RED]), true); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->outputLogger->output(''); |
71
|
|
|
return new Response(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|