Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 237 | Ir a la última revisión | | 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
 
97
        $userMapper = UserMapper::getInstance($this->adapter);
98
        $user = $userMapper->fetchOne($device->user_id);
99
 
100
        if(User::BLOCKED_YES == $user->blocked) {
101
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_IS_BLOCKED']);
102
        }
103
 
104
        if(User::STATUS_INACTIVE == $user->status) {
105
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_IS_INACTIVE']);
106
        }
107
 
108
        $dt = \DateTime::createFromFormat('Y-m-d\TH:i:s', $this->timestamp);
109
        if($dt) {
110
            $t = $dt->getTimestamp();
111
        } else {
112
            $t = 0;
113
        }
114
 
115
        $t = $this->timestamp;
116
 
117
        $dt = \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s',gmdate('Y-m-d\TH:i:s'));
118
        $t1 = $dt->sub(new \DateInterval('PT5M'));
119
        $t1 = $t1->getTimestamp();
120
 
121
        $t2 = $dt->add(new \DateInterval('PT5M'));
122
        $t2 = $t2->getTimestamp();
123
 
124
        /*
125
        if($t >= $t1 && $t <= $t2) {
126
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_WEBSERVICE_TIMESTAMP']);
127
        }*/
128
 
129
        $passworVerification = md5($device->password . ':' . $this->timestamp . ':'  . $this->rand);
130
 
131
 
132
        if($this->password != $passworVerification)
133
        {
134
           //error_log('verification = ' . $passworVerification);
135
 
136
 
137
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_WEBSERVICE_PASSWORD']);
138
        } else {
139
            $user->login_attempt = 0;
140
            $userMapper->update($user);
141
        }
142
 
143
        $ip = Functions::getUserIP();
144
 
145
        $deviceHistoryMapper = DeviceHistoryMapper::getInstance($this->adapter);
146
        $deviceHistory = $deviceHistoryMapper->fetchOneByDeviceIdAndUserIdAndIp($device->id, $user->id, $ip);
147
        if($deviceHistory) {
148
            $deviceHistoryMapper->update($deviceHistory);
149
        } else {
150
            $deviceHistory = new DeviceHistory();
151
            $deviceHistory->device_id = $device->id;
152
            $deviceHistory->user_id = $user->id;
153
            $deviceHistory->ip = $ip;
154
            $deviceHistoryMapper->insert($deviceHistory);
155
        }
156
 
157
        $data = [
158
            'user_id' => $user->id,
159
            'device_id' => $device->id,
160
        ];
161
 
162
        return new Result(Result::SUCCESS, $data, []);
163
    }
164
}