@@ -48,13 +48,15 @@ public class LoginActivity extends AccountAuthenticatorActivity {
48
48
Button signupButton ;
49
49
EditText usernameEdit ;
50
50
EditText passwordEdit ;
51
+ EditText twoFactorEdit ;
51
52
ProgressDialog dialog ;
52
53
53
54
private class LoginTask extends AsyncTask <String , String , String > {
54
55
55
56
Activity context ;
56
57
String username ;
57
58
String password ;
59
+ String twoFactorCode = "" ;
58
60
59
61
@ Override
60
62
protected void onPostExecute (String result ) {
@@ -107,20 +109,20 @@ protected void onPostExecute(String result) {
107
109
} else if (result .toLowerCase ().contains ("nosuchuser" .toLowerCase ()) || result .toLowerCase ().contains ("noname" .toLowerCase ())) {
108
110
// Matches nosuchuser, nosuchusershort, noname
109
111
response = R .string .login_failed_username ;
110
- passwordEdit .setText ("" );
111
-
112
+ emptySensitiveEditFields ();
112
113
} else if (result .toLowerCase ().contains ("wrongpassword" .toLowerCase ())) {
113
114
// Matches wrongpassword, wrongpasswordempty
114
115
response = R .string .login_failed_password ;
115
- passwordEdit . setText ( "" );
116
+ emptySensitiveEditFields ( );
116
117
} else if (result .toLowerCase ().contains ("throttle" .toLowerCase ())) {
117
118
// Matches unknown throttle error codes
118
119
response = R .string .login_failed_throttled ;
119
120
} else if (result .toLowerCase ().contains ("userblocked" .toLowerCase ())) {
120
121
// Matches login-userblocked
121
122
response = R .string .login_failed_blocked ;
122
123
} else if (result .equals ("2FA" )){
123
- response = R .string .login_failed_2fa_not_supported ;
124
+ twoFactorEdit .setVisibility (View .VISIBLE );
125
+ response = R .string .login_failed_2fa_needed ;
124
126
} else {
125
127
// Occurs with unhandled login failure codes
126
128
Timber .d ("Login failed with reason: %s" , result );
@@ -131,6 +133,11 @@ protected void onPostExecute(String result) {
131
133
}
132
134
}
133
135
136
+ private void emptySensitiveEditFields () {
137
+ passwordEdit .setText ("" );
138
+ twoFactorEdit .setText ("" );
139
+ }
140
+
134
141
@ Override
135
142
protected void onPreExecute () {
136
143
super .onPreExecute ();
@@ -150,8 +157,16 @@ protected void onPreExecute() {
150
157
protected String doInBackground (String ... params ) {
151
158
username = params [0 ];
152
159
password = params [1 ];
160
+ if (params .length > 2 ) {
161
+ twoFactorCode = params [2 ];
162
+ }
163
+
153
164
try {
154
- return app .getApi ().login (username , password );
165
+ if (twoFactorCode .isEmpty ()) {
166
+ return app .getApi ().login (username , password );
167
+ } else {
168
+ return app .getApi ().login (username , password , twoFactorCode );
169
+ }
155
170
} catch (IOException e ) {
156
171
// Do something better!
157
172
return "NetworkFailure" ;
@@ -168,6 +183,7 @@ public void onCreate(Bundle savedInstanceState) {
168
183
signupButton = (Button ) findViewById (R .id .signupButton );
169
184
usernameEdit = (EditText ) findViewById (R .id .loginUsername );
170
185
passwordEdit = (EditText ) findViewById (R .id .loginPassword );
186
+ twoFactorEdit = (EditText ) findViewById (R .id .loginTwoFactor );
171
187
final LoginActivity that = this ;
172
188
173
189
prefs = getSharedPreferences ("fr.free.nrw.commons" , MODE_PRIVATE );
@@ -181,7 +197,11 @@ public void onTextChanged(CharSequence charSequence, int start, int count, int a
181
197
182
198
@ Override
183
199
public void afterTextChanged (Editable editable ) {
184
- if (usernameEdit .getText ().length () != 0 && passwordEdit .getText ().length () != 0 ) {
200
+ if (
201
+ usernameEdit .getText ().length () != 0 &&
202
+ passwordEdit .getText ().length () != 0 &&
203
+ ( twoFactorEdit .getText ().length () != 0 || twoFactorEdit .getVisibility () != View .VISIBLE )
204
+ ) {
185
205
loginButton .setEnabled (true );
186
206
} else {
187
207
loginButton .setEnabled (false );
@@ -191,6 +211,7 @@ public void afterTextChanged(Editable editable) {
191
211
192
212
usernameEdit .addTextChangedListener (loginEnabler );
193
213
passwordEdit .addTextChangedListener (loginEnabler );
214
+ twoFactorEdit .addTextChangedListener (loginEnabler );
194
215
passwordEdit .setOnEditorActionListener (new TextView .OnEditorActionListener () {
195
216
@ Override
196
217
public boolean onEditorAction (TextView textView , int actionId , KeyEvent keyEvent ) {
@@ -249,9 +270,15 @@ private void performLogin() {
249
270
250
271
String password = passwordEdit .getText ().toString ();
251
272
273
+ String twoFactorCode = twoFactorEdit .getText ().toString ();
274
+
252
275
Timber .d ("Login to start!" );
253
276
LoginTask task = new LoginTask (this );
254
- task .execute (canonicalUsername , password );
277
+ if (twoFactorCode .isEmpty ()) {
278
+ task .execute (canonicalUsername , password );
279
+ } else {
280
+ task .execute (canonicalUsername , password , twoFactorCode );
281
+ }
255
282
}
256
283
257
284
@ Override
0 commit comments