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.Color;
|
---|
8 | import java.awt.FlowLayout;
|
---|
9 | import java.awt.Font;
|
---|
10 | import java.awt.GridBagConstraints;
|
---|
11 | import java.awt.GridBagLayout;
|
---|
12 | import java.awt.Insets;
|
---|
13 | import java.awt.event.ActionEvent;
|
---|
14 | import java.awt.event.ItemEvent;
|
---|
15 | import java.awt.event.ItemListener;
|
---|
16 | import java.beans.PropertyChangeEvent;
|
---|
17 | import java.beans.PropertyChangeListener;
|
---|
18 |
|
---|
19 | import javax.swing.AbstractAction;
|
---|
20 | import javax.swing.BorderFactory;
|
---|
21 | import javax.swing.JCheckBox;
|
---|
22 | import javax.swing.JLabel;
|
---|
23 | import javax.swing.JPanel;
|
---|
24 |
|
---|
25 | import org.openstreetmap.josm.Main;
|
---|
26 | import org.openstreetmap.josm.data.oauth.OAuthParameters;
|
---|
27 | import org.openstreetmap.josm.data.oauth.OAuthToken;
|
---|
28 | import org.openstreetmap.josm.gui.SideButton;
|
---|
29 | import org.openstreetmap.josm.gui.oauth.AdvancedOAuthPropertiesPanel;
|
---|
30 | import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard;
|
---|
31 | import org.openstreetmap.josm.gui.oauth.TestAccessTokenTask;
|
---|
32 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
33 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
34 | import org.openstreetmap.josm.io.OsmApi;
|
---|
35 | import org.openstreetmap.josm.io.auth.CredentialsManager;
|
---|
36 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * The preferences panel for the OAuth preferences. This just a summary panel
|
---|
40 | * showing the current Access Token Key and Access Token Secret, if the
|
---|
41 | * user already has an Access Token.
|
---|
42 | *
|
---|
43 | * For initial authorisation see {@link OAuthAuthorizationWizard}.
|
---|
44 | *
|
---|
45 | */
|
---|
46 | public class OAuthAuthenticationPreferencesPanel extends JPanel implements PropertyChangeListener {
|
---|
47 | private JPanel pnlAuthorisationMessage;
|
---|
48 | private NotYetAuthorisedPanel pnlNotYetAuthorised;
|
---|
49 | private AlreadyAuthorisedPanel pnlAlreadyAuthorised;
|
---|
50 | private AdvancedOAuthPropertiesPanel pnlAdvancedProperties;
|
---|
51 | private String apiUrl;
|
---|
52 | private JCheckBox cbShowAdvancedParameters;
|
---|
53 | private JCheckBox cbSaveToPreferences;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Builds the panel for entering the advanced OAuth parameters
|
---|
57 | *
|
---|
58 | * @return panel with advanced settings
|
---|
59 | */
|
---|
60 | protected JPanel buildAdvancedPropertiesPanel() {
|
---|
61 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
62 | GridBagConstraints gc= new GridBagConstraints();
|
---|
63 |
|
---|
64 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
65 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
66 | gc.weightx = 0.0;
|
---|
67 | gc.insets = new Insets(0,0,0,3);
|
---|
68 | pnl.add(cbShowAdvancedParameters = new JCheckBox(), gc);
|
---|
69 | cbShowAdvancedParameters.setSelected(false);
|
---|
70 | cbShowAdvancedParameters.addItemListener(
|
---|
71 | new ItemListener() {
|
---|
72 | @Override
|
---|
73 | public void itemStateChanged(ItemEvent evt) {
|
---|
74 | pnlAdvancedProperties.setVisible(evt.getStateChange() == ItemEvent.SELECTED);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | );
|
---|
78 |
|
---|
79 | gc.gridx = 1;
|
---|
80 | gc.weightx = 1.0;
|
---|
81 | JMultilineLabel lbl = new JMultilineLabel(tr("Display Advanced OAuth Parameters"));
|
---|
82 | lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN));
|
---|
83 | pnl.add(lbl, gc);
|
---|
84 |
|
---|
85 | gc.gridy = 1;
|
---|
86 | gc.gridx = 1;
|
---|
87 | gc.insets = new Insets(3,0,3,0);
|
---|
88 | gc.fill = GridBagConstraints.BOTH;
|
---|
89 | gc.weightx = 1.0;
|
---|
90 | gc.weighty = 1.0;
|
---|
91 | pnl.add(pnlAdvancedProperties = new AdvancedOAuthPropertiesPanel(), gc);
|
---|
92 | pnlAdvancedProperties.initFromPreferences(Main.pref);
|
---|
93 | pnlAdvancedProperties.setBorder(
|
---|
94 | BorderFactory.createCompoundBorder(
|
---|
95 | BorderFactory.createLineBorder(Color.GRAY, 1),
|
---|
96 | BorderFactory.createEmptyBorder(3,3,3,3)
|
---|
97 | )
|
---|
98 | );
|
---|
99 | pnlAdvancedProperties.setVisible(false);
|
---|
100 | return pnl;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * builds the UI
|
---|
105 | */
|
---|
106 | protected final void build() {
|
---|
107 | setLayout(new GridBagLayout());
|
---|
108 | setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
109 | GridBagConstraints gc = new GridBagConstraints();
|
---|
110 |
|
---|
111 | // the panel for the OAuth parameters. pnlAuthorisationMessage is an
|
---|
112 | // empty panel. It is going to be filled later, depending on the
|
---|
113 | // current OAuth state in JOSM.
|
---|
114 | gc.fill = GridBagConstraints.BOTH;
|
---|
115 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
116 | gc.weighty = 1.0;
|
---|
117 | gc.weightx = 1.0;
|
---|
118 | gc.insets = new Insets(10,0,0,0);
|
---|
119 | add(pnlAuthorisationMessage = new JPanel(), gc);
|
---|
120 | pnlAuthorisationMessage.setLayout(new BorderLayout());
|
---|
121 |
|
---|
122 | // create these two panels, they are going to be used later in refreshView
|
---|
123 | //
|
---|
124 | pnlAlreadyAuthorised = new AlreadyAuthorisedPanel();
|
---|
125 | pnlNotYetAuthorised = new NotYetAuthorisedPanel();
|
---|
126 | }
|
---|
127 |
|
---|
128 | protected void refreshView() {
|
---|
129 | pnlAuthorisationMessage.removeAll();
|
---|
130 | if (OAuthAccessTokenHolder.getInstance().containsAccessToken()) {
|
---|
131 | pnlAuthorisationMessage.add(pnlAlreadyAuthorised, BorderLayout.CENTER);
|
---|
132 | pnlAlreadyAuthorised.refreshView();
|
---|
133 | pnlAlreadyAuthorised.revalidate();
|
---|
134 | } else {
|
---|
135 | pnlAuthorisationMessage.add(pnlNotYetAuthorised, BorderLayout.CENTER);
|
---|
136 | pnlNotYetAuthorised.revalidate();
|
---|
137 | }
|
---|
138 | repaint();
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Create the panel
|
---|
143 | */
|
---|
144 | public OAuthAuthenticationPreferencesPanel() {
|
---|
145 | build();
|
---|
146 | refreshView();
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Sets the URL of the OSM API for which this panel is currently displaying OAuth properties.
|
---|
151 | *
|
---|
152 | * @param apiUrl the api URL
|
---|
153 | */
|
---|
154 | public void setApiUrl(String apiUrl) {
|
---|
155 | this.apiUrl = apiUrl;
|
---|
156 | pnlAdvancedProperties.setApiUrl(apiUrl);
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Initializes the panel from preferences
|
---|
161 | */
|
---|
162 | public void initFromPreferences() {
|
---|
163 | setApiUrl(Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL).trim());
|
---|
164 | refreshView();
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Saves the current values to preferences
|
---|
169 | */
|
---|
170 | public void saveToPreferences() {
|
---|
171 | OAuthAccessTokenHolder.getInstance().setSaveToPreferences(cbSaveToPreferences.isSelected());
|
---|
172 | OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManager.getInstance());
|
---|
173 | pnlAdvancedProperties.rememberPreferences(Main.pref);
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * The preferences panel displayed if there is currently no Access Token available.
|
---|
178 | * This means that the user didn't run through the OAuth authorisation procedure yet.
|
---|
179 | *
|
---|
180 | */
|
---|
181 | private class NotYetAuthorisedPanel extends JPanel {
|
---|
182 | protected void build() {
|
---|
183 | setLayout(new GridBagLayout());
|
---|
184 | GridBagConstraints gc = new GridBagConstraints();
|
---|
185 |
|
---|
186 | // A message explaining that the user isn't authorised yet
|
---|
187 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
188 | gc.insets = new Insets(0,0,3,0);
|
---|
189 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
190 | gc.weightx = 1.0;
|
---|
191 | JMultilineLabel lbl;
|
---|
192 | add(lbl = new JMultilineLabel(tr("You do not have an Access Token yet to access the OSM server using OAuth. Please authorize first.")), gc);
|
---|
193 | lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN));
|
---|
194 |
|
---|
195 | // Action for authorising now
|
---|
196 | gc.gridy = 1;
|
---|
197 | gc.fill = GridBagConstraints.NONE;
|
---|
198 | gc.weightx = 0.0;
|
---|
199 | add(new SideButton(new AuthoriseNowAction()), gc);
|
---|
200 |
|
---|
201 | // filler - grab remaining space
|
---|
202 | gc.gridy = 2;
|
---|
203 | gc.fill = GridBagConstraints.BOTH;
|
---|
204 | gc.weightx = 1.0;
|
---|
205 | gc.weighty = 1.0;
|
---|
206 | add(new JPanel(), gc);
|
---|
207 | }
|
---|
208 |
|
---|
209 | public NotYetAuthorisedPanel() {
|
---|
210 | build();
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * The preferences panel displayed if there is currently an AccessToken available.
|
---|
216 | *
|
---|
217 | */
|
---|
218 | private class AlreadyAuthorisedPanel extends JPanel {
|
---|
219 | private JosmTextField tfAccessTokenKey;
|
---|
220 | private JosmTextField tfAccessTokenSecret;
|
---|
221 |
|
---|
222 | protected void build() {
|
---|
223 | setLayout(new GridBagLayout());
|
---|
224 | GridBagConstraints gc = new GridBagConstraints();
|
---|
225 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
226 | gc.insets = new Insets(0,0,3,3);
|
---|
227 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
228 | gc.weightx = 1.0;
|
---|
229 | gc.gridwidth = 2;
|
---|
230 | JMultilineLabel lbl;
|
---|
231 | add(lbl = new JMultilineLabel(tr("You already have an Access Token to access the OSM server using OAuth.")), gc);
|
---|
232 | lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN));
|
---|
233 |
|
---|
234 | // -- access token key
|
---|
235 | gc.gridy = 1;
|
---|
236 | gc.gridx = 0;
|
---|
237 | gc.gridwidth = 1;
|
---|
238 | gc.weightx = 0.0;
|
---|
239 | add(new JLabel(tr("Access Token Key:")), gc);
|
---|
240 |
|
---|
241 | gc.gridx = 1;
|
---|
242 | gc.weightx = 1.0;
|
---|
243 | add(tfAccessTokenKey = new JosmTextField(), gc);
|
---|
244 | tfAccessTokenKey.setEditable(false);
|
---|
245 |
|
---|
246 | // -- access token secret
|
---|
247 | gc.gridy = 2;
|
---|
248 | gc.gridx = 0;
|
---|
249 | gc.gridwidth = 1;
|
---|
250 | gc.weightx = 0.0;
|
---|
251 | add(new JLabel(tr("Access Token Secret:")), gc);
|
---|
252 |
|
---|
253 | gc.gridx = 1;
|
---|
254 | gc.weightx = 1.0;
|
---|
255 | add(tfAccessTokenSecret = new JosmTextField(), gc);
|
---|
256 | tfAccessTokenSecret.setEditable(false);
|
---|
257 |
|
---|
258 | // -- access token secret
|
---|
259 | gc.gridy = 3;
|
---|
260 | gc.gridx = 0;
|
---|
261 | gc.gridwidth = 2;
|
---|
262 | gc.weightx = 1.0;
|
---|
263 | add(cbSaveToPreferences = new JCheckBox(tr("Save to preferences")), gc);
|
---|
264 | cbSaveToPreferences.setSelected(OAuthAccessTokenHolder.getInstance().isSaveToPreferences());
|
---|
265 |
|
---|
266 | // -- action buttons
|
---|
267 | JPanel btns = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
268 | btns.add(new SideButton(new RenewAuthorisationAction()));
|
---|
269 | btns.add(new SideButton(new TestAuthorisationAction()));
|
---|
270 | gc.gridy = 4;
|
---|
271 | gc.gridx = 0;
|
---|
272 | gc.gridwidth = 2;
|
---|
273 | gc.weightx = 1.0;
|
---|
274 | add(btns, gc);
|
---|
275 |
|
---|
276 | // the panel with the advanced options
|
---|
277 | gc.gridy = 5;
|
---|
278 | gc.gridx = 0;
|
---|
279 | gc.gridwidth = 2;
|
---|
280 | gc.weightx = 1.0;
|
---|
281 | add(buildAdvancedPropertiesPanel(), gc);
|
---|
282 |
|
---|
283 | // filler - grab the remaining space
|
---|
284 | gc.gridy = 6;
|
---|
285 | gc.fill = GridBagConstraints.BOTH;
|
---|
286 | gc.weightx = 1.0;
|
---|
287 | gc.weighty = 1.0;
|
---|
288 | add(new JPanel(), gc);
|
---|
289 |
|
---|
290 | }
|
---|
291 |
|
---|
292 | public final void refreshView() {
|
---|
293 | String v = OAuthAccessTokenHolder.getInstance().getAccessTokenKey();
|
---|
294 | tfAccessTokenKey.setText(v == null? "" : v);
|
---|
295 | v = OAuthAccessTokenHolder.getInstance().getAccessTokenSecret();
|
---|
296 | tfAccessTokenSecret.setText(v == null? "" : v);
|
---|
297 | cbSaveToPreferences.setSelected(OAuthAccessTokenHolder.getInstance().isSaveToPreferences());
|
---|
298 | }
|
---|
299 |
|
---|
300 | public AlreadyAuthorisedPanel() {
|
---|
301 | build();
|
---|
302 | refreshView();
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Action to authorise the current user
|
---|
308 | */
|
---|
309 | private class AuthoriseNowAction extends AbstractAction {
|
---|
310 | public AuthoriseNowAction() {
|
---|
311 | putValue(NAME, tr("Authorize now"));
|
---|
312 | putValue(SHORT_DESCRIPTION, tr("Click to step through the OAuth authorization process"));
|
---|
313 | putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth-small"));
|
---|
314 |
|
---|
315 | }
|
---|
316 | @Override
|
---|
317 | public void actionPerformed(ActionEvent arg0) {
|
---|
318 | OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard(
|
---|
319 | OAuthAuthenticationPreferencesPanel.this,
|
---|
320 | apiUrl
|
---|
321 | );
|
---|
322 | wizard.setVisible(true);
|
---|
323 | if (wizard.isCanceled()) return;
|
---|
324 | OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
|
---|
325 | holder.setAccessToken(wizard.getAccessToken());
|
---|
326 | holder.setSaveToPreferences(wizard.isSaveAccessTokenToPreferences());
|
---|
327 | pnlAdvancedProperties.setAdvancedParameters(wizard.getOAuthParameters());
|
---|
328 | refreshView();
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * Launches the OAuthAuthorisationWizard to generate a new Access Token
|
---|
334 | */
|
---|
335 | private class RenewAuthorisationAction extends AbstractAction {
|
---|
336 | public RenewAuthorisationAction() {
|
---|
337 | putValue(NAME, tr("New Access Token"));
|
---|
338 | putValue(SHORT_DESCRIPTION, tr("Click to step through the OAuth authorization process and generate a new Access Token"));
|
---|
339 | putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth-small"));
|
---|
340 |
|
---|
341 | }
|
---|
342 | @Override
|
---|
343 | public void actionPerformed(ActionEvent arg0) {
|
---|
344 | OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard(
|
---|
345 | OAuthAuthenticationPreferencesPanel.this,
|
---|
346 | apiUrl
|
---|
347 | );
|
---|
348 | wizard.setVisible(true);
|
---|
349 | if (wizard.isCanceled()) return;
|
---|
350 | OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
|
---|
351 | holder.setAccessToken(wizard.getAccessToken());
|
---|
352 | holder.setSaveToPreferences(wizard.isSaveAccessTokenToPreferences());
|
---|
353 | pnlAdvancedProperties.setAdvancedParameters(wizard.getOAuthParameters());
|
---|
354 | refreshView();
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | /**
|
---|
359 | * Runs a test whether we can access the OSM server with the current Access Token
|
---|
360 | */
|
---|
361 | private class TestAuthorisationAction extends AbstractAction {
|
---|
362 | /**
|
---|
363 | * Constructs a new {@code TestAuthorisationAction}.
|
---|
364 | */
|
---|
365 | public TestAuthorisationAction() {
|
---|
366 | putValue(NAME, tr("Test Access Token"));
|
---|
367 | putValue(SHORT_DESCRIPTION, tr("Click test access to the OSM server with the current access token"));
|
---|
368 | putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth-small"));
|
---|
369 |
|
---|
370 | }
|
---|
371 |
|
---|
372 | @Override
|
---|
373 | public void actionPerformed(ActionEvent evt) {
|
---|
374 | OAuthToken token = OAuthAccessTokenHolder.getInstance().getAccessToken();
|
---|
375 | OAuthParameters parameters = OAuthParameters.createFromPreferences(Main.pref);
|
---|
376 | TestAccessTokenTask task = new TestAccessTokenTask(
|
---|
377 | OAuthAuthenticationPreferencesPanel.this,
|
---|
378 | apiUrl,
|
---|
379 | parameters,
|
---|
380 | token
|
---|
381 | );
|
---|
382 | Main.worker.submit(task);
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | @Override
|
---|
387 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
388 | if (!evt.getPropertyName().equals(OsmApiUrlInputPanel.API_URL_PROP))
|
---|
389 | return;
|
---|
390 | setApiUrl((String)evt.getNewValue());
|
---|
391 | }
|
---|
392 | }
|
---|