diff --git a/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java b/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java
index d564184..ef1645f 100644
|
a
|
b
|
import java.awt.event.ActionEvent;
|
| 14 | 14 | import java.io.IOException; |
| 15 | 15 | import java.net.Authenticator.RequestorType; |
| 16 | 16 | import java.net.PasswordAuthentication; |
| | 17 | import java.util.concurrent.Executor; |
| 17 | 18 | |
| 18 | 19 | import javax.swing.AbstractAction; |
| 19 | 20 | import javax.swing.BorderFactory; |
| … |
… |
public class FullyAutomaticAuthorizationUI extends AbstractAuthorizationUI {
|
| 66 | 67 | private JPanel pnlPropertiesPanel; |
| 67 | 68 | private JPanel pnlActionButtonsPanel; |
| 68 | 69 | private JPanel pnlResult; |
| | 70 | private final Executor executor; |
| 69 | 71 | |
| 70 | 72 | /** |
| 71 | 73 | * Builds the panel with the three privileges the user can grant JOSM |
| … |
… |
public class FullyAutomaticAuthorizationUI extends AbstractAuthorizationUI {
|
| 314 | 316 | /** |
| 315 | 317 | * Constructs a new {@code FullyAutomaticAuthorizationUI} for the given API URL. |
| 316 | 318 | * @param apiUrl The OSM API URL |
| | 319 | * @param executor the executor used for running the HTTP requests for the authorization |
| 317 | 320 | * @since 5422 |
| 318 | 321 | */ |
| 319 | | public FullyAutomaticAuthorizationUI(String apiUrl) { |
| | 322 | public FullyAutomaticAuthorizationUI(String apiUrl, Executor executor) { |
| 320 | 323 | super(apiUrl); |
| | 324 | this.executor = executor; |
| 321 | 325 | build(); |
| 322 | 326 | } |
| 323 | 327 | |
| … |
… |
public class FullyAutomaticAuthorizationUI extends AbstractAuthorizationUI {
|
| 345 | 349 | |
| 346 | 350 | @Override |
| 347 | 351 | public void actionPerformed(ActionEvent evt) { |
| 348 | | Main.worker.submit(new FullyAutomaticAuthorisationTask(FullyAutomaticAuthorizationUI.this)); |
| | 352 | executor.execute(new FullyAutomaticAuthorisationTask(FullyAutomaticAuthorizationUI.this)); |
| 349 | 353 | } |
| 350 | 354 | |
| 351 | 355 | protected final void updateEnabledState() { |
| … |
… |
public class FullyAutomaticAuthorizationUI extends AbstractAuthorizationUI {
|
| 395 | 399 | |
| 396 | 400 | @Override |
| 397 | 401 | public void actionPerformed(ActionEvent arg0) { |
| 398 | | Main.worker.submit(new TestAccessTokenTask( |
| | 402 | executor.execute(new TestAccessTokenTask( |
| 399 | 403 | FullyAutomaticAuthorizationUI.this, |
| 400 | 404 | getApiUrl(), |
| 401 | 405 | getAdvancedPropertiesPanel().getAdvancedParameters(), |
diff --git a/src/org/openstreetmap/josm/gui/oauth/ManualAuthorizationUI.java b/src/org/openstreetmap/josm/gui/oauth/ManualAuthorizationUI.java
index 63dc2a2..865aaee 100644
|
a
|
b
|
import java.awt.Insets;
|
| 11 | 11 | import java.awt.event.ActionEvent; |
| 12 | 12 | import java.beans.PropertyChangeEvent; |
| 13 | 13 | import java.beans.PropertyChangeListener; |
| | 14 | import java.util.concurrent.Executor; |
| 14 | 15 | |
| 15 | 16 | import javax.swing.AbstractAction; |
| 16 | 17 | import javax.swing.BorderFactory; |
| … |
… |
import javax.swing.event.DocumentEvent;
|
| 22 | 23 | import javax.swing.event.DocumentListener; |
| 23 | 24 | import javax.swing.text.JTextComponent; |
| 24 | 25 | |
| 25 | | import org.openstreetmap.josm.Main; |
| 26 | 26 | import org.openstreetmap.josm.data.oauth.OAuthToken; |
| 27 | 27 | import org.openstreetmap.josm.gui.SideButton; |
| 28 | 28 | import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder; |
| … |
… |
public class ManualAuthorizationUI extends AbstractAuthorizationUI {
|
| 46 | 46 | private transient AccessTokenSecretValidator valAccessTokenSecret; |
| 47 | 47 | private JCheckBox cbSaveToPreferences; |
| 48 | 48 | private HtmlPanel pnlMessage; |
| | 49 | private final Executor executor; |
| 49 | 50 | |
| 50 | 51 | protected JPanel buildAccessTokenPanel() { |
| 51 | 52 | JPanel pnl = new JPanel(new GridBagLayout()); |
| … |
… |
public class ManualAuthorizationUI extends AbstractAuthorizationUI {
|
| 162 | 163 | /** |
| 163 | 164 | * Constructs a new {@code ManualAuthorizationUI} for the given API URL. |
| 164 | 165 | * @param apiUrl The OSM API URL |
| | 166 | * @param executor the executor used for running the HTTP requests for the authorization |
| 165 | 167 | * @since 5422 |
| 166 | 168 | */ |
| 167 | | public ManualAuthorizationUI(String apiUrl) { |
| | 169 | public ManualAuthorizationUI(String apiUrl, Executor executor) { |
| 168 | 170 | super(apiUrl); |
| | 171 | this.executor = executor; |
| 169 | 172 | build(); |
| 170 | 173 | } |
| 171 | 174 | |
| … |
… |
public class ManualAuthorizationUI extends AbstractAuthorizationUI {
|
| 260 | 263 | getAdvancedPropertiesPanel().getAdvancedParameters(), |
| 261 | 264 | getAccessToken() |
| 262 | 265 | ); |
| 263 | | Main.worker.submit(task); |
| | 266 | executor.execute(task); |
| 264 | 267 | } |
| 265 | 268 | |
| 266 | 269 | protected final void updateEnabledState() { |
diff --git a/src/org/openstreetmap/josm/gui/oauth/OAuthAuthorizationWizard.java b/src/org/openstreetmap/josm/gui/oauth/OAuthAuthorizationWizard.java
index 2b44935..7f57dfc 100644
|
a
|
b
|
import java.awt.event.WindowAdapter;
|
| 21 | 21 | import java.awt.event.WindowEvent; |
| 22 | 22 | import java.beans.PropertyChangeEvent; |
| 23 | 23 | import java.beans.PropertyChangeListener; |
| | 24 | import java.util.concurrent.Executor; |
| 24 | 25 | |
| 25 | 26 | import javax.swing.AbstractAction; |
| 26 | 27 | import javax.swing.BorderFactory; |
| … |
… |
import org.openstreetmap.josm.data.oauth.OAuthToken;
|
| 43 | 44 | import org.openstreetmap.josm.gui.SideButton; |
| 44 | 45 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction; |
| 45 | 46 | import org.openstreetmap.josm.gui.help.HelpUtil; |
| | 47 | import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder; |
| 46 | 48 | import org.openstreetmap.josm.gui.util.GuiHelper; |
| 47 | 49 | import org.openstreetmap.josm.gui.widgets.HtmlPanel; |
| 48 | 50 | import org.openstreetmap.josm.tools.CheckParameterUtil; |
| 49 | 51 | import org.openstreetmap.josm.tools.ImageProvider; |
| 50 | 52 | import org.openstreetmap.josm.tools.OpenBrowser; |
| | 53 | import org.openstreetmap.josm.tools.UserCancelException; |
| 51 | 54 | import org.openstreetmap.josm.tools.WindowGeometry; |
| 52 | 55 | |
| 53 | 56 | /** |
| … |
… |
public class OAuthAuthorizationWizard extends JDialog {
|
| 64 | 67 | private SemiAutomaticAuthorizationUI pnlSemiAutomaticAuthorisationUI; |
| 65 | 68 | private ManualAuthorizationUI pnlManualAuthorisationUI; |
| 66 | 69 | private JScrollPane spAuthorisationProcedureUI; |
| | 70 | private final Executor executor; |
| | 71 | |
| | 72 | /** |
| | 73 | * Launches the wizard, {@link OAuthAccessTokenHolder#setAccessToken(OAuthToken) sets the token} |
| | 74 | * and {@link OAuthAccessTokenHolder#setSaveToPreferences(boolean) saves to preferences}. |
| | 75 | * @throws UserCancelException if user cancels the operation |
| | 76 | */ |
| | 77 | public void showDialog() throws UserCancelException { |
| | 78 | setVisible(true); |
| | 79 | if (isCanceled()) { |
| | 80 | throw new UserCancelException(); |
| | 81 | } |
| | 82 | OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance(); |
| | 83 | holder.setAccessToken(getAccessToken()); |
| | 84 | holder.setSaveToPreferences(isSaveAccessTokenToPreferences()); |
| | 85 | } |
| 67 | 86 | |
| 68 | 87 | /** |
| 69 | 88 | * Builds the row with the action buttons |
| … |
… |
public class OAuthAuthorizationWizard extends JDialog {
|
| 169 | 188 | setTitle(tr("Get an Access Token for ''{0}''", apiUrl)); |
| 170 | 189 | this.setMinimumSize(new Dimension(600, 420)); |
| 171 | 190 | |
| 172 | | pnlFullyAutomaticAuthorisationUI = new FullyAutomaticAuthorizationUI(apiUrl); |
| 173 | | pnlSemiAutomaticAuthorisationUI = new SemiAutomaticAuthorizationUI(apiUrl); |
| 174 | | pnlManualAuthorisationUI = new ManualAuthorizationUI(apiUrl); |
| | 191 | pnlFullyAutomaticAuthorisationUI = new FullyAutomaticAuthorizationUI(apiUrl, executor); |
| | 192 | pnlSemiAutomaticAuthorisationUI = new SemiAutomaticAuthorizationUI(apiUrl, executor); |
| | 193 | pnlManualAuthorisationUI = new ManualAuthorizationUI(apiUrl, executor); |
| 175 | 194 | |
| 176 | 195 | spAuthorisationProcedureUI = GuiHelper.embedInVerticalScrollPane(new JPanel()); |
| 177 | 196 | spAuthorisationProcedureUI.getVerticalScrollBar().addComponentListener( |
| … |
… |
public class OAuthAuthorizationWizard extends JDialog {
|
| 208 | 227 | /** |
| 209 | 228 | * Creates the wizard. |
| 210 | 229 | * |
| 211 | | * @param apiUrl the API URL. Must not be null. |
| 212 | | * @throws IllegalArgumentException if apiUrl is null |
| 213 | | */ |
| 214 | | public OAuthAuthorizationWizard(String apiUrl) { |
| 215 | | this(Main.parent, apiUrl); |
| 216 | | } |
| 217 | | |
| 218 | | /** |
| 219 | | * Creates the wizard. |
| 220 | | * |
| 221 | 230 | * @param parent the component relative to which the dialog is displayed |
| 222 | 231 | * @param apiUrl the API URL. Must not be null. |
| | 232 | * @param executor the executor used for running the HTTP requests for the authorization |
| 223 | 233 | * @throws IllegalArgumentException if apiUrl is null |
| 224 | 234 | */ |
| 225 | | public OAuthAuthorizationWizard(Component parent, String apiUrl) { |
| | 235 | public OAuthAuthorizationWizard(Component parent, String apiUrl, Executor executor) { |
| 226 | 236 | super(JOptionPane.getFrameForComponent(parent), ModalityType.DOCUMENT_MODAL); |
| 227 | 237 | CheckParameterUtil.ensureParameterNotNull(apiUrl, "apiUrl"); |
| 228 | 238 | this.apiUrl = apiUrl; |
| | 239 | this.executor = executor; |
| 229 | 240 | build(); |
| 230 | 241 | } |
| 231 | 242 | |
diff --git a/src/org/openstreetmap/josm/gui/oauth/SemiAutomaticAuthorizationUI.java b/src/org/openstreetmap/josm/gui/oauth/SemiAutomaticAuthorizationUI.java
index c8f3b74..ddc0aad 100644
|
a
|
b
|
import java.awt.Insets;
|
| 13 | 13 | import java.awt.event.ActionEvent; |
| 14 | 14 | import java.awt.event.ItemEvent; |
| 15 | 15 | import java.awt.event.ItemListener; |
| | 16 | import java.util.concurrent.Executor; |
| 16 | 17 | |
| 17 | 18 | import javax.swing.AbstractAction; |
| 18 | 19 | import javax.swing.BorderFactory; |
| … |
… |
import javax.swing.JCheckBox;
|
| 20 | 21 | import javax.swing.JLabel; |
| 21 | 22 | import javax.swing.JPanel; |
| 22 | 23 | |
| 23 | | import org.openstreetmap.josm.Main; |
| 24 | 24 | import org.openstreetmap.josm.data.oauth.OAuthToken; |
| 25 | 25 | import org.openstreetmap.josm.gui.SideButton; |
| 26 | 26 | import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder; |
| … |
… |
public class SemiAutomaticAuthorizationUI extends AbstractAuthorizationUI {
|
| 46 | 46 | private RetrieveRequestTokenPanel pnlRetrieveRequestToken; |
| 47 | 47 | private RetrieveAccessTokenPanel pnlRetrieveAccessToken; |
| 48 | 48 | private ShowAccessTokenPanel pnlShowAccessToken; |
| 49 | | |
| | 49 | private final Executor executor; |
| | 50 | |
| 50 | 51 | /** |
| 51 | 52 | * build the UI |
| 52 | 53 | */ |
| … |
… |
public class SemiAutomaticAuthorizationUI extends AbstractAuthorizationUI {
|
| 62 | 63 | /** |
| 63 | 64 | * Constructs a new {@code SemiAutomaticAuthorizationUI} for the given API URL. |
| 64 | 65 | * @param apiUrl The OSM API URL |
| | 66 | * @param executor the executor used for running the HTTP requests for the authorization |
| 65 | 67 | * @since 5422 |
| 66 | 68 | */ |
| 67 | | public SemiAutomaticAuthorizationUI(String apiUrl) { |
| | 69 | public SemiAutomaticAuthorizationUI(String apiUrl, Executor executor) { |
| 68 | 70 | super(apiUrl); |
| | 71 | this.executor = executor; |
| 69 | 72 | build(); |
| 70 | 73 | } |
| 71 | 74 | |
| … |
… |
public class SemiAutomaticAuthorizationUI extends AbstractAuthorizationUI {
|
| 395 | 398 | SemiAutomaticAuthorizationUI.this, |
| 396 | 399 | getAdvancedPropertiesPanel().getAdvancedParameters() |
| 397 | 400 | ); |
| 398 | | Main.worker.submit(task); |
| | 401 | executor.execute(task); |
| 399 | 402 | Runnable r = new Runnable() { |
| 400 | 403 | @Override |
| 401 | 404 | public void run() { |
| … |
… |
public class SemiAutomaticAuthorizationUI extends AbstractAuthorizationUI {
|
| 410 | 413 | }); |
| 411 | 414 | } |
| 412 | 415 | }; |
| 413 | | Main.worker.submit(r); |
| | 416 | executor.execute(r); |
| 414 | 417 | } |
| 415 | 418 | } |
| 416 | 419 | |
| … |
… |
public class SemiAutomaticAuthorizationUI extends AbstractAuthorizationUI {
|
| 432 | 435 | getAdvancedPropertiesPanel().getAdvancedParameters(), |
| 433 | 436 | requestToken |
| 434 | 437 | ); |
| 435 | | Main.worker.submit(task); |
| | 438 | executor.execute(task); |
| 436 | 439 | Runnable r = new Runnable() { |
| 437 | 440 | @Override |
| 438 | 441 | public void run() { |
| … |
… |
public class SemiAutomaticAuthorizationUI extends AbstractAuthorizationUI {
|
| 447 | 450 | }); |
| 448 | 451 | } |
| 449 | 452 | }; |
| 450 | | Main.worker.submit(r); |
| | 453 | executor.execute(r); |
| 451 | 454 | } |
| 452 | 455 | } |
| 453 | 456 | |
| … |
… |
public class SemiAutomaticAuthorizationUI extends AbstractAuthorizationUI {
|
| 470 | 473 | getAdvancedPropertiesPanel().getAdvancedParameters(), |
| 471 | 474 | getAccessToken() |
| 472 | 475 | ); |
| 473 | | Main.worker.submit(task); |
| | 476 | executor.execute(task); |
| 474 | 477 | } |
| 475 | 478 | } |
| 476 | 479 | } |
diff --git a/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java b/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java
index 308bbde..f5837fc 100644
|
a
|
b
|
import javax.swing.JSeparator;
|
| 20 | 20 | import org.openstreetmap.josm.Main; |
| 21 | 21 | import org.openstreetmap.josm.gui.help.HelpUtil; |
| 22 | 22 | import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel; |
| | 23 | import org.openstreetmap.josm.io.OsmApi; |
| 23 | 24 | import org.openstreetmap.josm.io.auth.CredentialsManager; |
| 24 | 25 | |
| 25 | 26 | /** |
| … |
… |
public class AuthenticationPreferencesPanel extends VerticallyScrollablePanel im
|
| 117 | 118 | * Initializes the panel from preferences |
| 118 | 119 | */ |
| 119 | 120 | public final void initFromPreferences() { |
| 120 | | String authMethod = Main.pref.get("osm-server.auth-method", "basic"); |
| | 121 | final String authMethod = OsmApi.getAuthMethod(); |
| 121 | 122 | if ("basic".equals(authMethod)) { |
| 122 | 123 | rbBasicAuthentication.setSelected(true); |
| 123 | 124 | } else if ("oauth".equals(authMethod)) { |
diff --git a/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java b/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java
index 6a7f553..24b857f 100644
|
a
|
b
|
import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
| 34 | 34 | import org.openstreetmap.josm.io.OsmApi; |
| 35 | 35 | import org.openstreetmap.josm.io.auth.CredentialsManager; |
| 36 | 36 | import org.openstreetmap.josm.tools.ImageProvider; |
| | 37 | import org.openstreetmap.josm.tools.UserCancelException; |
| 37 | 38 | |
| 38 | 39 | /** |
| 39 | 40 | * The preferences panel for the OAuth preferences. This just a summary panel |
| … |
… |
public class OAuthAuthenticationPreferencesPanel extends JPanel implements Prope
|
| 324 | 325 | public void actionPerformed(ActionEvent arg0) { |
| 325 | 326 | OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard( |
| 326 | 327 | OAuthAuthenticationPreferencesPanel.this, |
| 327 | | apiUrl |
| 328 | | ); |
| 329 | | wizard.setVisible(true); |
| 330 | | if (wizard.isCanceled()) return; |
| 331 | | OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance(); |
| 332 | | holder.setAccessToken(wizard.getAccessToken()); |
| 333 | | holder.setSaveToPreferences(wizard.isSaveAccessTokenToPreferences()); |
| | 328 | apiUrl, |
| | 329 | Main.worker); |
| | 330 | try { |
| | 331 | wizard.showDialog(); |
| | 332 | } catch (UserCancelException ignore) { |
| | 333 | Main.trace(ignore.toString()); |
| | 334 | return; |
| | 335 | } |
| 334 | 336 | pnlAdvancedProperties.setAdvancedParameters(wizard.getOAuthParameters()); |
| 335 | 337 | refreshView(); |
| 336 | 338 | } |
| … |
… |
public class OAuthAuthenticationPreferencesPanel extends JPanel implements Prope
|
| 339 | 341 | /** |
| 340 | 342 | * Launches the OAuthAuthorisationWizard to generate a new Access Token |
| 341 | 343 | */ |
| 342 | | private class RenewAuthorisationAction extends AbstractAction { |
| | 344 | private class RenewAuthorisationAction extends AuthoriseNowAction { |
| 343 | 345 | /** |
| 344 | 346 | * Constructs a new {@code RenewAuthorisationAction}. |
| 345 | 347 | */ |
| … |
… |
public class OAuthAuthenticationPreferencesPanel extends JPanel implements Prope
|
| 347 | 349 | putValue(NAME, tr("New Access Token")); |
| 348 | 350 | putValue(SHORT_DESCRIPTION, tr("Click to step through the OAuth authorization process and generate a new Access Token")); |
| 349 | 351 | putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth-small")); |
| 350 | | |
| 351 | | } |
| 352 | | |
| 353 | | @Override |
| 354 | | public void actionPerformed(ActionEvent arg0) { |
| 355 | | OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard( |
| 356 | | OAuthAuthenticationPreferencesPanel.this, |
| 357 | | apiUrl |
| 358 | | ); |
| 359 | | wizard.setVisible(true); |
| 360 | | if (wizard.isCanceled()) return; |
| 361 | | OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance(); |
| 362 | | holder.setAccessToken(wizard.getAccessToken()); |
| 363 | | holder.setSaveToPreferences(wizard.isSaveAccessTokenToPreferences()); |
| 364 | | pnlAdvancedProperties.setAdvancedParameters(wizard.getOAuthParameters()); |
| 365 | | refreshView(); |
| 366 | 352 | } |
| 367 | 353 | } |
| 368 | 354 | |
diff --git a/src/org/openstreetmap/josm/io/OsmApi.java b/src/org/openstreetmap/josm/io/OsmApi.java
index be950e9..b3f93dd 100644
|
a
|
b
|
public class OsmApi extends OsmConnection {
|
| 581 | 581 | * @return {@code true} if JOSM is configured to access OSM API via OAuth, {@code false} otherwise |
| 582 | 582 | * @since 6349 |
| 583 | 583 | */ |
| 584 | | public static final boolean isUsingOAuth() { |
| 585 | | return "oauth".equals(Main.pref.get("osm-server.auth-method", "basic")); |
| | 584 | public static boolean isUsingOAuth() { |
| | 585 | return "oauth".equals(getAuthMethod()); |
| | 586 | } |
| | 587 | |
| | 588 | /** |
| | 589 | * Returns the authentication method set in the preferences |
| | 590 | * @return the authentication method |
| | 591 | */ |
| | 592 | public static String getAuthMethod() { |
| | 593 | return Main.pref.get("osm-server.auth-method", "oauth"); |
| 586 | 594 | } |
| 587 | 595 | |
| 588 | 596 | protected final String sendRequest(String requestMethod, String urlSuffix, String requestBody, ProgressMonitor monitor) |
diff --git a/src/org/openstreetmap/josm/io/OsmConnection.java b/src/org/openstreetmap/josm/io/OsmConnection.java
index 7d690a9..8615471 100644
|
a
|
b
|
package org.openstreetmap.josm.io;
|
| 3 | 3 | |
| 4 | 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
| 5 | 5 | |
| | 6 | import java.lang.reflect.InvocationTargetException; |
| 6 | 7 | import java.net.Authenticator.RequestorType; |
| | 8 | import java.net.MalformedURLException; |
| | 9 | import java.net.URL; |
| 7 | 10 | import java.nio.ByteBuffer; |
| 8 | 11 | import java.nio.CharBuffer; |
| 9 | 12 | import java.nio.charset.CharacterCodingException; |
| 10 | 13 | import java.nio.charset.CharsetEncoder; |
| 11 | 14 | import java.nio.charset.StandardCharsets; |
| | 15 | import java.util.Objects; |
| | 16 | import java.util.concurrent.Callable; |
| | 17 | import java.util.concurrent.Executor; |
| | 18 | import java.util.concurrent.FutureTask; |
| 12 | 19 | |
| 13 | 20 | import org.openstreetmap.josm.Main; |
| 14 | 21 | import org.openstreetmap.josm.data.oauth.OAuthParameters; |
| | 22 | import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard; |
| 15 | 23 | import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder; |
| 16 | 24 | import org.openstreetmap.josm.io.auth.CredentialsAgentException; |
| 17 | 25 | import org.openstreetmap.josm.io.auth.CredentialsAgentResponse; |
| … |
… |
import org.openstreetmap.josm.tools.HttpClient;
|
| 22 | 30 | import oauth.signpost.OAuthConsumer; |
| 23 | 31 | import oauth.signpost.exception.OAuthException; |
| 24 | 32 | |
| | 33 | import javax.swing.SwingUtilities; |
| | 34 | |
| 25 | 35 | /** |
| 26 | 36 | * Base class that handles common things like authentication for the reader and writer |
| 27 | 37 | * to the osm server. |
| … |
… |
public class OsmConnection {
|
| 95 | 105 | } |
| 96 | 106 | OAuthConsumer consumer = oauthParameters.buildConsumer(); |
| 97 | 107 | OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance(); |
| 98 | | if (!holder.containsAccessToken()) |
| | 108 | if (!holder.containsAccessToken()) { |
| | 109 | try { |
| | 110 | final URL apiUrl = new URL(Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL)); |
| | 111 | if (!Objects.equals(apiUrl.getHost(), connection.getURL().getHost())) { |
| | 112 | throw new MissingOAuthAccessTokenException(); |
| | 113 | } |
| | 114 | final FutureTask<OAuthAuthorizationWizard> authTask = new FutureTask<>(new Callable<OAuthAuthorizationWizard>() { |
| | 115 | @Override |
| | 116 | public OAuthAuthorizationWizard call() throws Exception { |
| | 117 | final OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard( |
| | 118 | Main.parent, apiUrl.toExternalForm(), new Executor() { |
| | 119 | @Override |
| | 120 | public void execute(Runnable command) { |
| | 121 | command.run(); |
| | 122 | } |
| | 123 | }); |
| | 124 | wizard.showDialog(); |
| | 125 | OAuthAccessTokenHolder.getInstance().setSaveToPreferences(true); |
| | 126 | OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManager.getInstance()); |
| | 127 | return wizard; |
| | 128 | } |
| | 129 | }); |
| | 130 | if (SwingUtilities.isEventDispatchThread()) { |
| | 131 | authTask.run(); |
| | 132 | } else { |
| | 133 | SwingUtilities.invokeAndWait(authTask); |
| | 134 | } |
| | 135 | } catch (MalformedURLException | InterruptedException | InvocationTargetException e) { |
| | 136 | throw new MissingOAuthAccessTokenException(); |
| | 137 | } |
| | 138 | } |
| | 139 | if (!holder.containsAccessToken()) { // check if wizard completed |
| 99 | 140 | throw new MissingOAuthAccessTokenException(); |
| | 141 | } |
| 100 | 142 | consumer.setTokenWithSecret(holder.getAccessTokenKey(), holder.getAccessTokenSecret()); |
| 101 | 143 | try { |
| 102 | 144 | consumer.sign(connection); |
| … |
… |
public class OsmConnection {
|
| 106 | 148 | } |
| 107 | 149 | |
| 108 | 150 | protected void addAuth(HttpClient connection) throws OsmTransferException { |
| 109 | | String authMethod = Main.pref.get("osm-server.auth-method", "basic"); |
| | 151 | final String authMethod = OsmApi.getAuthMethod(); |
| 110 | 152 | if ("basic".equals(authMethod)) { |
| 111 | 153 | addBasicAuthorizationHeader(connection); |
| 112 | 154 | } else if ("oauth".equals(authMethod)) { |