| Current Path : /var/www/v3.cesa.co.za/src/UserBundle/Command/ |
| Current File : /var/www/v3.cesa.co.za/src/UserBundle/Command/OauthClientCreateCommand.php |
<?php
namespace App\UserBundle\Command;
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\DependencyInjection\ContainerInterface;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Entities\ClientEntity;
class OauthClientCreateCommand extends Command
{
private ContainerInterface $container;
public function __construct(ContainerInterface $container)
{
parent::__construct();
$this->container = $container;
}
protected function configure()
{
$this
->setName('iga:oauth-server:client-create')
->setDescription('Create a new OAuth2 client')
->addArgument('name', InputArgument::REQUIRED, 'Sets the client name', null)
->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.', null)
->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.', null)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$redirectUris = $input->getOption('redirect-uri');
$grantTypes = $input->getOption('grant-type');
// Get the client manager from the container
$clientManager = $this->container->get('league.oauth2_server.manager.doctrine.client_manager');
// Create a new client
$client = new ClientEntity();
$client->setName($name);
$client->setRedirectUris($redirectUris);
$client->setGrants($grantTypes);
$clientManager->save($client);
$output->writeln(sprintf('Created OAuth2 client with name <info>%s</info>', $name));
$output->writeln(sprintf('Client ID: <info>%s</info>', $client->getIdentifier()));
$output->writeln(sprintf('Client Secret: <info>%s</info>', $client->getSecret()));
$output->writeln('Keep these credentials secure!');
return Command::SUCCESS;
}
}