1- The OAuthEvent class
1+ OAuth Events
22====================
33
4- When a user accepts to share his data with a client, it's a nice idea to save this state.
4+ When a user accepts to share their data with a client, it's a nice idea to save this state.
55By default, the FOSOAuthServerBundle will always show the authorization page to the user
66when an access token is asked. As an access token has a lifetime, it can be annoying for your
77users to always accept a client.
@@ -10,7 +10,7 @@ Thanks to the [Event Dispatcher](http://symfony.com/doc/current/components/event
1010you can listen before, and after the authorization form process. So, you can save the user's choice,
1111and even bypass the authorization process. Let's look at an example.
1212
13- Assuming we have a _ Many to Many_ relation between clients, and users. An ` OAuthEvent ` contains
13+ Assuming we have a _ Many to Many_ relation between clients, and users. A ` PreAuthorizationEvent ` or ` PostAuthorizationEvent ` contains
1414a ` ClientInterface ` instance, a ` UserInterface ` instance (coming from the [ Security Component] ( http://symfony.com/doc/current/book/security.html ) ),
1515and a flag to determine whether the client has been accepted, or not.
1616
@@ -21,11 +21,13 @@ The following class shows a Propel implementation of a basic listener:
2121
2222namespace Acme\DemoBundle\EventListener;
2323
24- use FOS\OAuthServerBundle\Event\OAuthEvent;
24+ use FOS\OAuthServerBundle\Event\AbstractAuthorizationEvent;
25+ use FOS\OAuthServerBundle\Event\PostAuthorizationEvent;
26+ use FOS\OAuthServerBundle\Event\PreAuthorizationEvent;
2527
2628class OAuthEventListener
2729{
28- public function onPreAuthorizationProcess(OAuthEvent $event)
30+ public function onPreAuthorization(PreAuthorizationEvent $event)
2931 {
3032 if ($user = $this->getUser($event)) {
3133 $event->setAuthorizedClient(
@@ -34,7 +36,7 @@ class OAuthEventListener
3436 }
3537 }
3638
37- public function onPostAuthorizationProcess(OAuthEvent $event)
39+ public function onPostAuthorization(PostAuthorizationEvent $event)
3840 {
3941 if ($event->isAuthorizedClient()) {
4042 if (null !== $client = $event->getClient()) {
@@ -45,7 +47,7 @@ class OAuthEventListener
4547 }
4648 }
4749
48- protected function getUser(OAuthEvent $event)
50+ protected function getUser(AbstractAuthorizationEvent $event)
4951 {
5052 return UserQuery::create()
5153 ->filterByUsername($event->getUser()->getUsername())
@@ -65,12 +67,39 @@ services:
6567 oauth_event_listener :
6668 class : Acme\DemoBundle\EventListener\OAuthEventListener
6769 tags :
68- - { name: kernel.event_listener, event: fos_oauth_server.pre_authorization_process , method: onPreAuthorizationProcess }
69- - { name: kernel.event_listener, event: fos_oauth_server.post_authorization_process , method: onPostAuthorizationProcess }
70+ - { name: kernel.event_listener, event: FOS\OAuthServerBundle\Event\PreAuthorizationEvent , method: onPreAuthorization }
71+ - { name: kernel.event_listener, event: FOS\OAuthServerBundle\Event\PostAuthorizationEvent , method: onPostAuthorization }
7072` ` `
7173
7274
73- ### Next?
75+ ## Using a Symfony EventSubscriber
76+
77+ The name of the event for Symfony's purposes is just the class name of the event class.
78+
79+ ` ` ` php
80+ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
81+
82+ class OAuthEventListener implements EventSubscriberInterface
83+ {
84+ public static function getSubscribedEvents()
85+ {
86+ return [
87+ PreAuthorizationEvent::class => 'onPreAuthorization',
88+ PostAuthorizationEvent::class => 'onPostAuthorization',
89+ ];
90+ }
91+
92+ public function onPreAuthorization(PreAuthorizationEvent $event)
93+ {
94+ }
95+
96+ public function onPostAuthorization(PostAuthorizationEvent $event)
97+ {
98+ }
99+ }
100+ ```
101+
102+ ## Next?
74103
75104You can build a panel for your users displaying this list. If they remove an entry from this list,
76105then the authorization page will be displayed to the user like the first time. And, if the user
0 commit comments