Proyectos de Subversion LeadersLinked - Services

Rev

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

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