123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace Base\OAuthServerBundle\Command;
- use \Base\OAuthServerBundle\Entity\OAuthClient;
- use \FOS\OAuthServerBundle\Entity\ClientManager;
- use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\Console\Input\InputOption;
- class OAuthClientCreateRedirectsCommand extends ContainerAwareCommand
- {
- protected function configure()
- {
- $this
- ->setName('oauth:client:createRedirects')
- ->setDescription('Create OAuht client for uri\'s.')
- ->setHelp('This command allows you to create an OAuth client with many uri\'s')
- ->addOption(
- 'redirect_uri', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'OAuth Redirect URI. Example: http://127.0.0.1/ftth/login_check'
- )
- ->addOption(
- 'client_id', null, InputOption::VALUE_OPTIONAL, 'OAuth Client random Id'
- )
- ->addOption(
- 'client_secret', null, InputOption::VALUE_OPTIONAL, 'OAuth Client random Secret'
- );
- }
- /**
- * @param InputInterface $input
- * @param OutputInterface $output
- */
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $redirectUri = $input->getOption('redirect_uri');
- $random = $input->getOption('client_id');
- $secret = $input->getOption('client_secret');
- $client = $this->createClient($redirectUri, $random, $secret);
- $output->writeln('<info>OAUTH_CLIENT_ID</info>=' . $client->getPublicId());
- $output->writeln('<info>OAUTH_CLIENT_SECRET</info>=' . $client->getSecret());
- }
- /**
- * @param array $redirectUri
- * @param string $random
- * @param string $secret
- *
- * @return OAuthClient
- */
- protected function createClient($redirectUri, $random = null, $secret = null)
- {
- /* @var $clientManager ClientManager */
- $clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');
- $em = $this->getContainer()->get('doctrine')->getEntityManager();
- /* @var $client OAuthClient */
- $client = $clientManager->createClient();
- if ($random) {
- $client->setRandomId($random);
- }
- if ($secret) {
- $client->setSecret($secret);
- }
- $client->setRedirectUris(
- $redirectUri
- );
- $client->setAllowedGrantTypes(array_keys(OAuthClient::getGrantTypesChoices()));
- $clientManager->updateClient($client);
- return $client;
- }
- }
|