Rev 1323 | AutorÃa | Comparar con el anterior | Ultima modificación | Ver Log |
<?phpdeclare(strict_types=1);namespace LeadersLinked\Authentication;use Laminas\Authentication\Adapter\AdapterInterface as AuthAdapterInterface;use Laminas\Authentication\Result;use Laminas\Db\Adapter\AdapterInterface;use Laminas\Log\LoggerInterface;use LeadersLinked\Model\User;use LeadersLinked\Mapper\UserMapper;use LeadersLinked\Library\Functions;use LeadersLinked\Mapper\DeviceMapper;use LeadersLinked\Mapper\DeviceHistoryMapper;use LeadersLinked\Model\DeviceHistory;class AuthTokenAdapter implements AuthAdapterInterface{/**** @var AdapterInterface*/private $adapter;/**** @var string*/private $device_id;/**** @var string*/private $password;/**** @var string*/private $timestamp;/**** @var int*/private $rand;/**** @param AdapterInterface $adapter*/public function __construct(AdapterInterface $adapter){$this->adapter = $adapter;}/**** @param string $device_id* @param string $token* @param string $timestamp* @param int $rand*/public function setData($device_id, $password, $timestamp, $rand){$this->device_id = $device_id;$this->password = $password;$this->timestamp = $timestamp;$this->rand = $rand;}/**** {@inheritDoc}* @see \Laminas\Authentication\Adapter\AdapterInterface::authenticate()*/public function authenticate(){$deviceMapper = DeviceMapper::getInstance($this->adapter);$device = $deviceMapper->fetchOne($this->device_id);/*echo 'device' . PHP_EOL;print_r($device); exit;*/if(!$device) {return new Result(Result::FAILURE_IDENTITY_NOT_FOUND, null, ['ERROR_DEVICE_NOT_FOUND']);}if(!$device->user_id) {return new Result(Result::FAILURE_IDENTITY_NOT_FOUND, null, ['ERROR_DEVICE_SESSION_NOT_FOUND']);}$userMapper = UserMapper::getInstance($this->adapter);$user = $userMapper->fetchOne($device->user_id);if(User::BLOCKED_YES == $user->blocked) {return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_IS_BLOCKED']);}if(User::STATUS_INACTIVE == $user->status) {return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_IS_INACTIVE']);}if(User::REQUEST_ACCESS_PENDING == $user->request_access) {return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_REQUEST_ACCESS_IS_PENDING']);}if(User::REQUEST_ACCESS_REJECTED == $user->request_access) {return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_REQUEST_ACCESS_IS_REJECTED']);}$dt = \DateTime::createFromFormat('Y-m-d\TH:i:s', $this->timestamp);if($dt) {$t = $dt->getTimestamp();} else {$t = 0;}$t = $this->timestamp;$dt = \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s',gmdate('Y-m-d\TH:i:s'));$t1 = $dt->sub(new \DateInterval('PT5M'));$t1 = $t1->getTimestamp();$t2 = $dt->add(new \DateInterval('PT5M'));$t2 = $t2->getTimestamp();/*if($t >= $t1 && $t <= $t2) {return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_WEBSERVICE_TIMESTAMP']);}*/$passworVerification = md5($device->password . ':' . $this->timestamp . ':' . $this->rand);if($this->password != $passworVerification){error_log("token : {$device->id} timestamp : {$this->timestamp} rand : {$this->rand} password : {$this->password} ERR password verificacion : {$passworVerification}" );return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_WEBSERVICE_PASSWORD']);} else {error_log("token : {$device->id} timestamp : {$this->timestamp} rand : {$this->rand} password : {$this->password} OK password verificacion : {$passworVerification}" );$user->login_attempt = 0;$userMapper->update($user);}$ip = Functions::getUserIP();$deviceHistoryMapper = DeviceHistoryMapper::getInstance($this->adapter);$deviceHistory = $deviceHistoryMapper->fetchOneByDeviceIdAndUserIdAndIp($device->id, $user->id, $ip);if($deviceHistory) {$deviceHistoryMapper->update($deviceHistory);} else {$deviceHistory = new DeviceHistory();$deviceHistory->device_id = $device->id;$deviceHistory->user_id = $user->id;$deviceHistory->ip = $ip;$deviceHistoryMapper->insert($deviceHistory);}$data = ['user_id' => $user->id,'device_id' => $device->id,];return new Result(Result::SUCCESS, $data, []);}}