[2801] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.preferences.server;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.BorderLayout;
|
---|
| 7 | import java.awt.GridBagConstraints;
|
---|
| 8 | import java.awt.GridBagLayout;
|
---|
| 9 | import java.awt.Insets;
|
---|
| 10 | import java.awt.event.ItemEvent;
|
---|
| 11 | import java.awt.event.ItemListener;
|
---|
| 12 | import java.beans.PropertyChangeEvent;
|
---|
| 13 | import java.beans.PropertyChangeListener;
|
---|
| 14 |
|
---|
| 15 | import javax.swing.ButtonGroup;
|
---|
| 16 | import javax.swing.JPanel;
|
---|
| 17 | import javax.swing.JRadioButton;
|
---|
[8218] | 18 | import javax.swing.JSeparator;
|
---|
[2801] | 19 |
|
---|
[12686] | 20 | import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder;
|
---|
[2801] | 21 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
| 22 | import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
|
---|
[9352] | 23 | import org.openstreetmap.josm.io.OsmApi;
|
---|
[4245] | 24 | import org.openstreetmap.josm.io.auth.CredentialsManager;
|
---|
[12846] | 25 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
[12620] | 26 | import org.openstreetmap.josm.tools.Logging;
|
---|
[2801] | 27 |
|
---|
| 28 | /**
|
---|
[10179] | 29 | * This is the preference panel for the authentication method and the authentication parameters.
|
---|
| 30 | * @since 2745
|
---|
[2801] | 31 | */
|
---|
[9059] | 32 | public class AuthenticationPreferencesPanel extends VerticallyScrollablePanel implements PropertyChangeListener {
|
---|
[2801] | 33 |
|
---|
| 34 | /** indicates whether we use basic authentication */
|
---|
[10179] | 35 | private final JRadioButton rbBasicAuthentication = new JRadioButton();
|
---|
[2801] | 36 | /** indicates whether we use OAuth as authentication scheme */
|
---|
[10179] | 37 | private final JRadioButton rbOAuth = new JRadioButton();
|
---|
| 38 | /** the panel which contains the authentication parameters for the respective authentication scheme */
|
---|
| 39 | private final JPanel pnlAuthenticationParameteters = new JPanel(new BorderLayout());
|
---|
[2801] | 40 | /** the panel for the basic authentication parameters */
|
---|
| 41 | private BasicAuthenticationPreferencesPanel pnlBasicAuthPreferences;
|
---|
| 42 | /** the panel for the OAuth authentication parameters */
|
---|
| 43 | private OAuthAuthenticationPreferencesPanel pnlOAuthPreferences;
|
---|
[6349] | 44 | /** the panel for messages notifier preferences */
|
---|
[8218] | 45 | private FeaturesPanel pnlFeaturesPreferences;
|
---|
[2801] | 46 |
|
---|
| 47 | /**
|
---|
[10179] | 48 | * Constructs a new {@code AuthenticationPreferencesPanel}.
|
---|
| 49 | */
|
---|
| 50 | public AuthenticationPreferencesPanel() {
|
---|
| 51 | build();
|
---|
| 52 | initFromPreferences();
|
---|
| 53 | HelpUtil.setHelpContext(this, HelpUtil.ht("/Preferences/Connection#AuthenticationSettings"));
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | /**
|
---|
[2801] | 57 | * builds the UI
|
---|
| 58 | */
|
---|
[6890] | 59 | protected final void build() {
|
---|
[2801] | 60 | setLayout(new GridBagLayout());
|
---|
| 61 | GridBagConstraints gc = new GridBagConstraints();
|
---|
| 62 |
|
---|
[6365] | 63 | AuthenticationMethodChangeListener authChangeListener = new AuthenticationMethodChangeListener();
|
---|
[2801] | 64 |
|
---|
| 65 | // -- radio button for basic authentication
|
---|
| 66 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
| 67 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
[9352] | 68 | gc.gridx = 1;
|
---|
| 69 | gc.weightx = 1.0;
|
---|
[8510] | 70 | gc.insets = new Insets(0, 0, 0, 3);
|
---|
[10179] | 71 | add(rbBasicAuthentication, gc);
|
---|
[2801] | 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 |
|
---|
| 76 | //-- radio button for OAuth
|
---|
[9352] | 77 | gc.gridx = 0;
|
---|
| 78 | gc.weightx = 0.0;
|
---|
[10179] | 79 | add(rbOAuth, gc);
|
---|
[2801] | 80 | rbOAuth.setText(tr("Use OAuth"));
|
---|
| 81 | rbOAuth.setToolTipText(tr("Select to use OAuth as authentication mechanism"));
|
---|
| 82 | rbOAuth.addItemListener(authChangeListener);
|
---|
| 83 |
|
---|
| 84 | //-- radio button for OAuth
|
---|
| 85 | ButtonGroup bg = new ButtonGroup();
|
---|
| 86 | bg.add(rbBasicAuthentication);
|
---|
| 87 | bg.add(rbOAuth);
|
---|
| 88 |
|
---|
[6349] | 89 | //-- add the panel which will hold the authentication parameters
|
---|
[2801] | 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;
|
---|
[6365] | 96 | add(pnlAuthenticationParameteters, gc);
|
---|
[2801] | 97 |
|
---|
[6349] | 98 | //-- the two panels for authentication parameters
|
---|
[2801] | 99 | pnlBasicAuthPreferences = new BasicAuthenticationPreferencesPanel();
|
---|
| 100 | pnlOAuthPreferences = new OAuthAuthenticationPreferencesPanel();
|
---|
| 101 |
|
---|
| 102 | rbBasicAuthentication.setSelected(true);
|
---|
| 103 | pnlAuthenticationParameteters.add(pnlBasicAuthPreferences, BorderLayout.CENTER);
|
---|
[7509] | 104 |
|
---|
[6349] | 105 | gc.gridy = 2;
|
---|
[8218] | 106 | add(new JSeparator(), gc);
|
---|
| 107 |
|
---|
| 108 | //-- the panel for API feature preferences
|
---|
| 109 | gc.gridy = 3;
|
---|
[6349] | 110 | gc.fill = GridBagConstraints.NONE;
|
---|
[8218] | 111 | pnlFeaturesPreferences = new FeaturesPanel();
|
---|
| 112 | add(pnlFeaturesPreferences, gc);
|
---|
[2801] | 113 | }
|
---|
| 114 |
|
---|
[6349] | 115 | /**
|
---|
| 116 | * Initializes the panel from preferences
|
---|
| 117 | */
|
---|
[6623] | 118 | public final void initFromPreferences() {
|
---|
[9352] | 119 | final String authMethod = OsmApi.getAuthMethod();
|
---|
[6990] | 120 | if ("basic".equals(authMethod)) {
|
---|
[2801] | 121 | rbBasicAuthentication.setSelected(true);
|
---|
[6990] | 122 | } else if ("oauth".equals(authMethod)) {
|
---|
[2801] | 123 | rbOAuth.setSelected(true);
|
---|
| 124 | } else {
|
---|
[12620] | 125 | Logging.warn(tr("Unsupported value in preference ''{0}'', got ''{1}''. Using authentication method ''Basic Authentication''.",
|
---|
[8509] | 126 | "osm-server.auth-method", authMethod));
|
---|
[2801] | 127 | rbBasicAuthentication.setSelected(true);
|
---|
| 128 | }
|
---|
| 129 | pnlBasicAuthPreferences.initFromPreferences();
|
---|
| 130 | pnlOAuthPreferences.initFromPreferences();
|
---|
[8218] | 131 | pnlFeaturesPreferences.initFromPreferences();
|
---|
[2801] | 132 | }
|
---|
| 133 |
|
---|
[6349] | 134 | /**
|
---|
[12928] | 135 | * Saves the current values to the preferences
|
---|
[6349] | 136 | */
|
---|
[6623] | 137 | public final void saveToPreferences() {
|
---|
[2801] | 138 | // save the authentication method
|
---|
| 139 | String authMethod;
|
---|
| 140 | if (rbBasicAuthentication.isSelected()) {
|
---|
| 141 | authMethod = "basic";
|
---|
| 142 | } else {
|
---|
| 143 | authMethod = "oauth";
|
---|
| 144 | }
|
---|
[12846] | 145 | Config.getPref().put("osm-server.auth-method", authMethod);
|
---|
[6990] | 146 | if ("basic".equals(authMethod)) {
|
---|
[2801] | 147 | // save username and password and clear the OAuth token
|
---|
| 148 | pnlBasicAuthPreferences.saveToPreferences();
|
---|
| 149 | OAuthAccessTokenHolder.getInstance().clear();
|
---|
[12928] | 150 | OAuthAccessTokenHolder.getInstance().save(CredentialsManager.getInstance());
|
---|
[6990] | 151 | } else if ("oauth".equals(authMethod)) {
|
---|
[2801] | 152 | // clear the password in the preferences
|
---|
| 153 | pnlBasicAuthPreferences.clearPassword();
|
---|
| 154 | pnlBasicAuthPreferences.saveToPreferences();
|
---|
| 155 | pnlOAuthPreferences.saveToPreferences();
|
---|
| 156 | }
|
---|
[6349] | 157 | // save message notifications preferences. To be done after authentication preferences.
|
---|
[8218] | 158 | pnlFeaturesPreferences.saveToPreferences();
|
---|
[2801] | 159 | }
|
---|
| 160 |
|
---|
| 161 | /**
|
---|
| 162 | * Listens to changes in the authentication method
|
---|
| 163 | */
|
---|
| 164 | class AuthenticationMethodChangeListener implements ItemListener {
|
---|
[6084] | 165 | @Override
|
---|
[2801] | 166 | 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 |
|
---|
[6084] | 180 | @Override
|
---|
[2801] | 181 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
| 182 | if (pnlOAuthPreferences != null) {
|
---|
| 183 | pnlOAuthPreferences.propertyChange(evt);
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 | }
|
---|