Changeset 1955 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2009-08-11T13:37:25+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/PreferenceDialog.java
r1865 r1955 41 41 // some common tabs 42 42 public final JPanel display = createPreferenceTab("display", tr("Display Settings"), tr("Various settings that influence the visual representation of the whole program.")); 43 public final JPanel connection = createPreferenceTab("connection", I18n.tr("Connection Settings"), I18n.tr("Connection Settings for the OSM server.") );43 public final JPanel connection = createPreferenceTab("connection", I18n.tr("Connection Settings"), I18n.tr("Connection Settings for the OSM server."),true); 44 44 public final JPanel map = createPreferenceTab("map", I18n.tr("Map Settings"), I18n.tr("Settings for the map projection and data interpretation.")); 45 45 public final JPanel audio = createPreferenceTab("audio", I18n.tr("Audio Settings"), I18n.tr("Settings for the audio player and audio markers.")); -
trunk/src/org/openstreetmap/josm/gui/preferences/ServerAccessPreference.java
r1742 r1955 12 12 import org.openstreetmap.josm.Main; 13 13 import org.openstreetmap.josm.tools.GBC; 14 import org.openstreetmap.josm.io.OsmConnection; 15 import org.openstreetmap.josm.io.CredentialsManager.PreferenceAdditions; 14 16 15 17 public class ServerAccessPreference implements PreferenceSetting { … … 26 28 private JTextField osmDataServer = new JTextField(20); 27 29 /** 28 * Editfield for the username to the OSM account. 30 * Provide username and password input editfields. 31 * Store the values if user hits OK. 29 32 */ 30 private JTextField osmDataUsername = new JTextField(20); 31 /** 32 * Passwordfield for the userpassword of the REST API. 33 */ 34 private JPasswordField osmDataPassword = new JPasswordField(20); 33 private PreferenceAdditions credentialsPA = OsmConnection.credentialsManager.newPreferenceAdditions(); 35 34 36 35 public void addGui(PreferenceDialog gui) { 37 36 osmDataServer.setText(Main.pref.get("osm-server.url", "http://api.openstreetmap.org/api")); 38 osmDataUsername.setText(Main.pref.get("osm-server.username"));39 osmDataPassword.setText(Main.pref.get("osm-server.password"));40 41 37 osmDataServer.setToolTipText(tr("The base URL for the OSM server (REST API)")); 42 osmDataUsername.setToolTipText(tr("Login name (e-mail) to the OSM account."));43 osmDataPassword.setToolTipText(tr("Login password to the OSM account. Leave blank to not store any password."));44 45 38 gui.connection.add(new JLabel(tr("Base Server URL")), GBC.std()); 46 39 gui.connection.add(osmDataServer, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); 47 gui.connection.add(new JLabel(tr("OSM username (e-mail)")), GBC.std()); 48 gui.connection.add(osmDataUsername, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); 49 gui.connection.add(new JLabel(tr("OSM password")), GBC.std()); 50 gui.connection.add(osmDataPassword, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,0)); 51 JLabel warning = new JLabel(tr("<html>" + 52 "WARNING: The password is stored in plain text in the preferences file.<br>" + 53 "The password is transferred in plain text to the server, encoded in the URL.<br>" + 54 "<b>Do not use a valuable Password.</b></html>")); 55 warning.setFont(warning.getFont().deriveFont(Font.ITALIC)); 56 gui.connection.add(warning, GBC.eop().fill(GBC.HORIZONTAL)); 40 credentialsPA.addPreferenceOptions(gui.connection); 57 41 } 58 42 59 43 public boolean ok() { 60 44 Main.pref.put("osm-server.url", osmDataServer.getText()); 61 Main.pref.put("osm-server.username", osmDataUsername.getText()); 62 Main.pref.put("osm-server.password", String.valueOf(osmDataPassword.getPassword())); 45 credentialsPA.preferencesChanged(); 63 46 return false; 64 47 } -
trunk/src/org/openstreetmap/josm/io/OsmConnection.java
r1881 r1955 25 25 import org.openstreetmap.josm.tools.Base64; 26 26 import org.openstreetmap.josm.tools.GBC; 27 import org.openstreetmap.josm.io.CredentialsManager.CMException; 27 28 28 29 /** … … 36 37 protected boolean cancel = false; 37 38 protected HttpURLConnection activeConnection; 39 /** 40 * Handles password storage and some related gui-components. 41 * It can be set by a plugin. This may happen at startup and 42 * by changing the preferences. 43 * Syncronize on this object to get or set a consistent 44 * username/password pair. 45 */ 46 public static CredentialsManager credentialsManager = new PlainCredentialsManager(); 38 47 39 48 private static OsmAuth authentication = new OsmAuth(); 49 40 50 /** 41 51 * Initialize the http defaults and the authenticator. … … 55 65 * The authentication class handling the login requests. 56 66 */ 57 p rivatestatic class OsmAuth extends Authenticator {67 public static class OsmAuth extends Authenticator { 58 68 /** 59 69 * Set to true, when the autenticator tried the password once. 60 70 */ 61 boolean passwordtried = false;71 public boolean passwordtried = false; 62 72 /** 63 73 * Whether the user cancelled the password dialog 64 74 */ 65 boolean authCancelled = false; 66 75 public boolean authCancelled = false; 67 76 @Override protected PasswordAuthentication getPasswordAuthentication() { 68 String username = Main.pref.get("osm-server.username"); 69 String password = Main.pref.get("osm-server.password"); 70 if (passwordtried || username.equals("") || password.equals("")) { 71 JPanel p = new JPanel(new GridBagLayout()); 72 if (!username.equals("") && !password.equals("")) { 73 p.add(new JLabel(tr("Incorrect password or username.")), GBC.eop()); 74 } 75 p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,10,0)); 76 JTextField usernameField = new JTextField(username, 20); 77 p.add(usernameField, GBC.eol()); 78 p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,10,0)); 79 JPasswordField passwordField = new JPasswordField(password, 20); 80 p.add(passwordField, GBC.eol()); 81 JLabel warning = new JLabel(tr("Warning: The password is transferred unencrypted.")); 82 warning.setFont(warning.getFont().deriveFont(Font.ITALIC)); 83 p.add(warning, GBC.eop()); 84 85 JCheckBox savePassword = new JCheckBox(tr("Save user and password (unencrypted)"), !username.equals("") && !password.equals("")); 86 p.add(savePassword, GBC.eop()); 87 88 int choice = new ExtendedDialog(Main.parent, 89 tr("Enter Password"), 90 p, 91 new String[] {tr("Login"), tr("Cancel")}, 92 new String[] {"ok.png", "cancel.png"}).getValue(); 93 94 if (choice != 1) { 95 authCancelled = true; 96 return null; 97 } 98 username = usernameField.getText(); 99 password = String.valueOf(passwordField.getPassword()); 100 if (savePassword.isSelected()) { 101 Main.pref.put("osm-server.username", username); 102 Main.pref.put("osm-server.password", password); 103 } 104 if (username.equals("")) 105 return null; 106 } 107 passwordtried = true; 108 return new PasswordAuthentication(username, password.toCharArray()); 77 return credentialsManager.getPasswordAuthentication(this); 109 78 } 110 79 } … … 141 110 protected void addAuth(HttpURLConnection con) throws CharacterCodingException { 142 111 CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder(); 143 String auth = Main.pref.get("osm-server.username") + ":" + Main.pref.get("osm-server.password"); 112 String auth; 113 try { 114 synchronized (credentialsManager) { 115 auth = credentialsManager.lookupUsername() + ":" + credentialsManager.lookupPassword(); 116 } 117 } catch (CMException e) { 118 auth = ":"; 119 } 144 120 ByteBuffer bytes = encoder.encode(CharBuffer.wrap(auth)); 145 121 con.addRequestProperty("Authorization", "Basic "+Base64.encode(bytes)); … … 155 131 return cancel; 156 132 } 133 134 public static class PlainCredentialsManager implements CredentialsManager { 135 public String lookupUsername() throws CMException { 136 String username = Main.pref.get("osm-server.username", null); 137 if (username == null) throw new CredentialsManager.NoContentException(); 138 return username; 139 } 140 public String lookupPassword() throws CMException { 141 String password = Main.pref.get("osm-server.password"); 142 if (password == null) throw new CredentialsManager.NoContentException(); 143 return password; 144 } 145 public void storeUsername(String username) { 146 Main.pref.put("osm-server.username", username); 147 } 148 public void storePassword(String password) { 149 Main.pref.put("osm-server.password", password); 150 } 151 public PasswordAuthentication getPasswordAuthentication(OsmAuth caller) { 152 String username, password; 153 try { 154 username = lookupUsername(); 155 } catch (CMException e) { 156 username = ""; 157 } 158 try { 159 password = lookupPassword(); 160 } catch (CMException e) { 161 password = ""; 162 } 163 if (caller.passwordtried || username.equals("") || password.equals("")) { 164 JPanel p = new JPanel(new GridBagLayout()); 165 if (!username.equals("") && !password.equals("")) { 166 p.add(new JLabel(tr("Incorrect password or username.")), GBC.eop()); 167 } 168 p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,10,0)); 169 JTextField usernameField = new JTextField(username, 20); 170 p.add(usernameField, GBC.eol()); 171 p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,10,0)); 172 JPasswordField passwordField = new JPasswordField(password, 20); 173 p.add(passwordField, GBC.eol()); 174 JLabel warning = new JLabel(tr("Warning: The password is transferred unencrypted.")); 175 warning.setFont(warning.getFont().deriveFont(Font.ITALIC)); 176 p.add(warning, GBC.eop()); 177 178 JCheckBox savePassword = new JCheckBox(tr("Save user and password (unencrypted)"), !username.equals("") && !password.equals("")); 179 p.add(savePassword, GBC.eop()); 180 181 int choice = new ExtendedDialog(Main.parent, 182 tr("Enter Password"), 183 p, 184 new String[] {tr("Login"), tr("Cancel")}, 185 new String[] {"ok.png", "cancel.png"}).getValue(); 186 187 if (choice != 1) { 188 caller.authCancelled = true; 189 return null; 190 } 191 username = usernameField.getText(); 192 password = String.valueOf(passwordField.getPassword()); 193 if (savePassword.isSelected()) { 194 storeUsername(username); 195 storePassword(password); 196 } 197 if (username.equals("")) 198 return null; 199 } 200 caller.passwordtried = true; 201 return new PasswordAuthentication(username, password.toCharArray()); 202 } 203 public PreferenceAdditions newPreferenceAdditions() { 204 return new PreferenceAdditions() { 205 /** 206 * Editfield for the username to the OSM account. 207 */ 208 private JTextField osmDataUsername = new JTextField(20); 209 /** 210 * Passwordfield for the userpassword of the REST API. 211 */ 212 private JPasswordField osmDataPassword = new JPasswordField(20); 213 214 private String oldUsername = ""; 215 private String oldPassword = ""; 216 217 public void addPreferenceOptions(JPanel panel) { 218 try { 219 oldUsername = lookupUsername(); 220 } catch (CMException e) { 221 oldUsername = ""; 222 } 223 try { 224 oldPassword = lookupPassword(); 225 } catch (CMException e) { 226 oldPassword = ""; 227 } 228 osmDataUsername.setText(oldUsername); 229 osmDataPassword.setText(oldPassword); 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("OSM username (e-mail)")), GBC.std()); 233 panel.add(osmDataUsername, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); 234 panel.add(new JLabel(tr("OSM password")), GBC.std()); 235 panel.add(osmDataPassword, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,0)); 236 JLabel warning = new JLabel(tr("<html>" + 237 "WARNING: The password is stored in plain text in the preferences file.<br>" + 238 "The password is transferred in plain text to the server, encoded in the URL.<br>" + 239 "<b>Do not use a valuable Password.</b></html>")); 240 warning.setFont(warning.getFont().deriveFont(Font.ITALIC)); 241 panel.add(warning, GBC.eop().fill(GBC.HORIZONTAL)); 242 } 243 public void preferencesChanged() { 244 String newUsername = osmDataUsername.getText(); 245 String newPassword = String.valueOf(osmDataPassword.getPassword()); 246 if (!oldUsername.equals(newUsername)) 247 storeUsername(newUsername); 248 if (!oldPassword.equals(newPassword)) 249 storePassword(newPassword); 250 } 251 }; 252 } 253 } 157 254 }
Note:
See TracChangeset
for help on using the changeset viewer.