001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.preferences.server; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.BorderLayout; 007import java.awt.GridBagConstraints; 008import java.awt.GridBagLayout; 009import java.awt.Insets; 010import java.awt.event.ItemEvent; 011import java.awt.event.ItemListener; 012import java.beans.PropertyChangeEvent; 013import java.beans.PropertyChangeListener; 014 015import javax.swing.ButtonGroup; 016import javax.swing.JPanel; 017import javax.swing.JRadioButton; 018 019import org.openstreetmap.josm.Main; 020import org.openstreetmap.josm.gui.help.HelpUtil; 021import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel; 022import 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 */ 029public 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 /** the panel for messages notifier preferences */ 044 private MessagesNotifierPanel pnlMessagesPreferences; 045 046 /** 047 * builds the UI 048 */ 049 protected final void build() { 050 setLayout(new GridBagLayout()); 051 GridBagConstraints gc = new GridBagConstraints(); 052 053 AuthenticationMethodChangeListener authChangeListener = new AuthenticationMethodChangeListener(); 054 055 // -- radio button for basic authentication 056 gc.anchor = GridBagConstraints.NORTHWEST; 057 gc.fill = GridBagConstraints.HORIZONTAL; 058 gc.weightx = 0.0; 059 gc.insets = new Insets(0,0,0, 3); 060 add(rbBasicAuthentication = new JRadioButton(), gc); 061 rbBasicAuthentication.setText(tr("Use Basic Authentication")); 062 rbBasicAuthentication.setToolTipText(tr("Select to use HTTP basic authentication with your OSM username and password")); 063 rbBasicAuthentication.addItemListener(authChangeListener); 064 065 //-- radio button for OAuth 066 gc.gridx = 1; 067 gc.weightx = 1.0; 068 add(rbOAuth = new JRadioButton(), gc); 069 rbOAuth.setText(tr("Use OAuth")); 070 rbOAuth.setToolTipText(tr("Select to use OAuth as authentication mechanism")); 071 rbOAuth.addItemListener(authChangeListener); 072 073 //-- radio button for OAuth 074 ButtonGroup bg = new ButtonGroup(); 075 bg.add(rbBasicAuthentication); 076 bg.add(rbOAuth); 077 078 //-- add the panel which will hold the authentication parameters 079 gc.gridx = 0; 080 gc.gridy = 1; 081 gc.gridwidth = 2; 082 gc.fill = GridBagConstraints.BOTH; 083 gc.weightx = 1.0; 084 gc.weighty = 1.0; 085 pnlAuthenticationParameteters = new JPanel(); 086 add(pnlAuthenticationParameteters, gc); 087 pnlAuthenticationParameteters.setLayout(new BorderLayout()); 088 089 //-- the two panels for authentication parameters 090 pnlBasicAuthPreferences = new BasicAuthenticationPreferencesPanel(); 091 pnlOAuthPreferences = new OAuthAuthenticationPreferencesPanel(); 092 093 rbBasicAuthentication.setSelected(true); 094 pnlAuthenticationParameteters.add(pnlBasicAuthPreferences, BorderLayout.CENTER); 095 096 //-- the panel for messages preferences 097 gc.gridy = 2; 098 gc.fill = GridBagConstraints.NONE; 099 pnlMessagesPreferences = new MessagesNotifierPanel(); 100 add(pnlMessagesPreferences, gc); 101 } 102 103 /** 104 * Constructs a new {@code AuthenticationPreferencesPanel}. 105 */ 106 public AuthenticationPreferencesPanel() { 107 build(); 108 initFromPreferences(); 109 HelpUtil.setHelpContext(this, HelpUtil.ht("/Preferences/Connection#AuthenticationSettings")); 110 } 111 112 /** 113 * Initializes the panel from preferences 114 */ 115 public final void initFromPreferences() { 116 String authMethod = Main.pref.get("osm-server.auth-method", "basic"); 117 if ("basic".equals(authMethod)) { 118 rbBasicAuthentication.setSelected(true); 119 } else if ("oauth".equals(authMethod)) { 120 rbOAuth.setSelected(true); 121 } else { 122 Main.warn(tr("Unsupported value in preference ''{0}'', got ''{1}''. Using authentication method ''Basic Authentication''.", "osm-server.auth-method", authMethod)); 123 rbBasicAuthentication.setSelected(true); 124 } 125 pnlBasicAuthPreferences.initFromPreferences(); 126 pnlOAuthPreferences.initFromPreferences(); 127 pnlMessagesPreferences.initFromPreferences(); 128 } 129 130 /** 131 * Saves the current values to preferences 132 */ 133 public final void saveToPreferences() { 134 // save the authentication method 135 String authMethod; 136 if (rbBasicAuthentication.isSelected()) { 137 authMethod = "basic"; 138 } else { 139 authMethod = "oauth"; 140 } 141 Main.pref.put("osm-server.auth-method", authMethod); 142 if ("basic".equals(authMethod)) { 143 // save username and password and clear the OAuth token 144 pnlBasicAuthPreferences.saveToPreferences(); 145 OAuthAccessTokenHolder.getInstance().clear(); 146 OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManager.getInstance()); 147 } else if ("oauth".equals(authMethod)) { 148 // clear the password in the preferences 149 pnlBasicAuthPreferences.clearPassword(); 150 pnlBasicAuthPreferences.saveToPreferences(); 151 pnlOAuthPreferences.saveToPreferences(); 152 } 153 // save message notifications preferences. To be done after authentication preferences. 154 pnlMessagesPreferences.saveToPreferences(); 155 } 156 157 /** 158 * Listens to changes in the authentication method 159 */ 160 class AuthenticationMethodChangeListener implements ItemListener { 161 @Override 162 public void itemStateChanged(ItemEvent e) { 163 if (rbBasicAuthentication.isSelected()) { 164 pnlAuthenticationParameteters.removeAll(); 165 pnlAuthenticationParameteters.add(pnlBasicAuthPreferences, BorderLayout.CENTER); 166 pnlBasicAuthPreferences.revalidate(); 167 } else { 168 pnlAuthenticationParameteters.removeAll(); 169 pnlAuthenticationParameteters.add(pnlOAuthPreferences, BorderLayout.CENTER); 170 pnlOAuthPreferences.revalidate(); 171 } 172 repaint(); 173 } 174 } 175 176 @Override 177 public void propertyChange(PropertyChangeEvent evt) { 178 if (pnlOAuthPreferences != null) { 179 pnlOAuthPreferences.propertyChange(evt); 180 } 181 } 182}