Skip to content

Commit 8e22136

Browse files
authored
Use dedicated authorization events (#655)
* Use dedicated authorization events * Add changelog
1 parent 3d4c441 commit 8e22136

File tree

6 files changed

+85
-66
lines changed

6 files changed

+85
-66
lines changed

CHANGELOG-2.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ This changelog references the relevant changes done in 6.0 versions.
1111
* Dropped support for Symfony versions anterior to `4.4` [[#648](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/648)]
1212
* Fixed form submission/validation [[#643](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/643)]
1313
* **[BC break]** Changed signature of method `FOS\OAuthServerBundle\Controller\AuthorizeController::renderAuthorize()` [[#653](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/653)]
14+
* **[BC break]** Removed class `FOS\OAuthServerBundle\Event\OAuthEvent`, use dedicated event classes instead: [[#655](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/655)]
15+
- `OAuthEvent::PRE_AUTHORIZATION_PROCESS` => `FOS\OAuthServerBundle\Event\PreAuthorizationEvent`
16+
- `OAuthEvent::POST_AUTHORIZATION_PROCESS` => `FOS\OAuthServerBundle\Event\PostAuthorizationEvent`
1417
* **[BC break]** Removed support for templating engine [[#653](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/653)]
1518

1619
### 2.0.0-ALPHA0 (2018-05-01)

Controller/AuthorizeController.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
namespace FOS\OAuthServerBundle\Controller;
1515

16-
use FOS\OAuthServerBundle\Event\OAuthEvent;
16+
use FOS\OAuthServerBundle\Event\PostAuthorizationEvent;
17+
use FOS\OAuthServerBundle\Event\PreAuthorizationEvent;
1718
use FOS\OAuthServerBundle\Form\Handler\AuthorizeFormHandler;
1819
use FOS\OAuthServerBundle\Model\ClientInterface;
1920
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
@@ -145,11 +146,8 @@ public function authorizeAction(Request $request)
145146
$form = $this->authorizeForm;
146147
$formHandler = $this->authorizeFormHandler;
147148

148-
/** @var OAuthEvent $event */
149-
$event = $this->eventDispatcher->dispatch(
150-
OAuthEvent::PRE_AUTHORIZATION_PROCESS,
151-
new OAuthEvent($user, $this->getClient())
152-
);
149+
/** @var PreAuthorizationEvent $event */
150+
$event = $this->eventDispatcher->dispatch(new PreAuthorizationEvent($user, $this->getClient()));
153151

154152
if ($event->isAuthorizedClient()) {
155153
$scope = $request->get('scope', null);
@@ -177,10 +175,7 @@ protected function processSuccess(UserInterface $user, AuthorizeFormHandler $for
177175
$this->session->invalidate();
178176
}
179177

180-
$this->eventDispatcher->dispatch(
181-
OAuthEvent::POST_AUTHORIZATION_PROCESS,
182-
new OAuthEvent($user, $this->getClient(), $formHandler->isAccepted())
183-
);
178+
$this->eventDispatcher->dispatch(new PostAuthorizationEvent($user, $this->getClient(), $formHandler->isAccepted()));
184179

185180
$formName = $this->authorizeForm->getName();
186181
if (!$request->query->all() && $request->request->has($formName)) {

Event/OAuthEvent.php renamed to Event/AbstractAuthorizationEvent.php

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@
1414
namespace FOS\OAuthServerBundle\Event;
1515

1616
use FOS\OAuthServerBundle\Model\ClientInterface;
17-
use Symfony\Component\EventDispatcher\Event;
1817
use Symfony\Component\Security\Core\User\UserInterface;
18+
use Symfony\Contracts\EventDispatcher\Event;
1919

20-
class OAuthEvent extends Event
20+
class AbstractAuthorizationEvent extends Event
2121
{
22-
const PRE_AUTHORIZATION_PROCESS = 'fos_oauth_server.pre_authorization_process';
23-
24-
const POST_AUTHORIZATION_PROCESS = 'fos_oauth_server.post_authorization_process';
25-
2622
/**
2723
* @var UserInterface
2824
*/
@@ -38,44 +34,29 @@ class OAuthEvent extends Event
3834
*/
3935
private $isAuthorizedClient;
4036

41-
/**
42-
* @param bool $isAuthorizedClient
43-
*/
44-
public function __construct(UserInterface $user, ClientInterface $client, $isAuthorizedClient = false)
37+
public function __construct(UserInterface $user, ClientInterface $client, bool $isAuthorizedClient = false)
4538
{
4639
$this->user = $user;
4740
$this->client = $client;
4841
$this->isAuthorizedClient = $isAuthorizedClient;
4942
}
5043

51-
/**
52-
* @return UserInterface
53-
*/
54-
public function getUser()
44+
public function getUser(): UserInterface
5545
{
5646
return $this->user;
5747
}
5848

59-
/**
60-
* @param bool $isAuthorizedClient
61-
*/
62-
public function setAuthorizedClient($isAuthorizedClient)
49+
public function setAuthorizedClient(bool $authorized)
6350
{
64-
$this->isAuthorizedClient = $isAuthorizedClient;
51+
$this->isAuthorizedClient = $authorized;
6552
}
6653

67-
/**
68-
* @return bool
69-
*/
70-
public function isAuthorizedClient()
54+
public function isAuthorizedClient(): bool
7155
{
7256
return $this->isAuthorizedClient;
7357
}
7458

75-
/**
76-
* @return ClientInterface
77-
*/
78-
public function getClient()
59+
public function getClient(): ClientInterface
7960
{
8061
return $this->client;
8162
}

Event/PostAuthorizationEvent.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the FOSOAuthServerBundle package.
7+
*
8+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace FOS\OAuthServerBundle\Event;
15+
16+
class PostAuthorizationEvent extends AbstractAuthorizationEvent
17+
{
18+
}

Event/PreAuthorizationEvent.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the FOSOAuthServerBundle package.
7+
*
8+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace FOS\OAuthServerBundle\Event;
15+
16+
class PreAuthorizationEvent extends AbstractAuthorizationEvent
17+
{
18+
}

Tests/Controller/AuthorizeControllerTest.php

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
namespace FOS\OAuthServerBundle\Tests\Controller;
1515

1616
use FOS\OAuthServerBundle\Controller\AuthorizeController;
17-
use FOS\OAuthServerBundle\Event\OAuthEvent;
17+
use FOS\OAuthServerBundle\Event\PostAuthorizationEvent;
18+
use FOS\OAuthServerBundle\Event\PreAuthorizationEvent;
1819
use FOS\OAuthServerBundle\Form\Handler\AuthorizeFormHandler;
1920
use FOS\OAuthServerBundle\Model\ClientInterface;
2021
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
@@ -117,9 +118,14 @@ class AuthorizeControllerTest extends \PHPUnit\Framework\TestCase
117118
protected $client;
118119

119120
/**
120-
* @var \PHPUnit_Framework_MockObject_MockObject|OAuthEvent
121+
* @var \PHPUnit_Framework_MockObject_MockObject|PreAuthorizationEvent
121122
*/
122-
protected $event;
123+
protected $preAuthorizationEvent;
124+
125+
/**
126+
* @var \PHPUnit_Framework_MockObject_MockObject|PostAuthorizationEvent
127+
*/
128+
protected $postAuthorizationEvent;
123129

124130
/**
125131
* @var \PHPUnit_Framework_MockObject_MockObject|FormView
@@ -206,7 +212,11 @@ public function setUp()
206212
->disableOriginalConstructor()
207213
->getMock()
208214
;
209-
$this->event = $this->getMockBuilder(OAuthEvent::class)
215+
$this->preAuthorizationEvent = $this->getMockBuilder(PreAuthorizationEvent::class)
216+
->disableOriginalConstructor()
217+
->getMock()
218+
;
219+
$this->postAuthorizationEvent = $this->getMockBuilder(PostAuthorizationEvent::class)
210220
->disableOriginalConstructor()
211221
->getMock()
212222
;
@@ -274,16 +284,15 @@ public function testAuthorizeActionWillRenderTemplate()
274284
$propertyReflection->setValue($this->instance, $this->client);
275285

276286
$this->eventDispatcher
277-
->expects($this->at(0))
287+
->expects($this->once())
278288
->method('dispatch')
279-
->with(OAuthEvent::PRE_AUTHORIZATION_PROCESS, new OAuthEvent($this->user, $this->client))
280-
->willReturn($this->event)
289+
->with(new PreAuthorizationEvent($this->user, $this->client))
290+
->willReturn($this->preAuthorizationEvent)
281291
;
282292

283-
$this->event
284-
->expects($this->at(0))
293+
$this->preAuthorizationEvent
294+
->expects($this->once())
285295
->method('isAuthorizedClient')
286-
->with()
287296
->willReturn(false)
288297
;
289298

@@ -347,16 +356,15 @@ public function testAuthorizeActionWillFinishClientAuthorization()
347356
$propertyReflection->setValue($this->instance, $this->client);
348357

349358
$this->eventDispatcher
350-
->expects($this->at(0))
359+
->expects($this->once())
351360
->method('dispatch')
352-
->with(OAuthEvent::PRE_AUTHORIZATION_PROCESS, new OAuthEvent($this->user, $this->client))
353-
->willReturn($this->event)
361+
->with(new PreAuthorizationEvent($this->user, $this->client))
362+
->willReturn($this->preAuthorizationEvent)
354363
;
355364

356-
$this->event
357-
->expects($this->at(0))
365+
$this->preAuthorizationEvent
366+
->expects($this->once())
358367
->method('isAuthorizedClient')
359-
->with()
360368
->willReturn(true)
361369
;
362370

@@ -431,16 +439,15 @@ public function testAuthorizeActionWillEnsureLogout()
431439
$propertyReflection->setValue($this->instance, $this->client);
432440

433441
$this->eventDispatcher
434-
->expects($this->at(0))
442+
->expects($this->once())
435443
->method('dispatch')
436-
->with(OAuthEvent::PRE_AUTHORIZATION_PROCESS, new OAuthEvent($this->user, $this->client))
437-
->willReturn($this->event)
444+
->with(new PreAuthorizationEvent($this->user, $this->client))
445+
->willReturn($this->preAuthorizationEvent)
438446
;
439447

440-
$this->event
441-
->expects($this->at(0))
448+
$this->preAuthorizationEvent
449+
->expects($this->once())
442450
->method('isAuthorizedClient')
443-
->with()
444451
->willReturn(false)
445452
;
446453

@@ -506,11 +513,11 @@ public function testAuthorizeActionWillProcessAuthorizationForm()
506513
$this->eventDispatcher
507514
->expects($this->at(0))
508515
->method('dispatch')
509-
->with(OAuthEvent::PRE_AUTHORIZATION_PROCESS, new OAuthEvent($this->user, $this->client))
510-
->willReturn($this->event)
516+
->with(new PreAuthorizationEvent($this->user, $this->client))
517+
->willReturn($this->preAuthorizationEvent)
511518
;
512519

513-
$this->event
520+
$this->preAuthorizationEvent
514521
->expects($this->once())
515522
->method('isAuthorizedClient')
516523
->willReturn(false)
@@ -531,10 +538,7 @@ public function testAuthorizeActionWillProcessAuthorizationForm()
531538
$this->eventDispatcher
532539
->expects($this->at(1))
533540
->method('dispatch')
534-
->with(
535-
OAuthEvent::POST_AUTHORIZATION_PROCESS,
536-
new OAuthEvent($this->user, $this->client, true)
537-
)
541+
->with(new PostAuthorizationEvent($this->user, $this->client, true))
538542
;
539543

540544
$formName = 'formName'.\random_bytes(10);

0 commit comments

Comments
 (0)