1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.oauth;
|
---|
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 |
|
---|
17 | import javax.swing.AbstractAction;
|
---|
18 | import javax.swing.BorderFactory;
|
---|
19 | import javax.swing.JCheckBox;
|
---|
20 | import javax.swing.JLabel;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 | import javax.swing.JTextField;
|
---|
23 | import javax.swing.SwingUtilities;
|
---|
24 |
|
---|
25 | import org.openstreetmap.josm.Main;
|
---|
26 | import org.openstreetmap.josm.data.oauth.OAuthToken;
|
---|
27 | import org.openstreetmap.josm.gui.JMultilineLabel;
|
---|
28 | import org.openstreetmap.josm.gui.SideButton;
|
---|
29 | import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
|
---|
30 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
31 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
32 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * This is the UI for running a semic-automic authorisation procedure.
|
---|
36 | *
|
---|
37 | * In contrast to the fully-automatic procedure the user is dispatched to an
|
---|
38 | * external browser for login and authorisation.
|
---|
39 | */
|
---|
40 | public class SemiAutomaticAuthorizationUI extends AbstractAuthorizationUI {
|
---|
41 | private AccessTokenInfoPanel pnlAccessTokenInfo;
|
---|
42 | private OAuthToken requestToken;
|
---|
43 |
|
---|
44 | private RetrieveRequestTokenPanel pnlRetrieveRequestToken;
|
---|
45 | private RetrieveAccessTokenPanel pnlRetrieveAccessToken;
|
---|
46 | private ShowAccessTokenPanel pnlShowAccessToken;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * build the UI
|
---|
50 | */
|
---|
51 | protected void build() {
|
---|
52 | setLayout(new BorderLayout());
|
---|
53 | setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
54 | pnlRetrieveRequestToken = new RetrieveRequestTokenPanel();
|
---|
55 | pnlRetrieveAccessToken = new RetrieveAccessTokenPanel();
|
---|
56 | pnlShowAccessToken = new ShowAccessTokenPanel();
|
---|
57 | add(pnlRetrieveRequestToken, BorderLayout.CENTER);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public SemiAutomaticAuthorizationUI() {
|
---|
61 | build();
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public boolean isSaveAccessTokenToPreferences() {
|
---|
66 | return pnlAccessTokenInfo.isSaveToPreferences();
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected void transitionToRetrieveAccessToken() {
|
---|
70 | OsmOAuthAuthorizationClient client = new OsmOAuthAuthorizationClient(
|
---|
71 | getAdvancedPropertiesPanel().getAdvancedParameters()
|
---|
72 | );
|
---|
73 | String authoriseUrl = client.getAuthoriseUrl(requestToken);
|
---|
74 | OpenBrowser.displayUrl(authoriseUrl);
|
---|
75 |
|
---|
76 | removeAll();
|
---|
77 | pnlRetrieveAccessToken.setAuthoriseUrl(authoriseUrl);
|
---|
78 | add(pnlRetrieveAccessToken, BorderLayout.CENTER);
|
---|
79 | pnlRetrieveAccessToken.invalidate();
|
---|
80 | validate();
|
---|
81 | repaint();
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected void transitionToRetrieveRequestToken() {
|
---|
85 | requestToken = null;
|
---|
86 | setAccessToken(null);
|
---|
87 | removeAll();
|
---|
88 | add(pnlRetrieveRequestToken, BorderLayout.CENTER);
|
---|
89 | pnlRetrieveRequestToken.invalidate();
|
---|
90 | validate();
|
---|
91 | repaint();
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected void transitionToShowAccessToken() {
|
---|
95 | removeAll();
|
---|
96 | add(pnlShowAccessToken, BorderLayout.CENTER);
|
---|
97 | pnlShowAccessToken.invalidate();
|
---|
98 | validate();
|
---|
99 | repaint();
|
---|
100 | pnlShowAccessToken.setAccessToken(getAccessToken());
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * This is the panel displayed in the first step of the semi-automatic authorisation
|
---|
105 | * process.
|
---|
106 | */
|
---|
107 | private class RetrieveRequestTokenPanel extends JPanel {
|
---|
108 | private JCheckBox cbShowAdvancedParameters;
|
---|
109 |
|
---|
110 | protected JPanel buildAdvancedParametersPanel() {
|
---|
111 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
112 | GridBagConstraints gc= new GridBagConstraints();
|
---|
113 |
|
---|
114 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
115 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
116 | gc.weightx = 0.0;
|
---|
117 | gc.insets = new Insets(0,0,0,3);
|
---|
118 | pnl.add(cbShowAdvancedParameters = new JCheckBox(), gc);
|
---|
119 | cbShowAdvancedParameters.setSelected(false);
|
---|
120 | cbShowAdvancedParameters.addItemListener(
|
---|
121 | new ItemListener() {
|
---|
122 | public void itemStateChanged(ItemEvent evt) {
|
---|
123 | getAdvancedPropertiesPanel().setVisible(evt.getStateChange() == ItemEvent.SELECTED);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | );
|
---|
127 |
|
---|
128 | gc.gridx = 1;
|
---|
129 | gc.weightx = 1.0;
|
---|
130 | JMultilineLabel lbl = new JMultilineLabel(tr("Display Advanced OAuth Parameters"));
|
---|
131 | lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN));
|
---|
132 | pnl.add(lbl, gc);
|
---|
133 |
|
---|
134 | gc.gridy = 1;
|
---|
135 | gc.gridx = 1;
|
---|
136 | gc.insets = new Insets(3,0,3,0);
|
---|
137 | gc.fill = GridBagConstraints.BOTH;
|
---|
138 | gc.weightx = 1.0;
|
---|
139 | gc.weighty = 1.0;
|
---|
140 | pnl.add(getAdvancedPropertiesPanel(), gc);
|
---|
141 | getAdvancedPropertiesPanel().setBorder(
|
---|
142 | BorderFactory.createCompoundBorder(
|
---|
143 | BorderFactory.createLineBorder(Color.GRAY, 1),
|
---|
144 | BorderFactory.createEmptyBorder(3,3,3,3)
|
---|
145 | )
|
---|
146 | );
|
---|
147 | getAdvancedPropertiesPanel().setVisible(false);
|
---|
148 | return pnl;
|
---|
149 | }
|
---|
150 |
|
---|
151 | protected JPanel buildCommandPanel() {
|
---|
152 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
153 | GridBagConstraints gc= new GridBagConstraints();
|
---|
154 |
|
---|
155 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
156 | gc.fill = GridBagConstraints.BOTH;
|
---|
157 | gc.weightx = 1.0;
|
---|
158 | gc.weighty = 1.0;
|
---|
159 | gc.insets = new Insets(0,0,0,3);
|
---|
160 |
|
---|
161 |
|
---|
162 | HtmlPanel h = new HtmlPanel();
|
---|
163 | h.setText(tr("<html>"
|
---|
164 | + "Please click on <strong>{0}</strong> to retrieve an OAuth Request Token from "
|
---|
165 | + "''{1}''.</html>",
|
---|
166 | tr("Retrieve Request Token"),
|
---|
167 | getAdvancedPropertiesPanel().getAdvancedParameters().getRequestTokenUrl()
|
---|
168 | ));
|
---|
169 | pnl.add(h, gc);
|
---|
170 |
|
---|
171 | JPanel pnl1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
172 | pnl1.add(new SideButton(new RetrieveRequestTokenAction()));
|
---|
173 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
174 | gc.weightx = 1.0;
|
---|
175 | gc.gridy = 1;
|
---|
176 | pnl.add(pnl1, gc);
|
---|
177 | return pnl;
|
---|
178 |
|
---|
179 | }
|
---|
180 | protected void build() {
|
---|
181 | setLayout(new BorderLayout(0,5));
|
---|
182 | JLabel lbl = new JLabel(tr("<html>Step 1/3: Retrieve an OAuth Request Token</html>"));
|
---|
183 | lbl.setFont(lbl.getFont().deriveFont(16f));
|
---|
184 | add(lbl, BorderLayout.NORTH);
|
---|
185 | add(buildAdvancedParametersPanel(), BorderLayout.CENTER);
|
---|
186 | add(buildCommandPanel(), BorderLayout.SOUTH);
|
---|
187 | }
|
---|
188 |
|
---|
189 | public RetrieveRequestTokenPanel() {
|
---|
190 | build();
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * This is the panel displayed in the second step of the semi-automatic authorization
|
---|
197 | * process.
|
---|
198 | */
|
---|
199 | private class RetrieveAccessTokenPanel extends JPanel {
|
---|
200 |
|
---|
201 | private JTextField tfAuthoriseUrl;
|
---|
202 |
|
---|
203 | protected JPanel buildTitlePanel() {
|
---|
204 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
205 | JLabel lbl = new JLabel(tr("<html>Step 2/3: Authorize and retrieve an Access Token</html>"));
|
---|
206 | lbl.setFont(lbl.getFont().deriveFont(16f));
|
---|
207 | pnl.add(lbl, BorderLayout.CENTER);
|
---|
208 | return pnl;
|
---|
209 | }
|
---|
210 |
|
---|
211 | protected JPanel buildContentPanel() {
|
---|
212 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
213 | GridBagConstraints gc = new GridBagConstraints();
|
---|
214 |
|
---|
215 | gc.anchor= GridBagConstraints.NORTHWEST;
|
---|
216 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
217 | gc.weightx = 1.0;
|
---|
218 | gc.gridwidth = 2;
|
---|
219 | HtmlPanel html = new HtmlPanel();
|
---|
220 | html.setText(tr("<html>"
|
---|
221 | + "JOSM successfully retrieved a Request Token. "
|
---|
222 | + "JOSM is now launching an authorization page in an external browser. "
|
---|
223 | + "Please login with your OSM username and password and follow the instructions "
|
---|
224 | + "to authorize the Request Token. Then switch back to this dialog and click on "
|
---|
225 | + "<strong>{0}</strong><br><br>"
|
---|
226 | + "If launching the external browser fails you can copy the following authorize URL "
|
---|
227 | + "and paste it into the address field of your browser.</html>",
|
---|
228 | tr("Request Access Token")
|
---|
229 | ));
|
---|
230 | pnl.add(html, gc);
|
---|
231 |
|
---|
232 | gc.gridx = 0;
|
---|
233 | gc.gridy = 1;
|
---|
234 | gc.weightx = 0.0;
|
---|
235 | gc.gridwidth = 1;
|
---|
236 | pnl.add(new JLabel(tr("Authorize URL:")), gc);
|
---|
237 |
|
---|
238 | gc.gridx = 1;
|
---|
239 | gc.weightx = 1.0;
|
---|
240 | pnl.add(tfAuthoriseUrl = new JTextField(), gc);
|
---|
241 | tfAuthoriseUrl.setEditable(false);
|
---|
242 |
|
---|
243 | return pnl;
|
---|
244 | }
|
---|
245 |
|
---|
246 | protected JPanel buildActionPanel() {
|
---|
247 | JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
248 |
|
---|
249 | pnl.add(new SideButton(new BackAction()));
|
---|
250 | pnl.add(new SideButton(new RetrieveAccessTokenAction()));
|
---|
251 | return pnl;
|
---|
252 | }
|
---|
253 |
|
---|
254 | protected void build() {
|
---|
255 | setLayout(new BorderLayout());
|
---|
256 | add(buildTitlePanel(), BorderLayout.NORTH);
|
---|
257 | add(buildContentPanel(), BorderLayout.CENTER);
|
---|
258 | add(buildActionPanel(), BorderLayout.SOUTH);
|
---|
259 | }
|
---|
260 |
|
---|
261 | public RetrieveAccessTokenPanel() {
|
---|
262 | build();
|
---|
263 | }
|
---|
264 |
|
---|
265 | public void setAuthoriseUrl(String url) {
|
---|
266 | tfAuthoriseUrl.setText(url);
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Action to go back to step 1 in the process
|
---|
271 | */
|
---|
272 | class BackAction extends AbstractAction {
|
---|
273 | public BackAction() {
|
---|
274 | putValue(NAME, tr("Back"));
|
---|
275 | putValue(SHORT_DESCRIPTION, tr("Go back to step 1/3"));
|
---|
276 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "previous"));
|
---|
277 | }
|
---|
278 |
|
---|
279 | public void actionPerformed(ActionEvent arg0) {
|
---|
280 | transitionToRetrieveRequestToken();
|
---|
281 | }
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * Displays the retrieved Access Token in step 3.
|
---|
287 | */
|
---|
288 | class ShowAccessTokenPanel extends JPanel {
|
---|
289 |
|
---|
290 | protected JPanel buildTitlePanel() {
|
---|
291 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
292 | JLabel lbl = new JLabel(tr("<html>Step 3/3: Successfully retrieved an Access Token</html>"));
|
---|
293 | lbl.setFont(lbl.getFont().deriveFont(16f));
|
---|
294 | pnl.add(lbl, BorderLayout.CENTER);
|
---|
295 | return pnl;
|
---|
296 | }
|
---|
297 |
|
---|
298 | protected JPanel buildContentPanel() {
|
---|
299 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
300 | GridBagConstraints gc = new GridBagConstraints();
|
---|
301 |
|
---|
302 | gc.anchor= GridBagConstraints.NORTHWEST;
|
---|
303 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
304 | gc.weightx = 1.0;
|
---|
305 | HtmlPanel html = new HtmlPanel();
|
---|
306 | html.setText(tr("<html>"
|
---|
307 | + "JOSM has successfully retrieved an Access Token. "
|
---|
308 | + "You can now accept this token. JOSM will use it in the future for authentication "
|
---|
309 | + "and authorization to the OSM server.<br><br>"
|
---|
310 | + "The access token is: </html>"
|
---|
311 | ));
|
---|
312 | pnl.add(html, gc);
|
---|
313 |
|
---|
314 | gc.gridx = 0;
|
---|
315 | gc.gridy = 1;
|
---|
316 | gc.weightx = 1.0;
|
---|
317 | gc.gridwidth = 1;
|
---|
318 | pnl.add(pnlAccessTokenInfo = new AccessTokenInfoPanel(), gc);
|
---|
319 | pnlAccessTokenInfo.setSaveToPreferences(
|
---|
320 | OAuthAccessTokenHolder.getInstance().isSaveToPreferences()
|
---|
321 | );
|
---|
322 | return pnl;
|
---|
323 | }
|
---|
324 |
|
---|
325 | protected JPanel buildActionPanel() {
|
---|
326 | JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
327 | pnl.add(new SideButton(new RestartAction()));
|
---|
328 | pnl.add(new SideButton(new TestAccessTokenAction()));
|
---|
329 | return pnl;
|
---|
330 | }
|
---|
331 |
|
---|
332 | protected void build() {
|
---|
333 | setLayout(new BorderLayout());
|
---|
334 | add(buildTitlePanel(), BorderLayout.NORTH);
|
---|
335 | add(buildContentPanel(), BorderLayout.CENTER);
|
---|
336 | add(buildActionPanel(), BorderLayout.SOUTH);
|
---|
337 | }
|
---|
338 |
|
---|
339 | public ShowAccessTokenPanel() {
|
---|
340 | build();
|
---|
341 | }
|
---|
342 |
|
---|
343 | /**
|
---|
344 | * Action to go back to step 1 in the process
|
---|
345 | */
|
---|
346 | class RestartAction extends AbstractAction {
|
---|
347 | public RestartAction() {
|
---|
348 | putValue(NAME, tr("Restart"));
|
---|
349 | putValue(SHORT_DESCRIPTION, tr("Go back to step 1/3"));
|
---|
350 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "previous"));
|
---|
351 | }
|
---|
352 |
|
---|
353 | public void actionPerformed(ActionEvent arg0) {
|
---|
354 | transitionToRetrieveRequestToken();
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | public void setAccessToken(OAuthToken accessToken) {
|
---|
359 | pnlAccessTokenInfo.setAccessToken(accessToken);
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 | /**
|
---|
364 | * Action for retrieving a request token
|
---|
365 | */
|
---|
366 | class RetrieveRequestTokenAction extends AbstractAction{
|
---|
367 |
|
---|
368 | public RetrieveRequestTokenAction() {
|
---|
369 | putValue(NAME, tr("Retrieve Request Token"));
|
---|
370 | putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth"));
|
---|
371 | putValue(SHORT_DESCRIPTION, tr("Click to retrieve a Request Token"));
|
---|
372 | }
|
---|
373 |
|
---|
374 | public void actionPerformed(ActionEvent evt) {
|
---|
375 | final RetrieveRequestTokenTask task = new RetrieveRequestTokenTask(
|
---|
376 | SemiAutomaticAuthorizationUI.this,
|
---|
377 | getAdvancedPropertiesPanel().getAdvancedParameters()
|
---|
378 | );
|
---|
379 | Main.worker.submit(task);
|
---|
380 | Runnable r = new Runnable() {
|
---|
381 | public void run() {
|
---|
382 | if (task.isCanceled()) return;
|
---|
383 | if (task.getRequestToken() == null) return;
|
---|
384 | requestToken = task.getRequestToken();
|
---|
385 | SwingUtilities.invokeLater(new Runnable() {
|
---|
386 | public void run() {
|
---|
387 | transitionToRetrieveAccessToken();
|
---|
388 | }
|
---|
389 | });
|
---|
390 | }
|
---|
391 | };
|
---|
392 | Main.worker.submit(r);
|
---|
393 | }
|
---|
394 | }
|
---|
395 |
|
---|
396 | /**
|
---|
397 | * Action for retrieving an Access Token
|
---|
398 | */
|
---|
399 | class RetrieveAccessTokenAction extends AbstractAction {
|
---|
400 |
|
---|
401 | public RetrieveAccessTokenAction() {
|
---|
402 | putValue(NAME, tr("Retrieve Access Token"));
|
---|
403 | putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth"));
|
---|
404 | putValue(SHORT_DESCRIPTION, tr("Click to retrieve an Access Token"));
|
---|
405 | }
|
---|
406 |
|
---|
407 | public void actionPerformed(ActionEvent evt) {
|
---|
408 | final RetrieveAccessTokenTask task = new RetrieveAccessTokenTask(
|
---|
409 | SemiAutomaticAuthorizationUI.this,
|
---|
410 | getAdvancedPropertiesPanel().getAdvancedParameters(),
|
---|
411 | requestToken
|
---|
412 | );
|
---|
413 | Main.worker.submit(task);
|
---|
414 | Runnable r = new Runnable() {
|
---|
415 | public void run() {
|
---|
416 | if (task.isCanceled()) return;
|
---|
417 | if (task.getAccessToken() == null) return;
|
---|
418 | setAccessToken(task.getAccessToken());
|
---|
419 | SwingUtilities.invokeLater(new Runnable() {
|
---|
420 | public void run() {
|
---|
421 | transitionToShowAccessToken();
|
---|
422 | }
|
---|
423 | });
|
---|
424 | }
|
---|
425 | };
|
---|
426 | Main.worker.submit(r);
|
---|
427 | }
|
---|
428 | }
|
---|
429 |
|
---|
430 | /**
|
---|
431 | * Action for testing an Access Token
|
---|
432 | */
|
---|
433 | class TestAccessTokenAction extends AbstractAction {
|
---|
434 |
|
---|
435 | public TestAccessTokenAction() {
|
---|
436 | putValue(NAME, tr("Test Access Token"));
|
---|
437 | putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth"));
|
---|
438 | putValue(SHORT_DESCRIPTION, tr("Click to test the Access Token"));
|
---|
439 | }
|
---|
440 |
|
---|
441 | public void actionPerformed(ActionEvent evt) {
|
---|
442 | TestAccessTokenTask task = new TestAccessTokenTask(
|
---|
443 | SemiAutomaticAuthorizationUI.this,
|
---|
444 | getApiUrl(),
|
---|
445 | getAdvancedPropertiesPanel().getAdvancedParameters(),
|
---|
446 | getAccessToken()
|
---|
447 | );
|
---|
448 | Main.worker.submit(task);
|
---|
449 | }
|
---|
450 | }
|
---|
451 | }
|
---|