1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PostmanGeneratorBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Vincent Chalamon <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PostmanGeneratorBundle\CommandParser; |
13
|
|
|
|
14
|
|
|
use PostmanGeneratorBundle\Model\Authentication; |
15
|
|
|
use PostmanGeneratorBundle\Model\AuthenticationValue; |
16
|
|
|
use Ramsey\Uuid\Uuid; |
17
|
|
|
use Symfony\Component\Console\Helper\FormatterHelper; |
18
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
use Symfony\Component\Console\Question\Question; |
22
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
23
|
|
|
|
24
|
|
|
class OAuth2CommandParser implements CommandParserInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var NormalizerInterface |
28
|
|
|
*/ |
29
|
|
|
private $normalizer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $authentication; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $token; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $rootDir; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param NormalizerInterface $normalizer |
48
|
|
|
* @param string $rootDir |
49
|
|
|
* @param string $authentication |
50
|
|
|
*/ |
51
|
|
|
public function __construct(NormalizerInterface $normalizer, $rootDir, $authentication = null) |
52
|
|
|
{ |
53
|
|
|
$this->normalizer = $normalizer; |
54
|
|
|
$this->rootDir = $rootDir; |
55
|
|
|
$this->authentication = $authentication; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function parse(InputInterface $input, OutputInterface $output) |
62
|
|
|
{ |
63
|
|
|
$questionHelper = new QuestionHelper(); |
64
|
|
|
$this->token = $questionHelper->ask($input, $output, new Question('[OAuth2] Please provide an access token: ')); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
71
|
|
|
{ |
72
|
|
|
$authentication = new Authentication(); |
73
|
|
|
$authentication->setId((string) Uuid::uuid4()); |
74
|
|
|
$authentication->setName('OAuth2 authentication'); |
75
|
|
|
|
76
|
|
|
$value = new AuthenticationValue(); |
77
|
|
|
$value->setKey('oauth2_access_token'); |
78
|
|
|
$value->setValue($this->token); |
79
|
|
|
$value->setName('OAuth2 access token'); |
80
|
|
|
$authentication->addValue($value); |
81
|
|
|
|
82
|
|
|
$filename = 'oauth2.json'; |
83
|
|
|
$filepath = $this->rootDir.'/../'.$filename; |
84
|
|
|
|
85
|
|
|
file_put_contents($filepath, json_encode($this->normalizer->normalize($authentication, 'json'))); |
86
|
|
|
|
87
|
|
|
$formatterHelper = new FormatterHelper(); |
88
|
|
|
$text = sprintf('Postman authentication environment has been successfully built in file %s.', $filename); |
89
|
|
|
$output->writeln([ |
90
|
|
|
'', |
91
|
|
|
$formatterHelper->formatBlock($text, 'bg=blue;fg=white', true), |
92
|
|
|
'', |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function supports() |
100
|
|
|
{ |
101
|
|
|
return 'oauth2' === strtolower($this->authentication); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|