Skip to content

Commit b84ac59

Browse files
committed
AC-10982::[2FA] Integrate with Duo Web SDK to support Universal Prompt-fixes for SVC, static and integration failures
1 parent f912e0b commit b84ac59

File tree

8 files changed

+79
-62
lines changed

8 files changed

+79
-62
lines changed

TwoFactorAuth/Api/Data/DuoDataInterface.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 2020 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

TwoFactorAuth/Api/DuoAuthenticateInterface.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 2020 Adobe
4+
* All Rights Reserved.
55
*/
66

77
declare(strict_types=1);

TwoFactorAuth/Api/DuoConfigureInterface.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 2020 Adobe
4+
* All Rights Reserved.
55
*/
66

77
declare(strict_types=1);

TwoFactorAuth/Block/Provider/Duo/Auth.php

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,29 @@
1111
use Magento\Backend\Block\Template;
1212
use Magento\Backend\Model\Auth\Session;
1313
use Magento\Framework\Exception\LocalizedException;
14-
use Magento\Framework\Message\ManagerInterface;
15-
use Magento\TwoFactorAuth\Model\Provider\Engine\DuoSecurity;
1614

1715
/**
1816
* @api
1917
*/
2018
class Auth extends Template
2119
{
22-
/**
23-
* @var DuoSecurity
24-
*/
25-
private $duoSecurity;
26-
2720
/**
2821
* @var Session
2922
*/
3023
private $session;
3124

32-
/**
33-
* @var ManagerInterface
34-
*/
35-
private $messageManager;
36-
3725
/**
3826
* @param Template\Context $context
3927
* @param Session $session
40-
* @param DuoSecurity $duoSecurity
41-
* @param ManagerInterface $messageManager
4228
* @param array $data
4329
*/
4430
public function __construct(
4531
Template\Context $context,
4632
Session $session,
47-
DuoSecurity $duoSecurity,
48-
ManagerInterface $messageManager,
4933
array $data = []
5034
) {
5135
parent::__construct($context, $data);
52-
$this->duoSecurity = $duoSecurity;
5336
$this->session = $session;
54-
$this->messageManager = $messageManager;
5537
}
5638

5739
/**
@@ -63,18 +45,10 @@ public function getJsLayout()
6345
if (!$user) {
6446
throw new LocalizedException(__('User session not found.'));
6547
}
66-
$username = $user->getUserName();
67-
$state = $this->duoSecurity->generateDuoState();
68-
$this->session->setDuoState($state);
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']);
48+
$authUrl = $this->getData('auth_url');
49+
if ($authUrl) {
50+
$this->jsLayout['components']['tfa-auth']['authUrl'] = $authUrl;
7551
}
76-
77-
$this->jsLayout['components']['tfa-auth']['authUrl'] = $response['redirect_url'];
7852
return parent::getJsLayout();
7953
}
8054
}

TwoFactorAuth/Controller/Adminhtml/Duo/Auth.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\Backend\Model\Auth\Session;
1111
use Magento\Backend\App\Action;
1212
use Magento\Framework\App\Action\HttpGetActionInterface;
13+
use Magento\Framework\Controller\Result\RedirectFactory;
14+
use Magento\Framework\Message\ManagerInterface;
1315
use Magento\Framework\View\Result\PageFactory;
1416
use Magento\TwoFactorAuth\Api\TfaInterface;
1517
use Magento\TwoFactorAuth\Api\UserConfigManagerInterface;
@@ -48,28 +50,46 @@ class Auth extends AbstractAction implements HttpGetActionInterface
4850
*/
4951
private $tokenVerifier;
5052

53+
/**
54+
* @var DuoSecurity
55+
*/
56+
private $duoSecurity;
57+
/**
58+
* @var ManagerInterface
59+
*/
60+
protected $messageManager;
61+
/**
62+
* @var RedirectFactory
63+
*/
64+
protected $resultRedirectFactory;
65+
5166
/**
5267
* @param Action\Context $context
5368
* @param Session $session
5469
* @param PageFactory $pageFactory
5570
* @param UserConfigManagerInterface $userConfigManager
5671
* @param TfaInterface $tfa
5772
* @param HtmlAreaTokenVerifier $tokenVerifier
73+
* @param DuoSecurity $duoSecurity
5874
*/
5975
public function __construct(
6076
Action\Context $context,
6177
Session $session,
6278
PageFactory $pageFactory,
6379
UserConfigManagerInterface $userConfigManager,
6480
TfaInterface $tfa,
65-
HtmlAreaTokenVerifier $tokenVerifier
81+
HtmlAreaTokenVerifier $tokenVerifier,
82+
DuoSecurity $duoSecurity
6683
) {
6784
parent::__construct($context);
6885
$this->tfa = $tfa;
6986
$this->session = $session;
7087
$this->pageFactory = $pageFactory;
7188
$this->userConfigManager = $userConfigManager;
7289
$this->tokenVerifier = $tokenVerifier;
90+
$this->duoSecurity = $duoSecurity;
91+
$this->messageManager = $context->getMessageManager();
92+
$this->resultRedirectFactory = $context->getResultRedirectFactory();
7393
}
7494

7595
/**
@@ -87,8 +107,31 @@ private function getUser()
87107
*/
88108
public function execute()
89109
{
110+
$user = $this->getUser();
111+
if (!$user) {
112+
$this->messageManager->addErrorMessage(__('User session not found.'));
113+
}
90114
$this->userConfigManager->setDefaultProvider((int)$this->getUser()->getId(), DuoSecurity::CODE);
91-
return $this->pageFactory->create();
115+
116+
$username = $this->getUser()->getUserName();
117+
$state = $this->duoSecurity->generateDuoState();
118+
$this->session->setDuoState($state);
119+
$response = $this->duoSecurity->initiateAuth($username, $state);
120+
if ($response['status'] === 'open') {
121+
// If fail mode is "open", skip the Duo prompt.
122+
$this->messageManager->addErrorMessage($response['message']);
123+
}
124+
if ($response['status'] === 'closed') {
125+
// If fail mode is "closed", show an error message.
126+
$this->messageManager->addErrorMessage($response['message']);
127+
}
128+
129+
$resultPage = $this->pageFactory->create();
130+
$block = $resultPage->getLayout()->getBlock('content');
131+
if ($block) {
132+
$block->setData('auth_url', $response['redirect_url']);
133+
}
134+
return $resultPage;
92135
}
93136

94137
/**

TwoFactorAuth/Model/Data/Provider/Engine/DuoSecurity/Data.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 2020 Adobe
4+
* All Rights Reserved.
55
*/
66

77
declare(strict_types=1);

TwoFactorAuth/Test/Integration/Model/Provider/Engine/DuoSecurity/AuthenticateTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function testVerifyInvalidCredentials()
8080
$this->duo
8181
->expects($this->never())
8282
->method('authorizeUser');
83-
$this->model->createAdminAccessTokenWithCredentials(
83+
$this->model->createAdminAccessTokenWithCredentialsAndPasscode(
8484
'adminUser',
8585
'abc',
8686
'123456'
@@ -107,7 +107,7 @@ public function testVerifyNotConfiguredProvider()
107107
$this->duo
108108
->expects($this->never())
109109
->method('authorizeUser');
110-
$this->model->createAdminAccessTokenWithCredentials(
110+
$this->model->createAdminAccessTokenWithCredentialsAndPasscode(
111111
'adminUser',
112112
Bootstrap::ADMIN_PASSWORD,
113113
'123456'
@@ -125,7 +125,7 @@ public function testVerifyUnavailableProvider()
125125
$this->duo
126126
->expects($this->never())
127127
->method('authorizeUser');
128-
$this->model->createAdminAccessTokenWithCredentials(
128+
$this->model->createAdminAccessTokenWithCredentialsAndPasscode(
129129
'adminUser',
130130
Bootstrap::ADMIN_PASSWORD,
131131
'123456'
@@ -163,7 +163,7 @@ public function testVerifyValidRequest()
163163
->willReturn(['status' => 'allow']);
164164

165165
// Attempt to create the access token
166-
$token = $this->model->createAdminAccessTokenWithCredentials(
166+
$token = $this->model->createAdminAccessTokenWithCredentialsAndPasscode(
167167
$username,
168168
$password,
169169
$passcode
@@ -207,7 +207,7 @@ public function testVerifyInvalidRequest()
207207
->willReturn(['status' => 'deny', 'msg' => 'Authentication denied']); // Simulate invalid response
208208

209209
// Attempt to create the access token, expecting an exception due to the invalid response
210-
$this->model->createAdminAccessTokenWithCredentials(
210+
$this->model->createAdminAccessTokenWithCredentialsAndPasscode(
211211
$username,
212212
$password,
213213
$passcode

0 commit comments

Comments
 (0)