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

View File

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

View File

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

View File

@ -48,7 +48,8 @@ public class ConnectionManager {
connection = null; connection = null;
} }
public void login(String username, String password) { public void login(String username, String password, boolean rememberCredentials) {
if (rememberCredentials) {
try { try {
JSONArray array = new JSONArray(); JSONArray array = new JSONArray();
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
@ -56,9 +57,12 @@ public class ConnectionManager {
obj.put("password", password); obj.put("password", password);
array.put(obj); array.put(obj);
prefStore.put(PREFERENCE_STORE_KEY, array); prefStore.put(PREFERENCE_STORE_KEY, array);
// TODO save preferences (find out how)
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
connection = ApiConnection.getWikidataApiConnection(); connection = ApiConnection.getWikidataApiConnection();
try { try {
connection.login(username, password); 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); JSONArray array = (JSONArray) prefStore.get(PREFERENCE_STORE_KEY);
if (array.length() > 0) { if (array.length() > 0) {
JSONObject obj; JSONObject obj;
try { try {
obj = array.getJSONObject(0); return array.getJSONObject(0);
String username = obj.getString("username");
String password = obj.getString("password");
login(username, password);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
return null;
} }
public void logout() { public void logout() {