Skip to content

Commit c6c1267

Browse files
committed
Merge remote-tracking branch 'gl_magento2ce/AC-11762' into AC-11762-8MAY
2 parents 3164218 + 170cd75 commit c6c1267

File tree

7 files changed

+33
-20
lines changed

7 files changed

+33
-20
lines changed

TwoFactorAuth/Model/Config/Backend/OtpWindow.php renamed to TwoFactorAuth/Model/Config/Backend/Leeway.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
use Magento\Framework\Exception\ValidatorException;
1313
use OTPHP\TOTPInterface;
1414

15-
class OtpWindow extends Value implements ProcessorInterface
15+
class Leeway extends Value implements ProcessorInterface
1616
{
1717
/**
18+
* Fetch Totp default period value
19+
*
1820
* @return int
1921
*/
2022
private function getDefaultPeriod(): int
@@ -32,7 +34,7 @@ private function getDefaultPeriod(): int
3234
public function processValue($value)
3335
{
3436
if (!is_numeric($value)) {
35-
throw new ValidatorException(__('The OTP window must be a numeric value.'));
37+
throw new ValidatorException(__('The Leeway must be a numeric value.'));
3638
}
3739
$numericValue = (int) $value;
3840
return $numericValue;
@@ -48,7 +50,13 @@ public function beforeSave()
4850
$value = $this->getValue();
4951
$period = $this->getDefaultPeriod();
5052
if (!is_numeric($value) || $value < 1 || $value >= $period) {
51-
throw new ValidatorException(__('Invalid OTP window value. It must be less than the OTP period value '.$period));
53+
throw new ValidatorException(
54+
__(
55+
'Invalid Leeway value. It must be between 1 and %1 as default period is %2',
56+
$period-1,
57+
$period
58+
)
59+
);
5260
}
5361

5462
return parent::beforeSave();

TwoFactorAuth/Model/Provider/Engine/Google.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Google implements EngineInterface
3535
/**
3636
* Config path for the OTP window
3737
*/
38-
const XML_PATH_OTP_WINDOW = 'twofactorauth/google/otp_window';
38+
public const XML_PATH_LEEWAY = 'twofactorauth/google/leeway';
3939

4040
/**
4141
* Engine code
@@ -199,7 +199,7 @@ public function verify(UserInterface $user, DataObject $request): bool
199199
return $totp->verify(
200200
$token,
201201
null,
202-
$config['window'] ?? (int)$this->scopeConfig->getValue(self::XML_PATH_OTP_WINDOW) ?: null
202+
$config['window'] ?? (int)$this->scopeConfig->getValue(self::XML_PATH_LEEWAY) ?: null
203203
);
204204
}
205205

TwoFactorAuth/Setup/Patch/Data/UpdateOtpWindow.php renamed to TwoFactorAuth/Setup/Patch/Data/UpdateLeeway.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,26 @@
1111
use Magento\Framework\Setup\ModuleDataSetupInterface;
1212
use OTPHP\TOTPInterface;
1313

14-
class UpdateOtpWindow implements DataPatchInterface
14+
class UpdateLeeway implements DataPatchInterface
1515
{
1616
/**
1717
* @var ModuleDataSetupInterface
1818
*/
1919
private $moduleDataSetup;
2020

21+
/**
22+
* @param ModuleDataSetupInterface $moduleDataSetup
23+
*/
2124
public function __construct(ModuleDataSetupInterface $moduleDataSetup)
2225
{
2326
$this->moduleDataSetup = $moduleDataSetup;
2427
}
2528

29+
/**
30+
* Fetch Totp default period
31+
*
32+
* @return int
33+
*/
2634
public function getDefaultPeriod()
2735
{
2836
return TOTPInterface::DEFAULT_PERIOD;
@@ -37,23 +45,21 @@ public function apply()
3745
$setup->startSetup();
3846
$select = $setup->select()
3947
->from('core_config_data', ['path'])
40-
->where('path = ?', 'twofactorauth/google/otp_window');
48+
->where('path = ?', 'twofactorauth/google/leeway');
4149

4250
$existingValue = $setup->fetchOne($select);
4351
$period = $this->getDefaultPeriod();
4452
if ($existingValue && $existingValue >= $period) {
4553
$newWindowValue = $period - 1;
46-
}
47-
48-
if ($existingValue) {
4954
$setup->update(
5055
'core_config_data',
5156
['value' => $newWindowValue],
52-
'path = "twofactorauth/google/otp_window"'
57+
'path = "twofactorauth/google/leeway"'
5358
);
5459
}
55-
5660
$setup->endSetup();
61+
62+
return $this;
5763
}
5864

5965
/**
@@ -71,5 +77,4 @@ public function getAliases()
7177
{
7278
return [];
7379
}
74-
7580
}

TwoFactorAuth/Test/Api/GoogleActivateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function testAlreadyActivatedProvider()
129129
/**
130130
* @magentoConfigFixture twofactorauth/general/force_providers google
131131
* @magentoApiDataFixture Magento/User/_files/user_with_custom_role.php
132-
* @magentoConfigFixture twofactorauth/google/otp_window 20
132+
* @magentoConfigFixture twofactorauth/google/leeway 29
133133
*/
134134
public function testActivate()
135135
{

TwoFactorAuth/Test/Api/GoogleAuthenticateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public function testNotConfiguredProvider(): void
223223
/**
224224
* @magentoConfigFixture twofactorauth/general/force_providers google
225225
* @magentoApiDataFixture Magento/User/_files/user_with_custom_role.php
226-
* @magentoConfigFixture twofactorauth/google/otp_window 20
226+
* @magentoConfigFixture twofactorauth/google/leeway 29
227227
*
228228
* @return void
229229
*/

TwoFactorAuth/etc/adminhtml/system.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
<group id="google" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="0"
4040
showInStore="0">
4141
<label>Google</label>
42-
<field id="otp_window" translate="label comment" type="text" sortOrder="10" showInDefault="1"
42+
<field id="leeway" translate="label comment" type="text" sortOrder="10" showInDefault="1"
4343
showInWebsite="0" showInStore="0" canRestore="1">
44-
<label>OTP Window</label>
45-
<comment>This determines how long the one-time-passwords are valid for. An OTP Window of 1 will result in the current OTP value plus 1 code in the past and 1 code in the future to be valid at any given point in time.</comment>
46-
<backend_model>Magento\TwoFactorAuth\Model\Config\Backend\OtpWindow</backend_model>
44+
<label>Leeway</label>
45+
<comment>This sets the time drift leeway for OTPs. A leeway of 29 with a period of 30 means OTPs are valid within ±29 seconds from the current time. The leeway must be smaller than the period</comment>
46+
<backend_model>Magento\TwoFactorAuth\Model\Config\Backend\Leeway</backend_model>
4747
</field>
4848
</group>
4949
<group id="duo" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="0"

TwoFactorAuth/etc/config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<application_key backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
2222
</duo>
2323
<google>
24-
<otp_window backend_model="Magento\TwoFactorAuth\Model\Config\Backend\OtpWindow">1</otp_window>
24+
<leeway backend_model="Magento\TwoFactorAuth\Model\Config\Backend\Leeway">29</leeway>
2525
</google>
2626
</twofactorauth>
2727
</default>

0 commit comments

Comments
 (0)