Skip to content

Commit 9453466

Browse files
committed
Use clientlogin API module
This is the first step to allowing 2 factor authentication #328. This uses the new API module clientlogin instead of the login module. We still report the same set of errors in a 'nice' way with real error messages, how ever there are lots more that can probably be handled, for example #507.
1 parent 2e7aadd commit 9453466

File tree

3 files changed

+85
-8
lines changed

3 files changed

+85
-8
lines changed

app/src/main/java/fr/free/nrw/commons/CommonsApplication.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
public class CommonsApplication extends Application {
5353

5454
private MWApi api;
55+
private LoginApi loginApi;
5556
private Account currentAccount = null; // Unlike a savings account...
5657
public static final String API_URL = "https://commons.wikimedia.org/w/api.php";
5758
public static final String IMAGE_URL_BASE = "https://upload.wikimedia.org/wikipedia/commons";
@@ -89,6 +90,10 @@ public static MWApi createMWApi() {
8990
return new MWApi(API_URL, createHttpClient());
9091
}
9192

93+
public static LoginApi createLoginApi(MWApi api) {
94+
return new LoginApi(api);
95+
}
96+
9297
@Override
9398
public void onCreate() {
9499
super.onCreate();
@@ -101,6 +106,7 @@ public void onCreate() {
101106
// Fire progress callbacks for every 3% of uploaded content
102107
System.setProperty("in.yuvi.http.fluent.PROGRESS_TRIGGER_THRESHOLD", "3.0");
103108
api = createMWApi();
109+
loginApi = createLoginApi( api );
104110

105111
ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(getApplicationContext())
106112
.discCache(new TotalSizeLimitedDiscCache(StorageUtils.getCacheDirectory(this), 128 * 1024 * 1024))
@@ -164,6 +170,10 @@ public void putBitmap(String key, Bitmap bitmap) {
164170
public MWApi getApi() {
165171
return api;
166172
}
173+
174+
public LoginApi getLoginApi() {
175+
return loginApi;
176+
}
167177

168178
public Account getCurrentAccount() {
169179
if(currentAccount == null) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package fr.free.nrw.commons;
2+
3+
import org.mediawiki.api.ApiResult;
4+
import org.mediawiki.api.MWApi;
5+
6+
import java.io.IOException;
7+
8+
/**
9+
* Class for interacting explicetly with the clientlogin mediawiki API module.
10+
*
11+
* @author Addshore
12+
*/
13+
public class LoginApi {
14+
15+
private MWApi api;
16+
17+
public LoginApi(MWApi api) {
18+
this.api = api;
19+
}
20+
21+
/**
22+
*
23+
* @param username String
24+
* @param password String
25+
* @return String On success: "PASS"
26+
* failure: A failure message code (deifned by mediawiki)
27+
* misc: genericerror-UI, genericerror-REDIRECT, genericerror-RESTART
28+
* @throws IOException
29+
*/
30+
public String login(String username, String password) throws IOException {
31+
32+
/** Request a login token to be used later to log in. */
33+
ApiResult tokenData = api.action("query").
34+
param("action", "query").
35+
param("meta", "tokens").
36+
param("type", "login").
37+
post();
38+
String token = tokenData.getString("/api/query/tokens/@logintoken");
39+
40+
/** Actually log in. */
41+
ApiResult loginData = api.action("clientlogin").
42+
param("rememberMe", "1").
43+
param("username", username).
44+
param("password", password).
45+
param("logintoken", token).
46+
param("loginreturnurl", "http://example.com/").//TODO return to url?
47+
post();
48+
String status = loginData.getString("/api/clientlogin/@status");
49+
50+
if(status.equals("PASS")) {
51+
api.isLoggedIn = true;
52+
return status;
53+
54+
}else if(status.equals("FAIL")) {
55+
return loginData.getString("/api/clientlogin/@messagecode");
56+
}
57+
58+
// UI, REDIRECT, RESTART
59+
return "genericerror-" + status;
60+
}
61+
62+
}

app/src/main/java/fr/free/nrw/commons/auth/LoginActivity.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected void onPostExecute(String result) {
6666
.param("result", result)
6767
.log();
6868

69-
if (result.equals("Success")) {
69+
if (result.equals("PASS")) {
7070
if (dialog != null && dialog.isShowing()) {
7171
dialog.dismiss();
7272
}
@@ -100,17 +100,22 @@ protected void onPostExecute(String result) {
100100

101101
} else {
102102
int response;
103-
if(result.equals("NetworkFailure")) {
104-
response = R.string.login_failed_network;
105-
} else if(result.equals("NotExists") || result.equals("Illegal") || result.equals("NotExists")) {
103+
// TODO remove R.string.login_failed_network string (no longer used)
104+
// Match known failure message codes and provide messages
105+
if(result.toLowerCase().contains("nosuchuser".toLowerCase()) || result.toLowerCase().contains("noname".toLowerCase())) {
106+
// Matches nosuchuser, nosuchusershort, noname
106107
response = R.string.login_failed_username;
107108
passwordEdit.setText("");
108-
} else if(result.equals("EmptyPass") || result.equals("WrongPass") || result.equals("WrongPluginPass")) {
109+
110+
} else if(result.toLowerCase().contains("wrongpassword".toLowerCase())) {
111+
// Matches wrongpassword, wrongpasswordempty
109112
response = R.string.login_failed_password;
110113
passwordEdit.setText("");
111-
} else if(result.equals("Throttled")) {
114+
} else if(result.toLowerCase().contains("throttle".toLowerCase())) {
115+
// Matches unknown throttle error codes
112116
response = R.string.login_failed_throttled;
113-
} else if(result.equals("Blocked")) {
117+
} else if(result.toLowerCase().contains("userblocked".toLowerCase())) {
118+
// Matches login-userblocked
114119
response = R.string.login_failed_blocked;
115120
} else {
116121
// Should never really happen
@@ -142,7 +147,7 @@ protected String doInBackground(String... params) {
142147
username = params[0];
143148
password = params[1];
144149
try {
145-
return app.getApi().login(username, password);
150+
return app.getLoginApi().login(username, password);
146151
} catch (IOException e) {
147152
// Do something better!
148153
return "NetworkFailure";

0 commit comments

Comments
 (0)