Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
ebe85e0d
Commit
ebe85e0d
authored
Mar 17, 2018
by
Haralan Dobrev
Committed by
Fabien Potencier
Mar 17, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Expose AuthenticationUtils
parent
418ee8d0
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
23 additions
and
2 deletions
+23
-2
doc/changelog.rst
doc/changelog.rst
+1
-0
doc/providers/security.rst
doc/providers/security.rst
+9
-2
src/Silex/Provider/SecurityServiceProvider.php
src/Silex/Provider/SecurityServiceProvider.php
+5
-0
tests/Silex/Tests/Provider/SecurityServiceProviderTest.php
tests/Silex/Tests/Provider/SecurityServiceProviderTest.php
+8
-0
No files found.
doc/changelog.rst
View file @
ebe85e0d
...
...
@@ -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)
------------------
...
...
doc/providers/security.rst
View file @
ebe85e0d
...
...
@@ -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
...
...
src/Silex/Provider/SecurityServiceProvider.php
View file @
ebe85e0d
...
...
@@ -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
)
...
...
tests/Silex/Tests/Provider/SecurityServiceProviderTest.php
View file @
ebe85e0d
...
...
@@ -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
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment