Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1323 | | Comparar con el anterior | Ultima modificación | Ver Log |

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