Skip to content

Commit

Permalink
Merge 1.6 into master
Browse files Browse the repository at this point in the history
  • Loading branch information
dinamic authored and dkarlovi committed Jan 31, 2018
1 parent 85409bb commit 93a30d0
Show file tree
Hide file tree
Showing 27 changed files with 732 additions and 277 deletions.
36 changes: 17 additions & 19 deletions Controller/AuthorizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Router;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\UserInterface;
Expand Down Expand Up @@ -86,7 +86,7 @@ class AuthorizeController implements ContainerAwareInterface
private $tokenStorage;

/**
* @var Router
* @var UrlGeneratorInterface
*/
private $router;

Expand Down Expand Up @@ -118,7 +118,7 @@ class AuthorizeController implements ContainerAwareInterface
* @param OAuth2 $oAuth2Server
* @param EngineInterface $templating
* @param TokenStorageInterface $tokenStorage
* @param Router $router
* @param UrlGeneratorInterface $router
* @param ClientManagerInterface $clientManager
* @param EventDispatcher $eventDispatcher
* @param string $templateEngineType
Expand All @@ -131,7 +131,7 @@ public function __construct(
OAuth2 $oAuth2Server,
EngineInterface $templating,
TokenStorageInterface $tokenStorage,
Router $router,
UrlGeneratorInterface $router,
ClientManagerInterface $clientManager,
EventDispatcher $eventDispatcher,
$templateEngineType = 'twig'
Expand Down Expand Up @@ -253,25 +253,23 @@ protected function getRedirectionUrl(UserInterface $user)
*/
protected function getClient()
{
if (null === $this->client) {
$request = $this->getCurrentRequest();
if (null !== $this->client) {
return $this->client;
}

$client = null;
if (null !== $request) {
if (null === $clientId = $request->get('client_id')) {
$form = $this->authorizeForm;
$formData = $request->get($form->getName(), []);
$clientId = isset($formData['client_id']) ? $formData['client_id'] : null;
}
if (null === $request = $this->getCurrentRequest()) {
throw new NotFoundHttpException('Client not found.');
}

$client = $this->clientManager->findClientByPublicId($clientId);
}
if (null === $clientId = $request->get('client_id')) {
$formData = $request->get($this->authorizeForm->getName(), []);
$clientId = isset($formData['client_id']) ? $formData['client_id'] : null;
}

if (null === $client) {
throw new NotFoundHttpException('Client not found.');
}
$this->client = $this->clientManager->findClientByPublicId($clientId);

$this->client = $client;
if (null === $this->client) {
throw new NotFoundHttpException('Client not found.');
}

return $this->client;
Expand Down
10 changes: 5 additions & 5 deletions Entity/AuthCodeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

namespace FOS\OAuthServerBundle\Entity;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use FOS\OAuthServerBundle\Model\AuthCodeInterface;
use FOS\OAuthServerBundle\Model\AuthCodeManager as BaseAuthCodeManager;

class AuthCodeManager extends BaseAuthCodeManager
{
/**
* @var EntityManager
* @var EntityManagerInterface
*/
protected $em;

Expand All @@ -30,10 +30,10 @@ class AuthCodeManager extends BaseAuthCodeManager
protected $class;

/**
* @param EntityManager $em
* @param string $class
* @param EntityManagerInterface $em
* @param string $class
*/
public function __construct(EntityManager $em, $class)
public function __construct(EntityManagerInterface $em, $class)
{
$this->em = $em;
$this->class = $class;
Expand Down
50 changes: 29 additions & 21 deletions Form/Handler/AuthorizeFormHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,34 @@ public function isRejected()
return !$this->form->getData()->accepted;
}

/**
* @return bool
*/
public function process()
{
$request = $this->getCurrentRequest();
if (null !== $request) {
$this->form->setData(new Authorize(
$request->request->has('accepted'),
$request->query->all()
));

if ('POST' === $request->getMethod()) {
$this->form->handleRequest($request);
if ($this->form->isValid()) {
$this->onSuccess();

return true;
}
}

if (null === $request) {
return false;
}

$this->form->setData(new Authorize(
$request->request->has('accepted'),
$request->query->all()
));

if ('POST' !== $request->getMethod()) {
return false;
}

$this->form->handleRequest($request);
if (!$this->form->isValid()) {
return false;
}

return false;
$this->onSuccess();

return true;
}

public function getScope()
Expand All @@ -119,14 +127,14 @@ protected function onSuccess()

private function getCurrentRequest()
{
if (null !== $this->requestStack) {
if ($this->requestStack instanceof Request) {
return $this->requestStack;
}
if (null === $this->requestStack) {
return $this->container->get('request');
}

return $this->requestStack->getCurrentRequest();
if ($this->requestStack instanceof Request) {
return $this->requestStack;
}

return $this->container->get('request');
return $this->requestStack->getCurrentRequest();
}
}
55 changes: 39 additions & 16 deletions Tests/Controller/AuthorizeControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?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\Tests\Controller;

use FOS\OAuthServerBundle\Controller\AuthorizeController;
Expand All @@ -8,6 +19,7 @@
use FOS\OAuthServerBundle\Model\ClientInterface;
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
use OAuth2\OAuth2;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormView;
Expand All @@ -18,12 +30,11 @@
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\UserInterface;

class AuthorizeControllerTest extends \PHPUnit_Framework_TestCase
class AuthorizeControllerTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject|RequestStack
Expand Down Expand Up @@ -90,6 +101,16 @@ class AuthorizeControllerTest extends \PHPUnit_Framework_TestCase
*/
protected $request;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|ParameterBag
*/
protected $requestQuery;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|ParameterBag
*/
protected $requestRequest;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|UserInterface
*/
Expand Down Expand Up @@ -168,18 +189,22 @@ public function setUp()
$this->templateEngineType
);

$this->request = $this->getMockBuilder(Request::class)
/** @var \PHPUnit_Framework_MockObject_MockObject&Request $request */
$request = $this->getMockBuilder(Request::class)
->disableOriginalConstructor()
->getMock()
;
$this->request->query = $this->getMockBuilder(ParameterBag::class)
$this->requestQuery = $this->getMockBuilder(ParameterBag::class)
->disableOriginalConstructor()
->getMock()
;
$this->request->request = $this->getMockBuilder(ParameterBag::class)
$this->requestRequest = $this->getMockBuilder(ParameterBag::class)
->disableOriginalConstructor()
->getMock()
;
$request->query = $this->requestQuery;
$request->request = $this->requestRequest;
$this->request = $request;
$this->user = $this->getMockBuilder(UserInterface::class)
->disableOriginalConstructor()
->getMock()
Expand Down Expand Up @@ -219,10 +244,8 @@ public function testAuthorizeActionWillThrowAccessDeniedException()
->willReturn(null)
;

$this->setExpectedException(
AccessDeniedException::class,
'This user does not have access to this section.');
;
$this->expectException(AccessDeniedException::class);
$this->expectExceptionMessage('This user does not have access to this section.');

$this->instance->authorizeAction($this->request);
}
Expand Down Expand Up @@ -292,7 +315,7 @@ public function testAuthorizeActionWillRenderTemplate()
->with(
'FOSOAuthServerBundle:Authorize:authorize.html.twig',
[
'form' => $this->formView,
'form' => $this->formView,
'client' => $this->client,
]
)
Expand Down Expand Up @@ -346,7 +369,7 @@ public function testAuthorizeActionWillFinishClientAuthorization()
->willReturn(true)
;

$randomScope = 'scope' . \random_bytes(10);
$randomScope = 'scope'.\random_bytes(10);

$this->request
->expects($this->at(0))
Expand Down Expand Up @@ -451,7 +474,7 @@ public function testAuthorizeActionWillEnsureLogout()
->with(
'FOSOAuthServerBundle:Authorize:authorize.html.twig',
[
'form' => $this->formView,
'form' => $this->formView,
'client' => $this->client,
]
)
Expand Down Expand Up @@ -525,28 +548,28 @@ public function testAuthorizeActionWillProcessAuthorizationForm()
)
;

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

$this->form
->expects($this->once())
->method('getName')
->willReturn($formName)
;

$this->request->query
$this->requestQuery
->expects($this->once())
->method('all')
->willReturn([])
;

$this->request->request
$this->requestRequest
->expects($this->once())
->method('has')
->with($formName)
->willReturn(false)
;

$randomScope = 'scope' . \random_bytes(10);
$randomScope = 'scope'.\random_bytes(10);

$this->authorizeFormHandler
->expects($this->once())
Expand Down
Loading

0 comments on commit 93a30d0

Please sign in to comment.