-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathCreateClientCommand.php
More file actions
175 lines (151 loc) · 6.21 KB
/
CreateClientCommand.php
File metadata and controls
175 lines (151 loc) · 6.21 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?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;
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
#[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;
private ?PasswordHasherInterface $passwordHasher = null;
public function __construct(ClientManagerInterface $clientManager, string $clientFqcn, ?PasswordHasherInterface $passwordHasher = null)
{
parent::__construct();
$this->clientManager = $clientManager;
$this->clientFqcn = $clientFqcn;
$this->passwordHasher = $passwordHasher;
if (null === $this->passwordHasher) {
trigger_deprecation('league/oauth2-server-bundle', '1.2', 'Not passing a "%s" to "%s" is deprecated since version 1.2 and will be required in 2.0.', PasswordHasherInterface::class, __CLASS__);
}
}
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 {
$plainSecret = $this->resolvePlainSecret($input);
$client = $this->buildClientFromInput($input, $plainSecret);
} 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(), $plainSecret],
];
$io->table($headers, $rows);
return 0;
}
private function resolvePlainSecret(InputInterface $input): ?string
{
$isPublic = $input->getOption('public');
if ($isPublic && null !== $input->getArgument('secret')) {
throw new \InvalidArgumentException('The client cannot have a secret and be public.');
}
return $isPublic ? null : $input->getArgument('secret') ?? bin2hex(random_bytes(32));
}
private function buildClientFromInput(InputInterface $input, ?string $plainSecret): ClientInterface
{
$name = $input->getArgument('name');
$identifier = (string) $input->getArgument('identifier') ?: hash('md5', random_bytes(16));
$hashedSecret = $plainSecret;
if ($this->passwordHasher && \is_string($plainSecret)) {
$hashedSecret = $this->passwordHasher->hash($plainSecret);
}
/** @var AbstractClient $client */
$client = new $this->clientFqcn($name, $identifier, $hashedSecret);
$client->setActive(true);
$client->setAllowPlainTextPkce($input->getOption('allow-plain-text-pkce'));
/** @var list<non-empty-string> $redirectUriStrings */
$redirectUriStrings = $input->getOption('redirect-uri');
/** @var list<non-empty-string> $grantStrings */
$grantStrings = $input->getOption('grant-type');
/** @var list<non-empty-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))
;
}
}