-
Notifications
You must be signed in to change notification settings - Fork 702
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #2221 deprecate the access denied listener (xabbuh)
This PR was merged into the 2.x branch. Discussion ---------- deprecate the access denied listener Commits ------- 0525474 deprecate the access denied listener
- Loading branch information
Showing
20 changed files
with
347 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ | |
/** | ||
* AccessDeniedListenerTest. | ||
* | ||
* @group legacy | ||
* | ||
* @author Boris Guéry <[email protected]> | ||
*/ | ||
class AccessDeniedListenerTest extends TestCase | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSRestBundle 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\RestBundle\Tests\Functional; | ||
|
||
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface; | ||
|
||
abstract class AbstractAuthenticatorTestCase extends WebTestCase | ||
{ | ||
protected static $client; | ||
|
||
public static function setUpBeforeClass() | ||
{ | ||
if (!interface_exists(ErrorRendererInterface::class)) { | ||
self::markTestSkipped(); | ||
} | ||
|
||
parent::setUpBeforeClass(); | ||
|
||
self::$client = self::createClient(['test_case' => static::getTestCase()]); | ||
} | ||
|
||
public static function tearDownAfterClass() | ||
{ | ||
self::deleteTmpDir(static::getTestCase()); | ||
|
||
parent::tearDownAfterClass(); | ||
} | ||
|
||
public function testNoCredentialsGives401() | ||
{ | ||
self::$client->request('POST', '/api/login', [], [], ['CONTENT_TYPE' => 'application/json']); | ||
$response = self::$client->getResponse(); | ||
|
||
$this->assertEquals(401, $response->getStatusCode()); | ||
$this->assertEquals('application/json', $response->headers->get('Content-Type')); | ||
} | ||
|
||
public function testWrongCredentialsGives401() | ||
{ | ||
$this->sendRequestContainingInvalidCredentials('/api/login'); | ||
|
||
$response = self::$client->getResponse(); | ||
|
||
$this->assertEquals(401, $response->getStatusCode()); | ||
$this->assertEquals('application/json', $response->headers->get('Content-Type')); | ||
} | ||
|
||
public function testSuccessfulLogin() | ||
{ | ||
$this->sendRequestContainingValidCredentials('/api/login'); | ||
|
||
$response = self::$client->getResponse(); | ||
|
||
$this->assertEquals(200, $response->getStatusCode()); | ||
$this->assertEquals('application/json', $response->headers->get('Content-Type')); | ||
} | ||
|
||
public function testAccessDeniedExceptionGives403() | ||
{ | ||
$this->sendRequestContainingValidCredentials('/api/comments'); | ||
|
||
$response = self::$client->getResponse(); | ||
|
||
$this->assertEquals(403, $response->getStatusCode()); | ||
$this->assertEquals('application/json', $response->headers->get('Content-Type')); | ||
} | ||
|
||
abstract protected static function getTestCase(): string; | ||
|
||
abstract protected function sendRequestContainingInvalidCredentials(string $path): void; | ||
|
||
abstract protected function sendRequestContainingValidCredentials(string $path): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSRestBundle 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\RestBundle\Tests\Functional; | ||
|
||
class BasicAuthTest extends AbstractAuthenticatorTestCase | ||
{ | ||
protected static function getTestCase(): string | ||
{ | ||
return 'BasicAuth'; | ||
} | ||
|
||
protected function sendRequestContainingInvalidCredentials(string $path): void | ||
{ | ||
self::$client->request('POST', $path, [], [], [ | ||
'PHP_AUTH_USER' => 'restapi', | ||
'PHP_AUTH_PW' => 'wrongpw', | ||
]); | ||
} | ||
|
||
protected function sendRequestContainingValidCredentials(string $path): void | ||
{ | ||
self::$client->request('POST', $path, [], [], [ | ||
'PHP_AUTH_USER' => 'restapi', | ||
'PHP_AUTH_PW' => 'secretpw', | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSRestBundle 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\RestBundle\Tests\Functional; | ||
|
||
class CustomGuardAuthenticatorTest extends AbstractAuthenticatorTestCase | ||
{ | ||
protected static function getTestCase(): string | ||
{ | ||
return 'CustomGuardAuthenticator'; | ||
} | ||
|
||
protected function sendRequestContainingInvalidCredentials(string $path): void | ||
{ | ||
self::$client->request('POST', $path, [], [], ['HTTP_X-FOO' => 'BAR', 'CONTENT_TYPE' => 'application/json']); | ||
} | ||
|
||
protected function sendRequestContainingValidCredentials(string $path): void | ||
{ | ||
self::$client->request('POST', $path, [], [], ['HTTP_X-FOO' => 'FOOBAR', 'CONTENT_TYPE' => 'application/json']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSRestBundle 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. | ||
*/ | ||
|
||
return [ | ||
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(), | ||
new \Symfony\Bundle\SecurityBundle\SecurityBundle(), | ||
new \FOS\RestBundle\FOSRestBundle(), | ||
new \FOS\RestBundle\Tests\Functional\Bundle\TestBundle\TestBundle(), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
imports: | ||
- { resource: ../config/default.yml } | ||
- { resource: security.php } | ||
|
||
framework: | ||
serializer: | ||
enabled: true | ||
router: { resource: "%kernel.project_dir%/BasicAuth/routing.yml" } | ||
|
||
fos_rest: | ||
body_listener: false | ||
exception: | ||
exception_listener: false | ||
serialize_exceptions: false | ||
routing_loader: false | ||
zone: | ||
- { path: ^/api/* } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
api: | ||
path: /api/comments | ||
defaults: | ||
_controller: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\Api\CommentController::getComments | ||
_format: json | ||
|
||
api_login: | ||
path: /api/login | ||
defaults: | ||
_controller: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\Api\CommentController::loginAction | ||
_format: json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSRestBundle 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. | ||
*/ | ||
|
||
use Symfony\Component\Security\Core\Security; | ||
use Symfony\Component\Security\Http\Controller\UserValueResolver; | ||
|
||
$defaultFirewall = []; | ||
if (method_exists(Security::class, 'getUser') && !class_exists(UserValueResolver::class)) { | ||
$defaultFirewall['logout_on_user_change'] = true; | ||
} | ||
|
||
$container->loadFromExtension('security', [ | ||
'encoders' => ['Symfony\Component\Security\Core\User\User' => 'plaintext'], | ||
'providers' => [ | ||
'in_memory' => [ | ||
'memory' => [ | ||
'users' => [ | ||
'restapi' => ['password' => 'secretpw', 'roles' => ['ROLE_API']], | ||
], | ||
], | ||
], | ||
], | ||
'firewalls' => [ | ||
'default' => array_merge($defaultFirewall, [ | ||
'provider' => 'in_memory', | ||
'anonymous' => 'lazy', | ||
'stateless' => true, | ||
'http_basic' => null, | ||
]), | ||
], | ||
'access_control' => [ | ||
['path' => '^/api/comments', 'roles' => 'ROLE_ADMIN'], | ||
['path' => '^/api', 'roles' => 'ROLE_API'], | ||
], | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSRestBundle 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. | ||
*/ | ||
|
||
return [ | ||
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(), | ||
new \Symfony\Bundle\SecurityBundle\SecurityBundle(), | ||
new \FOS\RestBundle\FOSRestBundle(), | ||
new \FOS\RestBundle\Tests\Functional\Bundle\TestBundle\TestBundle(), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
imports: | ||
- { resource: ../config/default.yml } | ||
- { resource: security.php } | ||
|
||
framework: | ||
serializer: | ||
enabled: true | ||
router: { resource: "%kernel.project_dir%/CustomGuardAuthenticator/routing.yml" } | ||
|
||
fos_rest: | ||
body_listener: false | ||
exception: | ||
exception_listener: false | ||
serialize_exceptions: false | ||
routing_loader: false | ||
zone: | ||
- { path: ^/api/* } | ||
|
||
services: | ||
api_token_authenticator: | ||
class: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Security\ApiTokenAuthenticator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
api: | ||
path: /api/comments | ||
defaults: | ||
_controller: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\Api\CommentController::getComments | ||
_format: json | ||
|
||
api_login: | ||
path: /api/login | ||
defaults: | ||
_controller: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\Api\CommentController::loginAction | ||
_format: json |
Oops, something went wrong.