|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Jitamin. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) Jitamin Team |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Jitamin\Bus\Subscriber; |
|
13
|
|
|
|
|
14
|
|
|
use Jitamin\Bus\Event\AuthFailureEvent; |
|
15
|
|
|
use Jitamin\Bus\Event\AuthSuccessEvent; |
|
16
|
|
|
use Jitamin\Foundation\Security\AuthenticationManager; |
|
17
|
|
|
use Jitamin\Foundation\Session\SessionManager; |
|
18
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Authentication Subscriber. |
|
22
|
|
|
*/ |
|
23
|
|
|
class AuthSubscriber extends BaseSubscriber implements EventSubscriberInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Get event listeners. |
|
27
|
|
|
* |
|
28
|
|
|
* @static |
|
29
|
|
|
* |
|
30
|
|
|
* @return array |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function getSubscribedEvents() |
|
33
|
|
|
{ |
|
34
|
|
|
return [ |
|
35
|
|
|
AuthenticationManager::EVENT_SUCCESS => 'afterLogin', |
|
36
|
|
|
AuthenticationManager::EVENT_FAILURE => 'onLoginFailure', |
|
37
|
|
|
SessionManager::EVENT_DESTROY => 'afterLogout', |
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* After Login callback. |
|
43
|
|
|
* |
|
44
|
|
|
* @param AuthSuccessEvent $event |
|
45
|
|
|
*/ |
|
46
|
|
|
public function afterLogin(AuthSuccessEvent $event) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->logger->debug('Subscriber executed: '.__METHOD__); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
$userAgent = $this->request->getUserAgent(); |
|
|
|
|
|
|
51
|
|
|
$ipAddress = $this->request->getIpAddress(); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
$this->userLockingModel->resetFailedLogin($this->userSession->getUsername()); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
$this->lastLoginModel->create( |
|
|
|
|
|
|
56
|
|
|
$event->getAuthType(), |
|
57
|
|
|
$this->userSession->getId(), |
|
|
|
|
|
|
58
|
|
|
$ipAddress, |
|
59
|
|
|
$userAgent |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
if ($event->getAuthType() === 'RememberMe') { |
|
63
|
|
|
$this->userSession->validatePostAuthentication(); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (isset($this->sessionStorage->hasRememberMe) && $this->sessionStorage->hasRememberMe) { |
|
|
|
|
|
|
67
|
|
|
$session = $this->rememberMeSessionModel->create($this->userSession->getId(), $ipAddress, $userAgent); |
|
|
|
|
|
|
68
|
|
|
$this->rememberMeCookie->write($session['token'], $session['sequence'], $session['expiration']); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Destroy RememberMe session on logout. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function afterLogout() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->logger->debug('Subscriber executed: '.__METHOD__); |
|
|
|
|
|
|
78
|
|
|
$credentials = $this->rememberMeCookie->read(); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
if ($credentials !== false) { |
|
81
|
|
|
$session = $this->rememberMeSessionModel->find($credentials['token'], $credentials['sequence']); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
if (!empty($session)) { |
|
84
|
|
|
$this->rememberMeSessionModel->remove($session['id']); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->rememberMeCookie->remove(); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Increment failed login counter. |
|
93
|
|
|
* |
|
94
|
|
|
* @param AuthFailureEvent $event |
|
95
|
|
|
*/ |
|
96
|
|
|
public function onLoginFailure(AuthFailureEvent $event) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->logger->debug('Subscriber executed: '.__METHOD__); |
|
|
|
|
|
|
99
|
|
|
$username = $event->getUsername(); |
|
100
|
|
|
|
|
101
|
|
|
if (!empty($username)) { |
|
102
|
|
|
$this->userLockingModel->incrementFailedLogin($username); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
if ($this->userLockingModel->getFailedLogin($username) > BRUTEFORCE_LOCKDOWN) { |
|
|
|
|
|
|
105
|
|
|
$this->userLockingModel->lock($username, BRUTEFORCE_LOCKDOWN_DURATION); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.