| 1 | Index: src/org/openstreetmap/josm/gui/preferences/ServerAccessPreference.java
|
|---|
| 2 | ===================================================================
|
|---|
| 3 | --- src/org/openstreetmap/josm/gui/preferences/ServerAccessPreference.java (revision 1979)
|
|---|
| 4 | +++ src/org/openstreetmap/josm/gui/preferences/ServerAccessPreference.java (working copy)
|
|---|
| 5 | @@ -3,16 +3,8 @@
|
|---|
| 6 |
|
|---|
| 7 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 8 |
|
|---|
| 9 | -import java.awt.Font;
|
|---|
| 10 | -
|
|---|
| 11 | -import javax.swing.JLabel;
|
|---|
| 12 | -import javax.swing.JPasswordField;
|
|---|
| 13 | -import javax.swing.JTextField;
|
|---|
| 14 | -
|
|---|
| 15 | -import org.openstreetmap.josm.Main;
|
|---|
| 16 | -import org.openstreetmap.josm.tools.GBC;
|
|---|
| 17 | import org.openstreetmap.josm.io.OsmConnection;
|
|---|
| 18 | -import org.openstreetmap.josm.io.CredentialsManager.PreferenceAdditions;
|
|---|
| 19 | +import org.openstreetmap.josm.io.CredentialsManager;
|
|---|
| 20 |
|
|---|
| 21 | public class ServerAccessPreference implements PreferenceSetting {
|
|---|
| 22 |
|
|---|
| 23 | @@ -23,25 +15,16 @@
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | - * Editfield for the Base url to the REST API from OSM.
|
|---|
| 28 | - */
|
|---|
| 29 | - private JTextField osmDataServer = new JTextField(20);
|
|---|
| 30 | - /**
|
|---|
| 31 | * Provide username and password input editfields.
|
|---|
| 32 | * Store the values if user hits OK.
|
|---|
| 33 | */
|
|---|
| 34 | - private PreferenceAdditions credentialsPA = OsmConnection.credentialsManager.newPreferenceAdditions();
|
|---|
| 35 | + private CredentialsManager.PreferenceAdditions credentialsPA = OsmConnection.credentialsManager.newPreferenceAdditions();
|
|---|
| 36 |
|
|---|
| 37 | public void addGui(PreferenceDialog gui) {
|
|---|
| 38 | - osmDataServer.setText(Main.pref.get("osm-server.url", "http://api.openstreetmap.org/api"));
|
|---|
| 39 | - osmDataServer.setToolTipText(tr("The base URL for the OSM server (REST API)"));
|
|---|
| 40 | - gui.connection.add(new JLabel(tr("Base Server URL")), GBC.std());
|
|---|
| 41 | - gui.connection.add(osmDataServer, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
|
|---|
| 42 | credentialsPA.addPreferenceOptions(gui.connection);
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | public boolean ok() {
|
|---|
| 46 | - Main.pref.put("osm-server.url", osmDataServer.getText());
|
|---|
| 47 | credentialsPA.preferencesChanged();
|
|---|
| 48 | return false;
|
|---|
| 49 | }
|
|---|
| 50 | Index: src/org/openstreetmap/josm/io/CredentialsManager.java
|
|---|
| 51 | ===================================================================
|
|---|
| 52 | --- src/org/openstreetmap/josm/io/CredentialsManager.java (revision 1979)
|
|---|
| 53 | +++ src/org/openstreetmap/josm/io/CredentialsManager.java (working copy)
|
|---|
| 54 | @@ -8,21 +8,28 @@
|
|---|
| 55 | * username/password-related user interaction is encapsulated here.
|
|---|
| 56 | */
|
|---|
| 57 | public interface CredentialsManager {
|
|---|
| 58 | + enum Key {
|
|---|
| 59 | + OSM_SERVER_URL("url"),
|
|---|
| 60 | + USERNAME("username"),
|
|---|
| 61 | + PASSWORD("password");
|
|---|
| 62 | + final private String pname;
|
|---|
| 63 | + private Key(String name) {
|
|---|
| 64 | + pname = name;
|
|---|
| 65 | + }
|
|---|
| 66 | + @Override public String toString() {
|
|---|
| 67 | + return pname;
|
|---|
| 68 | + }
|
|---|
| 69 | + };
|
|---|
| 70 | +
|
|---|
| 71 | /**
|
|---|
| 72 | - * lookupUsername, lookupPassword:
|
|---|
| 73 | - *
|
|---|
| 74 | * Should throw or return non-null, possibly empty String.
|
|---|
| 75 | */
|
|---|
| 76 | - public String lookupUsername() throws CMException;
|
|---|
| 77 | - public String lookupPassword() throws CMException;
|
|---|
| 78 | + public String lookup(Key key) throws CMException;
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | - * storeUsername, storePassword:
|
|---|
| 82 | - *
|
|---|
| 83 | * May silently fail to store.
|
|---|
| 84 | */
|
|---|
| 85 | - public void storeUsername(String username) throws CMException;
|
|---|
| 86 | - public void storePassword(String password) throws CMException;
|
|---|
| 87 | + public void store(Key key, String secret) throws CMException;
|
|---|
| 88 |
|
|---|
| 89 | /**
|
|---|
| 90 | * If authentication using the stored credentials fails, this method is
|
|---|
| 91 | Index: src/org/openstreetmap/josm/io/OsmConnection.java
|
|---|
| 92 | ===================================================================
|
|---|
| 93 | --- src/org/openstreetmap/josm/io/OsmConnection.java (revision 1979)
|
|---|
| 94 | +++ src/org/openstreetmap/josm/io/OsmConnection.java (working copy)
|
|---|
| 95 | @@ -111,7 +111,8 @@
|
|---|
| 96 | String auth;
|
|---|
| 97 | try {
|
|---|
| 98 | synchronized (credentialsManager) {
|
|---|
| 99 | - auth = credentialsManager.lookupUsername() + ":" + credentialsManager.lookupPassword();
|
|---|
| 100 | + auth = credentialsManager.lookup(CredentialsManager.Key.USERNAME) + ":" +
|
|---|
| 101 | + credentialsManager.lookup(CredentialsManager.Key.PASSWORD);
|
|---|
| 102 | }
|
|---|
| 103 | } catch (CredentialsManager.CMException e) {
|
|---|
| 104 | auth = ":";
|
|---|
| 105 | @@ -129,33 +130,28 @@
|
|---|
| 106 | public boolean isCanceled() {
|
|---|
| 107 | return cancel;
|
|---|
| 108 | }
|
|---|
| 109 | -
|
|---|
| 110 | + /**
|
|---|
| 111 | + * Default implementation of the CredentialsManager interface.
|
|---|
| 112 | + * Saves passwords in plain text file.
|
|---|
| 113 | + */
|
|---|
| 114 | public static class PlainCredentialsManager implements CredentialsManager {
|
|---|
| 115 | - public String lookupUsername() throws CMException {
|
|---|
| 116 | - String username = Main.pref.get("osm-server.username", null);
|
|---|
| 117 | - if (username == null) throw new CredentialsManager.NoContentException();
|
|---|
| 118 | - return username;
|
|---|
| 119 | + public String lookup(CredentialsManager.Key key) throws CMException {
|
|---|
| 120 | + String secret = Main.pref.get("osm-server." + key.toString(), null);
|
|---|
| 121 | + if (secret == null) throw new CredentialsManager.NoContentException();
|
|---|
| 122 | + return secret;
|
|---|
| 123 | }
|
|---|
| 124 | - public String lookupPassword() throws CMException {
|
|---|
| 125 | - String password = Main.pref.get("osm-server.password");
|
|---|
| 126 | - if (password == null) throw new CredentialsManager.NoContentException();
|
|---|
| 127 | - return password;
|
|---|
| 128 | + public void store(CredentialsManager.Key key, String secret) {
|
|---|
| 129 | + Main.pref.put("osm-server." + key.toString(), secret);
|
|---|
| 130 | }
|
|---|
| 131 | - public void storeUsername(String username) {
|
|---|
| 132 | - Main.pref.put("osm-server.username", username);
|
|---|
| 133 | - }
|
|---|
| 134 | - public void storePassword(String password) {
|
|---|
| 135 | - Main.pref.put("osm-server.password", password);
|
|---|
| 136 | - }
|
|---|
| 137 | public PasswordAuthentication getPasswordAuthentication(OsmAuth caller) {
|
|---|
| 138 | String username, password;
|
|---|
| 139 | try {
|
|---|
| 140 | - username = lookupUsername();
|
|---|
| 141 | + username = lookup(Key.USERNAME);
|
|---|
| 142 | } catch (CMException e) {
|
|---|
| 143 | username = "";
|
|---|
| 144 | }
|
|---|
| 145 | try {
|
|---|
| 146 | - password = lookupPassword();
|
|---|
| 147 | + password = lookup(Key.PASSWORD);
|
|---|
| 148 | } catch (CMException e) {
|
|---|
| 149 | password = "";
|
|---|
| 150 | }
|
|---|
| 151 | @@ -174,14 +170,16 @@
|
|---|
| 152 | warning.setFont(warning.getFont().deriveFont(Font.ITALIC));
|
|---|
| 153 | p.add(warning, GBC.eop());
|
|---|
| 154 |
|
|---|
| 155 | - JCheckBox savePassword = new JCheckBox(tr("Save user and password (unencrypted)"), !username.equals("") && !password.equals(""));
|
|---|
| 156 | + JCheckBox savePassword = new JCheckBox(tr("Save user and password (unencrypted)"),
|
|---|
| 157 | + !username.equals("") && !password.equals(""));
|
|---|
| 158 | p.add(savePassword, GBC.eop());
|
|---|
| 159 |
|
|---|
| 160 | - int choice = new ExtendedDialog(Main.parent,
|
|---|
| 161 | - tr("Enter Password"),
|
|---|
| 162 | - p,
|
|---|
| 163 | - new String[] {tr("Login"), tr("Cancel")},
|
|---|
| 164 | - new String[] {"ok.png", "cancel.png"}).getValue();
|
|---|
| 165 | + int choice = new ExtendedDialog(
|
|---|
| 166 | + Main.parent,
|
|---|
| 167 | + tr("Enter Password"),
|
|---|
| 168 | + p,
|
|---|
| 169 | + new String[] {tr("Login"), tr("Cancel")},
|
|---|
| 170 | + new String[] {"ok.png", "cancel.png"}).getValue();
|
|---|
| 171 |
|
|---|
| 172 | if (choice != 1) {
|
|---|
| 173 | caller.authCancelled = true;
|
|---|
| 174 | @@ -190,8 +188,8 @@
|
|---|
| 175 | username = usernameField.getText();
|
|---|
| 176 | password = String.valueOf(passwordField.getPassword());
|
|---|
| 177 | if (savePassword.isSelected()) {
|
|---|
| 178 | - storeUsername(username);
|
|---|
| 179 | - storePassword(password);
|
|---|
| 180 | + store(Key.USERNAME, username);
|
|---|
| 181 | + store(Key.PASSWORD, password);
|
|---|
| 182 | }
|
|---|
| 183 | if (username.equals(""))
|
|---|
| 184 | return null;
|
|---|
| 185 | @@ -202,32 +200,47 @@
|
|---|
| 186 | public PreferenceAdditions newPreferenceAdditions() {
|
|---|
| 187 | return new PreferenceAdditions() {
|
|---|
| 188 | /**
|
|---|
| 189 | + * Editfield for the Base url to the REST API from OSM.
|
|---|
| 190 | + */
|
|---|
| 191 | + final private JTextField osmDataServerURL = new JTextField(20);
|
|---|
| 192 | + /**
|
|---|
| 193 | * Editfield for the username to the OSM account.
|
|---|
| 194 | */
|
|---|
| 195 | - private JTextField osmDataUsername = new JTextField(20);
|
|---|
| 196 | + final private JTextField osmDataUsername = new JTextField(20);
|
|---|
| 197 | /**
|
|---|
| 198 | * Passwordfield for the userpassword of the REST API.
|
|---|
| 199 | */
|
|---|
| 200 | - private JPasswordField osmDataPassword = new JPasswordField(20);
|
|---|
| 201 | + final private JPasswordField osmDataPassword = new JPasswordField(20);
|
|---|
| 202 |
|
|---|
| 203 | + private String oldServerURL = "";
|
|---|
| 204 | private String oldUsername = "";
|
|---|
| 205 | private String oldPassword = "";
|
|---|
| 206 |
|
|---|
| 207 | public void addPreferenceOptions(JPanel panel) {
|
|---|
| 208 | try {
|
|---|
| 209 | - oldUsername = lookupUsername();
|
|---|
| 210 | + oldServerURL = lookup(Key.OSM_SERVER_URL); // result is not null (see CredentialsManager)
|
|---|
| 211 | } catch (CMException e) {
|
|---|
| 212 | + oldServerURL = "";
|
|---|
| 213 | + }
|
|---|
| 214 | + if (oldServerURL.equals("")) oldServerURL = "http://api.openstreetmap.org/api";
|
|---|
| 215 | + try {
|
|---|
| 216 | + oldUsername = lookup(Key.USERNAME);
|
|---|
| 217 | + } catch (CMException e) {
|
|---|
| 218 | oldUsername = "";
|
|---|
| 219 | }
|
|---|
| 220 | try {
|
|---|
| 221 | - oldPassword = lookupPassword();
|
|---|
| 222 | + oldPassword = lookup(Key.PASSWORD);
|
|---|
| 223 | } catch (CMException e) {
|
|---|
| 224 | oldPassword = "";
|
|---|
| 225 | }
|
|---|
| 226 | + osmDataServerURL.setText(oldServerURL);
|
|---|
| 227 | osmDataUsername.setText(oldUsername);
|
|---|
| 228 | osmDataPassword.setText(oldPassword);
|
|---|
| 229 | + osmDataServerURL.setToolTipText(tr("The base URL for the OSM server (REST API)"));
|
|---|
| 230 | osmDataUsername.setToolTipText(tr("Login name (e-mail) to the OSM account."));
|
|---|
| 231 | osmDataPassword.setToolTipText(tr("Login password to the OSM account. Leave blank to not store any password."));
|
|---|
| 232 | + panel.add(new JLabel(tr("Base Server URL")), GBC.std());
|
|---|
| 233 | + panel.add(osmDataServerURL, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
|
|---|
| 234 | panel.add(new JLabel(tr("OSM username (e-mail)")), GBC.std());
|
|---|
| 235 | panel.add(osmDataUsername, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
|
|---|
| 236 | panel.add(new JLabel(tr("OSM password")), GBC.std());
|
|---|
| 237 | @@ -240,13 +253,17 @@
|
|---|
| 238 | panel.add(warning, GBC.eop().fill(GBC.HORIZONTAL));
|
|---|
| 239 | }
|
|---|
| 240 | public void preferencesChanged() {
|
|---|
| 241 | + String newServerURL = osmDataServerURL.getText();
|
|---|
| 242 | String newUsername = osmDataUsername.getText();
|
|---|
| 243 | String newPassword = String.valueOf(osmDataPassword.getPassword());
|
|---|
| 244 | + if (!oldServerURL.equals(newServerURL)) {
|
|---|
| 245 | + store(Key.OSM_SERVER_URL, newServerURL);
|
|---|
| 246 | + }
|
|---|
| 247 | if (!oldUsername.equals(newUsername)) {
|
|---|
| 248 | - storeUsername(newUsername);
|
|---|
| 249 | + store(Key.USERNAME, newUsername);
|
|---|
| 250 | }
|
|---|
| 251 | if (!oldPassword.equals(newPassword)) {
|
|---|
| 252 | - storePassword(newPassword);
|
|---|
| 253 | + store(Key.PASSWORD, newPassword);
|
|---|
| 254 | }
|
|---|
| 255 | }
|
|---|
| 256 | };
|
|---|