source: josm/trunk/src/org/openstreetmap/josm/gui/oauth/AdvancedOAuthPropertiesPanel.java@ 9355

Last change on this file since 9355 was 9355, checked in by simon04, 8 years ago

fix #7943 - Make OAuth work for non-standard OSM APIs

You will need to obtain an application via
http://<server>/user/<user>/oauth_clients/new, and set the OAuth
consumer key+secret in the advanced OAuth parameters.

  • Property svn:eol-style set to native
File size: 11.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.oauth;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.GridBagConstraints;
8import java.awt.GridBagLayout;
9import java.awt.Insets;
10import java.awt.event.ItemEvent;
11import java.awt.event.ItemListener;
12
13import javax.swing.BorderFactory;
14import javax.swing.JCheckBox;
15import javax.swing.JLabel;
16import javax.swing.JOptionPane;
17
18import org.openstreetmap.josm.data.Preferences;
19import org.openstreetmap.josm.data.oauth.OAuthParameters;
20import org.openstreetmap.josm.gui.HelpAwareOptionPane;
21import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
22import org.openstreetmap.josm.gui.help.HelpUtil;
23import org.openstreetmap.josm.gui.widgets.JosmTextField;
24import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
25import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
26import org.openstreetmap.josm.tools.CheckParameterUtil;
27import org.openstreetmap.josm.tools.ImageProvider;
28
29/**
30 * Panel allowing the user to setup advanced OAuth parameters:
31 * <ul>
32 * <li>Consumer key</li>
33 * <li>Consumer secret</li>
34 * <li>Request token URL</li>
35 * <li>Access token URL</li>
36 * <li>Authorize URL</li>
37 * <li>OSM login URL</li>
38 * <li>OSM logout URL</li>
39 * </ul>
40 *
41 * @see OAuthParameters
42 * @since 2746
43 */
44public class AdvancedOAuthPropertiesPanel extends VerticallyScrollablePanel {
45
46 private JCheckBox cbUseDefaults;
47 private JosmTextField tfConsumerKey;
48 private JosmTextField tfConsumerSecret;
49 private JosmTextField tfRequestTokenURL;
50 private JosmTextField tfAccessTokenURL;
51 private JosmTextField tfAuthoriseURL;
52 private JosmTextField tfOsmLoginURL;
53 private JosmTextField tfOsmLogoutURL;
54 private transient UseDefaultItemListener ilUseDefault;
55 private String apiUrl;
56
57 protected final void build() {
58 setLayout(new GridBagLayout());
59 setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
60 GridBagConstraints gc = new GridBagConstraints();
61
62 gc.anchor = GridBagConstraints.NORTHWEST;
63 gc.fill = GridBagConstraints.HORIZONTAL;
64 gc.weightx = 1.0;
65 gc.insets = new Insets(0, 0, 3, 3);
66 gc.gridwidth = 2;
67 cbUseDefaults = new JCheckBox(tr("Use default settings"));
68 add(cbUseDefaults, gc);
69
70 // -- consumer key
71 gc.gridy = 1;
72 gc.weightx = 0.0;
73 gc.gridwidth = 1;
74 add(new JLabel(tr("Consumer Key:")), gc);
75
76 gc.gridx = 1;
77 gc.weightx = 1.0;
78 add(tfConsumerKey = new JosmTextField(), gc);
79 SelectAllOnFocusGainedDecorator.decorate(tfConsumerKey);
80
81 // -- consumer secret
82 gc.gridy = 2;
83 gc.gridx = 0;
84 gc.weightx = 0.0;
85 add(new JLabel(tr("Consumer Secret:")), gc);
86
87 gc.gridx = 1;
88 gc.weightx = 1.0;
89 add(tfConsumerSecret = new JosmTextField(), gc);
90 SelectAllOnFocusGainedDecorator.decorate(tfConsumerSecret);
91
92 // -- request token URL
93 gc.gridy = 3;
94 gc.gridx = 0;
95 gc.weightx = 0.0;
96 add(new JLabel(tr("Request Token URL:")), gc);
97
98 gc.gridx = 1;
99 gc.weightx = 1.0;
100 add(tfRequestTokenURL = new JosmTextField(), gc);
101 SelectAllOnFocusGainedDecorator.decorate(tfRequestTokenURL);
102
103 // -- access token URL
104 gc.gridy = 4;
105 gc.gridx = 0;
106 gc.weightx = 0.0;
107 add(new JLabel(tr("Access Token URL:")), gc);
108
109 gc.gridx = 1;
110 gc.weightx = 1.0;
111 add(tfAccessTokenURL = new JosmTextField(), gc);
112 SelectAllOnFocusGainedDecorator.decorate(tfAccessTokenURL);
113
114
115 // -- authorise URL
116 gc.gridy = 5;
117 gc.gridx = 0;
118 gc.weightx = 0.0;
119 add(new JLabel(tr("Authorize URL:")), gc);
120
121 gc.gridx = 1;
122 gc.weightx = 1.0;
123 add(tfAuthoriseURL = new JosmTextField(), gc);
124 SelectAllOnFocusGainedDecorator.decorate(tfAuthoriseURL);
125
126
127 // -- OSM login URL
128 gc.gridy = 6;
129 gc.gridx = 0;
130 gc.weightx = 0.0;
131 add(new JLabel(tr("OSM login URL:")), gc);
132
133 gc.gridx = 1;
134 gc.weightx = 1.0;
135 add(tfOsmLoginURL = new JosmTextField(), gc);
136 SelectAllOnFocusGainedDecorator.decorate(tfOsmLoginURL);
137
138
139 // -- OSM logout URL
140 gc.gridy = 7;
141 gc.gridx = 0;
142 gc.weightx = 0.0;
143 add(new JLabel(tr("OSM logout URL:")), gc);
144
145 gc.gridx = 1;
146 gc.weightx = 1.0;
147 add(tfOsmLogoutURL = new JosmTextField(), gc);
148 SelectAllOnFocusGainedDecorator.decorate(tfOsmLogoutURL);
149
150 cbUseDefaults.addItemListener(ilUseDefault = new UseDefaultItemListener());
151 }
152
153 protected boolean hasCustomSettings() {
154 OAuthParameters params = OAuthParameters.createDefault(apiUrl);
155 return !params.equals(getAdvancedParameters());
156 }
157
158 protected boolean confirmOverwriteCustomSettings() {
159 ButtonSpec[] buttons = new ButtonSpec[] {
160 new ButtonSpec(
161 tr("Continue"),
162 ImageProvider.get("ok"),
163 tr("Click to reset the OAuth settings to default values"),
164 null /* no dedicated help topic */
165 ),
166 new ButtonSpec(
167 tr("Cancel"),
168 ImageProvider.get("cancel"),
169 tr("Click to abort resetting to the OAuth default values"),
170 null /* no dedicated help topic */
171 )
172 };
173 int ret = HelpAwareOptionPane.showOptionDialog(
174 this,
175 tr(
176 "<html>JOSM is about to reset the OAuth settings to default values.<br>"
177 + "The current custom settings are not saved.</html>"
178 ),
179 tr("Overwrite custom OAuth settings?"),
180 JOptionPane.WARNING_MESSAGE,
181 null, /* no dedicated icon */
182 buttons,
183 buttons[0],
184 HelpUtil.ht("/Dialog/OAuthAuthorisationWizard")
185 );
186
187 return ret == 0; // OK button clicked
188 }
189
190 protected void resetToDefaultSettings() {
191 cbUseDefaults.setSelected(true);
192 OAuthParameters params = OAuthParameters.createDefault(apiUrl);
193 tfConsumerKey.setText(params.getConsumerKey());
194 tfConsumerSecret.setText(params.getConsumerSecret());
195 tfRequestTokenURL.setText(params.getRequestTokenUrl());
196 tfAccessTokenURL.setText(params.getAccessTokenUrl());
197 tfAuthoriseURL.setText(params.getAuthoriseUrl());
198 tfOsmLoginURL.setText(params.getOsmLoginUrl());
199 tfOsmLogoutURL.setText(params.getOsmLogoutUrl());
200
201 setChildComponentsEnabled(false);
202 }
203
204 protected void setChildComponentsEnabled(boolean enabled) {
205 for (Component c: getComponents()) {
206 if (c instanceof JosmTextField || c instanceof JLabel) {
207 c.setEnabled(enabled);
208 }
209 }
210 }
211
212 /**
213 * Replies the OAuth parameters currently edited in this properties panel.
214 *
215 * @return the OAuth parameters
216 */
217 public OAuthParameters getAdvancedParameters() {
218 if (cbUseDefaults.isSelected())
219 return OAuthParameters.createDefault(apiUrl);
220 return new OAuthParameters(
221 tfConsumerKey.getText(),
222 tfConsumerSecret.getText(),
223 tfRequestTokenURL.getText(),
224 tfAccessTokenURL.getText(),
225 tfAuthoriseURL.getText(),
226 tfOsmLoginURL.getText(),
227 tfOsmLogoutURL.getText());
228 }
229
230 /**
231 * Sets the advanced parameters to be displayed
232 *
233 * @param parameters the advanced parameters. Must not be null.
234 * @throws IllegalArgumentException if parameters is null.
235 */
236 public void setAdvancedParameters(OAuthParameters parameters) {
237 CheckParameterUtil.ensureParameterNotNull(parameters, "parameters");
238 if (parameters.equals(OAuthParameters.createDefault(apiUrl))) {
239 cbUseDefaults.setSelected(true);
240 setChildComponentsEnabled(false);
241 } else {
242 cbUseDefaults.setSelected(false);
243 setChildComponentsEnabled(true);
244 tfConsumerKey.setText(parameters.getConsumerKey() == null ? "" : parameters.getConsumerKey());
245 tfConsumerSecret.setText(parameters.getConsumerSecret() == null ? "" : parameters.getConsumerSecret());
246 tfRequestTokenURL.setText(parameters.getRequestTokenUrl() == null ? "" : parameters.getRequestTokenUrl());
247 tfAccessTokenURL.setText(parameters.getAccessTokenUrl() == null ? "" : parameters.getAccessTokenUrl());
248 tfAuthoriseURL.setText(parameters.getAuthoriseUrl() == null ? "" : parameters.getAuthoriseUrl());
249 tfOsmLoginURL.setText(parameters.getOsmLoginUrl() == null ? "" : parameters.getOsmLoginUrl());
250 tfOsmLogoutURL.setText(parameters.getOsmLogoutUrl() == null ? "" : parameters.getOsmLogoutUrl());
251 }
252 }
253
254 /**
255 * Constructs a new {@code AdvancedOAuthPropertiesPanel}.
256 */
257 public AdvancedOAuthPropertiesPanel() {
258 build();
259 }
260
261 /**
262 * Initializes the panel from the values in the preferences <code>preferences</code>.
263 *
264 * @param pref the preferences. Must not be null.
265 * @throws IllegalArgumentException if pref is null
266 */
267 public void initFromPreferences(Preferences pref) {
268 CheckParameterUtil.ensureParameterNotNull(pref, "pref");
269 setApiUrl(pref.get("osm-server.url"));
270 boolean useDefault = pref.getBoolean("oauth.settings.use-default", true);
271 ilUseDefault.setEnabled(false);
272 if (useDefault) {
273 resetToDefaultSettings();
274 } else {
275 setAdvancedParameters(OAuthParameters.createFromPreferences(pref));
276 }
277 ilUseDefault.setEnabled(true);
278 }
279
280 /**
281 * Remembers the current values in the preferences <code>pref</code>.
282 *
283 * @param pref the preferences. Must not be null.
284 * @throws IllegalArgumentException if pref is null.
285 */
286 public void rememberPreferences(Preferences pref) {
287 CheckParameterUtil.ensureParameterNotNull(pref, "pref");
288 pref.put("oauth.settings.use-default", cbUseDefaults.isSelected());
289 if (cbUseDefaults.isSelected()) {
290 new OAuthParameters(null, null, null, null, null, null, null).rememberPreferences(pref);
291 } else {
292 getAdvancedParameters().rememberPreferences(pref);
293 }
294 }
295
296 class UseDefaultItemListener implements ItemListener {
297 private boolean enabled;
298
299 @Override
300 public void itemStateChanged(ItemEvent e) {
301 if (!enabled) return;
302 switch (e.getStateChange()) {
303 case ItemEvent.SELECTED:
304 if (hasCustomSettings() && !confirmOverwriteCustomSettings()) {
305 cbUseDefaults.setSelected(false);
306 return;
307 }
308 resetToDefaultSettings();
309 break;
310 case ItemEvent.DESELECTED:
311 setChildComponentsEnabled(true);
312 break;
313 }
314 }
315
316 public void setEnabled(boolean enabled) {
317 this.enabled = enabled;
318 }
319 }
320
321 /**
322 * Sets the URL of the OSM API for which this panel is currently displaying OAuth properties.
323 *
324 * @param apiUrl the api URL
325 * @since 5422
326 */
327 public void setApiUrl(String apiUrl) {
328 this.apiUrl = apiUrl;
329 if (cbUseDefaults.isSelected()) {
330 resetToDefaultSettings();
331 }
332 }
333}
Note: See TracBrowser for help on using the repository browser.