Skip to content

Commit 12ad392

Browse files
committed
AC-10982::[2FA] Integrate with Duo Web SDK to support Universal Prompt-fixes for unit static and integration tests
1 parent 98929de commit 12ad392

File tree

6 files changed

+54
-30
lines changed

6 files changed

+54
-30
lines changed

TwoFactorAuth/Block/Provider/Duo/Auth.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,22 @@ public function __construct(
5959
*/
6060
public function getJsLayout()
6161
{
62-
$duoFailMode = $this->duoSecurity->getDuoFailmode();
63-
try {
64-
$this->duoSecurity->healthCheck();
65-
} catch (LocalizedException $e) {
66-
if ($duoFailMode === "OPEN") {
67-
$this->messageManager->addSuccessMessage(
68-
__("Login 'Successful', but 2FA Not Performed. Confirm Duo client/secret/host values are correct")
69-
);
70-
return $this->_url->getUrl('adminhtml/dashboard');
71-
} else {
72-
$this->messageManager->addErrorMessage(
73-
__("2FA Unavailable. Confirm Duo client/secret/host values are correct")
74-
);
75-
return $this->_url->getUrl('adminhtml');
76-
}
77-
}
78-
7962
$user = $this->session->getUser();
8063
if (!$user) {
8164
throw new LocalizedException(__('User session not found.'));
8265
}
8366
$username = $user->getUserName();
8467
$state = $this->duoSecurity->generateDuoState();
8568
$this->session->setDuoState($state);
86-
$prompt_uri = $this->duoSecurity->initiateAuth($username, $state);
87-
$this->jsLayout['components']['tfa-auth']['authUrl'] = $prompt_uri;
69+
$response = $this->duoSecurity->initiateAuth($username, $state);
70+
71+
if ($response['status'] == 'open') {
72+
$this->messageManager->addErrorMessage($response['message']);
73+
} elseif ($response['status'] == 'closed') {
74+
$this->messageManager->addErrorMessage($response['message']);
75+
}
76+
77+
$this->jsLayout['components']['tfa-auth']['authUrl'] = $response['redirect_url'];
8878
return parent::getJsLayout();
8979
}
9080
}

TwoFactorAuth/Model/Provider/Engine/DuoSecurity.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magento\TwoFactorAuth\Model\Provider\Engine;
1010

11+
use Duo\DuoUniversal\DuoException;
1112
use Magento\Framework\App\Config\ScopeConfigInterface;
1213
use Magento\Framework\DataObject;
1314
use Magento\Framework\UrlInterface;
@@ -182,7 +183,12 @@ private function getSkey(): string
182183
}
183184

184185
/**
185-
* @inheritDoc
186+
* Verify the user
187+
*
188+
* @param UserInterface $user
189+
* @param DataObject $request
190+
* @return bool
191+
* @throws \Duo\DuoUniversal\DuoException
186192
*/
187193
public function verify(UserInterface $user, DataObject $request): bool
188194
{
@@ -226,15 +232,43 @@ public function isEnabled(): bool
226232
}
227233

228234
/**
229-
* Generate URI to redirect to for the Duo Universal prompt.
235+
* Initiate authentication with Duo Universal Prompt
230236
*
231237
* @param string $username
232238
* @param string $state
233-
* @return string
239+
* @return array
240+
* @throws \Duo\DuoUniversal\DuoException
234241
*/
235-
public function initiateAuth($username, string $state): string
242+
public function initiateAuth($username, string $state): array
236243
{
237-
return $this->client->createAuthUrl($username, $state);
244+
$duoFailMode = $this->getDuoFailmode();
245+
try {
246+
$this->healthCheck();
247+
} catch (DuoException $e) {
248+
if ($duoFailMode === "OPEN") {
249+
return [
250+
'status' => 'open',
251+
'redirect_url' => '',
252+
'message' => __(
253+
"Login 'applicable',
254+
but 2FA Not Performed. Switch to other 2FA Provider.
255+
Confirm Duo client/secret/host values are correct"
256+
)
257+
];
258+
} else {
259+
return [
260+
'status' => 'closed',
261+
'redirect_url' => '',
262+
'message' => __("2FA Unavailable. Confirm Duo client/secret/host values are correct")
263+
];
264+
}
265+
}
266+
267+
return [
268+
'status' => 'success',
269+
'redirect_url' => $this->client->createAuthUrl($username, $state),
270+
'message' => __('Duo Auth URL created successfully.')
271+
];
238272
}
239273

240274
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
class AuthTest extends AbstractConfigureBackendController
2121
{
2222
/**
23-
* @inheritDoc
23+
* @var string
2424
*/
2525
protected $uri = 'backend/tfa/duo/auth';
2626

2727
/**
28-
* @inheritDoc
28+
* @var string
2929
*/
3030
protected $httpMethod = Request::METHOD_GET;
3131

TwoFactorAuth/Test/Integration/Controller/Adminhtml/Tfa/ConfigureLaterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
class ConfigureLaterTest extends AbstractBackendController
2222
{
2323
/**
24-
* @inheritDoc
24+
* @var string
2525
*/
2626
protected $uri = 'backend/tfa/tfa/configurelater';
2727

2828
/**
29-
* @inheritDoc
29+
* @var string
3030
*/
3131
protected $resource = 'Magento_TwoFactorAuth::tfa';
3232

TwoFactorAuth/Test/Integration/Controller/Adminhtml/Tfa/IndexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
class IndexTest extends AbstractBackendController
2727
{
2828
/**
29-
* @inheritDoc
29+
* @var string
3030
*/
3131
protected $uri = 'backend/tfa/tfa/index';
3232

TwoFactorAuth/view/adminhtml/web/template/duo/auth.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<form>
99
<fieldset class="admin__fieldset">
1010
<legend class="admin__legend"><span translate="'2FA - Duo Security'"></span></legend>
11-
<br/>
11+
<br>
1212
<button type="button" data-bind="click: redirectToAuthUrl, afterRender: onAfterRender">
1313
Go to Duo Universal Prompt
1414
</button>

0 commit comments

Comments
 (0)