Skip to content

Commit d3a759b

Browse files
Merge pull request #164 from magento-cia/cia-2.4.8-beta1-develop-2.4-develop-sync-08152024
Cia 2.4.8 beta1 develop 2.4 develop sync 08152024
2 parents 09c05b6 + 9661ad8 commit d3a759b

File tree

6 files changed

+73
-8
lines changed

6 files changed

+73
-8
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TwoFactorAuth\Model\Config\Backend;
9+
10+
use Magento\Framework\App\Config\Value;
11+
use Magento\Framework\App\Config\Data\ProcessorInterface;
12+
use Magento\Framework\Exception\ValidatorException;
13+
use OTPHP\TOTPInterface;
14+
15+
class Leeway extends Value implements ProcessorInterface
16+
{
17+
/**
18+
* Fetch Totp default period value
19+
*
20+
* @return int
21+
*/
22+
private function getDefaultPeriod(): int
23+
{
24+
return TOTPInterface::DEFAULT_PERIOD;
25+
}
26+
27+
/**
28+
* Process the value before saving.
29+
*
30+
* @param mixed $value The configuration value.
31+
* @return mixed The processed value.
32+
* @throws ValidatorException If the value is invalid.
33+
*/
34+
public function processValue($value)
35+
{
36+
if (!is_numeric($value)) {
37+
throw new ValidatorException(__('The Leeway must be a numeric value.'));
38+
}
39+
$numericValue = (int) $value;
40+
return $numericValue;
41+
}
42+
43+
/**
44+
* Validates the value before saving.
45+
*
46+
* @throws ValidatorException If the value is invalid.
47+
*/
48+
public function beforeSave()
49+
{
50+
$value = $this->getValue();
51+
$period = $this->getDefaultPeriod();
52+
if (!is_numeric($value) || $value < 1 || $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+
);
60+
}
61+
62+
return parent::beforeSave();
63+
}
64+
}

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/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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@
5454
<group id="google" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="0"
5555
showInStore="0">
5656
<label>Google</label>
57-
<field id="otp_window" translate="label comment" type="text" sortOrder="10" showInDefault="1"
57+
<field id="leeway" translate="label comment" type="text" sortOrder="10" showInDefault="1"
5858
showInWebsite="0" showInStore="0" canRestore="1">
59-
<label>OTP Window</label>
60-
<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>
59+
<label>Leeway</label>
60+
<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>
61+
<backend_model>Magento\TwoFactorAuth\Model\Config\Backend\Leeway</backend_model>
6162
</field>
6263
</group>
6364
<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
@@ -23,7 +23,7 @@
2323
<application_key backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
2424
</duo>
2525
<google>
26-
<otp_window>1</otp_window>
26+
<leeway backend_model="Magento\TwoFactorAuth\Model\Config\Backend\Leeway">29</leeway>
2727
</google>
2828
</twofactorauth>
2929
</default>

0 commit comments

Comments
 (0)