Proyectos de Subversion LeadersLinked - Services

Rev

Rev 283 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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