Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not patch rest api #1 #4

Merged
merged 2 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Plugin/BypassWebApiTwoFactorAuth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* This file is part of the MarkShust DisableTwoFactorAuth package
*
* @author Jitheesh V O <jitheeshvo@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/
markshust marked this conversation as resolved.
Show resolved Hide resolved
declare(strict_types=1);

namespace MarkShust\DisableTwoFactorAuth\Plugin;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Integration\Api\AdminTokenServiceInterface;
use Magento\TwoFactorAuth\Model\AdminAccessTokenService;

/**
* Class BypassWebApiTwoFactorAuth
*
* @package MarkShust\DisableTwoFactorAuth\Plugin
*/
class BypassWebApiTwoFactorAuth
{
const XML_PATH_CONFIG_API_ENABLE = 'twofactorauth/general/enable_api';

/** @var ScopeConfigInterface */
private $scopeConfig;
/**
markshust marked this conversation as resolved.
Show resolved Hide resolved
* @var AdminTokenServiceInterface
*/
private AdminTokenServiceInterface $adminTokenService;

public function __construct(
markshust marked this conversation as resolved.
Show resolved Hide resolved
AdminTokenServiceInterface $adminTokenService,
ScopeConfigInterface $scopeConfig
) {
$this->scopeConfig = $scopeConfig;
$this->adminTokenService = $adminTokenService;
}

/**
* This will bypass 2fa and allow us to use existing token generate end-point
* Recommended to use this until third-party service is ready to configure 2fa
*
* @param AdminAccessTokenService $subject
* @param \Closure $proceed
* @param $username
* @param $password
*
* @return string
* @throws \Magento\Framework\Exception\AuthenticationException
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function aroundCreateAdminAccessToken(
AdminAccessTokenService $subject,
\Closure $proceed,
markshust marked this conversation as resolved.
Show resolved Hide resolved
$username,
$password
): string {
return !$this->scopeConfig->isSetFlag(self::XML_PATH_CONFIG_API_ENABLE)
markshust marked this conversation as resolved.
Show resolved Hide resolved
? $this->adminTokenService->createAdminAccessToken($username, $password)
: $proceed($username, $password);
}
}
4 changes: 4 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Warning: Enabling 2FA will immediately prompt admin user for OTP code.</comment>
</field>
<field id="enable_api" translate="label" type="select" sortOrder="100" showInDefault="1" canRestore="1">
<label>Enable 2FA for token service</label>
markshust marked this conversation as resolved.
Show resolved Hide resolved
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="force_providers">
<depends>
<field id="enable">1</field>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<twofactorauth>
<general>
<enable>1</enable>
<enable_api>1</enable_api>
</general>
</twofactorauth>
</default>
Expand Down
3 changes: 3 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
<type name="Magento\TwoFactorAuth\Model\TfaSession">
<plugin name="bypassTwoFactorAuth" type="MarkShust\DisableTwoFactorAuth\Plugin\BypassTwoFactorAuth"/>
</type>
<type name="Magento\TwoFactorAuth\Model\AdminAccessTokenService">
<plugin name="bypassWebApiTwoFactorAuth" type="MarkShust\DisableTwoFactorAuth\Plugin\BypassWebApiTwoFactorAuth"/>
</type>
</config>