OAuthClientCreateRedirectsCommand.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Base\OAuthServerBundle\Command;
  3. use \Base\OAuthServerBundle\Entity\OAuthClient;
  4. use \FOS\OAuthServerBundle\Entity\ClientManager;
  5. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Symfony\Component\Console\Input\InputOption;
  9. class OAuthClientCreateRedirectsCommand extends ContainerAwareCommand
  10. {
  11. protected function configure()
  12. {
  13. $this
  14. ->setName('oauth:client:createRedirects')
  15. ->setDescription('Create OAuht client for uri\'s.')
  16. ->setHelp('This command allows you to create an OAuth client with many uri\'s')
  17. ->addOption(
  18. 'redirect_uri', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'OAuth Redirect URI. Example: http://127.0.0.1/ftth/login_check'
  19. )
  20. ->addOption(
  21. 'client_id', null, InputOption::VALUE_OPTIONAL, 'OAuth Client random Id'
  22. )
  23. ->addOption(
  24. 'client_secret', null, InputOption::VALUE_OPTIONAL, 'OAuth Client random Secret'
  25. );
  26. }
  27. /**
  28. * @param InputInterface $input
  29. * @param OutputInterface $output
  30. */
  31. protected function execute(InputInterface $input, OutputInterface $output)
  32. {
  33. $redirectUri = $input->getOption('redirect_uri');
  34. $random = $input->getOption('client_id');
  35. $secret = $input->getOption('client_secret');
  36. $client = $this->createClient($redirectUri, $random, $secret);
  37. $output->writeln('<info>OAUTH_CLIENT_ID</info>=' . $client->getPublicId());
  38. $output->writeln('<info>OAUTH_CLIENT_SECRET</info>=' . $client->getSecret());
  39. }
  40. /**
  41. * @param array $redirectUri
  42. * @param string $random
  43. * @param string $secret
  44. *
  45. * @return OAuthClient
  46. */
  47. protected function createClient($redirectUri, $random = null, $secret = null)
  48. {
  49. /* @var $clientManager ClientManager */
  50. $clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');
  51. $em = $this->getContainer()->get('doctrine')->getEntityManager();
  52. /* @var $client OAuthClient */
  53. $client = $clientManager->createClient();
  54. if ($random) {
  55. $client->setRandomId($random);
  56. }
  57. if ($secret) {
  58. $client->setSecret($secret);
  59. }
  60. $client->setRedirectUris(
  61. $redirectUri
  62. );
  63. $client->setAllowedGrantTypes(array_keys(OAuthClient::getGrantTypesChoices()));
  64. $clientManager->updateClient($client);
  65. return $client;
  66. }
  67. }