1
- The OAuthEvent class
1
+ OAuth Events
2
2
====================
3
3
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.
5
5
By default, the FOSOAuthServerBundle will always show the authorization page to the user
6
6
when an access token is asked. As an access token has a lifetime, it can be annoying for your
7
7
users to always accept a client.
@@ -10,7 +10,7 @@ Thanks to the [Event Dispatcher](http://symfony.com/doc/current/components/event
10
10
you can listen before, and after the authorization form process. So, you can save the user's choice,
11
11
and even bypass the authorization process. Let's look at an example.
12
12
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
14
14
a ` ClientInterface ` instance, a ` UserInterface ` instance (coming from the [ Security Component] ( http://symfony.com/doc/current/book/security.html ) ),
15
15
and a flag to determine whether the client has been accepted, or not.
16
16
@@ -21,11 +21,13 @@ The following class shows a Propel implementation of a basic listener:
21
21
22
22
namespace Acme\DemoBundle\EventListener;
23
23
24
- use FOS\OAuthServerBundle\Event\OAuthEvent;
24
+ use FOS\OAuthServerBundle\Event\AbstractAuthorizationEvent;
25
+ use FOS\OAuthServerBundle\Event\PostAuthorizationEvent;
26
+ use FOS\OAuthServerBundle\Event\PreAuthorizationEvent;
25
27
26
28
class OAuthEventListener
27
29
{
28
- public function onPreAuthorizationProcess(OAuthEvent $event)
30
+ public function onPreAuthorization(PreAuthorizationEvent $event)
29
31
{
30
32
if ($user = $this->getUser($event)) {
31
33
$event->setAuthorizedClient(
@@ -34,7 +36,7 @@ class OAuthEventListener
34
36
}
35
37
}
36
38
37
- public function onPostAuthorizationProcess(OAuthEvent $event)
39
+ public function onPostAuthorization(PostAuthorizationEvent $event)
38
40
{
39
41
if ($event->isAuthorizedClient()) {
40
42
if (null !== $client = $event->getClient()) {
@@ -45,7 +47,7 @@ class OAuthEventListener
45
47
}
46
48
}
47
49
48
- protected function getUser(OAuthEvent $event)
50
+ protected function getUser(AbstractAuthorizationEvent $event)
49
51
{
50
52
return UserQuery::create()
51
53
->filterByUsername($event->getUser()->getUsername())
@@ -65,12 +67,39 @@ services:
65
67
oauth_event_listener :
66
68
class : Acme\DemoBundle\EventListener\OAuthEventListener
67
69
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 }
70
72
` ` `
71
73
72
74
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?
74
103
75
104
You can build a panel for your users displaying this list. If they remove an entry from this list,
76
105
then the authorization page will be displayed to the user like the first time. And, if the user
0 commit comments