auth refactoring
This commit is contained in:
@@ -58,16 +58,17 @@ class Result
|
||||
|
||||
protected $failReason = null;
|
||||
|
||||
protected function __construct(string $status, ?User $user = null, ?StdClass $params = null)
|
||||
protected function __construct(string $status, ?User $user = null, ?ResultData $data = null)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->status = $status;
|
||||
if ($params) {
|
||||
$this->message = $params->message ?? null;
|
||||
$this->token = $params->token ?? null;
|
||||
$this->view = $params->view ?? null;
|
||||
$this->loggedUser = $params->loggedUser ?? null;
|
||||
$this->failReason = $params->failReason ?? null;
|
||||
|
||||
if ($data) {
|
||||
$this->message = $data->getMessage();
|
||||
$this->token = $data->getToken();
|
||||
$this->view = $data->getView();
|
||||
$this->loggedUser = $data->getLoggedUser();
|
||||
$this->failReason = $data->getFailReason();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,17 +85,19 @@ class Result
|
||||
*/
|
||||
public static function fail(?string $reason = null)
|
||||
{
|
||||
return new Result(self::STATUS_FAIL, null, (object) [
|
||||
'failReason' => $reason,
|
||||
]);
|
||||
$data = $reason ?
|
||||
ResultData::fromFailReason($reason) :
|
||||
ResultData::fromNothing();
|
||||
|
||||
return new Result(self::STATUS_FAIL, null, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance for a login requiring a second step. E.g. for 2FA.
|
||||
*/
|
||||
public static function secondStepRequired(User $user, StdClass $params)
|
||||
public static function secondStepRequired(User $user, ResultData $data)
|
||||
{
|
||||
return new Result(self::STATUS_SECOND_STEP_REQUIRED, $user, $params);
|
||||
return new Result(self::STATUS_SECOND_STEP_REQUIRED, $user, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,7 +133,8 @@ class Result
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a logged user. Considered that an admin user can log in as another user. The logged user will be an admin user.
|
||||
* Get a logged user. Considered that an admin user can log in as another user.
|
||||
* The logged user will be an admin user.
|
||||
*/
|
||||
public function getLoggedUser() : ?User
|
||||
{
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Authentication;
|
||||
|
||||
use Espo\Entities\User;
|
||||
|
||||
class ResultData
|
||||
{
|
||||
private $message = null;
|
||||
|
||||
private $token = null;
|
||||
|
||||
private $view = null;
|
||||
|
||||
private $loggedUser = null;
|
||||
|
||||
private $failReason = null;
|
||||
|
||||
private function __construct(
|
||||
?string $message = null,
|
||||
?string $failReason = null,
|
||||
?string $token = null,
|
||||
?string $view = null,
|
||||
?User $loggedUser = null
|
||||
) {
|
||||
$this->message = $message;
|
||||
$this->failReason = $failReason;
|
||||
$this->token = $token;
|
||||
$this->view = $view;
|
||||
$this->loggedUser = $loggedUser;
|
||||
}
|
||||
|
||||
public static function fromNothing() : self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
public static function fromFailReason(string $failReason) : self
|
||||
{
|
||||
return new self(null, $failReason);
|
||||
}
|
||||
|
||||
public static function fromMessage(string $message) : self
|
||||
{
|
||||
return new self($message);
|
||||
}
|
||||
|
||||
public static function fromArray(array $data) : self
|
||||
{
|
||||
return new self(
|
||||
$data['message'] ?? null,
|
||||
$data['failReason'] ?? null,
|
||||
$data['token'] ?? null,
|
||||
$data['view'] ?? null,
|
||||
$data['loggedUser'] ?? null
|
||||
);
|
||||
}
|
||||
|
||||
public function getLoggedUser() : ?User
|
||||
{
|
||||
return $this->loggedUser;
|
||||
}
|
||||
|
||||
public function getStatus() : string
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
public function getView() : ?string
|
||||
{
|
||||
return $this->view;
|
||||
}
|
||||
|
||||
public function getMessage() : ?string
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
public function getToken() : ?string
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
public function getFailReason() : ?string
|
||||
{
|
||||
return $this->failReason;
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,9 @@ namespace Espo\Core\Authentication\TwoFactor\Methods;
|
||||
|
||||
use Espo\Entities\User;
|
||||
|
||||
use StdClass;
|
||||
use Espo\Core\Authentication\{
|
||||
ResultData,
|
||||
};
|
||||
|
||||
interface CodeVerify
|
||||
{
|
||||
@@ -41,7 +43,7 @@ interface CodeVerify
|
||||
public function verifyCode(User $user, string $code) : bool;
|
||||
|
||||
/**
|
||||
* Data to be sent to frontend for showing a form for a second step.
|
||||
* Data to be sent to the front-end for showing a form for a second step.
|
||||
*/
|
||||
public function getLoginData(User $user) : StdClass;
|
||||
public function getLoginData(User $user) : ResultData;
|
||||
}
|
||||
|
||||
@@ -29,16 +29,20 @@
|
||||
|
||||
namespace Espo\Core\Authentication\TwoFactor\Methods;
|
||||
|
||||
use Espo\Entities\User;
|
||||
use Espo\{
|
||||
ORM\EntityManager,
|
||||
Entities\User,
|
||||
};
|
||||
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\Core\Authentication\TwoFactor\Utils\Totp as TotpUtils;
|
||||
|
||||
use StdClass;
|
||||
use Espo\Core\Authentication\{
|
||||
TwoFactor\Utils\Totp as TotpUtils,
|
||||
ResultData,
|
||||
};
|
||||
|
||||
class Totp implements CodeVerify
|
||||
{
|
||||
protected $entityManager;
|
||||
|
||||
protected $totp;
|
||||
|
||||
public function __construct(EntityManager $entityManager, TotpUtils $totp)
|
||||
@@ -61,7 +65,7 @@ class Totp implements CodeVerify
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($userData->get('auth2FAMethod') != 'Totp') {
|
||||
if ($userData->get('auth2FAMethod') !== 'Totp') {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -74,10 +78,8 @@ class Totp implements CodeVerify
|
||||
return $this->totp->verifyCode($secret, $code);
|
||||
}
|
||||
|
||||
public function getLoginData(User $user) : StdClass
|
||||
public function getLoginData(User $user) : ResultData
|
||||
{
|
||||
return (object) [
|
||||
'message' => 'enterTotpCode',
|
||||
];
|
||||
return ResultData::fromMessage('enterTotpCode');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,8 +30,11 @@
|
||||
namespace Espo\Core\Authentication\TwoFactor\User;
|
||||
|
||||
use Espo\Entities\UserData;
|
||||
use Espo\Core\Authentication\TwoFactor\Utils\Totp as TotpUtils;
|
||||
use Espo\Core\Utils\Config;
|
||||
|
||||
use Espo\Core\{
|
||||
Authentication\TwoFactor\Utils\Totp as TotpUtils,
|
||||
Utils\Config,
|
||||
};
|
||||
|
||||
use StdClass;
|
||||
|
||||
@@ -64,10 +67,10 @@ class Totp implements CodeVerify
|
||||
return false;
|
||||
}
|
||||
|
||||
$code = str_replace(' ', '', trim($code));
|
||||
$codeModified = str_replace(' ', '', trim($code));
|
||||
|
||||
$secret = $userData->get('auth2FATotpSecret');
|
||||
|
||||
return $this->totp->verifyCode($secret, $code);
|
||||
return $this->totp->verifyCode($secret, $codeModified);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user