Skip to content

Commit ae070bf

Browse files
committed
MC-37886: 2FA fixes
1 parent 861aade commit ae070bf

File tree

4 files changed

+24
-34
lines changed

4 files changed

+24
-34
lines changed

TwoFactorAuth/Controller/Adminhtml/Tfa/Requestconfig.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
use Magento\TwoFactorAuth\Api\UserConfigRequestManagerInterface;
2020
use Magento\TwoFactorAuth\Controller\Adminhtml\AbstractAction;
2121
use Magento\TwoFactorAuth\Model\UserConfig\HtmlAreaTokenVerifier;
22+
use Magento\TwoFactorAuth\Model\TfaSession;
23+
use Magento\Framework\App\ObjectManager;
24+
use Magento\TwoFactorAuth\Model\Exception\NotificationException;
2225

2326
/**
2427
* Request 2FA config from the user.
@@ -50,25 +53,33 @@ class Requestconfig extends AbstractAction implements HttpGetActionInterface, Ht
5053
*/
5154
private $session;
5255

56+
/**
57+
* @var TfaSession
58+
*/
59+
private $tfaSession;
60+
5361
/**
5462
* @param Context $context
5563
* @param UserConfigRequestManagerInterface $configRequestManager
5664
* @param HtmlAreaTokenVerifier $tokenVerifier
5765
* @param TfaInterface $tfa
5866
* @param Session $session
67+
* @param TfaSession $tfaSession
5968
*/
6069
public function __construct(
6170
Context $context,
6271
UserConfigRequestManagerInterface $configRequestManager,
6372
HtmlAreaTokenVerifier $tokenVerifier,
6473
TfaInterface $tfa,
65-
Session $session
74+
Session $session,
75+
TfaSession $tfaSession
6676
) {
6777
parent::__construct($context);
6878
$this->configRequestManager = $configRequestManager;
6979
$this->tokenVerifier = $tokenVerifier;
7080
$this->tfa = $tfa;
7181
$this->session = $session;
82+
$this->tfaSession = $tfaSession ?? ObjectManager::getInstance()->get(TfaSession::class);
7283
}
7384

7485
/**
@@ -89,7 +100,11 @@ public function execute()
89100
}
90101

91102
try {
103+
if ($this->tfaSession->isTfaEmailSent()) {
104+
throw new NotificationException();
105+
}
92106
$this->configRequestManager->sendConfigRequestTo($user);
107+
$this->tfaSession->setTfaEmailSentFlag();
93108
} catch (AuthorizationException $exception) {
94109
$this->messageManager->addErrorMessage(
95110
'Please ask an administrator with sufficient access to configure 2FA first'

TwoFactorAuth/Model/TfaSession.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
class TfaSession extends SessionManager implements TfaSessionInterface
1919
{
20-
private const SKIPPED_PROVIDERS_KEY = 'tfa_skipped_config';
20+
const SKIPPED_PROVIDERS_KEY = 'tfa_skipped_config';
2121

2222
private const TFA_EMAIL_SENT = 'tfa_email_sent';
2323

@@ -44,15 +44,15 @@ public function isGranted(): bool
4444
*/
4545
public function getSkippedProviderConfig(): array
4646
{
47-
return $this->getData(self::SKIPPED_PROVIDERS_KEY) ?? [];
47+
return $this->getData(static::SKIPPED_PROVIDERS_KEY) ?? [];
4848
}
4949

5050
/**
5151
* @inheritDoc
5252
*/
5353
public function setSkippedProviderConfig(array $config): void
5454
{
55-
$this->storage->setData(self::SKIPPED_PROVIDERS_KEY, $config);
55+
$this->storage->setData(static::SKIPPED_PROVIDERS_KEY, $config);
5656
}
5757

5858
/**

TwoFactorAuth/Model/UserConfig/SignedTokenManager.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
use Magento\Framework\Serialize\Serializer\Json;
1414
use Magento\Framework\Stdlib\DateTime\DateTime;
1515
use Magento\TwoFactorAuth\Api\UserConfigTokenManagerInterface;
16-
use Magento\Framework\App\ObjectManager;
17-
use Magento\TwoFactorAuth\Model\TfaSession;
1816

1917
/**
2018
* @inheritDoc
@@ -36,27 +34,16 @@ class SignedTokenManager implements UserConfigTokenManagerInterface
3634
*/
3735
private $dateTime;
3836

