Skip to content

Commit 9f3444c

Browse files
committed
Merge branch '2.x'
* 2.x: deprecate the access denied listener
2 parents 149eba2 + 30e08e1 commit 9f3444c

File tree

19 files changed

+344
-1
lines changed

19 files changed

+344
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ CHANGELOG
172172
in 3.0
173173
* deprecated the following options:
174174

175+
* `fos_rest.access_denied_listener`
175176
* `fos_rest.exception.exception_controller`
176177
* `fos_rest.exception.exception_listener`
177178
* `fos_rest.exception.service`
@@ -192,6 +193,7 @@ CHANGELOG
192193
* `FOS\RestBundle\Controller\ExceptionController`
193194
* `FOS\RestBundle\Controller\TemplatingExceptionController`
194195
* `FOS\RestBundle\Controller\TwigExceptionController`
196+
* `FOS\RestBundle\EventListener\AccessDeniedListener`
195197
* `FOS\RestBundle\EventListener\ExceptionListener`
196198
* `FOS\RestBundle\Inflector\DoctrineInflector`
197199
* `FOS\RestBundle\Inflector\InflectorInterface`
@@ -209,6 +211,7 @@ CHANGELOG
209211

210212
* the following services and aliases are marked as `deprecated`, they will be removed in 3.0:
211213

214+
* `fos_rest.access_denied_listener`
212215
* `fos_rest.exception_listener`
213216
* `fos_rest.exception.controller`
214217
* `fos_rest.exception.twig_controller`

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function getConfigTreeBuilder(): TreeBuilder
4949
->scalarNode('disable_csrf_role')->defaultNull()->end()
5050
->arrayNode('access_denied_listener')
5151
->canBeEnabled()
52+
->setDeprecated('The "%path%.%node%" option is deprecated since FOSRestBundle 2.8.')
5253
->beforeNormalization()
5354
->ifArray()->then(function ($v) {
5455
if (!empty($v) && empty($v['formats'])) {

EventListener/AccessDeniedListener.php

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

1212
namespace FOS\RestBundle\EventListener;
1313

14+
@trigger_error(sprintf('The %s\AccessDeniedListener class is deprecated since FOSRestBundle 2.8.', __NAMESPACE__), E_USER_DEPRECATED);
15+
1416
use FOS\RestBundle\FOSRestBundle;
1517
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
1618
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

Resources/config/access_denied_listener.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<tag name="monolog.logger" channel="request" />
1212
<argument type="collection" /> <!-- formats -->
1313
<argument /> <!-- unauthorized challenge -->
14+
<deprecated>The "%service_id%" service is deprecated since FOSRestBundle 2.8.</deprecated>
1415
</service>
1516

1617
</services>

Tests/EventListener/AccessDeniedListenerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
/**
2929
* AccessDeniedListenerTest.
3030
*
31+
* @group legacy
32+
*
3133
* @author Boris Guéry <[email protected]>
3234
*/
3335
class AccessDeniedListenerTest extends TestCase
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Tests\Functional;
13+
14+
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
15+
16+
abstract class AbstractAuthenticatorTestCase extends WebTestCase
17+
{
18+
protected static $client;
19+
20+
public static function setUpBeforeClass()
21+
{
22+
if (!interface_exists(ErrorRendererInterface::class)) {
23+
self::markTestSkipped();
24+
}
25+
26+
parent::setUpBeforeClass();
27+
28+
self::$client = self::createClient(['test_case' => static::getTestCase()]);
29+
}
30+
31+
public static function tearDownAfterClass()
32+
{
33+
self::deleteTmpDir(static::getTestCase());
34+
35+
parent::tearDownAfterClass();
36+
}
37+
38+
public function testNoCredentialsGives401()
39+
{
40+
self::$client->request('POST', '/api/login', [], [], ['CONTENT_TYPE' => 'application/json']);
41+
$response = self::$client->getResponse();
42+
43+
$this->assertEquals(401, $response->getStatusCode());
44+
$this->assertEquals('application/json', $response->headers->get('Content-Type'));
45+
}
46+
47+
public function testWrongCredentialsGives401()
48+
{
49+
$this->sendRequestContainingInvalidCredentials('/api/login');
50+
51+
$response = self::$client->getResponse();
52+
53+
$this->assertEquals(401, $response->getStatusCode());
54+
$this->assertEquals('application/json', $response->headers->get('Content-Type'));
55+
}
56+
57+
public function testSuccessfulLogin()
58+
{
59+
$this->sendRequestContainingValidCredentials('/api/login');
60+
61+
$response = self::$client->getResponse();
62+
63+
$this->assertEquals(200, $response->getStatusCode());
64+
$this->assertEquals('application/json', $response->headers->get('Content-Type'));
65+
}
66+
67+
public function testAccessDeniedExceptionGives403()
68+
{
69+
$this->sendRequestContainingValidCredentials('/api/comments');
70+
71+
$response = self::$client->getResponse();
72+
73+
$this->assertEquals(403, $response->getStatusCode());
74+
$this->assertEquals('application/json', $response->headers->get('Content-Type'));
75+
}
76+
77+
abstract protected static function getTestCase(): string;
78+
79+
abstract protected function sendRequestContainingInvalidCredentials(string $path): void;
80+
81+
abstract protected function sendRequestContainingValidCredentials(string $path): void;
82+
}

Tests/Functional/AccessDeniedListenerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
1515

16+
/**
17+
* @group legacy
18+
*/
1619
class AccessDeniedListenerTest extends WebTestCase
1720
{
1821
private static $client;

Tests/Functional/BasicAuthTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Tests\Functional;
13+
14+
class BasicAuthTest extends AbstractAuthenticatorTestCase
15+
{
16+
protected static function getTestCase(): string
17+
{
18+
return 'BasicAuth';
19+
}
20+
21+
protected function sendRequestContainingInvalidCredentials(string $path): void
22+
{
23+
self::$client->request('POST', $path, [], [], [
24+
'PHP_AUTH_USER' => 'restapi',
25+
'PHP_AUTH_PW' => 'wrongpw',
26+
]);
27+
}
28+
29+
protected function sendRequestContainingValidCredentials(string $path): void
30+
{
31+
self::$client->request('POST', $path, [], [], [
32+
'PHP_AUTH_USER' => 'restapi',
33+
'PHP_AUTH_PW' => 'secretpw',
34+
]);
35+
}
36+
}

Tests/Functional/Bundle/TestBundle/Security/ApiTokenAuthenticator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
5555

5656
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
5757
{
58-
throw new AuthenticationException('Token not valid');
58+
return new JsonResponse(null, 401);
5959
}
6060

6161
/**
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Tests\Functional;
13+
14+
class CustomGuardAuthenticatorTest extends AbstractAuthenticatorTestCase
15+
{
16+
protected static function getTestCase(): string
17+
{
18+
return 'CustomGuardAuthenticator';
19+
}
20+
21+
protected function sendRequestContainingInvalidCredentials(string $path): void
22+
{
23+
self::$client->request('POST', $path, [], [], ['HTTP_X-FOO' => 'BAR', 'CONTENT_TYPE' => 'application/json']);
24+
}
25+
26+
protected function sendRequestContainingValidCredentials(string $path): void
27+
{
28+
self::$client->request('POST', $path, [], [], ['HTTP_X-FOO' => 'FOOBAR', 'CONTENT_TYPE' => 'application/json']);
29+
}
30+
}

0 commit comments

Comments
 (0)