Ver Fonte

comando OAuthClientCreateCommand. EventListener para oauth

Guillermo Espinoza há 8 anos atrás
pai
commit
1ffe8686e8

+ 9 - 2
Command/CreateOAuthClientCommand.php

@@ -5,6 +5,7 @@ namespace Base\OAuthBundle\Command;
 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 CreateOAuthClientCommand extends ContainerAwareCommand
 {
@@ -15,7 +16,13 @@ class CreateOAuthClientCommand extends ContainerAwareCommand
             ->setName('oauth:client:create')
             ->setDescription('Create OAuht client')
             ->setHelp('This command allows you to create an OAuth client')
-        ;
+            ->addOption(
+                'redirect_uri',
+                'r',
+                InputOption::VALUE_REQUIRED,
+                'OAuth Redirect URI',
+                'http://127.0.0.1'
+            );
     }
 
     /**
@@ -27,7 +34,7 @@ class CreateOAuthClientCommand extends ContainerAwareCommand
         $clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');
         
         $client = $clientManager->createClient();
-        $client->setRedirectUris(array('http://127.0.0.1'));
+        $client->setRedirectUris(array($input->getOption('redirect_uri')));
         $client->setAllowedGrantTypes(array('password', 'token', 'authorization_code'));
         $clientManager->updateClient($client);
         

+ 26 - 0
EventListener/OAuthEventListener.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace Base\OAuthBundle\EventListener;
+
+use FOS\OAuthServerBundle\Event\OAuthEvent;
+
+class OAuthEventListener
+{
+
+    public function onPreAuthorizationProcess(OAuthEvent $event)
+    {
+        if ($user = $event->getUser()) {
+            $event->setAuthorizedClient($event->getClient());
+        }
+    }
+
+    public function onPostAuthorizationProcess(OAuthEvent $event)
+    {
+        if ($event->isAuthorizedClient()) {
+            if (null !== $client = $event->getClient()) {
+                $user = $event->getUser();
+            }
+        }
+    }
+
+}