Skip to content

Commit d511d08

Browse files
committed
AC-10982::[2FA] Integrate with Duo Web SDK to support Universal Prompt-duo model related changes
1 parent 31a770d commit d511d08

File tree

4 files changed

+64
-19
lines changed

4 files changed

+64
-19
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
55
*/
6+
67
declare(strict_types=1);
78
namespace Magento\TwoFactorAuth\Block\Adminhtml\System\Config;
89

TwoFactorAuth/Helper/Data.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
7+
namespace Magento\TwoFactorAuth\Helper;
8+
9+
use Magento\Framework\Data\Form\FormKey;
10+
11+
class Data
12+
{
13+
/**
14+
* @var FormKey
15+
*/
16+
private $formKey;
17+
18+
/**
19+
* @param FormKey $formKey
20+
*/
21+
public function __construct(FormKey $formKey)
22+
{
23+
$this->formKey = $formKey;
24+
}
25+
26+
/**
27+
* Get form key
28+
*
29+
* @return string
30+
*/
31+
public function getFormKey(): string
32+
{
33+
return $this->formKey->getFormKey();
34+
}
35+
}

TwoFactorAuth/Model/Provider/Engine/DuoSecurity.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
use Magento\Framework\App\Config\ScopeConfigInterface;
1212
use Magento\Framework\DataObject;
13-
use Magento\Framework\Data\Form\FormKey;
1413
use Magento\Framework\Encryption\EncryptorInterface;
1514
use Magento\Framework\UrlInterface;
15+
use Magento\TwoFactorAuth\Helper\Data as TwoFactorAuthHelper;
1616
use Magento\User\Api\Data\UserInterface;
1717
use Magento\TwoFactorAuth\Api\EngineInterface;
1818
use Duo\DuoUniversal\Client;
@@ -104,15 +104,15 @@ class DuoSecurity implements EngineInterface
104104
private $urlBuilder;
105105

106106
/**
107-
* @var FormKey
107+
* @var TwoFactorAuthHelper
108108
*/
109-
private $formKey;
109+
private $helper;
110110

111111
/**
112112
* @param ScopeConfigInterface $scopeConfig
113113
* @param EncryptorInterface $encryptor
114114
* @param UrlInterface $urlBuilder
115-
* @param FormKey $formKey
115+
* @param TwoFactorAuthHelper $helper
116116
* @param Client|null $client
117117
* @param DuoAuth|null $duoAuth
118118
* @throws \Duo\DuoUniversal\DuoException
@@ -121,24 +121,26 @@ public function __construct(
121121
ScopeConfigInterface $scopeConfig,
122122
EncryptorInterface $encryptor,
123123
UrlInterface $urlBuilder,
124+
TwoFactorAuthHelper $helper,
124125
Client $client = null,
125126
DuoAuth $duoAuth = null
126127
) {
127128
$this->scopeConfig = $scopeConfig;
128129
$this->encryptor = $encryptor;
129130
$this->urlBuilder = $urlBuilder;
131+
$this->helper = $helper;
130132
if ($this->isDuoForcedProvider()) {
131133
$this->client = $client ?? new Client(
132-
$this->getClientId(),
133-
$this->getClientSecret(),
134-
$this->getApiHostname(),
135-
$this->getCallbackUrl()
136-
);
134+
$this->getClientId(),
135+
$this->getClientSecret(),
136+
$this->getApiHostname(),
137+
$this->getCallbackUrl()
138+
);
137139
$this->duoAuth = $duoAuth ?? new DuoAuth(
138-
$this->getIkey(),
139-
$this->getSkey(),
140-
$this->getApiHostname()
141-
);
140+
$this->getIkey(),
141+
$this->getSkey(),
142+
$this->getApiHostname()
143+
);
142144
}
143145
}
144146

@@ -227,16 +229,23 @@ public function verify(UserInterface $user, DataObject $request): bool
227229
return false;
228230
}
229231

232+
if ($this->helper->getFormKey() . self::AUTH_SUFFIX != $savedState) {
233+
return false;
234+
}
235+
230236
try {
231-
// Not saving token as this is just for verificaiton purpose
232-
$decoded_token = $this->client->exchangeAuthorizationCodeFor2FAResult($duoCode, $username);
237+
// Not saving token as this is for verification purpose
238+
$this->client->exchangeAuthorizationCodeFor2FAResult($duoCode, $username);
233239
} catch (LocalizedException $e) {
234240
return false;
235241
}
236242
# Exchange happened successfully so render success page
237243
return true;
238244
}
239245

246+
/**
247+
* Check if Duo is selected as forced provider
248+
*/
240249
private function isDuoForcedProvider(): bool
241250
{
242251
$providers = $this->scopeConfig->getValue('twofactorauth/general/force_providers') ?? '';

TwoFactorAuth/Test/Integration/Controller/Adminhtml/Duo/AuthTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
55
*/
66

77
declare(strict_types=1);

0 commit comments

Comments
 (0)