Proyectos de Subversion LeadersLinked - Services

Rev

Rev 213 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 
283 www 88
 
1 efrain 89
        if(!$device) {
90
            return new Result(Result::FAILURE_IDENTITY_NOT_FOUND, null, ['ERROR_DEVICE_NOT_FOUND']);
91
        }
92
 
93
 
94
        if(!$device->user_id) {
95
            return new Result(Result::FAILURE_IDENTITY_NOT_FOUND, null, ['ERROR_DEVICE_SESSION_NOT_FOUND']);
96
        }
97
 
98
 
99
        $userMapper = UserMapper::getInstance($this->adapter);
100
        $user = $userMapper->fetchOne($device->user_id);
192 efrain 101
 
213 efrain 102
        if(User::STATUS_BANNED == $user->status) {
192 efrain 103
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_IS_BANNED']);
104
        }
1 efrain 105
 
106
        if(User::BLOCKED_YES == $user->blocked) {
107
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_IS_BLOCKED']);
108
        }
109
 
110
        if(User::STATUS_INACTIVE == $user->status) {
111
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_IS_INACTIVE']);
112
        }
113
 
114
 
115
        if(User::REQUEST_ACCESS_PENDING == $user->request_access) {
116
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_REQUEST_ACCESS_IS_PENDING']);
117
        }
118
 
119
 
120
        if(User::REQUEST_ACCESS_REJECTED == $user->request_access) {
121
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_REQUEST_ACCESS_IS_REJECTED']);
122
        }
123
 
124
        $dt = \DateTime::createFromFormat('Y-m-d\TH:i:s', $this->timestamp);
125
        if($dt) {
126
            $t = $dt->getTimestamp();
127
        } else {
128
            $t = 0;
129
        }
130
 
131
        $t = $this->timestamp;
132
 
133
        $dt = \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s',gmdate('Y-m-d\TH:i:s'));
134
        $t1 = $dt->sub(new \DateInterval('PT5M'));
135
        $t1 = $t1->getTimestamp();
136
 
137
        $t2 = $dt->add(new \DateInterval('PT5M'));
138
        $t2 = $t2->getTimestamp();
139
 
140
        /*
141
        if($t >= $t1 && $t <= $t2) {
142
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_WEBSERVICE_TIMESTAMP']);
143
        }*/
144
 
145
 
146
 
147
 
148
 
149
 
150
        $passworVerification = md5($device->password . ':' . $this->timestamp . ':'  . $this->rand);
151
 
152
 
153
        if($this->password != $passworVerification)
154
        {
155
 
156
            error_log("token : {$device->id}  timestamp : {$this->timestamp} rand : {$this->rand} password : {$this->password} ERR password verificacion : {$passworVerification}"  );
157
 
158
 
159
 
160
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_WEBSERVICE_PASSWORD']);
161
        } else {
162
 
163
            error_log("token : {$device->id} timestamp : {$this->timestamp} rand : {$this->rand} password : {$this->password} OK password verificacion : {$passworVerification}"  );
164
 
165
 
166
            $user->login_attempt = 0;
167
            $userMapper->update($user);
168
        }
169
 
170
        $ip = Functions::getUserIP();
171
 
172
        $deviceHistoryMapper = DeviceHistoryMapper::getInstance($this->adapter);
173
        $deviceHistory = $deviceHistoryMapper->fetchOneByDeviceIdAndUserIdAndIp($device->id, $user->id, $ip);
174
        if($deviceHistory) {
175
            $deviceHistoryMapper->update($deviceHistory);
176
        } else {
177
            $deviceHistory = new DeviceHistory();
178
            $deviceHistory->device_id = $device->id;
179
            $deviceHistory->user_id = $user->id;
180
            $deviceHistory->ip = $ip;
181
            $deviceHistoryMapper->insert($deviceHistory);
182
        }
183
 
184
        $data = [
185
            'user_id' => $user->id,
186
            'device_id' => $device->id,
187
        ];
188
 
189
        return new Result(Result::SUCCESS, $data, []);
190
    }
191
}