1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace LeadersLinked\Authentication;
|
|
|
6 |
|
|
|
7 |
use Laminas\Authentication\Adapter\AdapterInterface as AuthAdapterInterface;
|
|
|
8 |
use Laminas\Authentication\Result;
|
|
|
9 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
10 |
use Laminas\Log\LoggerInterface;
|
|
|
11 |
use LeadersLinked\Model\User;
|
|
|
12 |
use LeadersLinked\Mapper\UserMapper;
|
|
|
13 |
use LeadersLinked\Library\Functions;
|
|
|
14 |
use LeadersLinked\Mapper\DeviceMapper;
|
|
|
15 |
use LeadersLinked\Mapper\DeviceHistoryMapper;
|
|
|
16 |
use LeadersLinked\Model\DeviceHistory;
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
class AuthTokenAdapter implements AuthAdapterInterface
|
|
|
20 |
{
|
|
|
21 |
/**
|
|
|
22 |
*
|
|
|
23 |
* @var AdapterInterface
|
|
|
24 |
*/
|
|
|
25 |
private $adapter;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
*
|
|
|
29 |
* @var string
|
|
|
30 |
*/
|
|
|
31 |
private $device_id;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
*
|
|
|
35 |
* @var string
|
|
|
36 |
*/
|
|
|
37 |
private $password;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
*
|
|
|
41 |
* @var string
|
|
|
42 |
*/
|
|
|
43 |
private $timestamp;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
*
|
|
|
47 |
* @var int
|
|
|
48 |
*/
|
|
|
49 |
private $rand;
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
*
|
|
|
56 |
* @param AdapterInterface $adapter
|
|
|
57 |
*/
|
|
|
58 |
public function __construct(AdapterInterface $adapter)
|
|
|
59 |
{
|
|
|
60 |
$this->adapter = $adapter;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
*
|
|
|
65 |
* @param string $device_id
|
|
|
66 |
* @param string $token
|
|
|
67 |
* @param string $timestamp
|
|
|
68 |
* @param int $rand
|
|
|
69 |
*/
|
|
|
70 |
public function setData($device_id, $password, $timestamp, $rand)
|
|
|
71 |
{
|
|
|
72 |
$this->device_id = $device_id;
|
|
|
73 |
$this->password = $password;
|
|
|
74 |
$this->timestamp = $timestamp;
|
|
|
75 |
$this->rand = $rand;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
*
|
|
|
80 |
* {@inheritDoc}
|
|
|
81 |
* @see \Laminas\Authentication\Adapter\AdapterInterface::authenticate()
|
|
|
82 |
*/
|
|
|
83 |
public function authenticate()
|
|
|
84 |
{
|
|
|
85 |
$deviceMapper = DeviceMapper::getInstance($this->adapter);
|
|
|
86 |
$device = $deviceMapper->fetchOne($this->device_id);
|
|
|
87 |
|
|
|
88 |
/*
|
|
|
89 |
echo 'device' . PHP_EOL;
|
|
|
90 |
print_r($device); exit;
|
|
|
91 |
*/
|
|
|
92 |
if(!$device) {
|
|
|
93 |
return new Result(Result::FAILURE_IDENTITY_NOT_FOUND, null, ['ERROR_DEVICE_NOT_FOUND']);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
|
|
|
97 |
if(!$device->user_id) {
|
|
|
98 |
return new Result(Result::FAILURE_IDENTITY_NOT_FOUND, null, ['ERROR_DEVICE_SESSION_NOT_FOUND']);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
|
|
|
102 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
103 |
$user = $userMapper->fetchOne($device->user_id);
|
192 |
efrain |
104 |
|
|
|
105 |
if(User::IS_BANNED_YES == $user->is_banned) {
|
|
|
106 |
return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_IS_BANNED']);
|
|
|
107 |
}
|
1 |
efrain |
108 |
|
|
|
109 |
if(User::BLOCKED_YES == $user->blocked) {
|
|
|
110 |
return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_IS_BLOCKED']);
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
if(User::STATUS_INACTIVE == $user->status) {
|
|
|
114 |
return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_IS_INACTIVE']);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
if(User::REQUEST_ACCESS_PENDING == $user->request_access) {
|
|
|
119 |
return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_REQUEST_ACCESS_IS_PENDING']);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
|
|
|
123 |
if(User::REQUEST_ACCESS_REJECTED == $user->request_access) {
|
|
|
124 |
return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_REQUEST_ACCESS_IS_REJECTED']);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
$dt = \DateTime::createFromFormat('Y-m-d\TH:i:s', $this->timestamp);
|
|
|
128 |
if($dt) {
|
|
|
129 |
$t = $dt->getTimestamp();
|
|
|
130 |
} else {
|
|
|
131 |
$t = 0;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
$t = $this->timestamp;
|
|
|
135 |
|
|
|
136 |
$dt = \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s',gmdate('Y-m-d\TH:i:s'));
|
|
|
137 |
$t1 = $dt->sub(new \DateInterval('PT5M'));
|
|
|
138 |
$t1 = $t1->getTimestamp();
|
|
|
139 |
|
|
|
140 |
$t2 = $dt->add(new \DateInterval('PT5M'));
|
|
|
141 |
$t2 = $t2->getTimestamp();
|
|
|
142 |
|
|
|
143 |
/*
|
|
|
144 |
if($t >= $t1 && $t <= $t2) {
|
|
|
145 |
return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_WEBSERVICE_TIMESTAMP']);
|
|
|
146 |
}*/
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
|
152 |
|
|
|
153 |
$passworVerification = md5($device->password . ':' . $this->timestamp . ':' . $this->rand);
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
if($this->password != $passworVerification)
|
|
|
157 |
{
|
|
|
158 |
|
|
|
159 |
error_log("token : {$device->id} timestamp : {$this->timestamp} rand : {$this->rand} password : {$this->password} ERR password verificacion : {$passworVerification}" );
|
|
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
|
163 |
return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_WEBSERVICE_PASSWORD']);
|
|
|
164 |
} else {
|
|
|
165 |
|
|
|
166 |
error_log("token : {$device->id} timestamp : {$this->timestamp} rand : {$this->rand} password : {$this->password} OK password verificacion : {$passworVerification}" );
|
|
|
167 |
|
|
|
168 |
|
|
|
169 |
$user->login_attempt = 0;
|
|
|
170 |
$userMapper->update($user);
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
$ip = Functions::getUserIP();
|
|
|
174 |
|
|
|
175 |
$deviceHistoryMapper = DeviceHistoryMapper::getInstance($this->adapter);
|
|
|
176 |
$deviceHistory = $deviceHistoryMapper->fetchOneByDeviceIdAndUserIdAndIp($device->id, $user->id, $ip);
|
|
|
177 |
if($deviceHistory) {
|
|
|
178 |
$deviceHistoryMapper->update($deviceHistory);
|
|
|
179 |
} else {
|
|
|
180 |
$deviceHistory = new DeviceHistory();
|
|
|
181 |
$deviceHistory->device_id = $device->id;
|
|
|
182 |
$deviceHistory->user_id = $user->id;
|
|
|
183 |
$deviceHistory->ip = $ip;
|
|
|
184 |
$deviceHistoryMapper->insert($deviceHistory);
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
$data = [
|
|
|
188 |
'user_id' => $user->id,
|
|
|
189 |
'device_id' => $device->id,
|
|
|
190 |
];
|
|
|
191 |
|
|
|
192 |
return new Result(Result::SUCCESS, $data, []);
|
|
|
193 |
}
|
|
|
194 |
}
|