Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 238 | Ir a la última revisión | | 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
 
113
        $dt = \DateTime::createFromFormat('Y-m-d\TH:i:s', $this->timestamp);
114
        if($dt) {
115
            $t = $dt->getTimestamp();
116
        } else {
117
            $t = 0;
118
        }
119
 
120
        $t = $this->timestamp;
121
 
122
        $dt = \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s',gmdate('Y-m-d\TH:i:s'));
123
        $t1 = $dt->sub(new \DateInterval('PT5M'));
124
        $t1 = $t1->getTimestamp();
125
 
126
        $t2 = $dt->add(new \DateInterval('PT5M'));
127
        $t2 = $t2->getTimestamp();
128
 
129
        /*
130
        if($t >= $t1 && $t <= $t2) {
131
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_WEBSERVICE_TIMESTAMP']);
132
        }*/
133
 
237 efrain 134
 
1323 efrain 135
 
136
 
237 efrain 137
 
138
 
1 www 139
        $passworVerification = md5($device->password . ':' . $this->timestamp . ':'  . $this->rand);
140
 
141
 
142
        if($this->password != $passworVerification)
143
        {
237 efrain 144
 
1323 efrain 145
            error_log("token : {$device->id}  timestamp : {$this->timestamp} rand : {$this->rand} password : {$this->password} ERR password verificacion : {$passworVerification}"  );
1 www 146
 
147
 
1323 efrain 148
 
1 www 149
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_WEBSERVICE_PASSWORD']);
150
        } else {
238 efrain 151
 
1323 efrain 152
            error_log("token : {$device->id} timestamp : {$this->timestamp} rand : {$this->rand} password : {$this->password} OK password verificacion : {$passworVerification}"  );
238 efrain 153
 
1323 efrain 154
 
1 www 155
            $user->login_attempt = 0;
156
            $userMapper->update($user);
157
        }
158
 
159
        $ip = Functions::getUserIP();
160
 
161
        $deviceHistoryMapper = DeviceHistoryMapper::getInstance($this->adapter);
162
        $deviceHistory = $deviceHistoryMapper->fetchOneByDeviceIdAndUserIdAndIp($device->id, $user->id, $ip);
163
        if($deviceHistory) {
164
            $deviceHistoryMapper->update($deviceHistory);
165
        } else {
166
            $deviceHistory = new DeviceHistory();
167
            $deviceHistory->device_id = $device->id;
168
            $deviceHistory->user_id = $user->id;
169
            $deviceHistory->ip = $ip;
170
            $deviceHistoryMapper->insert($deviceHistory);
171
        }
172
 
173
        $data = [
174
            'user_id' => $user->id,
175
            'device_id' => $device->id,
176
        ];
177
 
178
        return new Result(Result::SUCCESS, $data, []);
179
    }
180
}