diff options
author | Jérémy Benoist <j0k3r@users.noreply.github.com> | 2017-06-09 11:05:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-09 11:05:34 +0200 |
commit | 5c762616c378d20b96eda0f0727eb4c760d4119d (patch) | |
tree | 086049b73cf8d547f18c28963a60f36c5679cfcc | |
parent | 3f474025d889c3eff20b481f005f4d292f1ef29d (diff) | |
parent | f81a34e37929a822755d120215d2f18f042ff713 (diff) | |
download | wallabag-5c762616c378d20b96eda0f0727eb4c760d4119d.tar.gz wallabag-5c762616c378d20b96eda0f0727eb4c760d4119d.tar.zst wallabag-5c762616c378d20b96eda0f0727eb4c760d4119d.zip |
Merge pull request #3195 from wallabag/authentication-error
Log an error level message when user auth fail
3 files changed, 114 insertions, 0 deletions
diff --git a/src/Wallabag/UserBundle/EventListener/AuthenticationFailureListener.php b/src/Wallabag/UserBundle/EventListener/AuthenticationFailureListener.php new file mode 100644 index 00000000..10f13233 --- /dev/null +++ b/src/Wallabag/UserBundle/EventListener/AuthenticationFailureListener.php | |||
@@ -0,0 +1,40 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\UserBundle\EventListener; | ||
4 | |||
5 | use Psr\Log\LoggerInterface; | ||
6 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
7 | use Symfony\Component\HttpFoundation\RequestStack; | ||
8 | use Symfony\Component\Security\Core\AuthenticationEvents; | ||
9 | |||
10 | class AuthenticationFailureListener implements EventSubscriberInterface | ||
11 | { | ||
12 | private $requestStack; | ||
13 | private $logger; | ||
14 | |||
15 | public function __construct(RequestStack $requestStack, LoggerInterface $logger) | ||
16 | { | ||
17 | $this->requestStack = $requestStack; | ||
18 | $this->logger = $logger; | ||
19 | } | ||
20 | |||
21 | /** | ||
22 | * {@inheritdoc} | ||
23 | */ | ||
24 | public static function getSubscribedEvents() | ||
25 | { | ||
26 | return [ | ||
27 | AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure', | ||
28 | ]; | ||
29 | } | ||
30 | |||
31 | /** | ||
32 | * On failure, add a custom error in log so server admin can configure fail2ban to block IP from people who try to login too much. | ||
33 | */ | ||
34 | public function onAuthenticationFailure() | ||
35 | { | ||
36 | $request = $this->requestStack->getMasterRequest(); | ||
37 | |||
38 | $this->logger->error('Authentication failure for user "'.$request->request->get('_username').'", from IP "'.$request->getClientIp().'", with UA: "'.$request->server->get('HTTP_USER_AGENT').'".'); | ||
39 | } | ||
40 | } | ||
diff --git a/src/Wallabag/UserBundle/Resources/config/services.yml b/src/Wallabag/UserBundle/Resources/config/services.yml index 72f6f12c..f2cd6e01 100644 --- a/src/Wallabag/UserBundle/Resources/config/services.yml +++ b/src/Wallabag/UserBundle/Resources/config/services.yml | |||
@@ -35,3 +35,11 @@ services: | |||
35 | - "%wallabag_core.list_mode%" | 35 | - "%wallabag_core.list_mode%" |
36 | tags: | 36 | tags: |
37 | - { name: kernel.event_subscriber } | 37 | - { name: kernel.event_subscriber } |
38 | |||
39 | wallabag_user.listener.authentication_failure_event_listener: | ||
40 | class: Wallabag\UserBundle\EventListener\AuthenticationFailureListener | ||
41 | arguments: | ||
42 | - "@request_stack" | ||
43 | - "@logger" | ||
44 | tags: | ||
45 | - { name: kernel.event_listener, event: security.authentication.failure, method: onAuthenticationFailure } | ||
diff --git a/tests/Wallabag/UserBundle/EventListener/AuthenticationFailureListenerTest.php b/tests/Wallabag/UserBundle/EventListener/AuthenticationFailureListenerTest.php new file mode 100644 index 00000000..6191ea13 --- /dev/null +++ b/tests/Wallabag/UserBundle/EventListener/AuthenticationFailureListenerTest.php | |||
@@ -0,0 +1,66 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\UserBundle\EventListener; | ||
4 | |||
5 | use Symfony\Component\EventDispatcher\EventDispatcher; | ||
6 | use Symfony\Component\HttpFoundation\Request; | ||
7 | use Wallabag\UserBundle\EventListener\AuthenticationFailureListener; | ||
8 | use Monolog\Logger; | ||
9 | use Monolog\Handler\TestHandler; | ||
10 | use Symfony\Component\HttpFoundation\RequestStack; | ||
11 | use Symfony\Component\Security\Core\AuthenticationEvents; | ||
12 | use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent; | ||
13 | |||
14 | class AuthenticationFailureListenerTest extends \PHPUnit_Framework_TestCase | ||
15 | { | ||
16 | private $requestStack; | ||
17 | private $logHandler; | ||
18 | private $listener; | ||
19 | private $dispatcher; | ||
20 | |||
21 | protected function setUp() | ||
22 | { | ||
23 | $request = Request::create('/'); | ||
24 | $request->request->set('_username', 'admin'); | ||
25 | |||
26 | $this->requestStack = new RequestStack(); | ||
27 | $this->requestStack->push($request); | ||
28 | |||
29 | $this->logHandler = new TestHandler(); | ||
30 | $logger = new Logger('test', [$this->logHandler]); | ||
31 | |||
32 | $this->listener = new AuthenticationFailureListener( | ||
33 | $this->requestStack, | ||
34 | $logger | ||
35 | ); | ||
36 | |||
37 | $this->dispatcher = new EventDispatcher(); | ||
38 | $this->dispatcher->addSubscriber($this->listener); | ||
39 | } | ||
40 | |||
41 | public function testOnAuthenticationFailure() | ||
42 | { | ||
43 | $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface') | ||
44 | ->disableOriginalConstructor() | ||
45 | ->getMock(); | ||
46 | |||
47 | $exception = $this->getMockBuilder('Symfony\Component\Security\Core\Exception\AuthenticationException') | ||
48 | ->disableOriginalConstructor() | ||
49 | ->getMock(); | ||
50 | |||
51 | $event = new AuthenticationFailureEvent( | ||
52 | $token, | ||
53 | $exception | ||
54 | ); | ||
55 | |||
56 | $this->dispatcher->dispatch( | ||
57 | AuthenticationEvents::AUTHENTICATION_FAILURE, | ||
58 | $event | ||
59 | ); | ||
60 | |||
61 | $records = $this->logHandler->getRecords(); | ||
62 | |||
63 | $this->assertCount(1, $records); | ||
64 | $this->assertSame('Authentication failure for user "admin", from IP "127.0.0.1", with UA: "Symfony/3.X".', $records[0]['message']); | ||
65 | } | ||
66 | } | ||