Skip to content

Commit de9c503

Browse files
committed
Merge remote-tracking branch 'gl_magento2ce/AC-11762' into AC-11762-8MAY
2 parents 8ce091b + a78a900 commit de9c503

File tree

4 files changed

+133
-1
lines changed

4 files changed

+133
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
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 OtpWindow extends Value implements ProcessorInterface
16+
{
17+
/**
18+
* @return int
19+
*/
20+
private function getDefaultPeriod(): int
21+
{
22+
return TOTPInterface::DEFAULT_PERIOD;
23+
}
24+
25+
/**
26+
* Process the value before saving.
27+
*
28+
* @param mixed $value The configuration value.
29+
* @return mixed The processed value.
30+
* @throws ValidatorException If the value is invalid.
31+
*/
32+
public function processValue($value)
33+
{
34+
if (!is_numeric($value)) {
35+
throw new ValidatorException(__('The OTP window must be a numeric value.'));
36+
}
37+
$numericValue = (int) $value;
38+
return $numericValue;
39+
}
40+
41+
/**
42+
* Validates the value before saving.
43+
*
44+
* @throws ValidatorException If the value is invalid.
45+
*/
46+
public function beforeSave()
47+
{
48+
$value = $this->getValue();
49+
$period = $this->getDefaultPeriod();
50+
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));
52+
}
53+
54+
return parent::beforeSave();
55+
}
56+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TwoFactorAuth\Setup\Patch\Data;
9+
10+
use Magento\Framework\Setup\Patch\DataPatchInterface;
11+
use Magento\Framework\Setup\ModuleDataSetupInterface;
12+
use OTPHP\TOTPInterface;
13+
14+
class UpdateOtpWindow implements DataPatchInterface
15+
{
16+
/**
17+
* @var ModuleDataSetupInterface
18+
*/
19+
private $moduleDataSetup;
20+
21+
public function __construct(ModuleDataSetupInterface $moduleDataSetup)
22+
{
23+
$this->moduleDataSetup = $moduleDataSetup;
24+
}
25+
26+
public function getDefaultPeriod()
27+
{
28+
return TOTPInterface::DEFAULT_PERIOD;
29+
}
30+
31+
/**
32+
* Apply the data patch
33+
*/
34+
public function apply()
35+
{
36+
$setup = $this->moduleDataSetup->getConnection();
37+
$setup->startSetup();
38+
$select = $setup->select()
39+
->from('core_config_data', ['path'])
40+
->where('path = ?', 'twofactorauth/google/otp_window');
41+
42+
$existingValue = $setup->fetchOne($select);
43+
$period = $this->getDefaultPeriod();
44+
if ($existingValue && $existingValue >= $period) {
45+
$newWindowValue = $period - 1;
46+
}
47+
48+
if ($existingValue) {
49+
$setup->update(
50+
'core_config_data',
51+
['value' => $newWindowValue],
52+
'path = "twofactorauth/google/otp_window"'
53+
);
54+
}
55+
56+
$setup->endSetup();
57+
}
58+
59+
/**
60+
* @inheritdoc
61+
*/
62+
public static function getDependencies()
63+
{
64+
return [];
65+
}
66+
67+
/**
68+
* @inheritdoc
69+
*/
70+
public function getAliases()
71+
{
72+
return [];
73+
}
74+
75+
}

TwoFactorAuth/etc/adminhtml/system.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
showInWebsite="0" showInStore="0" canRestore="1">
4444
<label>OTP Window</label>
4545
<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>
4647
</field>
4748
</group>
4849
<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>1</otp_window>
24+
<otp_window backend_model="Magento\TwoFactorAuth\Model\Config\Backend\OtpWindow">1</otp_window>
2525
</google>
2626
</twofactorauth>
2727
</default>

0 commit comments

Comments
 (0)