Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dedicated authorization events #655

Merged
merged 2 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ This changelog references the relevant changes done in 6.0 versions.
* Dropped support for Symfony versions anterior to `4.4` [[#648](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/648)]
* Fixed form submission/validation [[#643](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/643)]
* **[BC break]** Changed signature of method `FOS\OAuthServerBundle\Controller\AuthorizeController::renderAuthorize()` [[#653](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/653)]
* **[BC break]** Removed class `FOS\OAuthServerBundle\Event\OAuthEvent`, use dedicated event classes instead: [[#655](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/655)]
- `OAuthEvent::PRE_AUTHORIZATION_PROCESS` => `FOS\OAuthServerBundle\Event\PreAuthorizationEvent`
- `OAuthEvent::POST_AUTHORIZATION_PROCESS` => `FOS\OAuthServerBundle\Event\PostAuthorizationEvent`
* **[BC break]** Removed support for templating engine [[#653](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/653)]

### 2.0.0-ALPHA0 (2018-05-01)
Expand Down
15 changes: 5 additions & 10 deletions Controller/AuthorizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

namespace FOS\OAuthServerBundle\Controller;

use FOS\OAuthServerBundle\Event\OAuthEvent;
use FOS\OAuthServerBundle\Event\PostAuthorizationEvent;
use FOS\OAuthServerBundle\Event\PreAuthorizationEvent;
use FOS\OAuthServerBundle\Form\Handler\AuthorizeFormHandler;
use FOS\OAuthServerBundle\Model\ClientInterface;
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
Expand Down Expand Up @@ -145,11 +146,8 @@ public function authorizeAction(Request $request)
$form = $this->authorizeForm;
$formHandler = $this->authorizeFormHandler;

/** @var OAuthEvent $event */
$event = $this->eventDispatcher->dispatch(
OAuthEvent::PRE_AUTHORIZATION_PROCESS,
new OAuthEvent($user, $this->getClient())
);
/** @var PreAuthorizationEvent $event */
$event = $this->eventDispatcher->dispatch(new PreAuthorizationEvent($user, $this->getClient()));

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

$this->eventDispatcher->dispatch(
OAuthEvent::POST_AUTHORIZATION_PROCESS,
new OAuthEvent($user, $this->getClient(), $formHandler->isAccepted())
);
$this->eventDispatcher->dispatch(new PostAuthorizationEvent($user, $this->getClient(), $formHandler->isAccepted()));

$formName = $this->authorizeForm->getName();
if (!$request->query->all() && $request->request->has($formName)) {
Expand Down
35 changes: 8 additions & 27 deletions Event/OAuthEvent.php → Event/AbstractAuthorizationEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@
namespace FOS\OAuthServerBundle\Event;

use FOS\OAuthServerBundle\Model\ClientInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Contracts\EventDispatcher\Event;

class OAuthEvent extends Event
class AbstractAuthorizationEvent extends Event
{
const PRE_AUTHORIZATION_PROCESS = 'fos_oauth_server.pre_authorization_process';

const POST_AUTHORIZATION_PROCESS = 'fos_oauth_server.post_authorization_process';

/**
* @var UserInterface
*/
Expand All @@ -38,44 +34,29 @@ class OAuthEvent extends Event
*/
private $isAuthorizedClient;

/**
* @param bool $isAuthorizedClient
*/
public function __construct(UserInterface $user, ClientInterface $client, $isAuthorizedClient = false)
public function __construct(UserInterface $user, ClientInterface $client, bool $isAuthorizedClient = false)
{
$this->user = $user;
$this->client = $client;
$this->isAuthorizedClient = $isAuthorizedClient;
}

/**
* @return UserInterface
*/
public function getUser()
public function getUser(): UserInterface
{
return $this->user;
}

/**
* @param bool $isAuthorizedClient
*/
public function setAuthorizedClient($isAuthorizedClient)
public function setAuthorizedClient(bool $authorized)
{
$this->isAuthorizedClient = $isAuthorizedClient;
$this->isAuthorizedClient = $authorized;
}

/**
* @return bool
*/
public function isAuthorizedClient()
public function isAuthorizedClient(): bool
{
return $this->isAuthorizedClient;
}

/**
* @return ClientInterface
*/
public function getClient()
public function getClient(): ClientInterface
{
return $this->client;
}
Expand Down
18 changes: 18 additions & 0 deletions Event/PostAuthorizationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

/*
* 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;

class PostAuthorizationEvent extends AbstractAuthorizationEvent
{
}
18 changes: 18 additions & 0 deletions Event/PreAuthorizationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

/*
* 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;

class PreAuthorizationEvent extends AbstractAuthorizationEvent
{
}
62 changes: 33 additions & 29 deletions Tests/Controller/AuthorizeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
namespace FOS\OAuthServerBundle\Tests\Controller;

use FOS\OAuthServerBundle\Controller\AuthorizeController;
use FOS\OAuthServerBundle\Event\OAuthEvent;
use FOS\OAuthServerBundle\Event\PostAuthorizationEvent;
use FOS\OAuthServerBundle\Event\PreAuthorizationEvent;
use FOS\OAuthServerBundle\Form\Handler\AuthorizeFormHandler;
use FOS\OAuthServerBundle\Model\ClientInterface;
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
Expand Down Expand Up @@ -117,9 +118,14 @@ class AuthorizeControllerTest extends \PHPUnit\Framework\TestCase
protected $client;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|OAuthEvent
* @var \PHPUnit_Framework_MockObject_MockObject|PreAuthorizationEvent
*/
protected $event;
protected $preAuthorizationEvent;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|PostAuthorizationEvent
*/
protected $postAuthorizationEvent;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|FormView
Expand Down Expand Up @@ -206,7 +212,11 @@ public function setUp()
->disableOriginalConstructor()
->getMock()
;
$this->event = $this->getMockBuilder(OAuthEvent::class)
$this->preAuthorizationEvent = $this->getMockBuilder(PreAuthorizationEvent::class)
->disableOriginalConstructor()
->getMock()
;
$this->postAuthorizationEvent = $this->getMockBuilder(PostAuthorizationEvent::class)
->disableOriginalConstructor()
->getMock()
;
Expand Down Expand Up @@ -274,16 +284,15 @@ public function testAuthorizeActionWillRenderTemplate()
$propertyReflection->setValue($this->instance, $this->client);

$this->eventDispatcher
->expects($this->at(0))
->expects($this->once())
->method('dispatch')
->with(OAuthEvent::PRE_AUTHORIZATION_PROCESS, new OAuthEvent($this->user, $this->client))
->willReturn($this->event)
->with(new PreAuthorizationEvent($this->user, $this->client))
->willReturn($this->preAuthorizationEvent)
;

$this->event
->expects($this->at(0))
$this->preAuthorizationEvent
->expects($this->once())
->method('isAuthorizedClient')
->with()
->willReturn(false)
;

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

$this->eventDispatcher
->expects($this->at(0))
->expects($this->once())
->method('dispatch')
->with(OAuthEvent::PRE_AUTHORIZATION_PROCESS, new OAuthEvent($this->user, $this->client))
->willReturn($this->event)
->with(new PreAuthorizationEvent($this->user, $this->client))
->willReturn($this->preAuthorizationEvent)
;

$this->event
->expects($this->at(0))
$this->preAuthorizationEvent
->expects($this->once())
->method('isAuthorizedClient')
->with()
->willReturn(true)
;

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

$this->eventDispatcher
->expects($this->at(0))
->expects($this->once())
->method('dispatch')
->with(OAuthEvent::PRE_AUTHORIZATION_PROCESS, new OAuthEvent($this->user, $this->client))
->willReturn($this->event)
->with(new PreAuthorizationEvent($this->user, $this->client))
->willReturn($this->preAuthorizationEvent)
;

$this->event
->expects($this->at(0))
$this->preAuthorizationEvent
->expects($this->once())
->method('isAuthorizedClient')
->with()
->willReturn(false)
;

Expand Down Expand Up @@ -506,11 +513,11 @@ public function testAuthorizeActionWillProcessAuthorizationForm()
$this->eventDispatcher
->expects($this->at(0))
->method('dispatch')
->with(OAuthEvent::PRE_AUTHORIZATION_PROCESS, new OAuthEvent($this->user, $this->client))
->willReturn($this->event)
->with(new PreAuthorizationEvent($this->user, $this->client))
->willReturn($this->preAuthorizationEvent)
;

$this->event
$this->preAuthorizationEvent
->expects($this->once())
->method('isAuthorizedClient')
->willReturn(false)
Expand All @@ -531,10 +538,7 @@ public function testAuthorizeActionWillProcessAuthorizationForm()
$this->eventDispatcher
->expects($this->at(1))
->method('dispatch')
->with(
OAuthEvent::POST_AUTHORIZATION_PROCESS,
new OAuthEvent($this->user, $this->client, true)
)
->with(new PostAuthorizationEvent($this->user, $this->client, true))
;

$formName = 'formName'.\random_bytes(10);
Expand Down