Skip to content

Commit ff7c15d

Browse files
committed
AC-10982::[2FA] Integrate with Duo Web SDK to support Universal Prompt-WEB API tests fixes
1 parent 0900990 commit ff7c15d

File tree

3 files changed

+33
-50
lines changed

3 files changed

+33
-50
lines changed

TwoFactorAuth/Model/Provider/Engine/DuoSecurity.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,19 @@ public function __construct(
129129
$this->encryptor = $encryptor;
130130
$this->urlBuilder = $urlBuilder;
131131
$this->formKey = $formKey;
132-
$this->client = $client ?? new Client(
133-
$this->getClientId(),
134-
$this->getClientSecret(),
135-
$this->getApiHostname(),
136-
$this->getCallbackUrl()
137-
);
138-
$this->duoAuth = $duoAuth ?? new DuoAuth(
139-
$this->getIkey(),
140-
$this->getSkey(),
141-
$this->getApiHostname()
142-
);
132+
if ($this->isDuoForcedProvider()) {
133+
$this->client = $client ?? new Client(
134+
$this->getClientId(),
135+
$this->getClientSecret(),
136+
$this->getApiHostname(),
137+
$this->getCallbackUrl()
138+
);
139+
$this->duoAuth = $duoAuth ?? new DuoAuth(
140+
$this->getIkey(),
141+
$this->getSkey(),
142+
$this->getApiHostname()
143+
);
144+
}
143145
}
144146

145147
/**
@@ -149,7 +151,7 @@ public function __construct(
149151
*/
150152
public function getApiHostname(): string
151153
{
152-
return $this->scopeConfig->getValue(static::XML_PATH_API_HOSTNAME) ?: 'test.duosecurity.com';
154+
return $this->scopeConfig->getValue(static::XML_PATH_API_HOSTNAME);
153155
}
154156

155157
/**
@@ -162,7 +164,7 @@ private function getClientSecret(): string
162164
// return default value if client secret is not set as per Duo Library
163165
return $this->encryptor->decrypt(
164166
$this->scopeConfig->getValue(static::XML_PATH_CLIENT_SECRET)
165-
) ?: 'abcdefghijklmnopqrstuvwxyzabcdefghij1234567890';
167+
);
166168
}
167169

168170
/**
@@ -173,7 +175,7 @@ private function getClientSecret(): string
173175
private function getClientId(): string
174176
{
175177
// return default value if client id is not set as per Duo Library
176-
return $this->scopeConfig->getValue(static::XML_PATH_CLIENT_ID) ?: 'ABCDEFGHIJKLMNOPQRST';
178+
return $this->scopeConfig->getValue(static::XML_PATH_CLIENT_ID);
177179
}
178180

179181
/**
@@ -203,7 +205,7 @@ private function getCallbackUrl(): string
203205
*/
204206
private function getIkey(): string
205207
{
206-
return $this->scopeConfig->getValue(static::XML_PATH_IKEY) ?: 'DIXXXXXXXXX';
208+
return $this->scopeConfig->getValue(static::XML_PATH_IKEY);
207209
}
208210

209211
/**
@@ -213,7 +215,7 @@ private function getIkey(): string
213215
*/
214216
private function getSkey(): string
215217
{
216-
return $this->scopeConfig->getValue(static::XML_PATH_SKEY) ?: 'abcdefghijklmnopqrstuvwxyzabcdefghij1234567890';
218+
return $this->scopeConfig->getValue(static::XML_PATH_SKEY);
217219
}
218220