39-
/**
40-
* @var TfaSession
41-
*/
42-
private $tfaSession;
43-
4437
/**
4538
* @param EncryptorInterface $encryptor
4639
* @param Json $json
4740
* @param DateTime $dateTime
48-
* @param TfaSession|null $tfaSession
4941
*/
50-
public function __construct(
51-
EncryptorInterface $encryptor,
52-
Json $json,
53-
DateTime $dateTime,
54-
TfaSession $tfaSession = null
55-
) {
42+
public function __construct(EncryptorInterface $encryptor, Json $json, DateTime $dateTime)
43+
{
5644
$this->encryptor = $encryptor;
5745
$this->json = $json;
5846
$this->dateTime = $dateTime;
59-
$this->tfaSession = $tfaSession ?? ObjectManager::getInstance()->get(TfaSession::class);
6047
}
6148

6249
/**
@@ -67,7 +54,7 @@ public function issueFor(int $userId): string
6754
$data = ['user_id' => $userId, 'tfa_configuration' => true, 'iss' => $this->dateTime->timestamp()];
6855
$encodedData = $this->json->serialize($data);
6956
$signature = base64_encode($this->encryptor->hash($encodedData));
70-
$this->tfaSession->setTfaEmailSentFlag();
57+
7158
return base64_encode($encodedData .'.' .$signature);
7259
}
7360

TwoFactorAuth/Model/UserConfig/UserConfigRequestManager.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use Magento\TwoFactorAuth\Api\UserConfigTokenManagerInterface;
1616
use Magento\TwoFactorAuth\Api\UserNotifierInterface;
1717
use Magento\Framework\Authorization\PolicyInterface as Authorization;
18-
use Magento\Framework\App\ObjectManager;
19-
use Magento\TwoFactorAuth\Model\TfaSession;
2018

2119
/**
2220
* @inheritDoc
@@ -43,30 +41,22 @@ class UserConfigRequestManager implements UserConfigRequestManagerInterface
4341
*/
4442
private $auth;
4543

46-
/**
47-
* @var TfaSession
48-
*/
49-
private $tfaSession;
50-
5144
/**
5245
* @param TfaInterface $tfa
5346
* @param UserNotifierInterface $notifier
5447
* @param UserConfigTokenManagerInterface $tokenManager
5548
* @param Authorization $auth
56-
* @param TfaSession|null $tfaSession
5749
*/
5850
public function __construct(
5951
TfaInterface $tfa,
6052
UserNotifierInterface $notifier,
6153
UserConfigTokenManagerInterface $tokenManager,
62-
Authorization $auth,
63-
TfaSession $tfaSession = null
54+
Authorization $auth
6455
) {
6556
$this->tfa = $tfa;
6657
$this->notifier = $notifier;
6758
$this->tokenManager = $tokenManager;
6859
$this->auth = $auth;
69-
$this->tfaSession = $tfaSession ?? ObjectManager::getInstance()->get(TfaSession::class);
7060
}
7161

7262
/**
@@ -89,9 +79,7 @@ public function sendConfigRequestTo(User $user): void
8979
if (!$this->auth->isAllowed($user->getAclRole(), 'Magento_TwoFactorAuth::config')) {
9080
throw new AuthorizationException(__('User is not authorized to edit 2FA configuration'));
9181
}
92-
if (!$this->tfaSession->isTfaEmailSent()) {
93-
$this->notifier->sendAppConfigRequestMessage($user, $this->tokenManager->issueFor($userId));
94-
}
82+
$this->notifier->sendAppConfigRequestMessage($user, $this->tokenManager->issueFor($userId));
9583
} else {
9684
//Personal provider config required.
9785
$this->notifier->sendUserConfigRequestMessage($user, $this->tokenManager->issueFor($userId));

0 commit comments

Comments
 (0)