forked from thephpleague/oauth2-server-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateClientCommand.php
157 lines (137 loc) · 5.36 KB
/
CreateClientCommand.php
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
157
<?php
declare(strict_types=1);
namespace League\Bundle\OAuth2ServerBundle\Command;
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
use League\Bundle\OAuth2ServerBundle\Model\AbstractClient;
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\InputArgument;
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:create-client', description: 'Creates a new OAuth2 client')]
final class CreateClientCommand extends Command
{
/**
* @var ClientManagerInterface
*/
private $clientManager;
/**
* @var string
*/
private $clientFqcn;
public function __construct(ClientManagerInterface $clientManager, string $clientFqcn)
{
parent::__construct();
$this->clientManager = $clientManager;
$this->clientFqcn = $clientFqcn;
}
protected function configure(): void
{
$this
->setDescription('Creates a new OAuth2 client')
->addOption(
'redirect-uri',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Sets redirect uri for client. Use this option multiple times to set multiple redirect URIs.',
[]
)
->addOption(
'grant-type',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Sets allowed grant type for client. Use this option multiple times to set multiple grant types.',
[]
)
->addOption(
'scope',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Sets allowed scope for client. Use this option multiple times to set multiple scopes.',
[]
)
->addOption(
'public',
null,
InputOption::VALUE_NONE,
'Create a public client.'
)
->addOption(
'allow-plain-text-pkce',
null,
InputOption::VALUE_NONE,
'Create a client who is allowed to use plain challenge method for PKCE.'
)
->addArgument(
'name',
InputArgument::REQUIRED,
'The client name'
)
->addArgument(
'identifier',
InputArgument::OPTIONAL,
'The client identifier'
)
->addArgument(
'secret',
InputArgument::OPTIONAL,
'The client secret'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
try {
$client = $this->buildClientFromInput($input);
} catch (\InvalidArgumentException $exception) {
$io->error($exception->getMessage());
return 1;
}
$this->clientManager->save($client);
$io->success('New OAuth2 client created successfully.');
$headers = ['Identifier', 'Secret'];
$rows = [
[$client->getIdentifier(), $client->getSecret()],
];
$io->table($headers, $rows);
return 0;
}
private function buildClientFromInput(InputInterface $input): ClientInterface
{
$name = $input->getArgument('name');
$identifier = (string) $input->getArgument('identifier') ?: hash('md5', random_bytes(16));
$isPublic = $input->getOption('public');
if ($isPublic && null !== $input->getArgument('secret')) {
throw new \InvalidArgumentException('The client cannot have a secret and be public.');
}
$secret = $isPublic ? null : $input->getArgument('secret') ?? hash('sha512', random_bytes(32));
/** @var AbstractClient $client */
$client = new $this->clientFqcn($name, $identifier, $secret);
$client->setActive(true);
$client->setAllowPlainTextPkce($input->getOption('allow-plain-text-pkce'));
/** @var list<string> $redirectUriStrings */
$redirectUriStrings = $input->getOption('redirect-uri');
/** @var list<string> $grantStrings */
$grantStrings = $input->getOption('grant-type');
/** @var list<string> $scopeStrings */
$scopeStrings = $input->getOption('scope');
return $client
->setRedirectUris(...array_map(static function (string $redirectUri): RedirectUri {
return new RedirectUri($redirectUri);
}, $redirectUriStrings))
->setGrants(...array_map(static function (string $grant): Grant {
return new Grant($grant);
}, $grantStrings))
->setScopes(...array_map(static function (string $scope): Scope {
return new Scope($scope);
}, $scopeStrings))
;
}
}