Changeset 12992 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2017-10-14T00:09:56+02:00 (7 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/io/CredentialDialog.java
r12854 r12992 23 23 import javax.swing.AbstractAction; 24 24 import javax.swing.BorderFactory; 25 import javax.swing.JButton; 25 26 import javax.swing.JCheckBox; 26 27 import javax.swing.JDialog; … … 30 31 31 32 import org.openstreetmap.josm.Main; 32 import org.openstreetmap.josm.gui.SideButton;33 33 import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction; 34 34 import org.openstreetmap.josm.gui.help.HelpUtil; … … 127 127 protected JPanel createButtonPanel() { 128 128 JPanel pnl = new JPanel(new FlowLayout()); 129 pnl.add(new SideButton(new OKAction()));130 pnl.add(new SideButton(new CancelAction()));131 pnl.add(new SideButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Dialog/Password"))));129 pnl.add(new JButton(new OKAction())); 130 pnl.add(new JButton(new CancelAction())); 131 pnl.add(new JButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Dialog/Password")))); 132 132 return pnl; 133 133 } … … 266 266 gc.weighty = 1.0; 267 267 add(new JPanel(), gc); 268 269 268 } 270 269 … … 375 374 public void focusGained(FocusEvent e) { 376 375 if (e.getSource() instanceof JTextField) { 377 JTextField tf = (JTextField) e.getSource(); 378 tf.selectAll(); 376 ((JTextField) e.getSource()).selectAll(); 379 377 } 380 378 } … … 410 408 return; 411 409 } else { 412 OKAction okAction = owner.new OKAction(); 413 okAction.actionPerformed(null); 410 owner.new OKAction().actionPerformed(null); 414 411 } 415 412 } … … 421 418 putValue(NAME, tr("Authenticate")); 422 419 putValue(SHORT_DESCRIPTION, tr("Authenticate with the supplied username and password")); 423 putValue(SMALL_ICON, ImageProvider.get("ok"));424 } 425 426 @Override 427 public void actionPerformed(ActionEvent arg0) {420 new ImageProvider("ok").getResource().attachImageIcon(this); 421 } 422 423 @Override 424 public void actionPerformed(ActionEvent e) { 428 425 setCanceled(false); 429 426 setVisible(false); … … 435 432 putValue(NAME, tr("Cancel")); 436 433 putValue(SHORT_DESCRIPTION, tr("Cancel authentication")); 437 putValue(SMALL_ICON, ImageProvider.get("cancel"));434 new ImageProvider("cancel").getResource().attachImageIcon(this); 438 435 } 439 436 … … 444 441 445 442 @Override 446 public void actionPerformed(ActionEvent arg0) {443 public void actionPerformed(ActionEvent e) { 447 444 cancel(); 448 445 } -
trunk/src/org/openstreetmap/josm/io/OsmApi.java
r12846 r12992 9 9 import java.io.StringReader; 10 10 import java.io.StringWriter; 11 import java.net.Authenticator.RequestorType; 11 12 import java.net.ConnectException; 12 13 import java.net.HttpURLConnection; … … 33 34 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 34 35 import org.openstreetmap.josm.io.Capabilities.CapabilitiesParser; 36 import org.openstreetmap.josm.io.auth.CredentialsManager; 35 37 import org.openstreetmap.josm.spi.preferences.Config; 36 38 import org.openstreetmap.josm.tools.CheckParameterUtil; … … 704 706 else 705 707 throw new OsmApiException(retCode, errorHeader, errorBody); 708 case HttpURLConnection.HTTP_UNAUTHORIZED: 706 709 case HttpURLConnection.HTTP_FORBIDDEN: 707 OsmApiException e = new OsmApiException(retCode, errorHeader, errorBody);708 e.setAccessedUrl(activeConnection.getURL().toString());709 throw e;710 CredentialsManager.getInstance().purgeCredentialsCache(RequestorType.SERVER); 711 throw new OsmApiException(retCode, errorHeader, errorBody, activeConnection.getURL().toString(), 712 doAuthenticate ? retrieveBasicAuthorizationLogin(client) : null); 710 713 default: 711 714 throw new OsmApiException(retCode, errorHeader, errorBody); -
trunk/src/org/openstreetmap/josm/io/OsmApiException.java
r12620 r12992 15 15 private String errorBody; 16 16 private String accessedUrl; 17 private String login; 17 18 18 19 /** … … 23 24 * @param errorBody The error body, as transmitted in the HTTP response body 24 25 * @param accessedUrl The complete URL accessed when this error occured 25 * @since 5584 26 */ 27 public OsmApiException(int responseCode, String errorHeader, String errorBody, String accessedUrl) { 26 * @param login the login used to connect to OSM API (can be null) 27 * @since 12992 28 */ 29 public OsmApiException(int responseCode, String errorHeader, String errorBody, String accessedUrl, String login) { 28 30 this.responseCode = responseCode; 29 31 this.errorHeader = errorHeader; 30 32 this.errorBody = errorBody; 31 33 this.accessedUrl = accessedUrl; 34 this.login = login; 35 } 36 37 /** 38 * Constructs an {@code OsmApiException} with the specified response code, error header and error body 39 * @param responseCode The HTTP response code replied by the OSM server. 40 * See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values 41 * @param errorHeader The error header, as transmitted in the {@code Error} field of the HTTP response header 42 * @param errorBody The error body, as transmitted in the HTTP response body 43 * @param accessedUrl The complete URL accessed when this error occured 44 * @since 5584 45 */ 46 public OsmApiException(int responseCode, String errorHeader, String errorBody, String accessedUrl) { 47 this(responseCode, errorHeader, errorBody, accessedUrl, null); 32 48 } 33 49 … … 199 215 return accessedUrl; 200 216 } 217 218 /** 219 * Sets the login used to connect to OSM API. 220 * @param login the login used to connect to OSM API 221 * @since 12992 222 */ 223 public void setLogin(String login) { 224 this.login = login; 225 } 226 227 /** 228 * Replies the login used to connect to OSM API. 229 * @return the login used to connect to OSM API, or {@code null} 230 * @since 12992 231 */ 232 public String getLogin() { 233 return login; 234 } 201 235 } -
trunk/src/org/openstreetmap/josm/io/OsmConnection.java
r12931 r12992 31 31 */ 32 32 public class OsmConnection { 33 34 private static final String BASIC_AUTH = "Basic "; 35 33 36 protected boolean cancel; 34 37 protected HttpClient activeConnection; … … 75 78 76 79 /** 80 * Retrieves login from basic authentication header, if set. 81 * 82 * @param con the connection 83 * @return login from basic authentication header, or {@code null} 84 * @throws OsmTransferException if something went wrong. Check for nested exceptions 85 * @since 12992 86 */ 87 protected String retrieveBasicAuthorizationLogin(HttpClient con) throws OsmTransferException { 88 String auth = con.getRequestHeader("Authorization"); 89 if (auth != null && auth.startsWith(BASIC_AUTH)) { 90 try { 91 String[] token = new String(Base64.getDecoder().decode(auth.substring(BASIC_AUTH.length())), 92 StandardCharsets.UTF_8).split(":"); 93 if (token.length == 2) { 94 return token[0]; 95 } 96 } catch (IllegalArgumentException e) { 97 Logging.error(e); 98 } 99 } 100 return null; 101 } 102 103 /** 77 104 * Adds an authentication header for basic authentication 78 105 * … … 98 125 String password = response.getPassword() == null ? "" : String.valueOf(response.getPassword()); 99 126 String token = username + ':' + password; 100 con.setHeader("Authorization", "Basic "+Base64.getEncoder().encodeToString(token.getBytes(StandardCharsets.UTF_8)));127 con.setHeader("Authorization", BASIC_AUTH + Base64.getEncoder().encodeToString(token.getBytes(StandardCharsets.UTF_8))); 101 128 } 102 129 } -
trunk/src/org/openstreetmap/josm/io/OsmServerReader.java
r12711 r12992 6 6 import java.io.IOException; 7 7 import java.io.InputStream; 8 import java.net.Authenticator.RequestorType; 8 9 import java.net.HttpURLConnection; 9 10 import java.net.MalformedURLException; … … 195 196 } 196 197 try { 197 if (response.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) 198 if (response.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) { 199 CredentialsManager.getInstance().purgeCredentialsCache(RequestorType.SERVER); 198 200 throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED, null, null); 201 } 199 202 200 203 if (response.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH) -
trunk/src/org/openstreetmap/josm/io/auth/AbstractCredentialsAgent.java
r12869 r12992 62 62 63 63 /* 64 * Last request was successful and there was no credentials stored 65 * in file (or only the username is stored). 66 * -> Try to recall credentials that have been entered 67 * manually in this session. 64 * Last request was successful and there was no credentials stored in file (or only the username is stored). 65 * -> Try to recall credentials that have been entered manually in this session. 68 66 */ 69 67 if (!noSuccessWithLastResponse && memoryCredentialsCache.containsKey(requestorType) && … … 89 87 response.getPassword() 90 88 )); 91 /*92 * User decides not to save credentials to file. Keep it93 * in memory so we don't have to ask over and over again.94 */95 89 } else { 96 PasswordAuthentication pa = new PasswordAuthentication(response.getUsername(), response.getPassword());97 memoryCredentialsCache.put(requestorType, pa);90 // User decides not to save credentials to file. Keep it in memory so we don't have to ask over and over again. 91 memoryCredentialsCache.put(requestorType, new PasswordAuthentication(response.getUsername(), response.getPassword())); 98 92 } 99 /*100 * We got it from file.101 */102 93 } else { 94 // We got it from file. 103 95 response.setUsername(username); 104 96 response.setPassword(password.toCharArray()); … … 106 98 } 107 99 return response; 100 } 101 102 @Override 103 public final void purgeCredentialsCache(RequestorType requestorType) { 104 memoryCredentialsCache.remove(requestorType); 108 105 } 109 106 -
trunk/src/org/openstreetmap/josm/io/auth/CredentialsAgent.java
r8929 r12992 74 74 75 75 /** 76 * Purges the internal credentials cache for the given requestor type. 77 * @param requestorType the type of service. 78 * {@link RequestorType#SERVER} for the OSM API server, {@link RequestorType#PROXY} for a proxy server 79 * @since 12992 80 */ 81 void purgeCredentialsCache(RequestorType requestorType); 82 83 /** 76 84 * Provide a Panel that is shown below the API password / username fields 77 85 * in the JOSM Preferences. (E.g. a warning that password is saved unencrypted.) -
trunk/src/org/openstreetmap/josm/io/auth/CredentialsManager.java
r12743 r12992 43 43 private static CredentialsAgentFactory agentFactory; 44 44 45 /** 46 * Credentials agent factory. 47 */ 45 48 @FunctionalInterface 46 49 public interface CredentialsAgentFactory { 50 /** 51 * Returns the credentials agent instance. 52 * @return the credentials agent instance 53 */ 47 54 CredentialsAgent getCredentialsAgent(); 48 55 } … … 148 155 return delegate.getPreferencesDecorationPanel(); 149 156 } 157 158 @Override 159 public void purgeCredentialsCache(RequestorType requestorType) { 160 delegate.purgeCredentialsCache(requestorType); 161 } 150 162 } -
trunk/src/org/openstreetmap/josm/tools/ExceptionUtil.java
r12686 r12992 266 266 + "Please check the username and the password in the JOSM preferences." 267 267 + "</html>", 268 CredentialsManager.getInstance().getUsername()268 e.getLogin() != null ? e.getLogin() : CredentialsManager.getInstance().getUsername() 269 269 ); 270 270 }
Note:
See TracChangeset
for help on using the changeset viewer.