Skip to content

Commit c0f3162

Browse files
authored
Merge pull request #13 from drpayyne/auto-2fa
2 parents b45d0ce + 32f9264 commit c0f3162

File tree

9 files changed

+90
-26
lines changed

9 files changed

+90
-26
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [2.0.0] - 2021-11-10
8+
9+
This is a potentially breaking release, as it changes the overall functionality when Magento is in `developer` mode. A new "Disable 2FA in Developer Mode" system configuration has been created, which is a Yes/No toggle. By default, it is set to Yes so that 2FA is automatically disabled when a Magento site is in `developer` mode. When this is set to No, the two other 2FA configuration dropdowns set the configuration for 2FA. When not in `developer` mode, this toggle has no effect.
10+
11+
### Added
12+
- Add ability to automatically disable 2FA when in developer mode ([#13](https://github.com/markshust/magento2-module-disabletwofactorauth/pull/13)).
13+
714
## [1.1.4] - 2021-02-22
815

916
### Fixed

Plugin/BypassTwoFactorAuth.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace MarkShust\DisableTwoFactorAuth\Plugin;
55

66
use Magento\Framework\App\Config\ScopeConfigInterface;
7+
use Magento\Framework\App\State;
78
use Magento\TwoFactorAuth\Model\TfaSession;
89

910
/**
@@ -13,26 +14,36 @@
1314
class BypassTwoFactorAuth
1415
{
1516
const XML_PATH_CONFIG_ENABLE = 'twofactorauth/general/enable';
17+
const XML_PATH_CONFIG_ENABLE_FOR_API_TOKEN_GENERATION = 'twofactorauth/general/enable_for_api_token_generation';
18+
const XML_PATH_CONFIG_DISABLE_IN_DEVELOPER_MODE = 'twofactorauth/general/disable_in_developer_mode';
1619

1720
/** @var ScopeConfigInterface */
1821
private $scopeConfig;
1922

23+
/** @var State */
24+
private $appState;
25+
2026
/**
2127
* BypassTwoFactorAuth constructor.
2228
* @param ScopeConfigInterface $scopeConfig
29+
* @param State $appState
2330
*/
2431
public function __construct(
25-
ScopeConfigInterface $scopeConfig
32+
ScopeConfigInterface $scopeConfig,
33+
State $appState
2634
) {
2735
$this->scopeConfig = $scopeConfig;
36+
$this->appState = $appState;
2837
}
2938

3039
/**
3140
* Enables the bypass of 2FA for admin access.
32-
* This can be useful within development & integration environments.
41+
* This can be useful for within development & integration environments.
3342
*
3443
* If 2FA is enabled, return the original result.
35-
* If 2FA is disabled, always return true so all requests bypass 2FA.
44+
* If developer mode is enabled, 2FA is disabled unless "Disable 2FA in developer mode" is set to No.
45+
*
46+
* Returning true in this function bypasses 2FA.
3647
*
3748
* NOTE: Always keep 2FA enabled within production environments for security purposes.
3849
*
@@ -44,7 +55,15 @@ public function afterIsGranted(
4455
TfaSession $subject,
4556
$result
4657
): bool {
47-
return $this->scopeConfig->isSetFlag(self::XML_PATH_CONFIG_ENABLE)
58+
$is2faEnabled = $this->scopeConfig->isSetFlag(self::XML_PATH_CONFIG_ENABLE);
59+
$isDeveloperMode = $this->appState->getMode() == State::MODE_DEVELOPER;
60+
$alwaysDisableInDeveloperMode = $this->scopeConfig->isSetFlag(self::XML_PATH_CONFIG_DISABLE_IN_DEVELOPER_MODE);
61+
62+
if ($isDeveloperMode && $alwaysDisableInDeveloperMode) {
63+
$is2faEnabled = false;
64+
}
65+
66+
return $is2faEnabled
4867
? $result
4968
: true;
5069
}

Plugin/BypassTwoFactorAuthForApiTokenGeneration.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Closure;
77
use Magento\Framework\App\Config\ScopeConfigInterface;
8+
use Magento\Framework\App\State;
89
use Magento\Framework\Exception\AuthenticationException;
910
use Magento\Framework\Exception\InputException;
1011
use Magento\Framework\Exception\LocalizedException;
@@ -17,30 +18,39 @@
1718
*/
1819
class BypassTwoFactorAuthForApiTokenGeneration
1920
{
20-
const XML_PATH_CONFIG_ENABLE_FOR_API_TOKEN_GENERATION = 'twofactorauth/general/enable_for_api_token_generation';
21+
/** @var AdminTokenServiceInterface */
22+
private $adminTokenService;
2123

2224
/** @var ScopeConfigInterface */
2325
private $scopeConfig;
2426

25-
/** @var AdminTokenServiceInterface */
26-
private $adminTokenService;
27+
/** @var State */
28+
private $appState;
2729

2830
/**
2931
* BypassTwoFactorAuthForApiTokenGeneration constructor.
3032
* @param AdminTokenServiceInterface $adminTokenService
3133
* @param ScopeConfigInterface $scopeConfig
34+
* @param State $appState
3235
*/
3336
public function __construct(
3437
AdminTokenServiceInterface $adminTokenService,
35-
ScopeConfigInterface $scopeConfig
38+
ScopeConfigInterface $scopeConfig,
39+
State $appState
3640
) {
3741
$this->scopeConfig = $scopeConfig;
3842
$this->adminTokenService = $adminTokenService;
43+
$this->appState = $appState;
3944
}
4045

4146
/**
4247
* Enables the bypass of 2FA for API token generation.
43-
* This can be useful for third-party vendors during module development.
48+
* This can be useful for within development & integration environments.
49+
*
50+
* If 2FA is enabled, return the original result.
51+
* If developer mode is enabled, 2FA is disabled unless "Disable 2FA in developer mode" is set to No.
52+
*
53+
* Calling createAdminAccessToken within this function bypasses 2FA.
4454
*
4555
* NOTE: Always keep 2FA enabled within production environments for security purposes.
4656
*
@@ -59,7 +69,19 @@ public function aroundCreateAdminAccessToken(
5969
$username,
6070
$password
6171
): string {
62-
return $this->scopeConfig->isSetFlag(self::XML_PATH_CONFIG_ENABLE_FOR_API_TOKEN_GENERATION)
72+
$is2faEnabled = $this->scopeConfig->isSetFlag(
73+
BypassTwoFactorAuth::XML_PATH_CONFIG_ENABLE_FOR_API_TOKEN_GENERATION
74+
);
75+
$isDeveloperMode = $this->appState->getMode() == State::MODE_DEVELOPER;
76+
$alwaysDisableInDeveloperMode = $this->scopeConfig->isSetFlag(
77+
BypassTwoFactorAuth::XML_PATH_CONFIG_DISABLE_IN_DEVELOPER_MODE
78+
);
79+
80+
if ($isDeveloperMode && $alwaysDisableInDeveloperMode) {
81+
$is2faEnabled = false;
82+
}
83+
84+
return $is2faEnabled
6385
? $proceed($username, $password)
6486
: $this->adminTokenService->createAdminAccessToken($username, $password);
6587
}

README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ With the release of Magento 2.4, two-factor authentication (also known as 2FA) b
2222
ability to disable it in either the admin or console. However, there are situations which may require 2FA to be disabled
2323
or temporarily turned off, such as within development or testing environments.
2424

25-
This module adds the missing toggle to turn 2FA on or off from the admin. It does this by hooking into the core code in
25+
This module automatically disables 2FA while in developer mode (since version 2.0.0), and adds the missing toggle to turn 2FA on or off from the admin for other environments. It does this by hooking into the core code in
2626
a very seamless manner, just as would be done if this toggle existed in the core code. Installing this module should not
27-
open up any security holes, as it just works off of a simple config toggle which if not present, falls back to default
28-
functionality.
27+
open any security holes, as it just works off of a simple config toggle which, if not present, falls back to the default
28+
functionality.
29+
30+
You can also toggle 2FA back on while in developer mode, if you need to test your code functionality while 2FA is enabled.
2931

30-
![Demo](https://raw.githubusercontent.com/markshust/magento2-module-disabletwofactorauth/master/docs/demo.png)
32+
![Demo](https://raw.githubusercontent.com/markshust/magento2-module-disabletwofactorauth/master/docs/demo-2021-11-10.png)
3133

3234
## Installation
3335

@@ -39,28 +41,36 @@ bin/magento setup:upgrade
3941

4042
## Usage
4143

42-
This module keeps 2FA enabled by default. This is to prevent any unexpected side effects or security loopholes from
44+
This module automatically disables 2FA in developer mode (since version 2.0.0). In any other deployment mode, 2FA is kept enabled by default. This is to prevent any unexpected side effects or security loopholes from
4345
being introduced during automated installation processes.
4446

4547
### Disable 2FA
4648

47-
Enables the bypass of 2FA for admin access. This can be useful within development & integration environments.
48-
49-
Visit **Admin > Stores > Settings > Configuration > Security > 2FA** and set *Enable 2FA* to **No**.
49+
It may still be desirable to disable 2FA in non-production environments, such as within testing or internal staging environments. For these cases, 2FA is not automatically disabled. However, there are toggles to override the default Magento settings to disable 2FA within these environments.
5050

51-
CLI: `bin/magento config:set twofactorauth/general/enable 0`
51+
You can also bypass 2FA for API token generation. This can be useful for third-party vendors during module development.
5252

5353
*NOTE: Always keep 2FA enabled within production environments for security purposes.*
5454

55-
### Disable 2FA for API Token Generation
55+
#### 2FA
56+
57+
To disable 2FA, visit **Admin > Stores > Settings > Configuration > Security > 2FA** and set *Enable 2FA* to **No**.
58+
59+
CLI: `bin/magento config:set twofactorauth/general/enable 0`
5660

57-
Enables the bypass of 2FA for API token generation. This can be useful for third-party vendors during module development.
61+
#### 2FA for API Token Generation
5862

59-
Visit **Admin > Stores > Settings > Configuration > Security > 2FA** and set *Enable 2FA for API Token Generation* to **No**.
63+
To disable 2FA for API Token Generation, visit **Admin > Stores > Settings > Configuration > Security > 2FA** and set *Enable 2FA for API Token Generation* to **No**.
6064

6165
CLI: `bin/magento config:set twofactorauth/general/enable_for_api_token_generation 0`
6266

63-
*NOTE: Always keep 2FA enabled within production environments for security purposes.*
67+
### Enable 2FA in developer mode
68+
69+
This module automatically disables 2FA while developer mode is enabled, but there may be situations when you need 2FA enabled during development. Rather than needing to disable this module, you can just disable this configuration setting in the admin.
70+
71+
To enable 2FA while in developer mode, visit **Admin > Stores > Settings > Configuration > Security > 2FA** and set *Disable 2FA in Developer Mode* to **No**.
72+
73+
CLI: `bin/magento config:set twofactorauth/general/disable_in_developer_mode 0`
6474

6575
## License
6676

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"magento/framework": ">=103"
77
},
88
"type": "magento2-module",
9-
"version": "1.1.4",
9+
"version": "2.0.0",
1010
"license": [
1111
"MIT"
1212
],

docs/demo-2021-11-10.png

79.1 KB
Loading

docs/demo.png

-80.7 KB
Binary file not shown.

etc/adminhtml/system.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33
<system>
44
<section id="twofactorauth">
55
<group id="general">
6-
<field id="enable" translate="label" type="select" sortOrder="100" showInDefault="1" canRestore="1">
6+
<field id="enable" translate="label" type="select" sortOrder="900" showInDefault="1" canRestore="1">
77
<label>Enable 2FA</label>
88
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
99
<comment>Warning: Enabling 2FA will immediately prompt admin user for OTP code.</comment>
1010
</field>
11-
<field id="enable_for_api_token_generation" translate="label" type="select" sortOrder="200" showInDefault="1" canRestore="1">
11+
<field id="enable_for_api_token_generation" translate="label" type="select" sortOrder="910" showInDefault="1" canRestore="1">
1212
<label>Enable 2FA for API Token Generation</label>
1313
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
1414
</field>
15+
<field id="disable_in_developer_mode" translate="label" type="select" sortOrder="920" showInDefault="1" canRestore="1">
16+
<label>Disable 2FA in Developer Mode</label>
17+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
18+
<comment>Set to No to use the above settings, otherwise 2FA is disabled in developer mode.</comment>
19+
</field>
1520
<field id="force_providers">
1621
<depends>
1722
<field id="enable">1</field>

etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<general>
66
<enable>1</enable>
77
<enable_for_api_token_generation>1</enable_for_api_token_generation>
8+
<disable_in_developer_mode>1</disable_in_developer_mode>
89
</general>
910
</twofactorauth>
1011
</default>

0 commit comments

Comments
 (0)