Commit ebe85e0d authored by Haralan Dobrev's avatar Haralan Dobrev Committed by Fabien Potencier

Expose AuthenticationUtils

parent 418ee8d0
......@@ -15,6 +15,7 @@ Changelog
* dropped support for Symfony 2.x and 3.x
* added support for Symfony 4
* added support PSR-3 log levels in MonologServiceProvider
* exposed AuthenticationUtils in SecurityServiceProvider
2.2.3 (2018-02-25)
------------------
......
......@@ -39,8 +39,11 @@ Services
* **security.user_checker**: Checks user flags after authentication.
* **security.last_error**: Returns the last authentication errors when given a
Request object.
* **security.last_error**: Returns the last authentication error message when
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
passwords (uses ``security.default_encoder``).
......@@ -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.
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:
.. code-block:: jinja
......
......@@ -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\AuthenticationProviderManager;
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\DefaultAuthenticationFailureHandler;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
......@@ -685,6 +686,10 @@ class SecurityServiceProvider implements ServiceProviderInterface, EventListener
return new AnonymousAuthenticationProvider($name);
};
});
$app['security.authentication_utils'] = function ($app) {
return new AuthenticationUtils($app['request_stack']);
};
}
public function subscribe(Container $app, EventDispatcherInterface $dispatcher)
......
......@@ -17,6 +17,7 @@ use Silex\Provider\SecurityServiceProvider;
use Silex\Provider\SessionServiceProvider;
use Silex\Provider\ValidatorServiceProvider;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpKernel\Client;
use Symfony\Component\HttpFoundation\Request;
......@@ -181,6 +182,13 @@ class SecurityServiceProviderTest extends WebTestCase
$client->request('post', '/login_check', ['_username' => 'unknown', '_password' => 'bar']);
$this->assertEquals('Username "unknown" does not exist.', $app['security.last_error']($client->getRequest()));
$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()
......
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