19
19
use Magento \TwoFactorAuth \Controller \Adminhtml \AbstractAction ;
20
20
use Magento \TwoFactorAuth \Model \Provider \Engine \Google ;
21
21
use Magento \User \Model \User ;
22
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
23
+ use Magento \User \Model \ResourceModel \User as UserResource ;
22
24
23
25
/**
24
26
* Google authenticator post controller
@@ -61,6 +63,26 @@ class Authpost extends AbstractAction implements HttpPostActionInterface
61
63
*/
62
64
private $ alert ;
63
65
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
+
64
86
/**
65
87
* @param Action\Context $context
66
88
* @param Session $session
@@ -70,6 +92,8 @@ class Authpost extends AbstractAction implements HttpPostActionInterface
70
92
* @param TfaInterface $tfa
71
93
* @param AlertInterface $alert
72
94
* @param DataObjectFactory $dataObjectFactory
95
+ * @param UserResource $userResource
96
+ * @param ScopeConfigInterface $scopeConfig
73
97
*/
74
98
public function __construct (
75
99
Action \Context $ context ,
@@ -79,7 +103,9 @@ public function __construct(
79
103
TfaSessionInterface $ tfaSession ,
80
104
TfaInterface $ tfa ,
81
105
AlertInterface $ alert ,
82
- DataObjectFactory $ dataObjectFactory
106
+ DataObjectFactory $ dataObjectFactory ,
107
+ UserResource $ userResource ,
108
+ ScopeConfigInterface $ scopeConfig
83
109
) {
84
110
parent ::__construct ($ context );
85
111
$ this ->tfa = $ tfa ;
@@ -89,6 +115,8 @@ public function __construct(
89
115
$ this ->tfaSession = $ tfaSession ;
90
116
$ this ->dataObjectFactory = $ dataObjectFactory ;
91
117
$ this ->alert = $ alert ;
118
+ $ this ->userResource = $ userResource ;
119
+ $ this ->scopeConfig = $ scopeConfig ;
92
120
}
93
121
94
122
/**
@@ -103,18 +131,27 @@ public function execute()
103
131
/** @var \Magento\Framework\DataObject $request */
104
132
$ request = $ this ->dataObjectFactory ->create (['data ' => $ this ->getRequest ()->getParams ()]);
105
133
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
+ }
109
141
} 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
+ }
118
155
}
119
156
120
157
return $ response ;
@@ -133,4 +170,17 @@ protected function _isAllowed()
133
170
&& $ this ->tfa ->getProviderIsAllowed ((int )$ user ->getId (), Google::CODE )
134
171
&& $ this ->tfa ->getProvider (Google::CODE )->isActive ((int )$ user ->getId ());
135
172
}
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
+ }
136
186
}
0 commit comments