Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,34 @@

declare(strict_types=1);

namespace Magento\TwoFactorAuth\TestFramework;
namespace Magento\TwoFactorAuth\TestFramework\Plugin;

use Closure;
use Magento\Backend\App\AbstractAction;
use Magento\Framework\Event\Observer;
use Magento\TwoFactorAuth\Observer\ControllerActionPredispatch as ParentObserver;
use Magento\TestFramework\Request;
use Magento\TwoFactorAuth\Observer\ControllerActionPredispatch;

/**
* Observer that allows integration controller tests that are not aware of 2FA to run.
* Plugin that allows integration controller tests that are not aware of 2FA to run.
*/
class ControllerActionPredispatch extends ParentObserver
class BypassTwoFactorAuth
{
/**
* @inheritDoc
*/
public function execute(Observer $observer)
{
public function aroundExecute(
ControllerActionPredispatch $subject,
Closure $proceed,
Observer $observer
) : void {
/** @var $controllerAction AbstractAction */
$controllerAction = $observer->getEvent()->getData('controller_action');
if (method_exists($controllerAction, 'getRequest')
&& $controllerAction->getRequest() instanceof \Magento\TestFramework\Request
&& $controllerAction->getRequest() instanceof Request
&& !$controllerAction->getRequest()->getParam('tfa_enabled')
) {
//Hack that allows integration controller tests that are not aware of 2FA to run
return;
}

parent::execute($observer);
$proceed($observer);
}
}
6 changes: 4 additions & 2 deletions TwoFactorAuth/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
<preference for="Magento\TwoFactorAuth\Api\DuoAuthenticateInterface" type="Magento\TwoFactorAuth\Model\Provider\Engine\DuoSecurity\Authenticate"/>
<preference for="Magento\TwoFactorAuth\Api\Data\DuoDataInterface" type="Magento\TwoFactorAuth\Model\Data\Provider\Engine\DuoSecurity\Data"/>
<preference for="Magento\TwoFactorAuth\Api\U2fKeyConfigReaderInterface" type="Magento\TwoFactorAuth\Model\Provider\Engine\U2fKey\ConfigReader"/>
<preference for="OTPHP\TOTPInterface" type="OTPHP\TOTP"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class isn't within scope for dependency injection.

Screenshot_2021-06-15_22-38-40

From what I can tell, this is used as a return type in Magento\TwoFactorAuth\Model\Provider\Engine\Google::getTotp() and Magento\TwoFactorAuth\Model\Provider\Engine\Google\TotpFactory::create(), and as a mock in Magento\TwoFactorAuth\Test\Unit\Model\Provider\Engine\GoogleTest::setUp(). None of these seem to need a Magento preference.


<type name="Magento\Framework\Console\CommandListInterface">
<arguments>
Expand Down Expand Up @@ -87,6 +86,10 @@
<plugin name="fixDefaultUrl" type="Magento\TwoFactorAuth\Plugin\FirstAvailableMenu"/>
</type>

<type name="Magento\TwoFactorAuth\Observer\ControllerActionPredispatch">
<plugin name="bypass_twofactorauth_testframework" type="Magento\TwoFactorAuth\TestFramework\Plugin\BypassTwoFactorAuth" />
</type>

<virtualType name="Magento\TwoFactorAuth\Model\Provider\Google" type="Magento\TwoFactorAuth\Model\Provider">
<arguments>
<argument name="engine" xsi:type="object">Magento\TwoFactorAuth\Model\Provider\Engine\Google</argument>
Expand Down Expand Up @@ -159,5 +162,4 @@
<preference for="Magento\TwoFactorAuth\Api\UserConfigRequestManagerInterface" type="Magento\TwoFactorAuth\Model\UserConfig\UserConfigRequestManager" />
<preference for="Magento\TwoFactorAuth\Api\UserConfigTokenManagerInterface" type="Magento\TwoFactorAuth\Model\UserConfig\SignedTokenManager" />
<preference for="Magento\TwoFactorAuth\Api\UserNotifierInterface" type="Magento\TwoFactorAuth\Model\EmailUserNotifier" />
<preference for="Magento\TwoFactorAuth\Observer\ControllerActionPredispatch" type="Magento\TwoFactorAuth\TestFramework\ControllerActionPredispatch" />
</config>