-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathListClientsCommand.php
More file actions
156 lines (139 loc) · 5.76 KB
/
ListClientsCommand.php
File metadata and controls
156 lines (139 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
declare(strict_types=1);
namespace League\Bundle\OAuth2ServerBundle\Command;
use League\Bundle\OAuth2ServerBundle\Manager\ClientFilter;
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
use League\Bundle\OAuth2ServerBundle\Model\ClientInterface;
use League\Bundle\OAuth2ServerBundle\ValueObject\Grant;
use League\Bundle\OAuth2ServerBundle\ValueObject\RedirectUri;
use League\Bundle\OAuth2ServerBundle\ValueObject\Scope;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(name: 'league:oauth2-server:list-clients', description: 'Lists existing OAuth2 clients')]
final class ListClientsCommand extends Command
{
private const ALLOWED_COLUMNS = ['name', 'identifier', 'secret', 'scope', 'redirect uri', 'grant type'];
/**
* @var ClientManagerInterface
*/
private $clientManager;
public function __construct(ClientManagerInterface $clientManager)
{
parent::__construct();
$this->clientManager = $clientManager;
}
protected function configure(): void
{
$this
->setDescription('Lists existing OAuth2 clients')
->addOption(
'columns',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'Determine which columns are shown. Can be used multiple times to specify multiple columns.',
self::ALLOWED_COLUMNS
)
->addOption(
'redirect-uri',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'Finds by redirect uri for client. Use this option multiple times to filter by multiple redirect URIs.',
[]
)
->addOption(
'grant-type',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'Finds by allowed grant type for client. Use this option multiple times to filter by multiple grant types.',
[]
)
->addOption(
'scope',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'Finds by allowed scope for client. Use this option multiple times to find by multiple scopes.',
[]
)
->addOption(
'reveal-secret',
null,
InputOption::VALUE_NONE,
'Reveal the client secret in the output.'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$criteria = $this->getFindByCriteria($input);
$clients = $this->clientManager->list($criteria);
$this->drawTable($input, $output, $clients);
return 0;
}
private function getFindByCriteria(InputInterface $input): ClientFilter
{
/** @var list<non-empty-string> $grantStrings */
$grantStrings = $input->getOption('grant-type');
/** @var list<non-empty-string> $redirectUriStrings */
$redirectUriStrings = $input->getOption('redirect-uri');
/** @var list<non-empty-string> $scopeStrings */
$scopeStrings = $input->getOption('scope');
return ClientFilter::create()
->addGrantCriteria(...array_map(static function (string $grant): Grant {
return new Grant($grant);
}, $grantStrings))
->addRedirectUriCriteria(...array_map(static function (string $redirectUri): RedirectUri {
return new RedirectUri($redirectUri);
}, $redirectUriStrings))
->addScopeCriteria(...array_map(static function (string $scope): Scope {
return new Scope($scope);
}, $scopeStrings))
;
}
/**
* @param array<ClientInterface> $clients
*/
private function drawTable(InputInterface $input, OutputInterface $output, array $clients): void
{
$io = new SymfonyStyle($input, $output);
$columns = $this->getColumns($input);
$rows = $this->getRows($clients, $columns, $input->getOption('reveal-secret'));
$io->table($columns, $rows);
}
/**
* @param array<ClientInterface> $clients
* @param array<string> $columns
*
* @return array<array<string>>
*/
private function getRows(array $clients, array $columns, bool $revealSecret): array
{
return array_map(static function (ClientInterface $client) use ($columns, $revealSecret): array {
$values = [
'name' => $client->getName(),
'identifier' => $client->getIdentifier(),
'secret' => $revealSecret ? $client->getSecret() : ($client->isConfidential() ? '****' : '(public)'),
'scope' => implode(', ', $client->getScopes()),
'redirect uri' => implode(', ', $client->getRedirectUris()),
'grant type' => implode(', ', $client->getGrants()),
];
return array_map(static function (string $column) use ($values): string {
return $values[$column] ?? '';
}, $columns);
}, $clients);
}
/**
* @return array<string>
*/
private function getColumns(InputInterface $input): array
{
$requestedColumns = $input->getOption('columns');
$requestedColumns = array_map(static function (string $column): string {
return strtolower(trim($column));
}, $requestedColumns);
return array_intersect($requestedColumns, self::ALLOWED_COLUMNS);
}
}