Skip to content

Commit 096d1e7

Browse files
committed
MC-22950: Enable 2FA by default for Admins
- Code review feedback - Removed extra code - Fixed ACL check for unconfigured site with disallowed user role - Moved session data into dedicated locations - Refactored userId's to user UserContext
1 parent 28ba31b commit 096d1e7

File tree

19 files changed

+345
-107
lines changed

19 files changed

+345
-107
lines changed

TwoFactorAuth/Api/TfaSessionInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,18 @@ public function grantAccess(): void;
2727
* @return bool
2828
*/
2929
public function isGranted(): bool;
30+
31+
/**
32+
* Get the current configuration for skipped providers
33+
*
34+
* @return array
35+
*/
36+
public function getSkippedProviderConfig(): array;
37+
38+
/**
39+
* Set the configuration of skipped providers
40+
*
41+
* @param array $config
42+
*/
43+
public function setSkippedProviderConfig(array $config): void;
3044
}

TwoFactorAuth/Block/Adminhtml/System/Config/Providers.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@
1010
use Magento\Framework\Data\Form\Element\AbstractElement;
1111
use Magento\Backend\Block\Template\Context;
1212
use Magento\Framework\Phrase;
13-
use Magento\Framework\Serialize\SerializerInterface;
13+
use Magento\Framework\Serialize\Serializer\Json;
1414