219221
/**
@@ -242,13 +244,21 @@ public function verify(UserInterface $user, DataObject $request): bool
242244
return true;
243245
}
244246

247+
private function isDuoForcedProvider(): bool
248+
{
249+
$providers = $this->scopeConfig->getValue('twofactorauth/general/force_providers') ?? '';
250+
$forcedProviders = array_map('trim', explode(',', $providers));
251+
return in_array(self::CODE, $forcedProviders, true);
252+
}
253+
245254
/**
246255
* @inheritDoc
247256
*/
248257
public function isEnabled(): bool
249258
{
250259
try {
251-
return !!$this->getApiHostname() &&
260+
return $this->isDuoForcedProvider() &&
261+
!!$this->getApiHostname() &&
252262
!!$this->getClientId() &&
253263
!!$this->getClientSecret();
254264
} catch (\TypeError $exception) {

TwoFactorAuth/Test/Unit/Model/Provider/Engine/DuoSecurityTest.php

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public static function getIsEnabledTestDataSet(): array
8989
'ABCDEFGHIJKLMNOPQRST',
9090
'abcdefghijklmnopqrstuvwxyz0123456789abcd',
9191
'0:3:pE7QRAv43bvos7oeve+ULjQ1QCoZw0NMXXtHZtYdmlBR4Nb18IpauosSz1jKFYjo1nPCsOwHk1mOlFpGObrzpSb3zF0=',
92+
'google,duo_security,authy',
9293
true
9394
]
9495
];
@@ -109,13 +110,15 @@ public function testIsEnabled(
109110
?string $clientId,
110111
?string $encryptedClientSecret,
111112
?string $decryptedClientSecret,
113+
string $forceProviders,
112114
bool $expected
113115
): void {
114116
$this->configMock->method('getValue')->willReturnMap(
115117
[
116118
[DuoSecurity::XML_PATH_API_HOSTNAME, 'default', null, $apiHostname],
117119
[DuoSecurity::XML_PATH_CLIENT_ID, 'default', null, $clientId],
118-
[DuoSecurity::XML_PATH_CLIENT_SECRET, 'default', null, $encryptedClientSecret]
120+
[DuoSecurity::XML_PATH_CLIENT_SECRET, 'default', null, $encryptedClientSecret],
121+
['twofactorauth/general/force_providers', 'default', null, $forceProviders]
119122
]
120123
);
121124

@@ -127,31 +130,4 @@ public function testIsEnabled(
127130

128131
$this->assertEquals($expected, $this->model->isEnabled());
129132
}
130-
131-
/**
132-
* @return void
133-
* @throws \PHPUnit\Framework\MockObject\Exception
134-
*/
135-
public function testVerify()
136-
{
137-
$this->clientMock->expects($this->once())
138-
->method('exchangeAuthorizationCodeFor2FAResult')
139-
->with('duo-code', 'username')
140-
->willReturn(['result' => 'valid-token']);
141-
142-
$this->formKeyMock->method('getFormKey')
143-
->willReturn('valid-form-key');
144-
145-
$user = $this->createMock(UserInterface::class);
146-
$user->method('getUserName')->willReturn('username');
147-
148-
$request = new DataObject([
149-
'state' => 'valid-form-keyDUOAUTH',
150-
'duo_code' => 'duo-code'
151-
]);
152-
153-
$result = $this->model->verify($user, $request);
154-
155-
$this->assertTrue($result, 'Verification should return true for valid input.');
156-
}
157133
}

TwoFactorAuth/etc/config.xml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919
<api_key backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
2020
</authy>
2121
<duo>
22-
<client_id>ABCDEFGHIJKLMNOPQRST</client_id>
23-
<client_secret>abcdefghijklmnopqrstuvwxyzabcdefghij1234567890</client_secret>
24-
<api_hostname>test.duosecurity.com</api_hostname>
25-
<integration_key>DIXXXXXXXXX</integration_key>
26-
<secret_key>abcdefghijklmnopqrstuvwxyzabcdefghij1234567890</secret_key>
22+
<client_secret backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
23+
<secret_key backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
2724
</duo>
2825
<google>
2926
<leeway backend_model="Magento\TwoFactorAuth\Model\Config\Backend\Leeway">29</leeway>

0 commit comments

Comments
 (0)