1010
1111use Magento \Framework \App \Config \ScopeConfigInterface ;
1212use Magento \Framework \DataObject ;
13- use Magento \Framework \Data \Form \FormKey ;
1413use Magento \Framework \Encryption \EncryptorInterface ;
1514use Magento \Framework \UrlInterface ;
15+ use Magento \TwoFactorAuth \Helper \Data as TwoFactorAuthHelper ;
1616use Magento \User \Api \Data \UserInterface ;
1717use Magento \TwoFactorAuth \Api \EngineInterface ;
1818use 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 ' ) ?? '' ;
0 commit comments