1919use Magento \TwoFactorAuth \Controller \Adminhtml \AbstractAction ;
2020use Magento \TwoFactorAuth \Model \Provider \Engine \Google ;
2121use Magento \User \Model \User ;
22+ use Magento \Framework \App \Config \ScopeConfigInterface ;
23+ use Magento \User \Model \ResourceModel \User as UserResource ;
2224
2325/**
2426 * Google authenticator post controller
@@ -61,6 +63,26 @@ class Authpost extends AbstractAction implements HttpPostActionInterface
6163 */
6264 private $ alert ;
6365
66+ /**
67+ * Config path for the 2FA Attempts
68+ */
69+ private const XML_PATH_2FA_RETRY_ATTEMPTS = 'twofactorauth/general/twofactorauth_retry ' ;
70+
71+ /**
72+ * Config path for the 2FA Attempts
73+ */
74+ private const XML_PATH_2FA_LOCK_EXPIRE = 'twofactorauth/general/auth_lock_expire ' ;
75+
76+ /**
77+ * @var ScopeConfigInterface
78+ */
79+ private $ scopeConfig ;
80+
81+ /**
82+ * @var UserResource
83+ */
84+ protected $ userResource ;
85+
6486 /**
6587 * @param Action\Context $context
6688 * @param Session $session
@@ -70,6 +92,8 @@ class Authpost extends AbstractAction implements HttpPostActionInterface
7092 * @param TfaInterface $tfa
7193 * @param AlertInterface $alert
7294 * @param DataObjectFactory $dataObjectFactory
95+ * @param UserResource $userResource
96+ * @param ScopeConfigInterface $scopeConfig
7397 */
7498 public function __construct (
7599 Action \Context $ context ,
@@ -79,7 +103,9 @@ public function __construct(
79103 TfaSessionInterface $ tfaSession ,
80104 TfaInterface $ tfa ,
81105 AlertInterface $ alert ,
82- DataObjectFactory $ dataObjectFactory
106+ DataObjectFactory $ dataObjectFactory ,
107+ UserResource $ userResource ,
108+ ScopeConfigInterface $ scopeConfig
83109 ) {
84110 parent ::__construct ($ context );
85111 $ this ->tfa = $ tfa ;
@@ -89,6 +115,8 @@ public function __construct(
89115 $ this ->tfaSession = $ tfaSession ;
90116 $ this ->dataObjectFactory = $ dataObjectFactory ;
91117 $ this ->alert = $ alert ;
118+ $ this ->userResource = $ userResource ;
119+ $ this ->scopeConfig = $ scopeConfig ;
92120 }
93121
94122 /**
@@ -103,18 +131,27 @@ public function execute()
103131 /** @var \Magento\Framework\DataObject $request */
104132 $ request = $ this ->dataObjectFactory ->create (['data ' => $ this ->getRequest ()->getParams ()]);
105133
106- if ($ this ->google ->verify ($ user , $ request )) {
107- $ this ->tfaSession ->grantAccess ();
108- $ response ->setData (['success ' => true ]);
134+ $ maxRetries = $ this ->scopeConfig ->getValue (self ::XML_PATH_2FA_RETRY_ATTEMPTS );
135+ $ retries = $ this ->verifyRetryAttempts ();
136+ if ($ retries > $ maxRetries ) { //locked the user
137+ $ lockThreshold = $ this ->scopeConfig ->getValue (self ::XML_PATH_2FA_LOCK_EXPIRE );
138+ if ($ this ->userResource ->lock ($ user ->getId (),0 , $ lockThreshold )) {
139+ $ response ->setData (['success ' => false , 'message ' => "User is disabled temporarily! " ]);
140+ }
109141 } else {
110- $ this ->alert ->event (
111- 'Magento_TwoFactorAuth ' ,
112- 'Google auth invalid token ' ,
113- AlertInterface::LEVEL_WARNING ,
114- $ user ->getUserName ()
115- );
116-
117- $ response ->setData (['success ' => false , 'message ' => 'Invalid code ' ]);
142+ if ($ this ->google ->verify ($ user , $ request )) {
143+ $ this ->tfaSession ->grantAccess ();
144+ $ response ->setData (['success ' => true ]);
145+ } else {
146+ $ this ->alert ->event (
147+ 'Magento_TwoFactorAuth ' ,
148+ 'Google auth invalid token ' ,
149+ AlertInterface::LEVEL_WARNING ,
150+ $ user ->getUserName ()
151+ );
152+
153+ $ response ->setData (['success ' => false , 'message ' => 'Invalid code ' ]);
154+ }
118155 }
119156
120157 return $ response ;
@@ -133,4 +170,17 @@ protected function _isAllowed()
133170 && $ this ->tfa ->getProviderIsAllowed ((int )$ user ->getId (), Google::CODE )
134171 && $ this ->tfa ->getProvider (Google::CODE )->isActive ((int )$ user ->getId ());
135172 }
173+
174+ /**
175+ * Get retry attempt count
176+ *
177+ * @return int
178+ */
179+ private function verifyRetryAttempts () : int
180+ {
181+ $ verifyAttempts = $ this ->session ->getOtpAttempt ();
182+ $ verifyAttempts = is_null ($ verifyAttempts ) ? 0 : $ verifyAttempts +1 ;
183+ $ this ->session ->setOtpAttempt ($ verifyAttempts );
184+ return $ verifyAttempts ;
185+ }
136186}
0 commit comments