Skip to content

Commit 5de63df

Browse files
Rizwan KhanRizwan Khan
authored andcommitted
AC-9797: 2FA functionality enhancement
1 parent e644557 commit 5de63df

File tree

3 files changed

+44
-36
lines changed

3 files changed

+44
-36
lines changed

TwoFactorAuth/Controller/Adminhtml/Authy/Authpost.php

100644100755
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,18 @@ public function execute()
138138
$result = $this->jsonFactory->create();
139139

140140
try {
141-
$maxRetries = $this->scopeConfig->getValue(self::XML_PATH_2FA_RETRY_ATTEMPTS);
142-
$retries = $this->verifyRetryAttempts();
143-
if ($retries > $maxRetries) { //locked the user
141+
if (!$this->allowApiRetries()) { //locked the user
144142
$lockThreshold = $this->scopeConfig->getValue(self::XML_PATH_2FA_LOCK_EXPIRE);
145143
if ($this->userResource->lock($user->getId(), 0, $lockThreshold)) {
146144
$result->setData(['success' => false, 'message' => "User is disabled temporarily!"]);
145+
return $result;
147146
}
148-
} else {
149-
$this->authy->verify($user, $this->dataObjectFactory->create([
150-
'data' => $this->getRequest()->getParams(),
151-
]));
152-
$this->tfaSession->grantAccess();
153-
$result->setData(['success' => true]);
154147
}
148+
$this->authy->verify($user, $this->dataObjectFactory->create([
149+
'data' => $this->getRequest()->getParams(),
150+
]));
151+
$this->tfaSession->grantAccess();
152+
$result->setData(['success' => true]);
155153
} catch (Exception $e) {
156154
$this->alert->event(
157155
'Magento_TwoFactorAuth',
@@ -181,15 +179,20 @@ protected function _isAllowed()
181179
}
182180

183181
/**
184-
* Get retry attempt count
182+
* Check if retry attempt above threshold value
185183
*
186-
* @return int
184+
* @return bool
187185
*/
188-
private function verifyRetryAttempts() : int
186+
private function allowApiRetries() : bool
189187
{
188+
$maxRetries = $this->scopeConfig->getValue(self::XML_PATH_2FA_RETRY_ATTEMPTS);
190189
$verifyAttempts = $this->session->getOtpAttempt();
191190
$verifyAttempts = $verifyAttempts === null ? 1 : $verifyAttempts+1;
192191
$this->session->setOtpAttempt($verifyAttempts);
193-
return $verifyAttempts;
192+
if ($verifyAttempts > $maxRetries) {
193+
return false;
194+
}
195+
196+
return true;
194197
}
195198
}

TwoFactorAuth/Controller/Adminhtml/Google/Authpost.php

100644100755
Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\Controller\Result\JsonFactory;
1414
use Magento\Framework\DataObjectFactory;
1515
use Magento\Framework\Exception\NoSuchEntityException;
16+
use Magento\Tests\NamingConvention\true\bool;
1617
use Magento\TwoFactorAuth\Model\AlertInterface;
1718
use Magento\TwoFactorAuth\Api\TfaInterface;
1819
use Magento\TwoFactorAuth\Api\TfaSessionInterface;
@@ -132,27 +133,26 @@ public function execute()
132133
/** @var \Magento\Framework\DataObject $request */
133134
$request = $this->dataObjectFactory->create(['data' => $this->getRequest()->getParams()]);
134135

135-
$maxRetries = $this->scopeConfig->getValue(self::XML_PATH_2FA_RETRY_ATTEMPTS);
136-
$retries = $this->verifyRetryAttempts();
137-
if ($retries > $maxRetries) { //locked the user
136+
if (!$this->allowApiRetries()) { //locked the user
138137
$lockThreshold = $this->scopeConfig->getValue(self::XML_PATH_2FA_LOCK_EXPIRE);
139138
if ($this->userResource->lock($user->getId(), 0, $lockThreshold)) {
140139
$response->setData(['success' => false, 'message' => "User is disabled temporarily!"]);
140+
return $response;
141141
}
142+
}
143+
144+
if ($this->google->verify($user, $request)) {
145+
$this->tfaSession->grantAccess();
146+
$response->setData(['success' => true]);
142147
} else {
143-
if ($this->google->verify($user, $request)) {
144-
$this->tfaSession->grantAccess();
145-
$response->setData(['success' => true]);
146-
} else {
147-
$this->alert->event(
148-
'Magento_TwoFactorAuth',
149-
'Google auth invalid token',
150-
AlertInterface::LEVEL_WARNING,
151-
$user->getUserName()
152-
);
153-
154-
$response->setData(['success' => false, 'message' => 'Invalid code']);
155-
}
148+
$this->alert->event(
149+
'Magento_TwoFactorAuth',
150+
'Google auth invalid token',
151+
AlertInterface::LEVEL_WARNING,
152+
$user->getUserName()
153+
);
154+
155+
$response->setData(['success' => false, 'message' => 'Invalid code']);
156156
}
157157

158158
return $response;
@@ -173,15 +173,20 @@ protected function _isAllowed()
173173
}
174174

175175
/**
176-
* Get retry attempt count
176+
* Check if retry attempt above threshold value
177177
*
178-
* @return int
178+
* @return bool
179179
*/
180-
private function verifyRetryAttempts() : int
180+
private function allowApiRetries() : bool
181181
{
182+
$maxRetries = $this->scopeConfig->getValue(self::XML_PATH_2FA_RETRY_ATTEMPTS);
182183
$verifyAttempts = $this->session->getOtpAttempt();
183184
$verifyAttempts = $verifyAttempts === null ? 1 : $verifyAttempts+1;
184185
$this->session->setOtpAttempt($verifyAttempts);
185-
return $verifyAttempts;
186+
if ($verifyAttempts > $maxRetries) {
187+
return false;
188+
}
189+
190+
return true;
186191
}
187192
}

TwoFactorAuth/etc/adminhtml/system.xml

100644100755
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@
3838
<field canRestore="1" id="twofactorauth_retry" translate="label" type="text" sortOrder="40"
3939
showInDefault="1" showInWebsite="0" showInStore="0">
4040
<label>Configuration for 2FA retry attempts</label>
41-
<comment>Security configurations for TowFatcorAuth page.</comment>
41+
<comment>Security configurations for TwoFactorAuth page.</comment>
4242
</field>
4343
<field canRestore="1" id="auth_lock_expire" translate="label" type="text" sortOrder="40"
4444
showInDefault="1" showInWebsite="0" showInStore="0">
45-
<label>Configuration for 2FA lock expire time</label>
46-
<comment>TwoFactor Authentation Configuration.</comment>
45+
<label>Configuration for TwoFactorAuth lock expire time</label>
46+
<comment>TwoFactor Authentication Configuration.</comment>
4747
</field>
4848
</group>
4949
<group id="google" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="0"

0 commit comments

Comments
 (0)