1515
/**
16-
* Providers field
16+
* Displays a warning modal if the all currently available providers are deselected
1717
*/
1818
class Providers extends Field
1919
{
2020
/**
21-
* @var SerializerInterface
21+
* @var Json
2222
*/
23-
private $serializer;
23+
private $json;
2424

2525
/**
2626
* @param Context $context
27-
* @param SerializerInterface $serializer
27+
* @param Json $json
2828
* @param array $data
2929
*/
3030
public function __construct(
3131
Context $context,
32-
SerializerInterface $serializer,
32+
Json $json,
3333
array $data = []
3434
) {
3535
parent::__construct($context, $data);
36-
$this->serializer = $serializer;
36+
$this->json = $json;
3737
}
3838

3939
/**
@@ -50,7 +50,7 @@ protected function _getElementHtml(AbstractElement $element)
5050
]
5151
]
5252
];
53-
$html .= '<script type="text/x-magento-init">' . $this->serializer->serialize($config) . '</script>';
53+
$html .= '<script type="text/x-magento-init">' . $this->json->serialize($config) . '</script>';
5454

5555
return $html;
5656
}

TwoFactorAuth/Block/ChangeProvider.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77

88
namespace Magento\TwoFactorAuth\Block;
99

10+
use Magento\Authorization\Model\UserContextInterface;
1011
use Magento\Backend\Block\Template;
1112
use Magento\Backend\Model\Auth\Session;
12-
use Magento\TwoFactorAuth\Api\TfaSessionInterface;
13-
use Magento\User\Model\User;
1413
use Magento\TwoFactorAuth\Api\TfaInterface;
1514
use Magento\TwoFactorAuth\Api\ProviderInterface;
1615

@@ -24,6 +23,11 @@ class ChangeProvider extends Template
2423
*/
2524
private $tfa;
2625

26+
/**
27+
* @var UserContextInterface
28+
*/
29+
private $userContext;
30+
2731
/**
2832
* @var Session
2933
*/
@@ -33,27 +37,29 @@ class ChangeProvider extends Template
3337
* ChangeProvider constructor.
3438
* @param Template\Context $context
3539
* @param Session $session
40+
* @param UserContextInterface $userContext
3641
* @param TfaInterface $tfa
3742
* @param array $data
3843
*/
3944
public function __construct(
4045
Template\Context $context,
4146
Session $session,
47+
UserContextInterface $userContext,
4248
TfaInterface $tfa,
4349
array $data = []
4450
) {
4551
parent::__construct($context, $data);
4652
$this->tfa = $tfa;
4753
$this->session = $session;
54+
$this->userContext = $userContext;
4855
}
4956

5057
/**
5158
* @inheritDoc
5259
*/
5360
protected function _toHtml()
5461
{
55-
$userId = (int)$this->session->getUser()->getId();
56-
$toActivate = $this->tfa->getProvidersToActivate($userId);
62+
$toActivate = $this->tfa->getProvidersToActivate($this->userContext->getUserId());
5763

5864
foreach ($toActivate as $toActivateProvider) {
5965
if ($toActivateProvider->getCode() === $this->getData('provider')) {
@@ -71,7 +77,7 @@ public function getJsLayout()
7177
{
7278
$providers = [];
7379
foreach ($this->getProvidersList() as $provider) {
74-
if (!$provider->isActive((int)$this->session->getUser()->getId())) {
80+
if (!$provider->isActive($this->userContext->getUserId())) {
7581
continue;
7682
}
7783
$providers[] = [
@@ -89,15 +95,6 @@ public function getJsLayout()
8995
return parent::getJsLayout();
9096
}
9197

92-
/**
93-
* Get user
94-
* @return User|null
95-
*/
96-
private function getUser(): ?User
97-
{
98-
return $this->session->getUser();
99-
}
100-
10198
/**
10299
* Get a list of available providers
103100
* @return ProviderInterface[]
@@ -106,7 +103,7 @@ private function getProvidersList(): array
106103
{
107104
$res = [];
108105

109-
$providers = $this->tfa->getUserProviders((int) $this->getUser()->getId());
106+
$providers = $this->tfa->getUserProviders((int) $this->userContext->getUserId());
110107
foreach ($providers as $provider) {
111108
if ($provider->getCode() !== $this->getData('provider')) {
112109
$res[] = $provider;

TwoFactorAuth/Block/ConfigureLater.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77

88
namespace Magento\TwoFactorAuth\Block;
99

10+
use Magento\Authorization\Model\UserContextInterface;
1011
use Magento\Backend\Block\Template;
1112
use Magento\Backend\Block\Template\Context;
1213
use Magento\Backend\Model\Auth\Session;
1314
use Magento\Framework\Data\Form\FormKey;
1415
use Magento\Framework\Serialize\SerializerInterface;
15-
use Magento\User\Model\User;
1616
use Magento\TwoFactorAuth\Api\TfaInterface;
17-
use Magento\TwoFactorAuth\Api\ProviderInterface;
1817

1918
/**
2019
* @api
@@ -36,13 +35,19 @@ class ConfigureLater extends Template
3635
*/
3736
private $serializer;
3837

38+
/**
39+
* @var UserContextInterface
40+
*/
41+
private $userContext;
42+
3943
/**
4044
* ChangeProvider constructor.
4145
* @param Context $context
4246
* @param Session $session
4347
* @param TfaInterface $tfa
4448
* @param SerializerInterface $serializer
4549
* @param FormKey $formKey
50+
* @param UserContextInterface $userContext
4651
* @param array $data
4752
*/
4853
public function __construct(
@@ -51,21 +56,23 @@ public function __construct(
5156
TfaInterface $tfa,
5257
SerializerInterface $serializer,
5358
FormKey $formKey,
59+
UserContextInterface $userContext,
5460
array $data = []
5561
) {
5662
parent::__construct($context, $data);
5763
$this->tfa = $tfa;
5864
$this->session = $session;
5965
$this->serializer = $serializer;
6066
$this->formKey = $formKey;
67+
$this->userContext = $userContext;
6168
}
6269

6370
/**
6471
* @inheritDoc
6572
*/
6673
protected function _toHtml()
6774
{
68-
$userId = (int)$this->session->getUser()->getId();
75+
$userId = $this->userContext->getUserId();
6976
$providers = $this->tfa->getUserProviders($userId);
7077
$toActivate = $this->tfa->getProvidersToActivate($userId);
7178

TwoFactorAuth/Block/Provider/U2fKey/Auth.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Backend\Block\Template;
1111
use Magento\Backend\Model\Auth\Session;
1212
use Magento\TwoFactorAuth\Model\Provider\Engine\U2fKey;
13+
use Magento\TwoFactorAuth\Model\Provider\Engine\U2fKey\Session as U2fSession;
1314

1415
/**
1516
* @api
@@ -22,29 +23,32 @@ class Auth extends Template
2223
private $u2fKey;
2324

2425
/**
25-
* @var Session
26+
* @var U2fSession
2627
*/
27-
private $session;
28+
private $u2fSession;
2829

2930
/**
30-
* @var array
31+
* @var Session
3132
*/
32-
private $authenticateData;
33+
private $session;
3334

3435
/**
3536
* @param Template\Context $context
36-
* @param Session $session
37+
* @param U2fSession $u2fSession
3738
* @param U2fKey $u2fKey
39+
* @param Session $session
3840
* @param array $data
3941
*/
4042
public function __construct(
4143
Template\Context $context,
42-
Session $session,
44+
U2fSession $u2fSession,
4345
U2fKey $u2fKey,
46+
Session $session,
4447
array $data = []
4548
) {
4649
parent::__construct($context, $data);
4750
$this->u2fKey = $u2fKey;
51+
$this->u2fSession = $u2fSession;
4852
$this->session = $session;
4953
}
5054

@@ -74,7 +78,7 @@ public function getJsLayout()
7478
public function generateAuthenticateData(): array
7579
{
7680
$authenticateData = $this->u2fKey->getAuthenticateData($this->session->getUser());
77-
$this->session->setTfaU2fChallenge($authenticateData['credentialRequestOptions']['challenge']);
81+
$this->u2fSession->setU2fChallenge($authenticateData['credentialRequestOptions']['challenge']);
7882

7983
return $authenticateData;
8084
}

TwoFactorAuth/Block/Provider/U2fKey/Configure.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Backend\Block\Template;
1111
use Magento\Backend\Model\Auth\Session;
1212
use Magento\TwoFactorAuth\Model\Provider\Engine\U2fKey;
13+
use Magento\TwoFactorAuth\Model\Provider\Engine\U2fKey\Session as U2fSession;
1314

1415
/**
1516
* @api
@@ -21,6 +22,11 @@ class Configure extends Template
2122
*/
2223
private $u2fKey;
2324

25+
/**
26+
* @var U2fSession
27+
*/
28+
private $u2fSession;
29+
2430
/**
2531
* @var Session
2632
*/
@@ -29,18 +35,21 @@ class Configure extends Template
2935
/**
3036
* @param Template\Context $context
3137
* @param U2fKey $u2fKey
38+
* @param U2fSession $u2fSession
3239
* @param Session $session
3340
* @param array $data
3441
*/
3542
public function __construct(
3643
Template\Context $context,
3744
U2fKey $u2fKey,
45+
U2fSession $u2fSession,
3846
Session $session,
3947
array $data = []
4048
) {
4149

4250
parent::__construct($context, $data);
4351
$this->u2fKey = $u2fKey;
52+
$this->u2fSession = $u2fSession;
4453
$this->session = $session;
4554
}
4655

@@ -63,10 +72,15 @@ public function getJsLayout()
6372
return parent::getJsLayout();
6473
}
6574

75+
/**
76+
* Get the data required to issue a WebAuthn request
77+
*
78+
* @return array
79+
*/
6680
public function getRegisterData(): array
6781
{
6882
$registerData = $this->u2fKey->getRegisterData($this->session->getUser());
69-
$this->session->setTfaU2fChallenge($registerData['publicKey']['challenge']);
83+
$this->u2fSession->setU2fChallenge($registerData['publicKey']['challenge']);
7084

7185
return $registerData;
7286
}

0 commit comments

Comments
 (0)