Skip to content

Commit

Permalink
Merge pull request #1 from iisisrael/symfony-5
Browse files Browse the repository at this point in the history
Symfony 5 compatibility updates - cleanups and improvements by @iisisrael
  • Loading branch information
elchris authored Feb 13, 2020
2 parents d7ba5e4 + 24726a3 commit da7fd54
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 241 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Propel/om/
Propel/map/
composer.lock
.php_cs.cache
.phpunit.result.cache
38 changes: 11 additions & 27 deletions Controller/AuthorizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Templating\EngineInterface;
use Twig\Environment;

/**
* Controller handling basic authorization.
Expand Down Expand Up @@ -65,9 +65,9 @@ class AuthorizeController
private $oAuth2Server;

/**
* @var EngineInterface
* @var Environment
*/
private $templating;
private $twig;

/**
* @var RequestStack
Expand All @@ -89,11 +89,6 @@ class AuthorizeController
*/
private $clientManager;

/**
* @var string
*/
private $templateEngineType;

/**
* @var EventDispatcherInterface
*/
Expand All @@ -109,37 +104,34 @@ class AuthorizeController
* @param Form $authorizeForm
* @param AuthorizeFormHandler $authorizeFormHandler
* @param OAuth2 $oAuth2Server
* @param EngineInterface $templating
* @param Environment $twig
* @param TokenStorageInterface $tokenStorage
* @param UrlGeneratorInterface $router
* @param ClientManagerInterface $clientManager
* @param EventDispatcherInterface $eventDispatcher
* @param SessionInterface $session
* @param string $templateEngineType
*/
public function __construct(
RequestStack $requestStack,
Form $authorizeForm,
AuthorizeFormHandler $authorizeFormHandler,
OAuth2 $oAuth2Server,
EngineInterface $templating,
Environment $twig,
TokenStorageInterface $tokenStorage,
UrlGeneratorInterface $router,
ClientManagerInterface $clientManager,
EventDispatcherInterface $eventDispatcher,
SessionInterface $session = null,
$templateEngineType = 'twig'
SessionInterface $session = null
) {
$this->requestStack = $requestStack;
$this->session = $session;
$this->authorizeForm = $authorizeForm;
$this->authorizeFormHandler = $authorizeFormHandler;
$this->oAuth2Server = $oAuth2Server;
$this->templating = $templating;
$this->twig = $twig;
$this->tokenStorage = $tokenStorage;
$this->router = $router;
$this->clientManager = $clientManager;
$this->templateEngineType = $templateEngineType;
$this->eventDispatcher = $eventDispatcher;
}

Expand Down Expand Up @@ -183,7 +175,10 @@ public function authorizeAction(Request $request)
'client' => $this->getClient(),
];

return $this->renderAuthorize($data, $this->templating, $this->templateEngineType);
return $this->twig->render(
'@FOSOAuthServer/Authorize/authorize.html.twig',
$data
);
}

/**
Expand Down Expand Up @@ -258,17 +253,6 @@ protected function getClient()
return $this->client;
}

/**
* @throws \RuntimeException
*/
protected function renderAuthorize(array $data, EngineInterface $engine, string $engineType): string
{
return $engine->render(
'@FOSOAuthServer/Authorize/authorize.html.'.$engineType,
$data
);
}

/**
* @return null|Request
*/
Expand Down
36 changes: 0 additions & 36 deletions DependencyInjection/Compiler/TokenStorageCompilerPass.php

This file was deleted.

16 changes: 0 additions & 16 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function getConfigTreeBuilder()

/** @var ArrayNodeDefinition $rootNode */
$rootNode = $treeBuilder->getRootNode();
//$treeBuilder->root('fos_oauth_server');

$supportedDrivers = ['orm', 'mongodb', 'propel', 'custom'];

Expand Down Expand Up @@ -82,7 +81,6 @@ public function getConfigTreeBuilder()

$this->addAuthorizeSection($rootNode);
$this->addServiceSection($rootNode);
$this->addTemplateSection($rootNode);

return $treeBuilder;
}
Expand Down Expand Up @@ -138,18 +136,4 @@ private function addServiceSection(ArrayNodeDefinition $node)
->end()
;
}

private function addTemplateSection(ArrayNodeDefinition $node)
{
$node
->children()
->arrayNode('template')
->addDefaultsIfNotSet()
->children()
->scalarNode('engine')->defaultValue('twig')->end()
->end()
->end()
->end()
;
}
}
2 changes: 0 additions & 2 deletions FOSOAuthServerBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use FOS\OAuthServerBundle\DependencyInjection\Compiler\GrantExtensionsCompilerPass;
use FOS\OAuthServerBundle\DependencyInjection\Compiler\RequestStackCompilerPass;
use FOS\OAuthServerBundle\DependencyInjection\Compiler\TokenStorageCompilerPass;
use FOS\OAuthServerBundle\DependencyInjection\FOSOAuthServerExtension;
use FOS\OAuthServerBundle\DependencyInjection\Security\Factory\OAuthFactory;
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
Expand All @@ -38,7 +37,6 @@ public function build(ContainerBuilder $container)
$extension->addSecurityListenerFactory(new OAuthFactory());

