Skip to content

Commit 1111a0f

Browse files
ghostikaGuilhemN
authored andcommitted
Remove deprecated code and add new implementation for Symfony 3.0
1 parent 0c72d8b commit 1111a0f

File tree

8 files changed

+63
-24
lines changed

8 files changed

+63
-24
lines changed

Controller/AuthorizeController.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public function authorizeAction(Request $request)
5858
);
5959

6060
if ($event->isAuthorizedClient()) {
61-
$scope = $this->container->get('request')->get('scope', null);
61+
$scope = $request->get('scope', null);
6262

6363
return $this->container
6464
->get('fos_oauth_server.server')
6565
->finishClientAuthorization(true, $user, $request, $scope);
6666
}
6767

68-
if (true === $formHandler->process()) {
68+
if (true === $formHandler->process($request)) {
6969
return $this->processSuccess($user, $formHandler, $request);
7070
}
7171

@@ -79,8 +79,9 @@ public function authorizeAction(Request $request)
7979
}
8080

8181
/**
82-
* @param UserInterface $user
82+
* @param UserInterface $user
8383
* @param AuthorizeFormHandler $formHandler
84+
* @param Request $request
8485
*
8586
* @return Response
8687
*/
@@ -126,11 +127,11 @@ protected function getRedirectionUrl(UserInterface $user)
126127
*/
127128
protected function getClient()
128129
{
130+
$request = $this->container->get('request_stack')->getMasterRequest();
129131
if (null === $this->client) {
130-
if (null === $clientId = $this->container->get('request')->get('client_id')) {
132+
if (null === $clientId = $request->get('client_id')) {
131133
$form = $this->container->get('fos_oauth_server.authorize.form');
132-
$clientId = $this->container->get('request')
133-
->get(sprintf('%s[client_id]', $form->getName()), null, true);
134+
$clientId = $request->get(sprintf('%s[client_id]', $form->getName()), null, true);
134135
}
135136

136137
$client = $this->container
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace FOS\OAuthServerBundle\DependencyInjection\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
8+
/**
9+
* @author Andras Ratz <[email protected]>
10+
*/
11+
class TokenStorageCompilerPass implements CompilerPassInterface
12+
{
13+
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
public function process(ContainerBuilder $container)
18+
{
19+
$definition = $container->getDefinition('fos_oauth_server.security.authentication.listener');
20+
21+
if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
22+
$tokenStorageReference = new Reference('security.token_storage');
23+
} else {
24+
$tokenStorageReference = new Reference('security.context');
25+
}
26+
$definition->replaceArgument(0, $tokenStorageReference);
27+
}
28+
29+
}

FOSOAuthServerBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace FOS\OAuthServerBundle;
1313

14+
use FOS\OAuthServerBundle\DependencyInjection\Compiler\TokenStorageCompilerPass;
1415
use FOS\OAuthServerBundle\DependencyInjection\FOSOAuthServerExtension;
1516
use FOS\OAuthServerBundle\DependencyInjection\Security\Factory\OAuthFactory;
1617
use FOS\OAuthServerBundle\DependencyInjection\Compiler\GrantExtensionsCompilerPass;
@@ -35,5 +36,6 @@ public function build(ContainerBuilder $container)
3536
}
3637

3738
$container->addCompilerPass(new GrantExtensionsCompilerPass());
39+
$container->addCompilerPass(new TokenStorageCompilerPass());
3840
}
3941
}

Form/Handler/AuthorizeFormHandler.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Form\FormInterface;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use FOS\OAuthServerBundle\Form\Model\Authorize;
17+
use Symfony\Component\HttpFoundation\RequestStack;
1718

