From ac8debbb4bddff971e3a93525dee072a07e553d2 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sat, 12 Sep 2020 09:40:56 +0300 Subject: [PATCH] cs fixes and docs --- .../TwoFactor/Methods/CodeVerify.php | 6 +++++ .../Authentication/TwoFactor/Methods/Totp.php | 22 ++++++++++++++----- .../TwoFactor/User/CodeVerify.php | 6 +++++ .../Authentication/TwoFactor/User/Totp.php | 4 +++- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/application/Espo/Core/Authentication/TwoFactor/Methods/CodeVerify.php b/application/Espo/Core/Authentication/TwoFactor/Methods/CodeVerify.php index abece44938..c95e49f9eb 100644 --- a/application/Espo/Core/Authentication/TwoFactor/Methods/CodeVerify.php +++ b/application/Espo/Core/Authentication/TwoFactor/Methods/CodeVerify.php @@ -35,7 +35,13 @@ use StdClass; interface CodeVerify { + /** + * Verify a code for a user. + */ public function verifyCode(User $user, string $code) : bool; + /** + * Data to be sent to frontend for showing a form for a second step. + */ public function getLoginData(User $user) : StdClass; } diff --git a/application/Espo/Core/Authentication/TwoFactor/Methods/Totp.php b/application/Espo/Core/Authentication/TwoFactor/Methods/Totp.php index 25f2b679c2..1f277a127d 100644 --- a/application/Espo/Core/Authentication/TwoFactor/Methods/Totp.php +++ b/application/Espo/Core/Authentication/TwoFactor/Methods/Totp.php @@ -49,15 +49,27 @@ class Totp implements CodeVerify public function verifyCode(User $user, string $code) : bool { - $userData = $this->entityManager->getRepository('UserData')->getByUserId($user->id); + $userData = $this->entityManager + ->getRepository('UserData') + ->getByUserId($user->id); - if (!$userData) return false; - if (!$userData->get('auth2FA')) return false; - if ($userData->get('auth2FAMethod') != 'Totp') return false; + if (!$userData) { + return false; + } + + if (!$userData->get('auth2FA')) { + return false; + } + + if ($userData->get('auth2FAMethod') != 'Totp') { + return false; + } $secret = $userData->get('auth2FATotpSecret'); - if (!$secret) return false; + if (!$secret) { + return false; + } return $this->totp->verifyCode($secret, $code); } diff --git a/application/Espo/Core/Authentication/TwoFactor/User/CodeVerify.php b/application/Espo/Core/Authentication/TwoFactor/User/CodeVerify.php index e0aead22de..246c62b820 100644 --- a/application/Espo/Core/Authentication/TwoFactor/User/CodeVerify.php +++ b/application/Espo/Core/Authentication/TwoFactor/User/CodeVerify.php @@ -35,7 +35,13 @@ use StdClass; interface CodeVerify { + /** + * Generate data for a user. + */ public function generateData(UserData $userData, StdClass $data, string $userName) : StdClass; + /** + * Confirm code before storing. + */ public function verify(UserData $userData, string $code) : bool; } diff --git a/application/Espo/Core/Authentication/TwoFactor/User/Totp.php b/application/Espo/Core/Authentication/TwoFactor/User/Totp.php index c86be3b3e4..58d574f336 100644 --- a/application/Espo/Core/Authentication/TwoFactor/User/Totp.php +++ b/application/Espo/Core/Authentication/TwoFactor/User/Totp.php @@ -60,7 +60,9 @@ class Totp implements CodeVerify public function verify(UserData $userData, string $code) : bool { - if (!$code) return false; + if (!$code) { + return false; + } $code = str_replace(' ', '', trim($code));