001 // License: GPL. For details, see LICENSE file.
002 package org.openstreetmap.josm.gui.preferences.server;
003
004 import static org.openstreetmap.josm.tools.I18n.tr;
005
006 import java.awt.BorderLayout;
007 import java.awt.GridBagConstraints;
008 import java.awt.GridBagLayout;
009 import java.awt.Insets;
010 import java.awt.event.ItemEvent;
011 import java.awt.event.ItemListener;
012 import java.beans.PropertyChangeEvent;
013 import java.beans.PropertyChangeListener;
014
015 import javax.swing.ButtonGroup;
016 import javax.swing.JPanel;
017 import javax.swing.JRadioButton;
018
019 import org.openstreetmap.josm.Main;
020 import org.openstreetmap.josm.gui.help.HelpUtil;
021 import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
022 import org.openstreetmap.josm.io.auth.CredentialsManager;
023
024 /**
025 * This is the preference panel for the authentication method and the authentication
026 * parameters.
027 *
028 */
029 public class AuthenticationPreferencesPanel extends VerticallyScrollablePanel implements PropertyChangeListener{
030
031 /** indicates whether we use basic authentication */
032 private JRadioButton rbBasicAuthentication;
033 /** indicates whether we use OAuth as authentication scheme */
034 private JRadioButton rbOAuth;
035 /** the panel which contains the authentication parameters for the respective
036 * authentication scheme
037 */
038 private JPanel pnlAuthenticationParameteters;
039 /** the panel for the basic authentication parameters */
040 private BasicAuthenticationPreferencesPanel pnlBasicAuthPreferences;
041 /** the panel for the OAuth authentication parameters */
042 private OAuthAuthenticationPreferencesPanel pnlOAuthPreferences;
043
044 /**
045 * builds the UI
046 */
047 protected void build() {
048 setLayout(new GridBagLayout());
049 GridBagConstraints gc = new GridBagConstraints();
050
051 AuthenticationMethodChangeListener authChangeListener = new AuthenticationMethodChangeListener();
052
053 // -- radio button for basic authentication
054 gc.anchor = GridBagConstraints.NORTHWEST;
055 gc.fill = GridBagConstraints.HORIZONTAL;
056 gc.weightx = 0.0;
057 gc.insets = new Insets(0,0,0, 3);
058 add(rbBasicAuthentication = new JRadioButton(), gc);
059 rbBasicAuthentication.setText(tr("Use Basic Authentication"));
060 rbBasicAuthentication.setToolTipText(tr("Select to use HTTP basic authentication with your OSM username and password"));
061 rbBasicAuthentication.addItemListener(authChangeListener);
062
063 //-- radio button for OAuth
064 gc.gridx = 1;
065 gc.weightx = 1.0;
066 add(rbOAuth = new JRadioButton(), gc);
067 rbOAuth.setText(tr("Use OAuth"));
068 rbOAuth.setToolTipText(tr("Select to use OAuth as authentication mechanism"));
069 rbOAuth.addItemListener(authChangeListener);
070
071 //-- radio button for OAuth
072 ButtonGroup bg = new ButtonGroup();
073 bg.add(rbBasicAuthentication);
074 bg.add(rbOAuth);
075
076 //-- add the panel which will hld the authentication parameters
077 gc.gridx = 0;
078 gc.gridy = 1;
079 gc.gridwidth = 2;
080 gc.fill = GridBagConstraints.BOTH;
081 gc.weightx = 1.0;
082 gc.weighty = 1.0;
083 add(pnlAuthenticationParameteters = new JPanel(), gc);
084 pnlAuthenticationParameteters.setLayout(new BorderLayout());
085
086 //-- the two panel for authentication parameters
087 pnlBasicAuthPreferences = new BasicAuthenticationPreferencesPanel();
088 pnlOAuthPreferences = new OAuthAuthenticationPreferencesPanel();
089
090 rbBasicAuthentication.setSelected(true);
091 pnlAuthenticationParameteters.add(pnlBasicAuthPreferences, BorderLayout.CENTER);
092 }
093
094 public AuthenticationPreferencesPanel() {
095 build();
096 HelpUtil.setHelpContext(this, HelpUtil.ht("/Preferences/Connection#AuthenticationSettings"));
097 }
098
099 public void initFromPreferences() {
100 String authMethod = Main.pref.get("osm-server.auth-method", "basic");
101 if (authMethod.equals("basic")) {
102 rbBasicAuthentication.setSelected(true);
103 } else if (authMethod.equals("oauth")) {
104 rbOAuth.setSelected(true);
105 } else {
106 System.err.println(tr("Warning: Unsupported value in preference ''{0}'', got ''{1}''. Using authentication method ''Basic Authentication''.", "osm-server.auth-method", authMethod));
107 rbBasicAuthentication.setSelected(true);
108 }
109 pnlBasicAuthPreferences.initFromPreferences();
110 pnlOAuthPreferences.initFromPreferences();
111 }
112
113 public void saveToPreferences() {
114 // save the authentication method
115 String authMethod;
116 if (rbBasicAuthentication.isSelected()) {
117 authMethod = "basic";
118 } else {
119 authMethod = "oauth";
120 }
121 Main.pref.put("osm-server.auth-method", authMethod);
122 if (authMethod.equals("basic")) {
123 // save username and password and clear the OAuth token
124 pnlBasicAuthPreferences.saveToPreferences();
125 OAuthAccessTokenHolder.getInstance().clear();
126 OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManager.getInstance());
127 } else if (authMethod.equals("oauth")) {
128 // clear the password in the preferences
129 pnlBasicAuthPreferences.clearPassword();
130 pnlBasicAuthPreferences.saveToPreferences();
131 pnlOAuthPreferences.saveToPreferences();
132 }
133 }
134
135 /**
136 * Listens to changes in the authentication method
137 */
138 class AuthenticationMethodChangeListener implements ItemListener {
139 public void itemStateChanged(ItemEvent e) {
140 if (rbBasicAuthentication.isSelected()) {
141 pnlAuthenticationParameteters.removeAll();
142 pnlAuthenticationParameteters.add(pnlBasicAuthPreferences, BorderLayout.CENTER);
143 pnlBasicAuthPreferences.revalidate();
144 } else {
145 pnlAuthenticationParameteters.removeAll();
146 pnlAuthenticationParameteters.add(pnlOAuthPreferences, BorderLayout.CENTER);
147 pnlOAuthPreferences.revalidate();
148 }
149 repaint();
150 }
151 }
152
153 public void propertyChange(PropertyChangeEvent evt) {
154 if (pnlOAuthPreferences != null) {
155 pnlOAuthPreferences.propertyChange(evt);
156 }
157 }
158 }