Skip to content

Commit 9462735

Browse files
GKFXdeguif
andauthored
Update OAuth events example to use new classes (#661)
* Update OAuth events example to use new classes The changes to the Propel example are *not* tested but are based on the changes I have made to my own Symfony project. The Symfony example is deliberately sparse, since the Propel example already exists, and is mostly intended to resolve confusion regarding what the new name of the event is. * OAuth events example (from code review) Co-authored-by: François-Xavier de Guillebon <[email protected]> Co-authored-by: François-Xavier de Guillebon <[email protected]>
1 parent fc4ba11 commit 9462735

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

Resources/doc/the_oauth_event_class.md

+39-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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.
55
By default, the FOSOAuthServerBundle will always show the authorization page to the user
66
when an access token is asked. As an access token has a lifetime, it can be annoying for your
77
users to always accept a client.
@@ -10,7 +10,7 @@ Thanks to the [Event Dispatcher](http://symfony.com/doc/current/components/event
1010
you can listen before, and after the authorization form process. So, you can save the user's choice,
1111
and 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
1414
a `ClientInterface` instance, a `UserInterface` instance (coming from the [Security Component](http://symfony.com/doc/current/book/security.html)),
1515
and 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

2222
namespace 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

2628
class 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

75104
You can build a panel for your users displaying this list. If they remove an entry from this list,
76105
then the authorization page will be displayed to the user like the first time. And, if the user

0 commit comments

Comments
 (0)