Ticket #2710: initial_multiuser_rework_v3.patch
File initial_multiuser_rework_v3.patch, 56.1 KB (added by , 6 years ago) |
---|
-
src/org/openstreetmap/josm/data/UserIdentityManager.java
4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 6 import java.text.MessageFormat; 7 import java.util.ArrayList; 8 import java.util.LinkedHashMap; 9 import java.util.List; 10 import java.util.Map; 11 import java.util.TreeMap; 7 12 8 13 import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder; 9 14 import org.openstreetmap.josm.data.osm.User; … … 14 19 import org.openstreetmap.josm.io.OsmApi; 15 20 import org.openstreetmap.josm.io.OsmServerUserInfoReader; 16 21 import org.openstreetmap.josm.io.OsmTransferException; 22 import org.openstreetmap.josm.io.auth.CredentialsAgentResponse; 17 23 import org.openstreetmap.josm.io.auth.CredentialsManager; 18 24 import org.openstreetmap.josm.spi.preferences.Config; 19 25 import org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent; … … 78 84 instance.initFromPreferences(); 79 85 } 80 86 Config.getPref().addPreferenceChangeListener(instance); 87 instance.populateAllUsers(); 81 88 } 82 89 return instance; 83 90 } 84 91 85 private String userName;86 private UserInfo userInfo;87 92 private boolean accessTokenKeyChanged; 88 93 private boolean accessTokenSecretChanged; 89 94 95 private String userName; 96 private UserInfo userInfo; 97 private LinkedHashMap<String, UserInfo> users; 98 90 99 private UserIdentityManager() { 100 users = new LinkedHashMap<>(); 91 101 } 92 102 93 103 /** … … 112 122 if (trimmedUserName.isEmpty()) 113 123 throw new IllegalArgumentException( 114 124 MessageFormat.format("Expected non-empty value for parameter ''{0}'', got ''{1}''", "userName", userName)); 115 this.userName = trimmedUserName;125 userName = trimmedUserName; 116 126 userInfo = null; 117 127 } 118 128 … … 132 142 if (trimmedUserName.isEmpty()) 133 143 throw new IllegalArgumentException(tr("Expected non-empty value for parameter ''{0}'', got ''{1}''", "userName", userName)); 134 144 CheckParameterUtil.ensureParameterNotNull(userInfo, "userInfo"); 135 this.userName = trimmedUserName;145 userName = trimmedUserName; 136 146 this.userInfo = userInfo; 137 147 } 138 148 … … 238 248 } 239 249 240 250 /** 251 * Initializes the user identity manager from OAuth request of user details. 252 * @param oauth The {@code OAuthAccessTokenHolder} with the key and secret 253 * @see #initFromPreferences 254 * @since xxx 255 */ 256 public void initFromOauth(OAuthAccessTokenHolder oauth) { 257 try { 258 OsmServerUserInfoReader osmServerReader = new OsmServerUserInfoReader(); 259 UserInfo info = osmServerReader.fetchUserInfo(NullProgressMonitor.INSTANCE, oauth); 260 setFullyIdentified(info.getDisplayName(), info); 261 } catch (IllegalArgumentException | OsmTransferException e) { 262 Logging.error(e); 263 } 264 } 265 266 private List<Map<String, String>> getDefaultOAuthList() { 267 Map<String, String> variables = new TreeMap<>(); 268 TreeMap<String, String> vars = new TreeMap<>(); 269 vars.put("url", "osm-server.url"); 270 vars.put("login", "oauth.access-token.key"); 271 vars.put("password", "oauth.access-token.secret"); 272 vars.put("login-token-url", "oauth.settings.authorise-token-url"); 273 vars.put("auth-url", "oauth.settings.authorise-url"); 274 vars.put("consumer-key", "oauth.settings.consumer-key"); 275 vars.put("consumer-secret", "oauth.settings.consumer-secret"); 276 vars.put("login-url", "oauth.settings.osm-login-url"); 277 vars.put("logout-url", "oauth.settings.osm-logout-url"); 278 variables.put("auth-type", "oauth"); 279 280 vars.forEach((key, value) -> variables.put(key, Config.getPref().get(value))); 281 List<Map<String, String>> rList = new ArrayList<>(); 282 rList.add(variables); 283 return rList; 284 } 285 286 private List<Map<String, String>> getDefaultBasicAuthList() { 287 Map<String, String> variables = new TreeMap<>(); 288 TreeMap<String, String> vars = new TreeMap<>(); 289 290 vars.put("url", "osm-server-url"); 291 vars.put("login", "osm-server.username"); 292 vars.put("password", "osm-server.password"); 293 294 variables.put("auth-type", "basic"); 295 vars.forEach((key, value) -> variables.put(key, Config.getPref().get(value))); 296 List<Map<String, String>> rList = new ArrayList<>(); 297 return rList; 298 } 299 300 private List<Map<String, String>> getDefaultAuthList() { 301 List<Map<String, String>> rMap = getDefaultOAuthList(); 302 rMap.addAll(getDefaultBasicAuthList()); 303 return rMap; 304 } 305 306 /** 307 * Populate the users 308 * @since xxx 309 */ 310 public void populateAllUsers() { 311 List<Map<String, String>> authList = Config.getPref().getListOfMaps("all-authorizations", getDefaultAuthList()); 312 for (Map<String, String> map : authList) { // TODO fix 313 if ("oauth".equals(map.get("auth-type"))) { 314 OAuthAccessTokenHolder oauth = new OAuthAccessTokenHolder(); 315 oauth.setAccessToken(map.get("login"), map.get("password")); 316 oauth.setOsmApi(map.get("url")); 317 instance.initFromOauth(oauth); 318 users.put(getUserName(), getUserInfo()); 319 } else if ("basic".equals(map.get("auth-type"))) { 320 CredentialsAgentResponse response = new CredentialsAgentResponse(); 321 response.setUsername(map.get("login")); 322 response.setPassword(map.get("password").toCharArray()); 323 } 324 } 325 326 if (OsmApi.isUsingOAuth() && OAuthAccessTokenHolder.getInstance().containsAccessToken() && 327 !NetworkManager.isOffline(OnlineResource.OSM_API)) { 328 try { 329 instance.initFromOAuth(); 330 } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) { 331 Logging.error(e); 332 // Fall back to preferences if OAuth identification fails for any reason 333 instance.initFromPreferences(); 334 } 335 } else { 336 instance.initFromPreferences(); 337 } 338 } 339 340 /** 241 341 * Replies true if the user with name <code>username</code> is the current user 242 342 * 243 343 * @param userName the user name … … 264 364 } 265 365 } 266 366 367 /** 368 * Get all information on all users that have logged in to JOSM 369 * @return A {@code HashMap} with username/UserInfo pairs. 370 * @since xxx 371 */ 372 public Map<String, UserInfo> getAllUserInformation() { 373 if (users == null) { 374 populateAllUsers(); 375 } 376 return users; 377 } 378 379 /** 380 * Get user authentication information 381 * @return The user information (passwords are protected) 382 * @since xxx 383 */ 384 public List<Map<String, String>> getUserAuthInformation() { 385 List<Map<String, String>> authList = Config.getPref().getListOfMaps("all-authorizations", getDefaultAuthList()); 386 for (Map<String, String> map : authList) { 387 if ("basic".equals(map.get("auth-type"))) { 388 // TODO protect passwords, even though they are stored in plain text 389 // map.put("password", "PROTECTED"); 390 } 391 } 392 return authList; 393 } 394 395 /** 396 * Save authentication information 397 * @param users To save (any user NOT in this map WILL be deleted) 398 */ 399 public void saveUserAuthInformation(List<Map<String, String>> users) { 400 Config.getPref().putListOfMaps("all-authorizations", users); 401 } 402 267 403 /* ------------------------------------------------------------------- */ 268 404 /* interface PreferenceChangeListener */ 269 405 /* ------------------------------------------------------------------- */ -
src/org/openstreetmap/josm/data/oauth/OAuthAccessTokenHolder.java
3 3 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.util.List; 7 import java.util.Map; 8 import java.util.TreeMap; 9 10 import org.openstreetmap.josm.data.UserIdentityManager; 11 import org.openstreetmap.josm.io.OsmApi; 6 12 import org.openstreetmap.josm.io.auth.CredentialsAgent; 7 13 import org.openstreetmap.josm.io.auth.CredentialsAgentException; 8 14 import org.openstreetmap.josm.spi.preferences.Config; … … 16 22 public class OAuthAccessTokenHolder { 17 23 private static OAuthAccessTokenHolder instance; 18 24 25 private OsmApi osmApi = OsmApi.getOsmApi(); 26 19 27 /** 20 28 * Replies the unique instance. 21 29 * @return The unique instance of {@code OAuthAccessTokenHolder} … … 183 191 Logging.warn(tr("Failed to store OAuth Access Token to credentials manager")); 184 192 Logging.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName())); 185 193 } 194 List<Map<String, String>> users = UserIdentityManager.getInstance().getUserAuthInformation(); 195 boolean present = false; 196 for (Map<String, String> user : users) { 197 if ("oauth".equals(user.get("auth-type")) && accessTokenKey.equals(user.get("auth-type"))) { 198 user.put("password", accessTokenSecret); 199 user.put("url", osmApi.getHost()); 200 present = true; 201 break; 202 } 203 } 204 if (!present) { 205 Map<String, String> map = new TreeMap<>(); 206 map.put("login", accessTokenKey); 207 map.put("password", accessTokenSecret); 208 map.put("url", osmApi.getHost()); 209 } 186 210 } 187 211 188 212 /** 213 * Set the API to use with the user 214 * @param serverUrl The URL for the OSM server 215 * @since xxx 216 */ 217 public void setOsmApi(String serverUrl) { 218 osmApi = OsmApi.getOsmApi(serverUrl); 219 } 220 221 /** 222 * Get the osmApi for use with this oauth object 223 * @return The OsmApi to use 224 */ 225 public OsmApi getOsmApi() { 226 return osmApi; 227 } 228 229 /** 189 230 * Clears the content of this holder 190 231 */ 191 232 public void clear() { -
src/org/openstreetmap/josm/gui/io/UploadParameterSummaryPanel.java
5 5 import static org.openstreetmap.josm.tools.I18n.trn; 6 6 7 7 import java.awt.BorderLayout; 8 import java.awt.event.ItemEvent; 9 import java.awt.event.ItemListener; 8 10 import java.beans.PropertyChangeEvent; 9 11 import java.beans.PropertyChangeListener; 12 import java.util.Map; 10 13 import java.util.Optional; 11 14 12 15 import javax.swing.BorderFactory; 16 import javax.swing.DefaultComboBoxModel; 17 import javax.swing.JComboBox; 13 18 import javax.swing.JLabel; 14 19 import javax.swing.JPanel; 15 20 import javax.swing.event.HyperlinkEvent; 16 21 import javax.swing.event.HyperlinkListener; 17 22 23 import org.openstreetmap.josm.data.UserIdentityManager; 18 24 import org.openstreetmap.josm.data.osm.Changeset; 25 import org.openstreetmap.josm.data.osm.UserInfo; 19 26 import org.openstreetmap.josm.gui.widgets.JMultilineLabel; 20 27 import org.openstreetmap.josm.io.Capabilities; 21 28 import org.openstreetmap.josm.io.OsmApi; … … 22 29 import org.openstreetmap.josm.io.UploadStrategySpecification; 23 30 import org.openstreetmap.josm.spi.preferences.Config; 24 31 import org.openstreetmap.josm.tools.ImageProvider; 32 import org.openstreetmap.josm.tools.Logging; 25 33 26 34 /** 27 35 * A panel that displays a summary of data the user is about to upload … … 114 122 return msg; 115 123 } 116 124 125 protected JComboBox<String> buildPossibleUserBox() { 126 DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(); 127 Map<String, UserInfo> info = UserIdentityManager.getInstance().getAllUserInformation(); 128 info.forEach((userName, userInfo) -> model.addElement(userName)); 129 JComboBox<String> rBox = new JComboBox<>(model); 130 UserInfo user = UserIdentityManager.getInstance().getUserInfo(); 131 String userName = user.getDisplayName() != null ? user.getDisplayName() : tr("Please login"); 132 if (model.getIndexOf(userName) < 0) model.addElement(userName); 133 model.setSelectedItem(userName); 134 rBox.addItemListener(new ItemListener() { 135 @Override 136 public void itemStateChanged(ItemEvent e) { 137 if (e.getStateChange() == ItemEvent.SELECTED) { 138 Logging.info("{0} was selected, switching to {1}", userName, e.getItem()); 139 model.setSelectedItem(e.getItem()); 140 } 141 } 142 }); 143 return rBox; 144 } 145 117 146 protected void build() { 118 147 jepMessage = new JMultilineLabel(""); 119 148 jepMessage.addHyperlinkListener(this); … … 128 157 JPanel pnl = new JPanel(new BorderLayout()); 129 158 pnl.add(lblWarning, BorderLayout.NORTH); 130 159 add(pnl, BorderLayout.WEST); 160 JComboBox<String> options = buildPossibleUserBox(); 161 if (options.getItemCount() > 1) add(buildPossibleUserBox(), BorderLayout.SOUTH); 131 162 } 132 163 133 164 public void setConfigurationParameterRequestListener(ConfigurationParameterRequestHandler handler) { -
src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java
3 3 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt. BorderLayout;7 import java.awt. GridBagConstraints;6 import java.awt.Component; 7 import java.awt.Dimension; 8 8 import java.awt.GridBagLayout; 9 import java.awt.Insets; 10 import java.awt.event.ItemEvent; 11 import java.awt.event.ItemListener; 9 import java.awt.event.ActionEvent; 12 10 import java.beans.PropertyChangeEvent; 13 11 import java.beans.PropertyChangeListener; 12 import java.util.ArrayList; 13 import java.util.List; 14 import java.util.Map; 14 15 15 import javax.swing.ButtonGroup; 16 import javax.swing.AbstractAction; 17 import javax.swing.JButton; 16 18 import javax.swing.JPanel; 17 import javax.swing.JRadioButton;18 19 import javax.swing.JSeparator; 19 20 20 import org.openstreetmap.josm.data. oauth.OAuthAccessTokenHolder;21 import org.openstreetmap.josm.data.UserIdentityManager; 21 22 import org.openstreetmap.josm.gui.help.HelpUtil; 22 23 import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel; 23 24 import org.openstreetmap.josm.io.OsmApi; 24 import org.openstreetmap.josm.io.auth.CredentialsManager;25 25 import org.openstreetmap.josm.spi.preferences.Config; 26 import org.openstreetmap.josm.tools.GBC; 26 27 import org.openstreetmap.josm.tools.Logging; 27 28 28 29 /** … … 31 32 */ 32 33 public class AuthenticationPreferencesPanel extends VerticallyScrollablePanel implements PropertyChangeListener { 33 34 34 /** indicates whether we use basic authentication*/35 private final J RadioButton rbBasicAuthentication = new JRadioButton();36 /** indicates whether we use OAuth as authentication scheme*/37 private final J RadioButton rbOAuth = new JRadioButton();35 /** add a basic authentication method */ 36 private final JButton rbBasicAuthentication = new JButton(tr("Add Basic Authentication")); 37 /** add a OAuth method */ 38 private final JButton rbOAuth = new JButton(tr("Add OAuth")); 38 39 /** the panel which contains the authentication parameters for the respective authentication scheme */ 39 private final JPanel pnlAuthenticationParameteters = new JPanel( new BorderLayout());40 /** the panel for the basic authentication parameters */41 private BasicAuthenticationPreferencesPanelpnlBasicAuthPreferences;42 /** the panel for the OAuth authentication parameters */43 private OAuthAuthenticationPreferencesPanelpnlOAuthPreferences;40 private final JPanel pnlAuthenticationParameteters = new JPanel(); 41 /** the panels for the basic authentication parameters */ 42 private List<BasicAuthenticationPreferencesPanel> pnlBasicAuthPreferences; 43 /** the panels for the OAuth authentication parameters */ 44 private List<OAuthAuthenticationPreferencesPanel> pnlOAuthPreferences; 44 45 /** the panel for messages notifier preferences */ 45 46 private FeaturesPanel pnlFeaturesPreferences; 46 47 48 /** The authentication preference key */ 49 public static final String AUTH_TYPE_KEY = "auth-type"; 50 /** The value in {@code AUTH_TYPE_KEY}'s pref for OAuth */ 51 public static final String AUTH_OAUTH = "oauth"; 52 /** The value in {@code AUTH_TYPE_KEY}'s pref for basic authentication */ 53 public static final String AUTH_BASIC = "basic"; 54 47 55 /** 48 56 * Constructs a new {@code AuthenticationPreferencesPanel}. 49 57 */ … … 58 66 */ 59 67 protected final void build() { 60 68 setLayout(new GridBagLayout()); 61 GridBagConstraints gc = new GridBagConstraints();62 69 63 AuthenticationMethodChangeListener authChangeListener = new AuthenticationMethodChangeListener(); 70 //-- the panel for API feature preferences 71 pnlFeaturesPreferences = new FeaturesPanel(); 72 add(pnlFeaturesPreferences, GBC.eol()); 64 73 65 74 // -- radio button for basic authentication 66 gc.anchor = GridBagConstraints.NORTHWEST; 67 gc.fill = GridBagConstraints.HORIZONTAL; 68 gc.gridx = 1; 69 gc.weightx = 1.0; 70 gc.insets = new Insets(0, 0, 0, 3); 71 add(rbBasicAuthentication, gc); 72 rbBasicAuthentication.setText(tr("Use Basic Authentication")); 73 rbBasicAuthentication.setToolTipText(tr("Select to use HTTP basic authentication with your OSM username and password")); 74 rbBasicAuthentication.addItemListener(authChangeListener); 75 rbBasicAuthentication.setAction(new AbstractAction() { 76 @Override 77 public void actionPerformed(ActionEvent e) { 78 BasicAuthenticationPreferencesPanel panel = new BasicAuthenticationPreferencesPanel(); 79 panel.initFromPreferences(); 80 if (isPresent(panel)) { 81 panel.setUserName(""); 82 } 83 if (!isPresent(panel)) { 84 redrawAuthenticationPanel(panel); 85 } 86 } 87 }); 88 rbBasicAuthentication.setText(tr("Add Basic Authentication")); 89 rbBasicAuthentication.setToolTipText(tr("Select to add HTTP basic authentication with your OSM username and password")); 75 90 76 91 //-- radio button for OAuth 77 gc.gridx = 0; 78 gc.weightx = 0.0; 79 add(rbOAuth, gc); 80 rbOAuth.setText(tr("Use OAuth")); 81 rbOAuth.setToolTipText(tr("Select to use OAuth as authentication mechanism")); 82 rbOAuth.addItemListener(authChangeListener); 92 rbOAuth.setAction(new AbstractAction() { 93 @Override 94 public void actionPerformed(ActionEvent e) { 95 OAuthAuthenticationPreferencesPanel panel = new OAuthAuthenticationPreferencesPanel(); 96 panel.initFromPreferences(); 97 redrawAuthenticationPanel(panel); 98 } 99 }); 100 rbOAuth.setText(tr("Add OAuth")); 101 rbOAuth.setToolTipText(tr("Select to add a OAuth authentication mechanism")); 83 102 84 //-- radio button for OAuth 85 ButtonGroup bg = new ButtonGroup(); 86 bg.add(rbBasicAuthentication); 87 bg.add(rbOAuth); 103 add(new JSeparator(), GBC.eol()); 88 104 89 105 //-- add the panel which will hold the authentication parameters 90 gc.gridx = 0;91 gc.gridy = 1;92 gc.gridwidth = 2;93 gc.fill = GridBagConstraints.BOTH;94 gc.weightx = 1.0;95 gc.weighty = 1.0;96 add(pnlAuthenticationParameteters, gc);97 106 107 add(pnlAuthenticationParameteters, GBC.eol()); 108 add(rbBasicAuthentication); 109 add(rbOAuth, GBC.eol()); 110 98 111 //-- the two panels for authentication parameters 99 pnlBasicAuthPreferences = new BasicAuthenticationPreferencesPanel();100 pnlOAuthPreferences = new OAuthAuthenticationPreferencesPanel();112 pnlBasicAuthPreferences = new ArrayList<>(); 113 pnlOAuthPreferences = new ArrayList<>(); 101 114 102 rbBasicAuthentication.setSelected(true);103 pnlAuthenticationParameteters.add(pnlBasicAuthPreferences, BorderLayout.CENTER);115 populateAuthPanels(); 116 } 104 117 105 gc.gridy = 2; 106 add(new JSeparator(), gc); 118 private void populateAuthPanels() { 119 List<Map<String, String>> users = UserIdentityManager.getInstance().getUserAuthInformation(); 120 for (Map<String, String> user : users) { 121 if (AUTH_OAUTH.equals(user.get(AUTH_TYPE_KEY))) { 122 OAuthAuthenticationPreferencesPanel panel = new OAuthAuthenticationPreferencesPanel(); 123 panel.initFromMap(user); 124 redrawAuthenticationPanel(panel); 125 } else if (AUTH_BASIC.equals(user.get(AUTH_TYPE_KEY))) { 126 BasicAuthenticationPreferencesPanel panel = new BasicAuthenticationPreferencesPanel(); 127 panel.initFromMap(user); 128 redrawAuthenticationPanel(panel); 129 } else if (user.get(AUTH_TYPE_KEY) != null) { 130 Logging.info("Unknown authentication type: {0}", user.get(AUTH_TYPE_KEY)); 131 } else { 132 Logging.error("There was no authentication type"); 133 } 134 } 135 } 107 136 108 //-- the panel for API feature preferences 109 gc.gridy = 3; 110 gc.fill = GridBagConstraints.NONE; 111 pnlFeaturesPreferences = new FeaturesPanel(); 112 add(pnlFeaturesPreferences, gc); 137 private void redrawAuthenticationPanel(JPanel panel) { 138 if (panel == null) { 139 recalculateAuthenticationParametersSize(); 140 pnlAuthenticationParameteters.revalidate(); 141 return; 142 } 143 List<Component> components = new ArrayList<>(); 144 JButton remove = new JButton(tr("Remove")); 145 components.add(panel); 146 components.add(new JSeparator()); 147 remove.setAction(new AbstractAction() { 148 @Override 149 public void actionPerformed(ActionEvent e) { 150 for (Component component : components) { 151 if (pnlOAuthPreferences.contains(component)) { 152 pnlOAuthPreferences.remove(component); 153 } 154 if (pnlBasicAuthPreferences.contains(component)) { 155 pnlBasicAuthPreferences.remove(component); 156 } 157 pnlAuthenticationParameteters.remove(component); 158 redrawAuthenticationPanel(null); 159 } 160 } 161 }); 162 remove.setText(tr("Remove")); 163 components.add(remove); 164 for (Component component : components) { 165 pnlAuthenticationParameteters.add(component, GBC.eol()); 166 } 167 if (panel instanceof BasicAuthenticationPreferencesPanel) { 168 pnlBasicAuthPreferences.add((BasicAuthenticationPreferencesPanel) panel); 169 } else if (panel instanceof OAuthAuthenticationPreferencesPanel) { 170 pnlOAuthPreferences.add((OAuthAuthenticationPreferencesPanel) panel); 171 } 172 recalculateAuthenticationParametersSize(); 173 pnlAuthenticationParameteters.revalidate(); 113 174 } 114 175 176 private void recalculateAuthenticationParametersSize() { 177 Dimension d = new Dimension(); 178 for (Component component : pnlAuthenticationParameteters.getComponents()) { 179 Dimension dComponent = component.getPreferredSize(); 180 d.setSize(Math.max(d.getWidth(), dComponent.getWidth()), d.getHeight() + dComponent.getHeight()); 181 } 182 pnlAuthenticationParameteters.setPreferredSize(d); 183 } 184 185 private boolean isPresent(Component component) { 186 List<Component> components = new ArrayList<>(); 187 for (Component tcomponent : pnlAuthenticationParameteters.getComponents()) { 188 components.add(tcomponent); 189 } 190 if (component instanceof BasicAuthenticationPreferencesPanel) { 191 BasicAuthenticationPreferencesPanel realComponent = (BasicAuthenticationPreferencesPanel) component; 192 for (BasicAuthenticationPreferencesPanel check : pnlBasicAuthPreferences) { 193 if (check.equals(realComponent)) return true; 194 } 195 } else if (component instanceof OAuthAuthenticationPreferencesPanel) { 196 OAuthAuthenticationPreferencesPanel realComponent = (OAuthAuthenticationPreferencesPanel) component; 197 for (OAuthAuthenticationPreferencesPanel check : pnlOAuthPreferences) { 198 if (check.equals(realComponent)) return true; 199 } 200 } 201 return false; 202 } 203 115 204 /** 116 205 * Initializes the panel from preferences 117 206 */ 118 207 public final void initFromPreferences() { 119 208 final String authMethod = OsmApi.getAuthMethod(); 120 if ( "basic".equals(authMethod)) {209 if (AUTH_BASIC.equals(authMethod)) { 121 210 rbBasicAuthentication.setSelected(true); 122 } else if ( "oauth".equals(authMethod)) {211 } else if (AUTH_OAUTH.equals(authMethod)) { 123 212 rbOAuth.setSelected(true); 124 213 } else { 125 214 Logging.warn(tr("Unsupported value in preference ''{0}'', got ''{1}''. Using authentication method ''Basic Authentication''.", … … 126 215 "osm-server.auth-method", authMethod)); 127 216 rbBasicAuthentication.setSelected(true); 128 217 } 129 pnlBasicAuthPreferences.initFromPreferences(); 130 pnlOAuthPreferences.initFromPreferences(); 218 List<Map<String, String>> users = UserIdentityManager.getInstance().getUserAuthInformation(); 219 for (Map<String, String> user : users) { 220 if (AUTH_OAUTH.equals(user.get(AUTH_TYPE_KEY))) { 221 OAuthAuthenticationPreferencesPanel panel = new OAuthAuthenticationPreferencesPanel(); 222 panel.initFromMap(user); 223 pnlOAuthPreferences.add(panel); 224 } else if (AUTH_BASIC.equals(user.get(AUTH_TYPE_KEY))) { 225 BasicAuthenticationPreferencesPanel panel = new BasicAuthenticationPreferencesPanel(); 226 panel.initFromMap(user); 227 pnlBasicAuthPreferences.add(panel); 228 } 229 } 131 230 pnlFeaturesPreferences.initFromPreferences(); 132 231 } 133 232 … … 137 236 public final void saveToPreferences() { 138 237 // save the authentication method 139 238 String authMethod; 140 if ( rbBasicAuthentication.isSelected()) {141 authMethod = "basic";239 if (pnlOAuthPreferences.isEmpty()) { 240 authMethod = AUTH_OAUTH; 142 241 } else { 143 authMethod = "oauth";242 authMethod = AUTH_BASIC; 144 243 } 145 244 Config.getPref().put("osm-server.auth-method", authMethod); 146 if ("basic".equals(authMethod)) { 147 // save username and password and clear the OAuth token 148 pnlBasicAuthPreferences.saveToPreferences(); 149 OAuthAccessTokenHolder.getInstance().clear(); 150 OAuthAccessTokenHolder.getInstance().save(CredentialsManager.getInstance()); 151 } else if ("oauth".equals(authMethod)) { 152 // clear the password in the preferences 153 pnlBasicAuthPreferences.clearPassword(); 154 pnlBasicAuthPreferences.saveToPreferences(); 155 pnlOAuthPreferences.saveToPreferences(); 245 for (BasicAuthenticationPreferencesPanel panel : pnlBasicAuthPreferences) { 246 panel.saveToPreferences(); 156 247 } 248 for (OAuthAuthenticationPreferencesPanel panel : pnlOAuthPreferences) { 249 panel.saveToPreferences(); 250 } 157 251 // save message notifications preferences. To be done after authentication preferences. 158 252 pnlFeaturesPreferences.saveToPreferences(); 159 253 } 160 254 161 /**162 * Listens to changes in the authentication method163 */164 class AuthenticationMethodChangeListener implements ItemListener {165 @Override166 public void itemStateChanged(ItemEvent e) {167 if (rbBasicAuthentication.isSelected()) {168 pnlAuthenticationParameteters.removeAll();169 pnlAuthenticationParameteters.add(pnlBasicAuthPreferences, BorderLayout.CENTER);170 pnlBasicAuthPreferences.revalidate();171 } else {172 pnlAuthenticationParameteters.removeAll();173 pnlAuthenticationParameteters.add(pnlOAuthPreferences, BorderLayout.CENTER);174 pnlOAuthPreferences.revalidate();175 }176 repaint();177 }178 }179 180 255 @Override 181 256 public void propertyChange(PropertyChangeEvent evt) { 182 if (pnlOAuthPreferences != null) { 183 pnlOAuthPreferences.propertyChange(evt); 257 /** TODO is this still necessary? */ 258 if (pnlOAuthPreferences != null && !pnlOAuthPreferences.isEmpty()) { 259 for (OAuthAuthenticationPreferencesPanel panel : pnlOAuthPreferences) { 260 panel.propertyChange(evt); 261 } 184 262 } 185 263 } 186 264 } -
src/org/openstreetmap/josm/gui/preferences/server/BasicAuthenticationPreferencesPanel.java
9 9 import java.awt.Insets; 10 10 import java.net.Authenticator.RequestorType; 11 11 import java.net.PasswordAuthentication; 12 import java.util.Arrays; 13 import java.util.List; 14 import java.util.Map; 15 import java.util.TreeMap; 12 16 13 17 import javax.swing.BorderFactory; 14 18 import javax.swing.JLabel; 15 19 import javax.swing.JPanel; 16 20 21 import org.openstreetmap.josm.data.UserIdentityManager; 17 22 import org.openstreetmap.josm.gui.widgets.JosmPasswordField; 18 23 import org.openstreetmap.josm.gui.widgets.JosmTextField; 19 24 import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator; … … 33 38 private final JosmTextField tfOsmUserName = new JosmTextField(); 34 39 /** the OSM password */ 35 40 private final JosmPasswordField tfOsmPassword = new JosmPasswordField(); 41 /** The password key in the user map */ 42 public static final String KEY_PASSWORD = "password"; 43 /** The username key in the user map */ 44 public static final String KEY_USER = "login"; 36 45 /** a panel with further information, e.g. some warnings */ 37 46 private JPanel decorationPanel; 38 47 … … 93 102 * Inits contents from preferences. 94 103 */ 95 104 public void initFromPreferences() { 96 CredentialsAgent cm = CredentialsManager.getInstance(); 97 try { 98 decorationPanel.removeAll(); 99 decorationPanel.add(cm.getPreferencesDecorationPanel(), BorderLayout.CENTER); 100 PasswordAuthentication pa = cm.lookup(RequestorType.SERVER, OsmApi.getOsmApi().getHost()); 101 if (pa == null) { 102 tfOsmUserName.setText(""); 103 tfOsmPassword.setText(""); 104 } else { 105 tfOsmUserName.setText(pa.getUserName() == null ? "" : pa.getUserName()); 106 tfOsmPassword.setText(pa.getPassword() == null ? "" : String.valueOf(pa.getPassword())); 105 initFromMap(null); 106 } 107 108 /** 109 * Initializes contents from a map 110 * @param user The map with the login/password 111 */ 112 public void initFromMap(Map<String, String> user) { 113 if (user == null) { 114 user = new TreeMap<>(); 115 CredentialsAgent cm = CredentialsManager.getInstance(); 116 try { 117 decorationPanel.removeAll(); 118 decorationPanel.add(cm.getPreferencesDecorationPanel(), BorderLayout.CENTER); 119 PasswordAuthentication pa = cm.lookup(RequestorType.SERVER, OsmApi.getOsmApi().getHost()); 120 if (pa == null) { 121 user.put(KEY_USER, ""); 122 user.put(KEY_PASSWORD, ""); 123 } else { 124 user.put("login", pa.getUserName() == null ? "" : pa.getUserName()); 125 user.put("password", pa.getPassword() == null ? "" : String.valueOf(pa.getPassword())); 126 } 127 } catch (CredentialsAgentException e) { 128 Logging.error(e); 129 Logging.warn(tr("Failed to retrieve OSM credentials from credential manager.")); 130 Logging.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName())); 131 user.put(KEY_USER, ""); 132 user.put(KEY_PASSWORD, ""); 107 133 } 108 } catch (CredentialsAgentException e) {109 Logging.error(e);110 Logging.warn(tr("Failed to retrieve OSM credentials from credential manager."));111 Logging.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));112 tfOsmUserName.setText("");113 tfOsmPassword.setText("");114 134 } 135 136 tfOsmUserName.setText(user.get(KEY_USER) != null ? user.get(KEY_USER) : ""); 137 tfOsmPassword.setText(user.get(KEY_PASSWORD) != null ? user.get(KEY_PASSWORD) : ""); 115 138 } 116 139 117 140 /** … … 130 153 Logging.warn(tr("Failed to save OSM credentials to credential manager.")); 131 154 Logging.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName())); 132 155 } 156 List<Map<String, String>> users = UserIdentityManager.getInstance().getUserAuthInformation(); 157 boolean present = false; 158 for (Map<String, String> user : users) { 159 if (AuthenticationPreferencesPanel.AUTH_BASIC.equals(user.get(AuthenticationPreferencesPanel.AUTH_TYPE_KEY)) 160 && tfOsmUserName.getText().trim().equals(user.get(KEY_USER))) { 161 user.put(KEY_PASSWORD, Arrays.toString(tfOsmPassword.getPassword())); 162 user.put("url", OsmApi.getOsmApi().getHost()); 163 present = true; 164 break; 165 } 166 } 167 if (!present) { 168 Map<String, String> map = new TreeMap<>(); 169 map.put(KEY_USER, tfOsmUserName.getText().trim()); 170 map.put(KEY_PASSWORD, tfOsmPassword.getParent().toString()); 171 map.put("url", OsmApi.getOsmApi().getHost()); 172 users.add(map); 173 UserIdentityManager.getInstance().saveUserAuthInformation(users); 174 } 133 175 } 134 176 135 177 /** … … 138 180 public void clearPassword() { 139 181 tfOsmPassword.setText(""); 140 182 } 183 184 @Override 185 public boolean equals(Object obj) { 186 if (!(obj instanceof BasicAuthenticationPreferencesPanel)) return false; 187 BasicAuthenticationPreferencesPanel testPanel = (BasicAuthenticationPreferencesPanel) obj; 188 if (testPanel.tfOsmUserName.getText().equals(tfOsmUserName.getText())) return true; 189 return false; 190 } 191 192 @Override 193 public int hashCode() { 194 return super.hashCode(); 195 } 196 197 /** 198 * Get the username in the field 199 * @return The username for this BasicAuthenticationPreference 200 * @since xxx 201 */ 202 public String getUserName() { 203 return tfOsmUserName.getText(); 204 } 205 206 /** 207 * Set the username in the field 208 * @param userName for this BasicAuthenticationPreference 209 * @since xxx 210 */ 211 public void setUserName(String userName) { 212 tfOsmUserName.setText(userName); 213 } 141 214 } -
src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java
14 14 import java.awt.event.ItemEvent; 15 15 import java.beans.PropertyChangeEvent; 16 16 import java.beans.PropertyChangeListener; 17 import java.util.Map; 18 import java.util.TreeMap; 17 19 18 20 import javax.swing.AbstractAction; 19 21 import javax.swing.BorderFactory; … … 51 53 private final JPanel pnlAuthorisationMessage = new JPanel(new BorderLayout()); 52 54 private final NotYetAuthorisedPanel pnlNotYetAuthorised = new NotYetAuthorisedPanel(); 53 55 private final AdvancedOAuthPropertiesPanel pnlAdvancedProperties = new AdvancedOAuthPropertiesPanel(); 54 private final AlreadyAuthorisedPanel pnlAlreadyAuthorised = new AlreadyAuthorisedPanel( );56 private final AlreadyAuthorisedPanel pnlAlreadyAuthorised = new AlreadyAuthorisedPanel(null); 55 57 private String apiUrl; 56 58 57 59 /** … … 125 127 } 126 128 127 129 protected void refreshView() { 130 Map<String, String> map = new TreeMap<>(); 131 if (OAuthAccessTokenHolder.getInstance().containsAccessToken()) { 132 map.put("login", OAuthAccessTokenHolder.getInstance().getAccessTokenKey()); 133 map.put("password", OAuthAccessTokenHolder.getInstance().getAccessTokenSecret()); 134 } 135 refreshView(map); 136 } 137 138 protected void refreshView(Map<String, String> user) { 128 139 pnlAuthorisationMessage.removeAll(); 129 if ( OAuthAccessTokenHolder.getInstance().containsAccessToken()) {140 if (user.containsKey("login") && user.containsKey("password")) { 130 141 pnlAuthorisationMessage.add(pnlAlreadyAuthorised, BorderLayout.CENTER); 131 pnlAlreadyAuthorised.refreshView( );142 pnlAlreadyAuthorised.refreshView(user); 132 143 pnlAlreadyAuthorised.revalidate(); 133 144 } else { 134 145 pnlAuthorisationMessage.add(pnlNotYetAuthorised, BorderLayout.CENTER); … … 156 167 } 157 168 158 169 /** 170 * Initializes the panel from a map 171 * @param user The map with a {@code login} and {@code password}. 172 * Optional information includes {@code url}. 173 */ 174 public void initFromMap(Map<String, String> user) { 175 if (user.containsKey("url")) { 176 setApiUrl(user.get("url").trim()); 177 } else { 178 setApiUrl(OsmApi.getOsmApi().getServerUrl().trim()); 179 } 180 refreshView(user); 181 } 182 183 /** 159 184 * Saves the current values to preferences 160 185 */ 161 186 public void saveToPreferences() { … … 215 240 private final JosmTextField tfAccessTokenSecret = new JosmTextField(null, null, 0, false); 216 241 217 242 /** 218 * Constructs a new {@code AlreadyAuthorisedPanel}. 243 * Constructs a new {@code AlreadyAUthorisedPanel}. 244 * @param map The map with the authentication information 219 245 */ 220 AlreadyAuthorisedPanel() { 246 AlreadyAuthorisedPanel(Map<String, String> map) { 247 if (map == null) { 248 map = new TreeMap<>(); 249 map.put("login", OAuthAccessTokenHolder.getInstance().getAccessTokenKey()); 250 map.put("password", OAuthAccessTokenHolder.getInstance().getAccessTokenSecret()); 251 } 252 tfAccessTokenKey.setText(map.get("login")); 253 tfAccessTokenSecret.setText(map.get("password")); 221 254 build(); 222 refreshView( );255 refreshView(map); 223 256 } 224 257 225 258 protected void build() { … … 291 324 add(new JPanel(), gc); 292 325 } 293 326 294 protected final void refreshView( ) {295 String v = OAuthAccessTokenHolder.getInstance().getAccessTokenKey();327 protected final void refreshView(Map<String, String> user) { 328 String v = user.get("login"); 296 329 tfAccessTokenKey.setText(v == null ? "" : v); 297 v = OAuthAccessTokenHolder.getInstance().getAccessTokenSecret();330 v = user.get("password"); 298 331 tfAccessTokenSecret.setText(v == null ? "" : v); 299 332 cbSaveToPreferences.setSelected(OAuthAccessTokenHolder.getInstance().isSaveToPreferences()); 300 333 } … … 374 407 return; 375 408 setApiUrl((String) evt.getNewValue()); 376 409 } 410 411 @Override 412 public boolean equals(Object obj) { 413 if (!(obj instanceof OAuthAuthenticationPreferencesPanel)) return false; 414 OAuthAuthenticationPreferencesPanel check = (OAuthAuthenticationPreferencesPanel) obj; 415 if (check.pnlAlreadyAuthorised == null && pnlAlreadyAuthorised == null) return true; 416 if ((check.pnlAlreadyAuthorised == null && pnlAlreadyAuthorised != null) 417 || (check.pnlAlreadyAuthorised != null && pnlAlreadyAuthorised == null)) return false; 418 419 if (check.pnlAlreadyAuthorised.tfAccessTokenKey.getText().equals(pnlAlreadyAuthorised.tfAccessTokenKey.getText()) 420 && check.pnlAlreadyAuthorised.tfAccessTokenSecret.getText().equals(pnlAlreadyAuthorised.tfAccessTokenSecret.getText())) { 421 return true; 422 } 423 return false; 424 } 425 426 @Override 427 public int hashCode() { 428 return super.hashCode(); 429 } 377 430 } -
src/org/openstreetmap/josm/io/OsmConnection.java
104 104 * Adds an authentication header for basic authentication 105 105 * 106 106 * @param con the connection 107 * @ throws OsmTransferException if something went wrong. Check for nested exceptions107 * @param response the response with username/password information 108 108 */ 109 protected void addBasicAuthorizationHeader(HttpClient con) throws OsmTransferException { 110 CredentialsAgentResponse response; 111 try { 112 synchronized (CredentialsManager.getInstance()) { 113 response = CredentialsManager.getInstance().getCredentials(RequestorType.SERVER, 114 con.getURL().getHost(), false /* don't know yet whether the credentials will succeed */); 115 } 116 } catch (CredentialsAgentException e) { 117 throw new OsmTransferException(e); 118 } 109 protected void addBasicAuthorizationHeader(HttpClient con, CredentialsAgentResponse response) { 119 110 if (response != null) { 120 111 if (response.isCanceled()) { 121 112 cancel = true; … … 132 123 * Signs the connection with an OAuth authentication header 133 124 * 134 125 * @param connection the connection 126 * @param holder specific OAuth access token 135 127 * 136 128 * @throws MissingOAuthAccessTokenException if there is currently no OAuth Access Token configured 137 129 * @throws OsmTransferException if signing fails 138 130 */ 139 protected void addOAuthAuthorizationHeader(HttpClient connection ) throws OsmTransferException {131 protected void addOAuthAuthorizationHeader(HttpClient connection, OAuthAccessTokenHolder holder) throws OsmTransferException { 140 132 if (oauthParameters == null) { 141 133 oauthParameters = OAuthParameters.createFromApiUrl(OsmApi.getOsmApi().getServerUrl()); 142 134 } 143 135 OAuthConsumer consumer = oauthParameters.buildConsumer(); 144 OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();145 136 if (!holder.containsAccessToken()) { 146 137 obtainAccessToken(connection); 147 138 } … … 177 168 } 178 169 179 170 protected void addAuth(HttpClient connection) throws OsmTransferException { 180 final String authMethod = OsmApi.getAuthMethod(); 181 if ("basic".equals(authMethod)) { 182 addBasicAuthorizationHeader(connection); 183 } else if ("oauth".equals(authMethod)) { 184 addOAuthAuthorizationHeader(connection); 171 addAuth(connection, null); 172 } 173 174 /** 175 * Add authorization information to a connection 176 * @param connection to add authorization information to 177 * @param holder A {@code CredentialsAgentResponse} for basic authorization, 178 * {@code OAuthAccessTokenHolder} for OAuth, or {@code null} for defaults. 179 * @throws OsmTransferException if the authorization is not valid 180 */ 181 protected void addAuth(HttpClient connection, Object holder) throws OsmTransferException { 182 if (holder == null) { 183 final String authMethod = OsmApi.getAuthMethod(); 184 if ("basic".equals(authMethod)) { 185 CredentialsAgentResponse response; 186 try { 187 synchronized (CredentialsManager.getInstance()) { 188 response = CredentialsManager.getInstance().getCredentials(RequestorType.SERVER, 189 connection.getURL().getHost(), false /* don't know yet whether the credentials will succeed */); 190 } 191 } catch (CredentialsAgentException e) { 192 throw new OsmTransferException(e); 193 } 194 holder = response; 195 } else if ("oauth".equals(authMethod)) { 196 holder = OAuthAccessTokenHolder.getInstance(); 197 } else { 198 String msg = tr("Unexpected value for preference ''{0}''. Got ''{1}''.", "osm-server.auth-method", authMethod); 199 Logging.warn(msg); 200 throw new OsmTransferException(msg); 201 } 202 } 203 204 if (holder instanceof OAuthAccessTokenHolder) { 205 addOAuthAuthorizationHeader(connection, (OAuthAccessTokenHolder) holder); 206 } else if (holder instanceof CredentialsAgentResponse) { 207 addBasicAuthorizationHeader(connection, (CredentialsAgentResponse) holder); 185 208 } else { 186 String msg = tr("Unexpected value for preference ''{0}''. Got ''{1}''.", "osm-server.auth-method", authMethod);209 String msg = tr("Unexpected object for authorizations. Got ''{0}''.", holder.getClass().getName()); 187 210 Logging.warn(msg); 188 211 throw new OsmTransferException(msg); 189 212 } -
src/org/openstreetmap/josm/io/OsmServerReader.java
79 79 * @throws OsmTransferException if data transfer errors occur 80 80 */ 81 81 protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor, String reason) throws OsmTransferException { 82 return getInputStream(urlStr, progressMonitor, reason, null); 83 } 84 85 /** 86 * Open a connection to the given url and return a reader on the input stream 87 * from that connection. In case of user cancel, return <code>null</code>. 88 * Relative URL's are directed to API base URL. 89 * @param urlStr The url to connect to. 90 * @param progressMonitor progress monitoring and abort handler 91 * @param reason The reason to show on console. Can be {@code null} if no reason is given 92 * @param authentication A {@code CredentialsAgentResponse} for basic authorization, 93 * {@code OAuthAccessTokenHolder} for OAuth, or {@code null} for defaults. 94 * @return A reader reading the input stream (servers answer) or <code>null</code>. 95 * @throws OsmTransferException if data transfer errors occur 96 */ 97 protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor, String reason, Object authentication) 98 throws OsmTransferException { 82 99 try { 83 100 api.initialize(progressMonitor); 84 101 String url = urlStr.startsWith("http") ? urlStr : (getBaseUrl() + urlStr); 85 return getInputStreamRaw(url, progressMonitor, reason );102 return getInputStreamRaw(url, progressMonitor, reason, authentication); 86 103 } finally { 87 104 progressMonitor.invalidate(); 88 105 } … … 122 139 } 123 140 124 141 /** 142 * Open a connection to the given url and return a reader on the input stream 143 * from that connection. In case of user cancel, return <code>null</code>. 144 * @param urlStr The exact url to connect to. 145 * @param progressMonitor progress monitoring and abort handler 146 * @param reason The reason to show on console. Can be {@code null} if no reason is given 147 * @return An reader reading the input stream (servers answer) or <code>null</code>. 148 * @throws OsmTransferException if data transfer errors occur 149 * @since xxx 150 */ 151 protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason, Object auth) 152 throws OsmTransferException { 153 return getInputStreamRaw(urlStr, progressMonitor, reason, false, "GET", null, auth); 154 } 155 156 /** 125 157 * Open a connection to the given url (if HTTP, trough a GET request) and return a reader on the input stream 126 158 * from that connection. In case of user cancel, return <code>null</code>. 127 159 * @param urlStr The exact url to connect to. … … 151 183 * @throws OsmTransferException if data transfer errors occur 152 184 * @since 12596 153 185 */ 186 protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason, 187 boolean uncompressAccordingToContentDisposition, String httpMethod, byte[] requestBody) throws OsmTransferException { 188 return getInputStreamRaw(urlStr, progressMonitor, reason, uncompressAccordingToContentDisposition, httpMethod, requestBody, null); 189 } 190 191 /** 192 * Open a connection to the given url (if HTTP, with the specified method) and return a reader on the input stream 193 * from that connection. In case of user cancel, return <code>null</code>. 194 * @param urlStr The exact url to connect to. 195 * @param progressMonitor progress monitoring and abort handler 196 * @param reason The reason to show on console. Can be {@code null} if no reason is given 197 * @param uncompressAccordingToContentDisposition Whether to inspect the HTTP header {@code Content-Disposition} 198 * for {@code filename} and uncompress a gzip/bzip2/xz/zip stream. 199 * @param httpMethod HTTP method ("GET", "POST" or "PUT") 200 * @param requestBody HTTP request body (for "POST" and "PUT" methods only). Must be null for "GET" method. 201 * @param auth A {@code CredentialsAgentResponse} for basic authorization, 202 * {@code OAuthAccessTokenHolder} for OAuth, or {@code null} for defaults. 203 * @return An reader reading the input stream (servers answer) or {@code null}. 204 * @throws OsmTransferException if data transfer errors occur 205 * @since xxx 206 */ 154 207 @SuppressWarnings("resource") 155 208 protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason, 156 boolean uncompressAccordingToContentDisposition, String httpMethod, byte[] requestBody) throws OsmTransferException { 209 boolean uncompressAccordingToContentDisposition, String httpMethod, byte[] requestBody, 210 Object auth) throws OsmTransferException { 157 211 try { 158 212 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(urlStr, Config.getUrls().getJOSMWebsite()); 159 213 OnlineResource.OSM_API.checkOfflineAccess(urlStr, OsmApi.getOsmApi().getServerUrl()); … … 182 236 activeConnection = client; 183 237 adaptRequest(client); 184 238 if (doAuthenticate) { 185 addAuth(client );239 addAuth(client, auth); 186 240 } 187 241 if (cancel) 188 242 throw new OsmTransferCanceledException("Operation canceled"); … … 415 469 */ 416 470 public <T> T fetchData(String api, String subtask, DomParser<T> parser, ProgressMonitor monitor, String reason) 417 471 throws OsmTransferException { 472 return fetchData(api, subtask, parser, monitor, reason, null); 473 } 474 475 /** 476 * Fetches generic data from the DOM document resulting an API call. 477 * @param api the OSM API call 478 * @param subtask the subtask translated message 479 * @param parser the parser converting the DOM document (OSM API result) 480 * @param <T> data type 481 * @param monitor The progress monitor 482 * @param reason The reason to show on console. Can be {@code null} if no reason is given 483 * @param authentication A {@code CredentialsAgentResponse} for basic authorization, 484 * {@code OAuthAccessTokenHolder} for OAuth, or {@code null} for defaults. 485 * @return The converted data 486 * @throws OsmTransferException if something goes wrong 487 * @since 12510 488 */ 489 public <T> T fetchData(String api, String subtask, DomParser<T> parser, ProgressMonitor monitor, String reason, Object authentication) 490 throws OsmTransferException { 418 491 try { 419 492 monitor.beginTask(""); 420 493 monitor.indeterminateSubTask(subtask); 421 try (InputStream in = getInputStream(api, monitor.createSubTaskMonitor(1, true), reason )) {494 try (InputStream in = getInputStream(api, monitor.createSubTaskMonitor(1, true), reason, authentication)) { 422 495 return parser.parse(XmlUtils.parseSafeDOM(in)); 423 496 } 424 497 } catch (OsmTransferException e) { -
src/org/openstreetmap/josm/io/OsmServerUserInfoReader.java
13 13 import javax.xml.xpath.XPathFactory; 14 14 15 15 import org.openstreetmap.josm.data.coor.LatLon; 16 import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder; 16 17 import org.openstreetmap.josm.data.osm.DataSet; 17 18 import org.openstreetmap.josm.data.osm.UserInfo; 18 19 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 20 import org.openstreetmap.josm.io.auth.CredentialsAgentResponse; 21 import org.openstreetmap.josm.tools.Logging; 19 22 import org.openstreetmap.josm.tools.UncheckedParseException; 20 23 import org.openstreetmap.josm.tools.XmlParsingException; 21 24 import org.openstreetmap.josm.tools.date.DateUtils; … … 159 162 } 160 163 161 164 /** 165 * Fetches user info without explicit reason with a specific authentication 166 * @param monitor The progress monitor 167 * @param authentication The authentication object ({@code OAuthAccessTokenHolder} 168 * or {@code CredentialsAgentResponse}) 169 * @return The user info 170 * @throws OsmTransferException if something goes wrong 171 * @since xxx 172 */ 173 public UserInfo fetchUserInfo(ProgressMonitor monitor, Object authentication) throws OsmTransferException { 174 if (authentication instanceof String) { 175 return fetchUserInfo(monitor, null, (String) authentication); 176 } else { 177 return fetchUserInfo(monitor, authentication, null); 178 } 179 } 180 181 /** 162 182 * Fetches user info, with an explicit reason. 163 183 * @param monitor The progress monitor 164 184 * @param reason The reason to show on console. Can be {@code null} if no reason is given … … 167 187 * @since 6695 168 188 */ 169 189 public UserInfo fetchUserInfo(ProgressMonitor monitor, String reason) throws OsmTransferException { 170 return fetchData("user/details", tr("Reading user info ..."), 171 OsmServerUserInfoReader::buildFromXML, monitor, reason); 190 return fetchUserInfo(monitor, null, reason); 172 191 } 192 193 /** 194 * Fetches user info, with an explicit reason. 195 * @param monitor The progress monitor 196 * @param authentication A {@code CredentialsAgentResponse} for basic authorization, 197 * {@code OAuthAccessTokenHolder} for OAuth, or {@code null} for defaults. 198 * @param reason The reason to show on console. Can be {@code null} if no reason is given 199 * @return The user info 200 * @throws OsmTransferException if something goes wrong 201 * @since xxx 202 */ 203 public UserInfo fetchUserInfo(ProgressMonitor monitor, Object authentication, String reason) throws OsmTransferException { 204 if (authentication instanceof OAuthAccessTokenHolder || authentication instanceof CredentialsAgentResponse 205 || authentication == null) { 206 return fetchData("user/details", tr("Reading user info ..."), 207 OsmServerUserInfoReader::buildFromXML, monitor, reason, authentication); 208 } else { 209 String msg = tr("We did not get a valid authentication object ({0})", authentication.getClass().getName()); 210 Logging.warn(msg); 211 throw new OsmTransferException(msg); 212 } 213 } 173 214 }