|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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 Symfony\Component\Security\Http\Command; |
| 13 | + |
| 14 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Completion\CompletionInput; |
| 17 | +use Symfony\Component\Console\Completion\CompletionSuggestions; |
| 18 | +use Symfony\Component\Console\Completion\Suggestion; |
| 19 | +use Symfony\Component\Console\Input\InputArgument; |
| 20 | +use Symfony\Component\Console\Input\InputInterface; |
| 21 | +use Symfony\Component\Console\Input\InputOption; |
| 22 | +use Symfony\Component\Console\Output\OutputInterface; |
| 23 | +use Symfony\Component\Security\Http\AccessToken\Oidc\OidcTokenGenerator; |
| 24 | + |
| 25 | +#[AsCommand(name: 'security:oidc:generate-token', description: 'Generate an OIDC token for a given user')] |
| 26 | +final class OidcTokenGenerateCommand extends Command |
| 27 | +{ |
| 28 | + /** @var array<string, OidcTokenGenerator> */ |
| 29 | + private array $generators = []; |
| 30 | + /** @var array<string, list<string>> */ |
| 31 | + private array $algorithms; |
| 32 | + /** @var array<string, list<string>> */ |
| 33 | + private array $issuers; |
| 34 | + |
| 35 | + protected function configure(): void |
| 36 | + { |
| 37 | + $this |
| 38 | + ->addArgument('user-identifier', InputArgument::REQUIRED, 'User identifier') |
| 39 | + ->addOption('firewall', null, InputOption::VALUE_REQUIRED, 'Firewall') |
| 40 | + ->addOption('algorithm', null, InputOption::VALUE_REQUIRED, 'Algorithm name to use to sign') |
| 41 | + ->addOption('issuer', null, InputOption::VALUE_REQUIRED, 'Set the Issuer claim (iss)') |
| 42 | + ->addOption('ttl', null, InputOption::VALUE_REQUIRED, 'Set the Expiration Time claim (exp) (time to live in seconds)') |
| 43 | + ->addOption('not-before', null, InputOption::VALUE_REQUIRED, 'Set the Not Before claim (nbf)') |
| 44 | + ; |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + /** |
| 49 | + * @params array<string, list<string>> $algorithms |
| 50 | + * @params array<string, list<string>> $issuers |
| 51 | + */ |
| 52 | + public function addGenerator(string $firewall, OidcTokenGenerator $oidcTokenGenerator, array $algorithms, array $issuers): void |
| 53 | + { |
| 54 | + $this->generators[$firewall] = $oidcTokenGenerator; |
| 55 | + foreach ($algorithms as $algorithm) { |
| 56 | + $this->algorithms[$algorithm] ??= []; |
| 57 | + $this->algorithms[$algorithm][] = $firewall; |
| 58 | + } |
| 59 | + foreach ($issuers as $issuer) { |
| 60 | + $this->issuers[$issuer] ??= []; |
| 61 | + $this->issuers[$issuer][] = $firewall; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 66 | + { |
| 67 | + $generator = $this->getGenerator($input->getOption('firewall')); |
| 68 | + $token = $generator->generate( |
| 69 | + $input->getArgument('user-identifier'), |
| 70 | + $input->getOption('algorithm'), |
| 71 | + $input->getOption('issuer'), |
| 72 | + $input->getOption('ttl'), |
| 73 | + ($nbf = $input->getOption('not-before')) ? new \DateTimeImmutable($nbf) : null, |
| 74 | + ); |
| 75 | + |
| 76 | + $output->writeln($token); |
| 77 | + |
| 78 | + return self::SUCCESS; |
| 79 | + } |
| 80 | + |
| 81 | + private function getGenerator(?string $firewall): OidcTokenGenerator |
| 82 | + { |
| 83 | + if (0 === count($this->generators)) { |
| 84 | + throw new \InvalidArgumentException('No OIDC token generator configured.'); |
| 85 | + } |
| 86 | + |
| 87 | + if ($firewall) { |
| 88 | + return $this->generators[$firewall] ?? throw new \InvalidArgumentException(sprintf('Invalid firewall. Available firewalls: "%s".', implode('", "', array_keys($this->generators)))); |
| 89 | + } |
| 90 | + |
| 91 | + if (1 === count($this->generators)) { |
| 92 | + return end($this->generators); |
| 93 | + } |
| 94 | + |
| 95 | + throw new \InvalidArgumentException(sprintf('Please choose an firewall. Available firewalls: "%s".', implode('", "', array_keys($this->generators)))); |
| 96 | + } |
| 97 | + |
| 98 | + public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void |
| 99 | + { |
| 100 | + if ($input->mustSuggestOptionValuesFor('firewall')) { |
| 101 | + $suggestions->suggestValues(array_keys($this->generators)); |
| 102 | + } |
| 103 | + |
| 104 | + if ($input->mustSuggestOptionValuesFor('algorithm')) { |
| 105 | + foreach ($this->algorithms as $algorithm => $firewalls) { |
| 106 | + $suggestions->suggestValue(new Suggestion($algorithm, sprintf('Available firewalls: "%s".', implode('", "', $firewalls)))); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if ($input->mustSuggestOptionValuesFor('issuer')) { |
| 111 | + foreach ($this->issuers as $issuer => $firewalls) { |
| 112 | + $suggestions->suggestValue(new Suggestion($issuer, sprintf('Available firewalls: "%s".', implode('", "', $firewalls)))); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments