Skip to content

Commit 56a445f

Browse files
committed
AC-10982::[2FA] Integrate with Duo Web SDK to support Universal Prompt-moved functionality for checking duo state in controller
1 parent 702f6bc commit 56a445f

File tree

4 files changed

+14
-56
lines changed

4 files changed

+14
-56
lines changed

TwoFactorAuth/Block/Provider/Duo/Auth.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function getJsLayout()
8282
}
8383
$username = $user->getUserName();
8484
$state = $this->duoSecurity->generateDuoState();
85+
$this->session->setDuoState($state);
8586
$prompt_uri = $this->duoSecurity->initiateAuth($username, $state);
8687
$this->jsLayout['components']['tfa-auth']['authUrl'] = $prompt_uri;
8788
return parent::getJsLayout();

TwoFactorAuth/Controller/Adminhtml/Duo/Authpost.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,17 @@ private function getUser()
122122
public function execute()
123123
{
124124
$user = $this->getUser();
125-
126-
if ($this->duoSecurity->verify($user, $this->dataObjectFactory->create([
127-
'data' => $this->getRequest()->getParams(),
128-
]))) {
129-
$this->tfa->getProvider(DuoSecurity::CODE)->activate((int) $user->getId());
130-
$this->tfaSession->grantAccess();
131-
return $this->_redirect($this->context->getBackendUrl()->getStartupPageUrl());
125+
$username = $user->getUserName();
126+
$savedState = $this->session->getDuoState();
127+
128+
if (!empty($savedState) && !empty($username) && ($this->getRequest()->getParam('state') == $savedState)) {
129+
if ($this->duoSecurity->verify($user, $this->dataObjectFactory->create([
130+
'data' => $this->getRequest()->getParams(),
131+
]))) {
132+
$this->tfa->getProvider(DuoSecurity::CODE)->activate((int) $user->getId());
133+
$this->tfaSession->grantAccess();
134+
return $this->_redirect($this->context->getBackendUrl()->getStartupPageUrl());
135+
}
132136
} else {
133137
$this->alert->event(
134138
'Magento_TwoFactorAuth',

TwoFactorAuth/Helper/Data.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

TwoFactorAuth/Model/Provider/Engine/DuoSecurity.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Magento\Framework\DataObject;
1313
use Magento\Framework\Encryption\EncryptorInterface;
1414
use Magento\Framework\UrlInterface;
15-
use Magento\TwoFactorAuth\Helper\Data as TwoFactorAuthHelper;
1615
use Magento\User\Api\Data\UserInterface;
1716
use Magento\TwoFactorAuth\Api\EngineInterface;
1817
use Duo\DuoUniversal\Client;
@@ -103,16 +102,10 @@ class DuoSecurity implements EngineInterface
103102
*/
104103
private $urlBuilder;
105104

106-
/**
107-
* @var TwoFactorAuthHelper
108-
*/
109-
private $helper;
110-
111105
/**
112106
* @param ScopeConfigInterface $scopeConfig
113107
* @param EncryptorInterface $encryptor
114108
* @param UrlInterface $urlBuilder
115-
* @param TwoFactorAuthHelper $helper
116109
* @param Client|null $client
117110
* @param DuoAuth|null $duoAuth
118111
* @throws \Duo\DuoUniversal\DuoException
@@ -121,14 +114,12 @@ public function __construct(
121114
ScopeConfigInterface $scopeConfig,
122115
EncryptorInterface $encryptor,
123116
UrlInterface $urlBuilder,
124-
TwoFactorAuthHelper $helper,
125117
Client $client = null,
126118
DuoAuth $duoAuth = null
127119
) {
128120
$this->scopeConfig = $scopeConfig;
129121
$this->encryptor = $encryptor;
130122
$this->urlBuilder = $urlBuilder;
131-
$this->helper = $helper;
132123
if ($this->isDuoForcedProvider()) {
133124
$this->client = $client ?? new Client(
134125
$this->getClientId(),
@@ -221,14 +212,9 @@ private function getSkey(): string
221212
*/
222213
public function verify(UserInterface $user, DataObject $request): bool
223214
{
224-
$state = $request->getData('state');
225215
$duoCode = $request->getData('duo_code');
226216
$username = $user->getUserName();
227217

228-
if (empty($state) || empty($username)) {
229-
return false;
230-
}
231-
232218
try {
233219
// Not saving token as this is for verification purpose
234220
$this->client->exchangeAuthorizationCodeFor2FAResult($duoCode, $username);
@@ -289,6 +275,8 @@ public function healthCheck(): void
289275
}
290276

291277
/**
278+
* Generate a state for Duo Universal prompt
279+
*
292280
* @return string
293281
*/
294282
public function generateDuoState() : string

0 commit comments

Comments
 (0)