From 1e89c0b77d10458f15aa4b2f41d0ba1de8451035 Mon Sep 17 00:00:00 2001 From: "taylor.smock" Date: Fri, 15 Sep 2023 13:57:24 +0000 Subject: [PATCH] See #22810: OSM OAuth 1.0a/Basic auth deprecation and removal This prevents users from using `Basic Authentication` or `OAuth 1.0a` unless one of the following is true: * They were previously using the authentication method * They have enabled `Expert Mode` * They are not using the default OSM API git-svn-id: https://josm.openstreetmap.de/svn/trunk@18828 0c6e7542-c601-0410-84e7-c038aed88b3b --- .../AuthenticationPreferencesPanel.java | 26 +++++++++++++++++-- .../io/importexport/OsmPbfImporterTest.java | 2 -- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java b/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java index b310fcbf17b..898aa62b803 100644 --- a/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java +++ b/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java @@ -17,9 +17,11 @@ import javax.swing.JPanel; import javax.swing.JRadioButton; +import org.openstreetmap.josm.actions.ExpertToggleAction; import org.openstreetmap.josm.data.UserIdentityManager; import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder; import org.openstreetmap.josm.data.oauth.OAuthVersion; +import org.openstreetmap.josm.data.preferences.JosmUrls; import org.openstreetmap.josm.gui.help.HelpUtil; import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel; import org.openstreetmap.josm.io.OsmApi; @@ -49,6 +51,16 @@ public class AuthenticationPreferencesPanel extends VerticallyScrollablePanel im /** the panel for the OAuth 2.0 authentication parameters */ private OAuthAuthenticationPreferencesPanel pnlOAuth20Preferences; + /** Used to determine which API we are using for disabling/enabling Basic Auth/OAuth 1.0a */ + private String apiUrl = OsmApi.getOsmApi().getServerUrl(); + /** ExpertToggleAction uses weak references; we don't want this listener to be garbage collected */ + private final ExpertToggleAction.ExpertModeChangeListener expertModeChangeListener = isExpert -> { + final String authMethod = OsmApi.getAuthMethod(); + final boolean defaultApi = JosmUrls.getInstance().getDefaultOsmApiUrl().equals(apiUrl); + rbBasicAuthentication.setEnabled(rbBasicAuthentication.isSelected() || "basic".equals(authMethod) || isExpert || !defaultApi); + rbOAuth.setEnabled(rbOAuth.isSelected() || "oauth".equals(authMethod) || isExpert || !defaultApi); + }; + /** * Constructs a new {@code AuthenticationPreferencesPanel}. */ @@ -109,8 +121,10 @@ protected final void build() { pnlOAuthPreferences = new OAuthAuthenticationPreferencesPanel(OAuthVersion.OAuth10a); pnlOAuth20Preferences = new OAuthAuthenticationPreferencesPanel(OAuthVersion.OAuth20); - rbBasicAuthentication.setSelected(true); - pnlAuthenticationParameters.add(pnlBasicAuthPreferences, BorderLayout.CENTER); + ExpertToggleAction.addExpertModeChangeListener(expertModeChangeListener, true); + + rbOAuth20.setSelected(true); + pnlAuthenticationParameters.add(pnlOAuth20Preferences, BorderLayout.CENTER); } /** @@ -177,6 +191,7 @@ public final void saveToPreferences() { UserIdentityManager.getInstance().initFromOAuth(); } } + ExpertToggleAction.removeExpertModeChangeListener(this.expertModeChangeListener); } /** @@ -209,5 +224,12 @@ public void propertyChange(PropertyChangeEvent evt) { if (pnlOAuthPreferences != null) { pnlOAuthPreferences.propertyChange(evt); } + if (pnlOAuth20Preferences != null) { + pnlOAuth20Preferences.propertyChange(evt); + } + if (OsmApiUrlInputPanel.API_URL_PROP.equals(evt.getPropertyName())) { + this.apiUrl = (String) evt.getNewValue(); + this.expertModeChangeListener.expertChanged(ExpertToggleAction.isExpert()); + } } } diff --git a/test/unit/org/openstreetmap/josm/gui/io/importexport/OsmPbfImporterTest.java b/test/unit/org/openstreetmap/josm/gui/io/importexport/OsmPbfImporterTest.java index 56ed1ee368b..988a9e49b9c 100644 --- a/test/unit/org/openstreetmap/josm/gui/io/importexport/OsmPbfImporterTest.java +++ b/test/unit/org/openstreetmap/josm/gui/io/importexport/OsmPbfImporterTest.java @@ -18,8 +18,6 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; import org.openstreetmap.josm.TestUtils; import org.openstreetmap.josm.data.coor.ILatLon; import org.openstreetmap.josm.data.coor.LatLon;