-
Notifications
You must be signed in to change notification settings - Fork 343
/
Db.php
207 lines (184 loc) · 6.03 KB
/
Db.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace ZfcUser\Authentication\Adapter;
use DateTime;
use Zend\Authentication\Result as AuthenticationResult;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;
use Zend\Crypt\Password\Bcrypt;
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;
use ZfcUser\Mapper\User as UserMapperInterface;
use ZfcUser\Options\AuthenticationOptionsInterface;
class Db extends AbstractAdapter implements ServiceManagerAwareInterface
{
/**
* @var UserMapperInterface
*/
protected $mapper;
/**
* @var closure / invokable object
*/
protected $credentialPreprocessor;
/**
* @var ServiceManager
*/
protected $serviceManager;
/**
* @var AuthenticationOptionsInterface
*/
protected $options;
public function authenticate(AuthEvent $e)
{
if ($this->isSatisfied()) {
$storage = $this->getStorage()->read();
$e->setIdentity($storage['identity'])
->setCode(AuthenticationResult::SUCCESS)
->setMessages(array('Authentication successful.'));
return;
}
$identity = $e->getRequest()->getPost()->get('identity');
$credential = $e->getRequest()->getPost()->get('credential');
$credential = $this->preProcessCredential($credential);
$userObject = NULL;
// Cycle through the configured identity sources and test each
$fields = $this->getOptions()->getAuthIdentityFields();
while ( !is_object($userObject) && count($fields) > 0 ) {
$mode = array_shift($fields);
switch ($mode) {
case 'username':
$userObject = $this->getMapper()->findByUsername($identity);
break;
case 'email':
$userObject = $this->getMapper()->findByEmail($identity);
break;
}
}
if (!$userObject) {
$e->setCode(AuthenticationResult::FAILURE_IDENTITY_NOT_FOUND)
->setMessages(array('A record with the supplied identity could not be found.'));
$this->setSatisfied(false);
return false;
}
if ($this->getOptions()->getEnableUserState()) {
// Don't allow user to login if state is not in allowed list
if (!in_array($userObject->getState(), $this->getOptions()->getAllowedLoginStates())) {
$e->setCode(AuthenticationResult::FAILURE_INACTIVE)
->setMessages(array('A record with the supplied identity is not active.'));
$this->setSatisfied(false);
return false;
}
}
$bcrypt = new Bcrypt();
$bcrypt->setCost($this->getOptions()->getPasswordCost());
if (!$bcrypt->verify($credential,$userObject->getPassword())) {
// Password does not match
$e->setCode(AuthenticationResult::FAILURE_CREDENTIAL_INVALID)
->setMessages(array('Supplied credential is invalid.'));
$this->setSatisfied(false);
return false;
}
// Success!
$e->setIdentity($userObject->getId());
// Update user's password hash if the cost parameter has changed
$this->updateUserPasswordHash($userObject, $credential, $bcrypt);
$this->setSatisfied(true);
$storage = $this->getStorage()->read();
$storage['identity'] = $e->getIdentity();
$this->getStorage()->write($storage);
$e->setCode(AuthenticationResult::SUCCESS)
->setMessages(array('Authentication successful.'));
}
protected function updateUserPasswordHash($userObject, $password, $bcrypt)
{
$hash = explode('$', $userObject->getPassword());
if ($hash[2] === $bcrypt->getCost()) return;
$userObject->setPassword($bcrypt->create($password));
$this->getMapper()->update($userObject);
return $this;
}
public function preprocessCredential($credential)
{
$processor = $this->getCredentialPreprocessor();
if (is_callable($processor)) {
return $processor($credential);
}
return $credential;
}
/**
* getMapper
*
* @return UserMapperInterface
*/
public function getMapper()
{
if (null === $this->mapper) {
$this->mapper = $this->getServiceManager()->get('zfcuser_user_mapper');
}
return $this->mapper;
}
/**
* setMapper
*
* @param UserMapperInterface $mapper
* @return Db
*/
public function setMapper(UserMapperInterface $mapper)
{
$this->mapper = $mapper;
return $this;
}
/**
* Get credentialPreprocessor.
*
* @return \callable
*/
public function getCredentialPreprocessor()
{
return $this->credentialPreprocessor;
}
/**
* Set credentialPreprocessor.
*
* @param $credentialPreprocessor the value to be set
*/
public function setCredentialPreprocessor($credentialPreprocessor)
{
$this->credentialPreprocessor = $credentialPreprocessor;
return $this;
}
/**
* Retrieve service manager instance
*
* @return ServiceManager
*/
public function getServiceManager()
{
return $this->serviceManager;
}
/**
* Set service manager instance
*
* @param ServiceManager $locator
* @return void
*/
public function setServiceManager(ServiceManager $serviceManager)
{
$this->serviceManager = $serviceManager;
}
/**
* @param AuthenticationOptionsInterface $options
*/
public function setOptions(AuthenticationOptionsInterface $options)
{
$this->options = $options;
}
/**
* @return AuthenticationOptionsInterface
*/
public function getOptions()
{
if (!$this->options instanceof AuthenticationOptionsInterface) {
$this->setOptions($this->getServiceManager()->get('zfcuser_module_options'));
}
return $this->options;
}
}