- Timestamp:
- 2009-08-30T18:57:05+02:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/preferences/ServerAccessPreference.java
r1955 r2015 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.Font;7 8 import javax.swing.JLabel;9 import javax.swing.JPasswordField;10 import javax.swing.JTextField;11 12 import org.openstreetmap.josm.Main;13 import org.openstreetmap.josm.tools.GBC;14 6 import org.openstreetmap.josm.io.OsmConnection; 15 import org.openstreetmap.josm.io.CredentialsManager .PreferenceAdditions;7 import org.openstreetmap.josm.io.CredentialsManager; 16 8 17 9 public class ServerAccessPreference implements PreferenceSetting { … … 24 16 25 17 /** 26 * Editfield for the Base url to the REST API from OSM.27 */28 private JTextField osmDataServer = new JTextField(20);29 /**30 18 * Provide username and password input editfields. 31 19 * Store the values if user hits OK. 32 20 */ 33 private PreferenceAdditions credentialsPA = OsmConnection.credentialsManager.newPreferenceAdditions();21 private CredentialsManager.PreferenceAdditions credentialsPA = OsmConnection.credentialsManager.newPreferenceAdditions(); 34 22 35 23 public void addGui(PreferenceDialog gui) { 36 osmDataServer.setText(Main.pref.get("osm-server.url", "http://api.openstreetmap.org/api"));37 osmDataServer.setToolTipText(tr("The base URL for the OSM server (REST API)"));38 gui.connection.add(new JLabel(tr("Base Server URL")), GBC.std());39 gui.connection.add(osmDataServer, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));40 24 credentialsPA.addPreferenceOptions(gui.connection); 41 25 } 42 26 43 27 public boolean ok() { 44 Main.pref.put("osm-server.url", osmDataServer.getText());45 28 credentialsPA.preferencesChanged(); 46 29 return false; -
trunk/src/org/openstreetmap/josm/io/CredentialsManager.java
r1958 r2015 9 9 */ 10 10 public interface CredentialsManager { 11 enum Key { 12 OSM_SERVER_URL("url"), 13 USERNAME("username"), 14 PASSWORD("password"); 15 final private String pname; 16 private Key(String name) { 17 pname = name; 18 } 19 @Override public String toString() { 20 return pname; 21 } 22 }; 23 11 24 /** 12 * lookupUsername, lookupPassword:13 *14 25 * Should throw or return non-null, possibly empty String. 15 26 */ 16 public String lookupUsername() throws CMException; 17 public String lookupPassword() throws CMException; 27 public String lookup(Key key) throws CMException; 18 28 19 29 /** 20 * storeUsername, storePassword:21 *22 30 * May silently fail to store. 23 31 */ 24 public void storeUsername(String username) throws CMException; 25 public void storePassword(String password) throws CMException; 32 public void store(Key key, String secret) throws CMException; 26 33 27 34 /** -
trunk/src/org/openstreetmap/josm/io/OsmConnection.java
r1962 r2015 112 112 try { 113 113 synchronized (credentialsManager) { 114 auth = credentialsManager.lookupUsername() + ":" + credentialsManager.lookupPassword(); 114 auth = credentialsManager.lookup(CredentialsManager.Key.USERNAME) + ":" + 115 credentialsManager.lookup(CredentialsManager.Key.PASSWORD); 115 116 } 116 117 } catch (CredentialsManager.CMException e) { … … 130 131 return cancel; 131 132 } 132 133 /** 134 * Default implementation of the CredentialsManager interface. 135 * Saves passwords in plain text file. 136 */ 133 137 public static class PlainCredentialsManager implements CredentialsManager { 134 public String lookupUsername() throws CMException { 135 String username = Main.pref.get("osm-server.username", null); 136 if (username == null) throw new CredentialsManager.NoContentException(); 137 return username; 138 } 139 public String lookupPassword() throws CMException { 140 String password = Main.pref.get("osm-server.password"); 141 if (password == null) throw new CredentialsManager.NoContentException(); 142 return password; 143 } 144 public void storeUsername(String username) { 145 Main.pref.put("osm-server.username", username); 146 } 147 public void storePassword(String password) { 148 Main.pref.put("osm-server.password", password); 138 public String lookup(CredentialsManager.Key key) throws CMException { 139 String secret = Main.pref.get("osm-server." + key.toString(), null); 140 if (secret == null) throw new CredentialsManager.NoContentException(); 141 return secret; 142 } 143 public void store(CredentialsManager.Key key, String secret) { 144 Main.pref.put("osm-server." + key.toString(), secret); 149 145 } 150 146 public PasswordAuthentication getPasswordAuthentication(OsmAuth caller) { 151 147 String username, password; 152 148 try { 153 username = lookup Username();149 username = lookup(Key.USERNAME); 154 150 } catch (CMException e) { 155 151 username = ""; 156 152 } 157 153 try { 158 password = lookup Password();154 password = lookup(Key.PASSWORD); 159 155 } catch (CMException e) { 160 156 password = ""; … … 175 171 p.add(warning, GBC.eop()); 176 172 177 JCheckBox savePassword = new JCheckBox(tr("Save user and password (unencrypted)"), !username.equals("") && !password.equals("")); 173 JCheckBox savePassword = new JCheckBox(tr("Save user and password (unencrypted)"), 174 !username.equals("") && !password.equals("")); 178 175 p.add(savePassword, GBC.eop()); 179 176 180 int choice = new ExtendedDialog(Main.parent, 181 tr("Enter Password"), 182 p, 183 new String[] {tr("Login"), tr("Cancel")}, 184 new String[] {"ok.png", "cancel.png"}).getValue(); 177 int choice = new ExtendedDialog( 178 Main.parent, 179 tr("Enter Password"), 180 p, 181 new String[] {tr("Login"), tr("Cancel")}, 182 new String[] {"ok.png", "cancel.png"}).getValue(); 185 183 186 184 if (choice != 1) { … … 191 189 password = String.valueOf(passwordField.getPassword()); 192 190 if (savePassword.isSelected()) { 193 store Username(username);194 store Password(password);191 store(Key.USERNAME, username); 192 store(Key.PASSWORD, password); 195 193 } 196 194 if (username.equals("")) … … 203 201 return new PreferenceAdditions() { 204 202 /** 203 * Editfield for the Base url to the REST API from OSM. 204 */ 205 final private JTextField osmDataServerURL = new JTextField(20); 206 /** 205 207 * Editfield for the username to the OSM account. 206 208 */ 207 private JTextField osmDataUsername = new JTextField(20);209 final private JTextField osmDataUsername = new JTextField(20); 208 210 /** 209 211 * Passwordfield for the userpassword of the REST API. 210 212 */ 211 private JPasswordField osmDataPassword = new JPasswordField(20); 212 213 final private JPasswordField osmDataPassword = new JPasswordField(20); 214 215 private String oldServerURL = ""; 213 216 private String oldUsername = ""; 214 217 private String oldPassword = ""; … … 216 219 public void addPreferenceOptions(JPanel panel) { 217 220 try { 218 oldUsername = lookupUsername(); 221 oldServerURL = lookup(Key.OSM_SERVER_URL); // result is not null (see CredentialsManager) 222 } catch (CMException e) { 223 oldServerURL = ""; 224 } 225 if (oldServerURL.equals("")) oldServerURL = "http://api.openstreetmap.org/api"; 226 try { 227 oldUsername = lookup(Key.USERNAME); 219 228 } catch (CMException e) { 220 229 oldUsername = ""; 221 230 } 222 231 try { 223 oldPassword = lookup Password();232 oldPassword = lookup(Key.PASSWORD); 224 233 } catch (CMException e) { 225 234 oldPassword = ""; 226 235 } 236 osmDataServerURL.setText(oldServerURL); 227 237 osmDataUsername.setText(oldUsername); 228 238 osmDataPassword.setText(oldPassword); 239 osmDataServerURL.setToolTipText(tr("The base URL for the OSM server (REST API)")); 229 240 osmDataUsername.setToolTipText(tr("Login name (e-mail) to the OSM account.")); 230 241 osmDataPassword.setToolTipText(tr("Login password to the OSM account. Leave blank to not store any password.")); 242 panel.add(new JLabel(tr("Base Server URL")), GBC.std()); 243 panel.add(osmDataServerURL, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); 231 244 panel.add(new JLabel(tr("OSM username (e-mail)")), GBC.std()); 232 245 panel.add(osmDataUsername, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); … … 241 254 } 242 255 public void preferencesChanged() { 256 String newServerURL = osmDataServerURL.getText(); 243 257 String newUsername = osmDataUsername.getText(); 244 258 String newPassword = String.valueOf(osmDataPassword.getPassword()); 259 if (!oldServerURL.equals(newServerURL)) { 260 store(Key.OSM_SERVER_URL, newServerURL); 261 } 245 262 if (!oldUsername.equals(newUsername)) { 246 store Username(newUsername);263 store(Key.USERNAME, newUsername); 247 264 } 248 265 if (!oldPassword.equals(newPassword)) { 249 store Password(newPassword);266 store(Key.PASSWORD, newPassword); 250 267 } 251 268 }
Note:
See TracChangeset
for help on using the changeset viewer.