Commit bc50a7e6 authored by Fabien Potencier's avatar Fabien Potencier

feature #1629 Expose AuthenticationUtils (hkdobrev)

This PR was squashed before being merged into the 2.3.x-dev branch (closes #1629).

Discussion
----------

Expose AuthenticationUtils

Close #1620.

Commits
-------

ebe85e0d Expose AuthenticationUtils
parents 418ee8d0 ebe85e0d
...@@ -15,6 +15,7 @@ Changelog ...@@ -15,6 +15,7 @@ Changelog
* dropped support for Symfony 2.x and 3.x * dropped support for Symfony 2.x and 3.x
* added support for Symfony 4 * added support for Symfony 4
* added support PSR-3 log levels in MonologServiceProvider * added support PSR-3 log levels in MonologServiceProvider
* exposed AuthenticationUtils in SecurityServiceProvider
2.2.3 (2018-02-25) 2.2.3 (2018-02-25)
------------------ ------------------
......
...@@ -39,8 +39,11 @@ Services ...@@ -39,8 +39,11 @@ Services
* **security.user_checker**: Checks user flags after authentication. * **security.user_checker**: Checks user flags after authentication.
* **security.last_error**: Returns the last authentication errors when given a * **security.last_error**: Returns the last authentication error message when
Request object. given a Request object.
* **security.authentication_utils**: Returns the AuthenticationUtils service
allowing you to get last authentication exception or last username.
* **security.encoder_factory**: Defines the encoding strategies for user * **security.encoder_factory**: Defines the encoding strategies for user
passwords (uses ``security.default_encoder``). passwords (uses ``security.default_encoder``).
...@@ -247,6 +250,10 @@ The ``error`` and ``last_username`` variables contain the last authentication ...@@ -247,6 +250,10 @@ The ``error`` and ``last_username`` variables contain the last authentication
error and the last username entered by the user in case of an authentication error and the last username entered by the user in case of an authentication
error. error.
If you want to have the last error message translated, you would need to use
the ``security.authentication_utils`` service and retrieve
the actual ``AuthenticationException`` instance.
Create the associated template: Create the associated template:
.. code-block:: jinja .. code-block:: jinja
......
...@@ -32,6 +32,7 @@ use Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationPro ...@@ -32,6 +32,7 @@ use Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationPro
use Symfony\Component\Security\Core\Authentication\Provider\AnonymousAuthenticationProvider; use Symfony\Component\Security\Core\Authentication\Provider\AnonymousAuthenticationProvider;
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager; use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver; use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler; use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler; use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
...@@ -685,6 +686,10 @@ class SecurityServiceProvider implements ServiceProviderInterface, EventListener ...@@ -685,6 +686,10 @@ class SecurityServiceProvider implements ServiceProviderInterface, EventListener
return new AnonymousAuthenticationProvider($name); return new AnonymousAuthenticationProvider($name);
}; };
}); });
$app['security.authentication_utils'] = function ($app) {
return new AuthenticationUtils($app['request_stack']);
};
} }
public function subscribe(Container $app, EventDispatcherInterface $dispatcher) public function subscribe(Container $app, EventDispatcherInterface $dispatcher)
......
...@@ -17,6 +17,7 @@ use Silex\Provider\SecurityServiceProvider; ...@@ -17,6 +17,7 @@ use Silex\Provider\SecurityServiceProvider;
use Silex\Provider\SessionServiceProvider; use Silex\Provider\SessionServiceProvider;
use Silex\Provider\ValidatorServiceProvider; use Silex\Provider\ValidatorServiceProvider;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpKernel\Client; use Symfony\Component\HttpKernel\Client;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
...@@ -181,6 +182,13 @@ class SecurityServiceProviderTest extends WebTestCase ...@@ -181,6 +182,13 @@ class SecurityServiceProviderTest extends WebTestCase
$client->request('post', '/login_check', ['_username' => 'unknown', '_password' => 'bar']); $client->request('post', '/login_check', ['_username' => 'unknown', '_password' => 'bar']);
$this->assertEquals('Username "unknown" does not exist.', $app['security.last_error']($client->getRequest())); $this->assertEquals('Username "unknown" does not exist.', $app['security.last_error']($client->getRequest()));
$client->getRequest()->getSession()->save(); $client->getRequest()->getSession()->save();
$client->request('post', '/login_check', ['_username' => 'unknown', '_password' => 'bar']);
$app['request_stack']->push($client->getRequest());
$authenticationException = $app['security.authentication_utils']->getLastAuthenticationError();
$this->assertInstanceOf(AuthenticationException::class, $authenticationException);
$this->assertEquals('Username "unknown" does not exist.', $authenticationException->getMessage());
$client->getRequest()->getSession()->save();
} }
public function testFakeRoutesAreSerializable() public function testFakeRoutesAreSerializable()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment