Start storing user credentials in OpenRefine's preferences

This commit is contained in:
Antonin Delpeuch 2018-01-02 18:32:44 +01:00
parent 7a6e4f49b1
commit 5faa432aa6
4 changed files with 47 additions and 18 deletions

View File

@ -3,7 +3,6 @@
<div class="dialog-body" bind="dialogBody">
<p class="body-text">
Logging in to Wikidata will allow you to perform edits directly from OpenRefine.
Your credentials will be stored unencrypted in OpenRefine's preferences.
</p>
<div class="wikibase-user-management-area">
<div class="wikibase-user-login" bind="loginArea">
@ -18,6 +17,10 @@
<td><label for="wb-password">Password:</label></td>
<td><input name="wb-password" type="password" placeholder="Enter your password" /></td>
</tr>
<tr>
<td><input type="checkbox" name="remember-credentials" /></td>
<td><label for="remember-credentials">Remember credentials (stored unencrypted in OpenRefine's preferences)</label></td>
</tr>
</table>
</form>
<div class="wikibase-login-buttons">

View File

@ -1,8 +1,29 @@
var ManageAccountDialog = {};
/*
$.post(
"command/core/get-all-preferences",
null,
populatePreferences,
"json"
);
*/
ManageAccountDialog.launch = function(logged_in_username, callback) {
$.post(
"command/core/get-all-preferences",
null,
function (preferences) {
ManageAccountDialog.display(logged_in_username, preferences.wikidata_credentials, callback);
},
"json"
);
};
ManageAccountDialog.display = function(logged_in_username, saved_credentials, callback) {
var self = this;
var frame = $(DOM.loadHTML("wikidata", "scripts/dialogs/manage-account-dialog.html"));
var elmts = this._elmts = DOM.bind(frame);
console.log(saved_credentials);
this._level = DialogSystem.showDialog(frame);

View File

@ -12,7 +12,8 @@ import org.json.JSONWriter;
import org.openrefine.wikidata.editing.ConnectionManager;
import com.google.refine.commands.Command;
import com.google.refine.preference.PreferenceStore;
import com.google.refine.ProjectManager;
public class LoginCommand extends Command {
@Override
@ -20,9 +21,11 @@ public class LoginCommand extends Command {
throws ServletException, IOException {
String username = request.getParameter("wb-username");
String password = request.getParameter("wb-password");
String remember = request.getParameter("remember-credentials");
System.out.println(remember);
ConnectionManager manager = ConnectionManager.getInstance();
if (username != null && password != null) {
manager.login(username, password);
manager.login(username, password, "on".equals(remember));
} else if ("true".equals(request.getParameter("logout"))) {
manager.logout();
}

View File

@ -48,17 +48,21 @@ public class ConnectionManager {
connection = null;
}
public void login(String username, String password) {
try {
JSONArray array = new JSONArray();
JSONObject obj = new JSONObject();
obj.put("username", username);
obj.put("password", password);
array.put(obj);
prefStore.put(PREFERENCE_STORE_KEY, array);
} catch (JSONException e) {
e.printStackTrace();
public void login(String username, String password, boolean rememberCredentials) {
if (rememberCredentials) {
try {
JSONArray array = new JSONArray();
JSONObject obj = new JSONObject();
obj.put("username", username);
obj.put("password", password);
array.put(obj);
prefStore.put(PREFERENCE_STORE_KEY, array);
// TODO save preferences (find out how)
} catch (JSONException e) {
e.printStackTrace();
}
}
connection = ApiConnection.getWikidataApiConnection();
try {
connection.login(username, password);
@ -67,19 +71,17 @@ public class ConnectionManager {
}
}
public void restorePreviousLogin() {
public JSONObject getStoredCredentials() {
JSONArray array = (JSONArray) prefStore.get(PREFERENCE_STORE_KEY);
if (array.length() > 0) {
JSONObject obj;
try {
obj = array.getJSONObject(0);
String username = obj.getString("username");
String password = obj.getString("password");
login(username, password);
return array.getJSONObject(0);
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
public void logout() {