$container->addCompilerPass(new GrantExtensionsCompilerPass());
$container->addCompilerPass(new TokenStorageCompilerPass());
$container->addCompilerPass(new RequestStackCompilerPass());
}
}
3 changes: 1 addition & 2 deletions Resources/config/authorize.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@
<argument type="service" id="fos_oauth_server.authorize.form" />
<argument type="service" id="fos_oauth_server.authorize.form.handler" />
<argument type="service" id="fos_oauth_server.server" />
<!-- <argument type="service" id="templating" /> -->
<argument type="service" id="twig" />
<argument type="service" id="security.token_storage" />
<argument type="service" id="router" />
<argument type="service" id="fos_oauth_server.client_manager" />
<argument type="service" id="event_dispatcher" />
<argument type="service" id="session" on-invalid="null" />
<argument>%fos_oauth_server.template.engine%</argument>
</service>
</services>

Expand Down
18 changes: 8 additions & 10 deletions Security/Firewall/OAuthListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,25 @@ public function __invoke(RequestEvent $event)
$this->handle($event);
}

public function handle(RequestEvent $event)
public function handle(RequestEvent $event): void
{
//TODO - This method needs to be re-examined for inconsistent return types

if (null === $oauthToken = $this->serverService->getBearerToken($event->getRequest(), true)) {
//return;
throw new LogicException('Token for event was null');
}

$token = new OAuthToken();
$token->setToken($oauthToken);

try {
$returnValue = $this->authenticationManager->authenticate($token);
$authenticateResult = $this->authenticationManager->authenticate($token);

if ($returnValue instanceof TokenInterface) {
return $this->tokenStorage->setToken($returnValue);
}
if ($authenticateResult instanceof TokenInterface) {

$this->tokenStorage->setToken($authenticateResult);

} elseif ($authenticateResult instanceof Response) {

if ($returnValue instanceof Response) {
return $event->setResponse($returnValue);
$event->setResponse($authenticateResult);
}
} catch (AuthenticationException $e) {
if (null !== $p = $e->getPrevious()) {
Expand Down
23 changes: 8 additions & 15 deletions Tests/Controller/AuthorizeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormView;
Expand All @@ -36,6 +35,7 @@
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\UserInterface;
use Twig\Environment;

class AuthorizeControllerTest extends TestCase
{
Expand Down Expand Up @@ -65,9 +65,9 @@ class AuthorizeControllerTest extends TestCase
protected $oAuth2Server;

/**
* @var MockObject|EngineInterface
* @var MockObject|Environment
*/
protected $templateEngine;
protected $twig;

/**
* @var MockObject|TokenStorageInterface
Expand All @@ -89,11 +89,6 @@ class AuthorizeControllerTest extends TestCase
*/
protected $eventDispatcher;

/**
* @var string
*/
protected $templateEngineType;

/**
* @var AuthorizeController
*/
Expand Down Expand Up @@ -152,7 +147,7 @@ public function setUp() : void
->disableOriginalConstructor()
->getMock()
;
$this->templateEngine = $this->getMockBuilder(EngineInterface::class)
$this->twig = $this->getMockBuilder(Environment::class)
->disableOriginalConstructor()
->getMock()
;
Expand All @@ -176,20 +171,18 @@ public function setUp() : void
->disableOriginalConstructor()
->getMock()
;
$this->templateEngineType = 'twig';

$this->instance = new AuthorizeController(
$this->requestStack,
$this->form,
$this->authorizeFormHandler,
$this->oAuth2Server,
$this->templateEngine,
$this->twig,
$this->tokenStorage,
$this->router,
$this->clientManager,
$this->eventDispatcher,
$this->session,
$this->templateEngineType
$this->session
);

/** @var MockObject&Request $request */
Expand Down Expand Up @@ -312,7 +305,7 @@ public function testAuthorizeActionWillRenderTemplate(): void

$response = '';

$this->templateEngine
$this->twig
->expects($this->at(0))
->method('render')
->with(
Expand Down Expand Up @@ -471,7 +464,7 @@ public function testAuthorizeActionWillEnsureLogout(): void

$response = '';

$this->templateEngine
$this->twig
->expects($this->at(0))
->method('render')
->with(
Expand Down
Loading

0 comments on commit da7fd54

Please sign in to comment.