|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JwPersistentUser\Service; |
|
4
|
|
|
|
|
5
|
|
|
use JwPersistentUser\Authentication\Adapter\ForceLogin; |
|
6
|
|
|
|
|
7
|
|
|
use Zend\Http\Request; |
|
8
|
|
|
use Zend\Http\Response; |
|
9
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
10
|
|
|
use Zend\Stdlib\RequestInterface; |
|
11
|
|
|
use Zend\Stdlib\ResponseInterface; |
|
12
|
|
|
use Zend\EventManager\EventManagerAwareTrait; |
|
13
|
|
|
use Zend\Authentication\AuthenticationService; |
|
14
|
|
|
|
|
15
|
|
|
use ZfcUser\Mapper\User as ZfcUserMapper; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Note that dependencies in this service are pulled directory out of the service locator for |
|
19
|
|
|
* a reason: performance. Would we NOT do this than we would instantiate those resources on |
|
20
|
|
|
* every request. |
|
21
|
|
|
*/ |
|
22
|
|
|
class CookieAuthenticationService |
|
23
|
|
|
{ |
|
24
|
|
|
use EventManagerAwareTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ServiceLocatorInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $serviceLocator; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var AuthenticationService |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $authService; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var RememberMeService |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $rememberMeService; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var CookieService |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $cookieService; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var ZfcUserMapper |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $userMapper; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function __construct(ServiceLocatorInterface $serviceLocator) |
|
55
|
|
|
{ |
|
56
|
1 |
|
$this->serviceLocator = $serviceLocator; |
|
57
|
1 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param RequestInterface $request |
|
61
|
|
|
* @param ResponseInterface $response |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function loginFrom(RequestInterface $request, ResponseInterface $response) |
|
64
|
|
|
{ |
|
65
|
1 |
|
if (!($request instanceof Request) || !($response instanceof Response)) { |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
$serieToken = $this->getCookieService()->read($request, $response); |
|
70
|
1 |
|
if (!$serieToken) { |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// TODO use a session once we validated using Cookie. So we do not need to instantiate |
|
75
|
|
|
// the AuthService on every following request. |
|
76
|
|
|
|
|
77
|
1 |
|
if ($this->getAuthService()->hasIdentity()) { |
|
78
|
|
|
return; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
$user = $this->getUserMapper()->findById($serieToken->getUserId()); |
|
82
|
1 |
|
if (!$user) { |
|
83
|
|
|
$this->invalidAttempt($response); |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
$nextSerieToken = $this->getRememberMeService()->getNextInSerie($serieToken); |
|
88
|
1 |
|
if (!$nextSerieToken) { |
|
89
|
|
|
$this->invalidAttempt($response); |
|
90
|
|
|
return; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
$this->getCookieService()->writeSerie($response, $nextSerieToken); |
|
94
|
|
|
|
|
95
|
1 |
|
$this->getAuthService()->authenticate(new ForceLogin($nextSerieToken->getUserId())); |
|
96
|
|
|
|
|
97
|
1 |
|
$this->getEventManager()->trigger('login', $this, [ |
|
98
|
1 |
|
'token' => $nextSerieToken, |
|
99
|
|
|
]); |
|
100
|
1 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param Response $response |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function invalidAttempt(Response $response) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->getAuthService()->clearIdentity(); |
|
108
|
|
|
|
|
109
|
|
|
$this->getCookieService()->writeNull($response); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return AuthenticationService |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function getAuthService() |
|
116
|
|
|
{ |
|
117
|
1 |
|
if ($this->authService === null) { |
|
118
|
|
|
$this->authService = $this->serviceLocator->get('zfcuser_auth_service'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
return $this->authService; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param AuthenticationService $authService |
|
126
|
|
|
* @return $this |
|
127
|
|
|
*/ |
|
128
|
1 |
|
public function setAuthService($authService) |
|
129
|
|
|
{ |
|
130
|
1 |
|
$this->authService = $authService; |
|
131
|
1 |
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return RememberMeService |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public function getRememberMeService() |
|
138
|
|
|
{ |
|
139
|
1 |
|
if ($this->rememberMeService === null) { |
|
140
|
|
|
$this->rememberMeService = $this->serviceLocator->get('JwPersistentUser\Service\RememberMe'); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
1 |
|
return $this->rememberMeService; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param RememberMeService $rememberMeService |
|
148
|
|
|
* @return $this |
|
149
|
|
|
*/ |
|
150
|
1 |
|
public function setRememberMeService($rememberMeService) |
|
151
|
|
|
{ |
|
152
|
1 |
|
$this->rememberMeService = $rememberMeService; |
|
153
|
1 |
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return CookieService |
|
158
|
|
|
*/ |
|
159
|
1 |
|
public function getCookieService() |
|
160
|
|
|
{ |
|
161
|
1 |
|
if ($this->cookieService === null) { |
|
162
|
|
|
$this->cookieService = $this->serviceLocator->get('JwPersistentUser\Service\Cookie'); |
|
163
|
|
|
} |
|
164
|
1 |
|
return $this->cookieService; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param CookieService $cookieService |
|
169
|
|
|
* @return $this |
|
170
|
|
|
*/ |
|
171
|
1 |
|
public function setCookieService($cookieService) |
|
172
|
|
|
{ |
|
173
|
1 |
|
$this->cookieService = $cookieService; |
|
174
|
1 |
|
return $this; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return ZfcUserMapper |
|
179
|
|
|
*/ |
|
180
|
1 |
|
public function getUserMapper() |
|
181
|
|
|
{ |
|
182
|
1 |
|
if ($this->userMapper === null) { |
|
183
|
|
|
$this->userMapper = $this->serviceLocator->get('zfcuser_user_mapper'); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
1 |
|
return $this->userMapper; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param ZfcUserMapper $mapper |
|
191
|
|
|
*/ |
|
192
|
1 |
|
public function setUserMapper(ZfcUserMapper $mapper) |
|
193
|
|
|
{ |
|
194
|
1 |
|
$this->userMapper = $mapper; |
|
195
|
1 |
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|