diff --git a/application/Espo/Core/Utils/ApiKey.php b/application/Espo/Core/Utils/ApiKey.php index 6ef41cc7cb..80c500a05a 100644 --- a/application/Espo/Core/Utils/ApiKey.php +++ b/application/Espo/Core/Utils/ApiKey.php @@ -43,9 +43,9 @@ class ApiKey return $this->config; } - public static function hash($secretKey) + public static function hash($secretKey, $string = '') { - return hash_hmac('sha256', '', $secretKey, true); + return hash_hmac('sha256', $string, $secretKey, true); } public function getSecretKeyForUserId($id) diff --git a/application/Espo/Core/Utils/Auth.php b/application/Espo/Core/Utils/Auth.php index 5a42f76972..8fc4a96f64 100644 --- a/application/Espo/Core/Utils/Auth.php +++ b/application/Espo/Core/Utils/Auth.php @@ -193,7 +193,11 @@ class Auth $authentication = $this->getAuthentication($authenticationMethod); - $user = $authentication->login($username, $password, $authToken, $this->isPortal()); + $params = [ + 'isPortal' => $this->isPortal() + ]; + + $user = $authentication->login($username, $password, $authToken, $params, $this->request); $authLogRecord = null; diff --git a/application/Espo/Core/Utils/Authentication/Espo.php b/application/Espo/Core/Utils/Authentication/Espo.php index 0770c8753f..5161dd05a6 100644 --- a/application/Espo/Core/Utils/Authentication/Espo.php +++ b/application/Espo/Core/Utils/Authentication/Espo.php @@ -33,7 +33,7 @@ use \Espo\Core\Exceptions\Error; class Espo extends Base { - public function login($username, $password, \Espo\Entities\AuthToken $authToken = null, $isPortal = null) + public function login($username, $password, \Espo\Entities\AuthToken $authToken = null, $params = [], $request) { if (!$password) return; @@ -43,12 +43,12 @@ class Espo extends Base $hash = $this->getPasswordHash()->hash($password); } - $user = $this->getEntityManager()->getRepository('User')->findOne(array( - 'whereClause' => array( + $user = $this->getEntityManager()->getRepository('User')->findOne([ + 'whereClause' => [ 'userName' => $username, 'password' => $hash - ) - )); + ] + ]); return $user; } diff --git a/application/Espo/Core/Utils/Authentication/Hmac.php b/application/Espo/Core/Utils/Authentication/Hmac.php index 7d08fb30e8..821949d25b 100644 --- a/application/Espo/Core/Utils/Authentication/Hmac.php +++ b/application/Espo/Core/Utils/Authentication/Hmac.php @@ -33,7 +33,7 @@ use \Espo\Core\Exceptions\Error; class Hmac extends Base { - public function login($username, $password, $authToken = null, $isPortal = null) + public function login($username, $password, $authToken = null, $params = [], $request) { $apiKey = $username; $hash = $password; @@ -52,7 +52,9 @@ class Hmac extends Base $secretKey = $apiKeyUtil->getSecretKeyForUserId($user->id); if (!$secretKey) return; - if ($hash === \Espo\Core\Utils\ApiKey::hash($secretKey)) { + $string = $request->getMethod() . ' ' . $request->getResourceUri(); + + if ($hash === \Espo\Core\Utils\ApiKey::hash($secretKey, $string)) { return $user; } diff --git a/application/Espo/Core/Utils/Authentication/LDAP.php b/application/Espo/Core/Utils/Authentication/LDAP.php index bf11c3195a..30e7751a95 100644 --- a/application/Espo/Core/Utils/Authentication/LDAP.php +++ b/application/Espo/Core/Utils/Authentication/LDAP.php @@ -110,10 +110,12 @@ class LDAP extends Espo * * @return \Espo\Entities\User | null */ - public function login($username, $password, \Espo\Entities\AuthToken $authToken = null, $isPortal = null) + public function login($username, $password, \Espo\Entities\AuthToken $authToken = null, $params = [], $request) { if (!$password) return; + $isPortal = !empty($params['isPortal']); + if ($authToken) { return $this->loginByToken($username, $authToken); }