1819
/**
1920
* @author Chris Jones <[email protected]>
@@ -23,10 +24,10 @@ class AuthorizeFormHandler
2324
protected $request;
2425
protected $form;
2526

26-
public function __construct(FormInterface $form, Request $request)
27+
public function __construct(FormInterface $form, RequestStack $requestStack)
2728
{
2829
$this->form = $form;
29-
$this->request = $request;
30+
$this->request = $requestStack->getMasterRequest();
3031
}
3132

3233
public function isAccepted()
@@ -47,7 +48,7 @@ public function process()
4748
));
4849

4950
if ('POST' === $this->request->getMethod()) {
50-
$this->form->bind($this->request);
51+
$this->form->submit($this->request);
5152
if ($this->form->isValid()) {
5253
$this->onSuccess();
5354

Resources/config/authorize.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
<tag name="form.type" alias="fos_oauth_server_authorize" />
1919
</service>
2020

21-
<service id="fos_oauth_server.authorize.form.handler.default" class="FOS\OAuthServerBundle\Form\Handler\AuthorizeFormHandler" scope="request">
21+
<service id="fos_oauth_server.authorize.form.handler.default" class="FOS\OAuthServerBundle\Form\Handler\AuthorizeFormHandler">
2222
<argument type="service" id="fos_oauth_server.authorize.form" />
23-
<argument type="service" id="request" />
23+
<argument type="service" id="request_stack" />
2424
</service>
2525
</services>
2626

Resources/config/security.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</service>
1919

2020
<service id="fos_oauth_server.security.authentication.listener" class="%fos_oauth_server.security.authentication.listener.class%" public="false">
21-
<argument type="service" id="security.context"/>
21+
<argument /> <!-- security.token_storage or security.context for Symfony < 2.6 -->
2222
<argument type="service" id="security.authentication.manager" />
2323
<argument type="service" id="fos_oauth_server.server" />
2424
</service>

Security/Firewall/OAuthListener.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1818
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
1919
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
20+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2021
use Symfony\Component\Security\Core\Exception\AuthenticationException;
2122
use Symfony\Component\Security\Core\SecurityContextInterface;
2223
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
@@ -29,7 +30,7 @@
2930
class OAuthListener implements ListenerInterface
3031
{
3132
/**
32-
* @var \Symfony\Component\Security\Core\SecurityContextInterface
33+
* @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface|\Symfony\Component\Security\Core\SecurityContextInterface
3334
*/
3435
protected $securityContext;
3536

@@ -44,11 +45,11 @@ class OAuthListener implements ListenerInterface
4445
protected $serverService;
4546

4647
/**
47-
* @param \Symfony\Component\Security\Core\SecurityContextInterface $securityContext The security context.
48-
* @param \Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface $authenticationManager The authentication manager.
49-
* @param \OAuth2\OAuth2 $serverService
48+
* @param \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface|\Symfony\Component\Security\Core\SecurityContextInterface $tokenStorage The token storage.
49+
* @param \Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface $authenticationManager The authentication manager.
50+
* @param \OAuth2\OAuth2 $serverService
5051
*/
51-
public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, OAuth2 $serverService)
52+
public function __construct($securityContext, AuthenticationManagerInterface $authenticationManager, OAuth2 $serverService)
5253
{
5354
$this->securityContext = $securityContext;
5455
$this->authenticationManager = $authenticationManager;

Tests/Security/Firewall/OAuthListenerTest.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class OAuthListenerTest extends TestCase
2020

2121
protected $authManager;
2222

23-
protected $securityContext;
23+
protected $tokenStorage;
2424

2525
protected $event;
2626

@@ -34,8 +34,13 @@ public function setUp()
3434
$this->authManager = $this
3535
->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
3636

37-
$this->securityContext = $this
38-
->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
37+
if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
38+
$this->tokenStorage = $this
39+
->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
40+
} else {
41+
$this->tokenStorage = $this
42+
->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
43+
}
3944

4045
$this->event = $this
4146
->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
@@ -45,7 +50,7 @@ public function setUp()
4550

4651
public function testHandle()
4752
{
48-
$listener = new OAuthListener($this->securityContext, $this->authManager, $this->serverService);
53+
$listener = new OAuthListener($this->tokenStorage, $this->authManager, $this->serverService);
4954

5055
$this->serverService
5156
->expects($this->once())
@@ -57,7 +62,7 @@ public function testHandle()
5762
->method('authenticate')
5863
->will($this->returnArgument(0));
5964

60-
$this->securityContext
65+
$this->tokenStorage
6166
->expects($this->once())
6267
->method('setToken')
6368
->will($this->returnArgument(0));
@@ -70,7 +75,7 @@ public function testHandle()
7075

7176
public function testHandleResponse()
7277
{
73-
$listener = new OAuthListener($this->securityContext, $this->authManager, $this->serverService);
78+
$listener = new OAuthListener($this->tokenStorage, $this->authManager, $this->serverService);
7479

7580
$this->serverService
7681
->expects($this->once())
@@ -84,7 +89,7 @@ public function testHandleResponse()
8489
->method('authenticate')
8590
->will($this->returnValue($response));
8691

87-
$this->securityContext
92+
$this->tokenStorage
8893
->expects($this->never())
8994
->method('setToken');
9095

0 commit comments

Comments
 (0)