Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added OAuthEvent

Allows to do actions before, and after the authorization form process
  • Loading branch information...
commit 96192518bdef5e91ccf624a27639e5e034dd66f8 1 parent 0a0af81
@willdurand willdurand authored
Showing with 133 additions and 18 deletions.
  1. +53 −18 Controller/AuthorizeController.php
  2. +80 −0 Event/OAuthEvent.php
View
71 Controller/AuthorizeController.php
@@ -11,6 +11,8 @@
namespace FOS\OAuthServerBundle\Controller;
+use FOS\OAuthServerBundle\Event\OAuthEvent;
+use FOS\OAuthServerBundle\Form\Handler\AuthorizeFormHandler;
use OAuth2\OAuth2;
use OAuth2\OAuth2ServerException;
use OAuth2\OAuth2RedirectException;
@@ -43,38 +45,53 @@ public function authorizeAction(Request $request)
$form = $this->container->get('fos_oauth_server.authorize.form');
$formHandler = $this->container->get('fos_oauth_server.authorize.form.handler');
- if ($process = $formHandler->process()) {
- if (true === $this->container->get('session')->get('_fos_oauth_server.ensure_logout')) {
- $this->container->get('session')->invalidate();
- }
+ $event = $this->container->get('event_dispatcher')->dispatch(
+ OAuthEvent::PRE_AUTHORIZATION_PROCESS,
+ new OAuthEvent($user, $this->getClient())
+ );
- try {
- return $server->finishClientAuthorization($formHandler->isAccepted(), $user, null, null);
- } catch (OAuth2ServerException $e) {
- return $e->getHttpResponse();
- }
+ if ($event->isAuthorizedClient()) {
+ return $server->finishClientAuthorization(true, $user, null, null);
}
- $client = $this->container
- ->get('fos_oauth_server.client_manager')
- ->findClientByPublicId(
- $this->container->get('request')->query->get('client_id')
- );
-
- if (null === $client) {
- throw new NotFoundHttpException('No client found.');
+ if (true === $formHandler->process()) {
+ return $this->processSuccess($user, $formHandler);
}
return $this->container->get('templating')->renderResponse(
'FOSOAuthServerBundle:Authorize:authorize.html.' . $this->container->getParameter('fos_oauth_server.template.engine'),
array(
'form' => $form->createView(),
- 'client' => $client,
+ 'client' => $this->getClient(),
)
);
}
/**
+ * @param UserInterface $user
+ * @param AuthorizeFormHandler $formHandler
+ *
+ * @return Response
+ */
+ protected function processSuccess(UserInterface $user, AuthorizeFormHandler $formHandler)
+ {
+ if (true === $this->container->get('session')->get('_fos_oauth_server.ensure_logout')) {
+ $this->container->get('session')->invalidate();
+ }
+
+ $this->container->get('dispatcher')->dispatch(
+ OAuthEvent::POST_AUTHORIZATION_PROCESS,
+ new OAuthEvent($user, $this->getClient(), $formHandler->isAccepted())
+ );
+
+ try {
+ return $server->finishClientAuthorization($formHandler->isAccepted(), $user, null, null);
+ } catch (OAuth2ServerException $e) {
+ return $e->getHttpResponse();
+ }
+ }
+
+ /**
* Generate the redirection url when the authorize is completed
*
* @param \FOS\OAuthServerBundle\Model\UserInterface $user
@@ -84,4 +101,22 @@ protected function getRedirectionUrl(UserInterface $user)
{
return $this->container->get('router')->generate('fos_oauth_server_profile_show');
}
+
+ /**
+@return ClientInterface
+ */
+ protected function getClient()
+ {
+ $client = $this->container
+ ->get('fos_oauth_server.client_manager')
+ ->findClientByPublicId(
+ $this->container->get('request')->query->get('client_id')
+ );
+
+ if (null === $client) {
+ throw new NotFoundHttpException('Client not found.');
+ }
+
+ return $client;
+ }
}
View
80 Event/OAuthEvent.php
@@ -0,0 +1,80 @@
+<?php
+
+/*
+ * This file is part of the FOSOAuthServerBundle package.
+ *
+ * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace FOS\OAuthServerBundle\Event;
+
+use FOS\OAuthServerBundle\Model\ClientInterface;
+use Symfony\Component\EventDispatcher\Event;
+use Symfony\Component\Security\Core\User\UserInterface;
+
+class OAuthEvent extends Event
+{
+ const PRE_AUTHORIZATION_PROCESS = 'fos_oauth_server.pre_authorization_process';
+
+ const POST_AUTHORIZATION_PROCESS = 'fos_oauth_server.post_authorization_process';
+
+ /**
+ * @var \Symfony\Component\Security\Core\User\UserInterface
+ */
+ private $user;
+
+ /**
+ * @var \FOS\FOSOAuthServerBundle\Model\ClientInterface
+ */
+ private $client;
+
+ /**
+ * @var Boolean
+ */
+ private $isAuthorizedClient;
+
+ /**
+ * @param UserInterface $user
+ */
+ public function __construct(UserInterface $user, ClientInterface $client, $isAuthorizedClient = false)
+ {
+ $this->user = $user;
+ $this->client = $client;
+ $this->isAuthorizedClient = $isAuthorizedClient;
+ }
+
+ /**
+ * return UserInterface
+ */
+ public function getUser()
+ {
+ return $this->user;
+ }
+
+ /**
+ * @param Boolean $isAuthorizedClient
+ */
+ public function setAuthorizedClient($isAuthorizedClient)
+ {
+ $this->isAuthorizedClient = $isAuthorizedClient;
+ }
+
+ /**
+ * @return Boolean
+ */
+ public function isAuthorizedClient()
+ {
+ return $this->isAuthorizedClient;
+ }
+
+ /**
+ * @return ClientInterface
+ */
+ public function getClient()
+ {
+ return $this->client;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.