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.Component;
|
---|
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.io.IOException;
|
---|
15 | import java.net.Authenticator.RequestorType;
|
---|
16 | import java.net.PasswordAuthentication;
|
---|
17 |
|
---|
18 | import javax.swing.AbstractAction;
|
---|
19 | import javax.swing.BorderFactory;
|
---|
20 | import javax.swing.JLabel;
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 | import javax.swing.JPanel;
|
---|
23 | import javax.swing.JTabbedPane;
|
---|
24 | import javax.swing.event.DocumentEvent;
|
---|
25 | import javax.swing.event.DocumentListener;
|
---|
26 | import javax.swing.text.JTextComponent;
|
---|
27 | import javax.swing.text.html.HTMLEditorKit;
|
---|
28 |
|
---|
29 | import org.openstreetmap.josm.Main;
|
---|
30 | import org.openstreetmap.josm.data.Preferences;
|
---|
31 | import org.openstreetmap.josm.data.oauth.OAuthToken;
|
---|
32 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
33 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
34 | import org.openstreetmap.josm.gui.SideButton;
|
---|
35 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
36 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
37 | import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
|
---|
38 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
39 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
40 | import org.openstreetmap.josm.gui.widgets.JosmPasswordField;
|
---|
41 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
42 | import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
|
---|
43 | import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
|
---|
44 | import org.openstreetmap.josm.io.OsmApi;
|
---|
45 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
46 | import org.openstreetmap.josm.io.auth.CredentialsAgent;
|
---|
47 | import org.openstreetmap.josm.io.auth.CredentialsAgentException;
|
---|
48 | import org.openstreetmap.josm.io.auth.CredentialsManager;
|
---|
49 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
50 | import org.xml.sax.SAXException;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * This is an UI which supports a JOSM user to get an OAuth Access Token in a fully
|
---|
54 | * automatic process.
|
---|
55 | *
|
---|
56 | * @since 2746
|
---|
57 | */
|
---|
58 | public class FullyAutomaticAuthorizationUI extends AbstractAuthorizationUI {
|
---|
59 |
|
---|
60 | private JosmTextField tfUserName;
|
---|
61 | private JosmPasswordField tfPassword;
|
---|
62 | private UserNameValidator valUserName;
|
---|
63 | private PasswordValidator valPassword;
|
---|
64 | private AccessTokenInfoPanel pnlAccessTokenInfo;
|
---|
65 | private OsmPrivilegesPanel pnlOsmPrivileges;
|
---|
66 | private JPanel pnlPropertiesPanel;
|
---|
67 | private JPanel pnlActionButtonsPanel;
|
---|
68 | private JPanel pnlResult;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Builds the panel with the three privileges the user can grant JOSM
|
---|
72 | *
|
---|
73 | * @return constructed panel for the privileges
|
---|
74 | */
|
---|
75 | protected VerticallyScrollablePanel buildGrantsPanel() {
|
---|
76 | pnlOsmPrivileges = new OsmPrivilegesPanel();
|
---|
77 | return pnlOsmPrivileges;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Builds the panel for entering the username and password
|
---|
82 | *
|
---|
83 | * @return constructed panel for the creditentials
|
---|
84 | */
|
---|
85 | protected VerticallyScrollablePanel buildUserNamePasswordPanel() {
|
---|
86 | VerticallyScrollablePanel pnl = new VerticallyScrollablePanel(new GridBagLayout());
|
---|
87 | GridBagConstraints gc = new GridBagConstraints();
|
---|
88 | pnl.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
89 |
|
---|
90 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
91 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
92 | gc.weightx = 1.0;
|
---|
93 | gc.gridwidth = 2;
|
---|
94 | HtmlPanel pnlMessage = new HtmlPanel();
|
---|
95 | HTMLEditorKit kit = (HTMLEditorKit)pnlMessage.getEditorPane().getEditorKit();
|
---|
96 | kit.getStyleSheet().addRule(".warning-body {background-color:rgb(253,255,221);padding: 10pt; border-color:rgb(128,128,128);border-style: solid;border-width: 1px;}");
|
---|
97 | kit.getStyleSheet().addRule("ol {margin-left: 1cm}");
|
---|
98 | pnlMessage.setText("<html><body><p class=\"warning-body\">"
|
---|
99 | + tr("Please enter your OSM user name and password. The password will <strong>not</strong> be saved "
|
---|
100 | + "in clear text in the JOSM preferences and it will be submitted to the OSM server <strong>only once</strong>. "
|
---|
101 | + "Subsequent data upload requests don''t use your password any more.")
|
---|
102 | + "</p>"
|
---|
103 | + "</body></html>");
|
---|
104 | pnl.add(pnlMessage, gc);
|
---|
105 |
|
---|
106 | // the user name input field
|
---|
107 | gc.gridy = 1;
|
---|
108 | gc.gridwidth = 1;
|
---|
109 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
110 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
111 | gc.weightx = 0.0;
|
---|
112 | gc.insets = new Insets(0,0,3,3);
|
---|
113 | pnl.add(new JLabel(tr("Username: ")), gc);
|
---|
114 |
|
---|
115 | gc.gridx = 1;
|
---|
116 | gc.weightx = 1.0;
|
---|
117 | pnl.add(tfUserName = new JosmTextField(), gc);
|
---|
118 | SelectAllOnFocusGainedDecorator.decorate(tfUserName);
|
---|
119 | valUserName = new UserNameValidator(tfUserName);
|
---|
120 | valUserName.validate();
|
---|
121 |
|
---|
122 | // the password input field
|
---|
123 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
124 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
125 | gc.gridy = 2;
|
---|
126 | gc.gridx = 0;
|
---|
127 | gc.weightx = 0.0;
|
---|
128 | pnl.add(new JLabel(tr("Password: ")), gc);
|
---|
129 |
|
---|
130 | gc.gridx = 1;
|
---|
131 | gc.weightx = 1.0;
|
---|
132 | pnl.add(tfPassword = new JosmPasswordField(), gc);
|
---|
133 | SelectAllOnFocusGainedDecorator.decorate(tfPassword);
|
---|
134 | valPassword = new PasswordValidator(tfPassword);
|
---|
135 | valPassword.validate();
|
---|
136 |
|
---|
137 | gc.gridy = 3;
|
---|
138 | gc.gridx = 0;
|
---|
139 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
140 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
141 | gc.weightx = 1.0;
|
---|
142 | gc.gridwidth = 2;
|
---|
143 | pnlMessage = new HtmlPanel();
|
---|
144 | kit = (HTMLEditorKit)pnlMessage.getEditorPane().getEditorKit();
|
---|
145 | kit.getStyleSheet().addRule(".warning-body {background-color:rgb(253,255,221);padding: 10pt; border-color:rgb(128,128,128);border-style: solid;border-width: 1px;}");
|
---|
146 | kit.getStyleSheet().addRule("ol {margin-left: 1cm}");
|
---|
147 | pnlMessage.setText("<html><body>"
|
---|
148 | + "<p class=\"warning-body\">"
|
---|
149 | + tr("<strong>Warning:</strong> JOSM does login <strong>once</strong> using a secure connection.")
|
---|
150 | + "</p>"
|
---|
151 | + "</body></html>");
|
---|
152 | pnl.add(pnlMessage, gc);
|
---|
153 |
|
---|
154 | // filler - grab remaining space
|
---|
155 | gc.gridy = 4;
|
---|
156 | gc.gridwidth = 2;
|
---|
157 | gc.fill = GridBagConstraints.BOTH;
|
---|
158 | gc.weightx = 1.0;
|
---|
159 | gc.weighty = 1.0;
|
---|
160 | pnl.add(new JPanel(), gc);
|
---|
161 |
|
---|
162 | return pnl;
|
---|
163 | }
|
---|
164 |
|
---|
165 | protected JPanel buildPropertiesPanel() {
|
---|
166 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
167 |
|
---|
168 | JTabbedPane tpProperties = new JTabbedPane();
|
---|
169 | tpProperties.add(buildUserNamePasswordPanel().getVerticalScrollPane());
|
---|
170 | tpProperties.add(buildGrantsPanel().getVerticalScrollPane());
|
---|
171 | tpProperties.add(getAdvancedPropertiesPanel().getVerticalScrollPane());
|
---|
172 | tpProperties.setTitleAt(0, tr("Basic"));
|
---|
173 | tpProperties.setTitleAt(1, tr("Granted rights"));
|
---|
174 | tpProperties.setTitleAt(2, tr("Advanced OAuth properties"));
|
---|
175 |
|
---|
176 | pnl.add(tpProperties, BorderLayout.CENTER);
|
---|
177 | return pnl;
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Initializes the panel with values from the preferences
|
---|
182 | * @param pref Preferences structure
|
---|
183 | */
|
---|
184 | @Override
|
---|
185 | public void initFromPreferences(Preferences pref) {
|
---|
186 | super.initFromPreferences(pref);
|
---|
187 | CredentialsAgent cm = CredentialsManager.getInstance();
|
---|
188 | try {
|
---|
189 | PasswordAuthentication pa = cm.lookup(RequestorType.SERVER, OsmApi.getOsmApi().getHost());
|
---|
190 | if (pa == null) {
|
---|
191 | tfUserName.setText("");
|
---|
192 | tfPassword.setText("");
|
---|
193 | } else {
|
---|
194 | tfUserName.setText(pa.getUserName() == null ? "" : pa.getUserName());
|
---|
195 | tfPassword.setText(pa.getPassword() == null ? "" : String.valueOf(pa.getPassword()));
|
---|
196 | }
|
---|
197 | } catch(CredentialsAgentException e) {
|
---|
198 | Main.error(e);
|
---|
199 | tfUserName.setText("");
|
---|
200 | tfPassword.setText("");
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Builds the panel with the action button for starting the authorisation
|
---|
206 | *
|
---|
207 | * @return constructed button panel
|
---|
208 | */
|
---|
209 | protected JPanel buildActionButtonPanel() {
|
---|
210 | JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
211 |
|
---|
212 | RunAuthorisationAction runAuthorisationAction= new RunAuthorisationAction();
|
---|
213 | tfPassword.getDocument().addDocumentListener(runAuthorisationAction);
|
---|
214 | tfUserName.getDocument().addDocumentListener(runAuthorisationAction);
|
---|
215 | pnl.add(new SideButton(runAuthorisationAction));
|
---|
216 | return pnl;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Builds the panel which displays the generated Access Token.
|
---|
221 | *
|
---|
222 | * @return constructed panel for the results
|
---|
223 | */
|
---|
224 | protected JPanel buildResultsPanel() {
|
---|
225 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
226 | GridBagConstraints gc = new GridBagConstraints();
|
---|
227 | pnl.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
228 |
|
---|
229 | // the message panel
|
---|
230 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
231 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
232 | gc.weightx = 1.0;
|
---|
233 | JMultilineLabel msg = new JMultilineLabel("");
|
---|
234 | msg.setFont(msg.getFont().deriveFont(Font.PLAIN));
|
---|
235 | String lbl = tr("Accept Access Token");
|
---|
236 | msg.setText(tr("<html>"
|
---|
237 | + "You have successfully retrieved an OAuth Access Token from the OSM website. "
|
---|
238 | + "Click on <strong>{0}</strong> to accept the token. JOSM will use it in "
|
---|
239 | + "subsequent requests to gain access to the OSM API."
|
---|
240 | + "</html>",lbl));
|
---|
241 | pnl.add(msg, gc);
|
---|
242 |
|
---|
243 | // infos about the access token
|
---|
244 | gc.gridy = 1;
|
---|
245 | gc.insets = new Insets(5,0,0,0);
|
---|
246 | pnl.add(pnlAccessTokenInfo = new AccessTokenInfoPanel(), gc);
|
---|
247 |
|
---|
248 | // the actions
|
---|
249 | JPanel pnl1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
250 | pnl1.add(new SideButton(new BackAction()));
|
---|
251 | pnl1.add(new SideButton(new TestAccessTokenAction()));
|
---|
252 | gc.gridy = 2;
|
---|
253 | pnl.add(pnl1, gc);
|
---|
254 |
|
---|
255 | // filler - grab the remaining space
|
---|
256 | gc.gridy = 3;
|
---|
257 | gc.fill = GridBagConstraints.BOTH;
|
---|
258 | gc.weightx = 1.0;
|
---|
259 | gc.weighty = 1.0;
|
---|
260 | pnl.add(new JPanel(), gc);
|
---|
261 |
|
---|
262 | return pnl;
|
---|
263 | }
|
---|
264 |
|
---|
265 | protected final void build() {
|
---|
266 | setLayout(new BorderLayout());
|
---|
267 | pnlPropertiesPanel = buildPropertiesPanel();
|
---|
268 | pnlActionButtonsPanel = buildActionButtonPanel();
|
---|
269 | pnlResult = buildResultsPanel();
|
---|
270 |
|
---|
271 | prepareUIForEnteringRequest();
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Prepares the UI for the first step in the automatic process: entering the authentication
|
---|
276 | * and authorisation parameters.
|
---|
277 | *
|
---|
278 | */
|
---|
279 | protected void prepareUIForEnteringRequest() {
|
---|
280 | removeAll();
|
---|
281 | add(pnlPropertiesPanel, BorderLayout.CENTER);
|
---|
282 | add(pnlActionButtonsPanel, BorderLayout.SOUTH);
|
---|
283 | pnlPropertiesPanel.revalidate();
|
---|
284 | pnlActionButtonsPanel.revalidate();
|
---|
285 | validate();
|
---|
286 | repaint();
|
---|
287 |
|
---|
288 | setAccessToken(null);
|
---|
289 | }
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Prepares the UI for the second step in the automatic process: displaying the access token
|
---|
293 | *
|
---|
294 | */
|
---|
295 | protected void prepareUIForResultDisplay() {
|
---|
296 | removeAll();
|
---|
297 | add(pnlResult, BorderLayout.CENTER);
|
---|
298 | validate();
|
---|
299 | repaint();
|
---|
300 | }
|
---|
301 |
|
---|
302 | protected String getOsmUserName() {
|
---|
303 | return tfUserName.getText();
|
---|
304 | }
|
---|
305 |
|
---|
306 | protected String getOsmPassword() {
|
---|
307 | return String.valueOf(tfPassword.getPassword());
|
---|
308 | }
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Constructs a new {@code FullyAutomaticAuthorizationUI} for the given API URL.
|
---|
312 | * @param apiUrl The OSM API URL
|
---|
313 | * @since 5422
|
---|
314 | */
|
---|
315 | public FullyAutomaticAuthorizationUI(String apiUrl) {
|
---|
316 | super(apiUrl);
|
---|
317 | build();
|
---|
318 | }
|
---|
319 |
|
---|
320 | @Override
|
---|
321 | public boolean isSaveAccessTokenToPreferences() {
|
---|
322 | return pnlAccessTokenInfo.isSaveToPreferences();
|
---|
323 | }
|
---|
324 |
|
---|
325 | @Override
|
---|
326 | protected void setAccessToken(OAuthToken accessToken) {
|
---|
327 | super.setAccessToken(accessToken);
|
---|
328 | pnlAccessTokenInfo.setAccessToken(accessToken);
|
---|
329 | }
|
---|
330 |
|
---|
331 | /**
|
---|
332 | * Starts the authorisation process
|
---|
333 | */
|
---|
334 | class RunAuthorisationAction extends AbstractAction implements DocumentListener{
|
---|
335 | public RunAuthorisationAction() {
|
---|
336 | putValue(NAME, tr("Authorize now"));
|
---|
337 | putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth"));
|
---|
338 | putValue(SHORT_DESCRIPTION, tr("Click to redirect you to the authorization form on the JOSM web site"));
|
---|
339 | updateEnabledState();
|
---|
340 | }
|
---|
341 |
|
---|
342 | @Override
|
---|
343 | public void actionPerformed(ActionEvent evt) {
|
---|
344 | Main.worker.submit(new FullyAutomaticAuthorisationTask(FullyAutomaticAuthorizationUI.this));
|
---|
345 | }
|
---|
346 |
|
---|
347 | protected final void updateEnabledState() {
|
---|
348 | setEnabled(valPassword.isValid() && valUserName.isValid());
|
---|
349 | }
|
---|
350 |
|
---|
351 | @Override
|
---|
352 | public void changedUpdate(DocumentEvent e) {
|
---|
353 | updateEnabledState();
|
---|
354 | }
|
---|
355 |
|
---|
356 | @Override
|
---|
357 | public void insertUpdate(DocumentEvent e) {
|
---|
358 | updateEnabledState();
|
---|
359 | }
|
---|
360 |
|
---|
361 | @Override
|
---|
362 | public void removeUpdate(DocumentEvent e) {
|
---|
363 | updateEnabledState();
|
---|
364 | }
|
---|
365 | }
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Action to go back to step 1 in the process
|
---|
369 | */
|
---|
370 | class BackAction extends AbstractAction {
|
---|
371 | public BackAction() {
|
---|
372 | putValue(NAME, tr("Back"));
|
---|
373 | putValue(SHORT_DESCRIPTION, tr("Run the automatic authorization steps again"));
|
---|
374 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "previous"));
|
---|
375 | }
|
---|
376 |
|
---|
377 | @Override
|
---|
378 | public void actionPerformed(ActionEvent arg0) {
|
---|
379 | prepareUIForEnteringRequest();
|
---|
380 | }
|
---|
381 | }
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * Action to test an access token.
|
---|
385 | */
|
---|
386 | class TestAccessTokenAction extends AbstractAction {
|
---|
387 | public TestAccessTokenAction() {
|
---|
388 | putValue(NAME, tr("Test Access Token"));
|
---|
389 | putValue(SMALL_ICON, ImageProvider.get("logo"));
|
---|
390 | }
|
---|
391 |
|
---|
392 | @Override
|
---|
393 | public void actionPerformed(ActionEvent arg0) {
|
---|
394 | Main.worker.submit(new TestAccessTokenTask(
|
---|
395 | FullyAutomaticAuthorizationUI.this,
|
---|
396 | getApiUrl(),
|
---|
397 | getAdvancedPropertiesPanel().getAdvancedParameters(),
|
---|
398 | getAccessToken()
|
---|
399 | ));
|
---|
400 | }
|
---|
401 | }
|
---|
402 |
|
---|
403 | private static class UserNameValidator extends AbstractTextComponentValidator {
|
---|
404 | public UserNameValidator(JTextComponent tc) {
|
---|
405 | super(tc);
|
---|
406 | }
|
---|
407 |
|
---|
408 | @Override
|
---|
409 | public boolean isValid() {
|
---|
410 | return getComponent().getText().trim().length() > 0;
|
---|
411 | }
|
---|
412 |
|
---|
413 | @Override
|
---|
414 | public void validate() {
|
---|
415 | if (isValid()) {
|
---|
416 | feedbackValid(tr("Please enter your OSM user name"));
|
---|
417 | } else {
|
---|
418 | feedbackInvalid(tr("The user name cannot be empty. Please enter your OSM user name"));
|
---|
419 | }
|
---|
420 | }
|
---|
421 | }
|
---|
422 |
|
---|
423 | private static class PasswordValidator extends AbstractTextComponentValidator {
|
---|
424 |
|
---|
425 | public PasswordValidator(JTextComponent tc) {
|
---|
426 | super(tc);
|
---|
427 | }
|
---|
428 |
|
---|
429 | @Override
|
---|
430 | public boolean isValid() {
|
---|
431 | return getComponent().getText().trim().length() > 0;
|
---|
432 | }
|
---|
433 |
|
---|
434 | @Override
|
---|
435 | public void validate() {
|
---|
436 | if (isValid()) {
|
---|
437 | feedbackValid(tr("Please enter your OSM password"));
|
---|
438 | } else {
|
---|
439 | feedbackInvalid(tr("The password cannot be empty. Please enter your OSM password"));
|
---|
440 | }
|
---|
441 | }
|
---|
442 | }
|
---|
443 |
|
---|
444 | class FullyAutomaticAuthorisationTask extends PleaseWaitRunnable {
|
---|
445 | private boolean canceled;
|
---|
446 | private OsmOAuthAuthorizationClient authClient;
|
---|
447 |
|
---|
448 | public FullyAutomaticAuthorisationTask(Component parent) {
|
---|
449 | super(parent, tr("Authorize JOSM to access the OSM API"), false /* don't ignore exceptions */);
|
---|
450 | }
|
---|
451 |
|
---|
452 | @Override
|
---|
453 | protected void cancel() {
|
---|
454 | canceled = true;
|
---|
455 | }
|
---|
456 |
|
---|
457 | @Override
|
---|
458 | protected void finish() {}
|
---|
459 |
|
---|
460 | protected void alertAuthorisationFailed(OsmOAuthAuthorizationException e) {
|
---|
461 | HelpAwareOptionPane.showOptionDialog(
|
---|
462 | FullyAutomaticAuthorizationUI.this,
|
---|
463 | tr("<html>"
|
---|
464 | + "The automatic process for retrieving an OAuth Access Token<br>"
|
---|
465 | + "from the OSM server failed.<br><br>"
|
---|
466 | + "Please try again or choose another kind of authorization process,<br>"
|
---|
467 | + "i.e. semi-automatic or manual authorization."
|
---|
468 | +"</html>"),
|
---|
469 | tr("OAuth authorization failed"),
|
---|
470 | JOptionPane.ERROR_MESSAGE,
|
---|
471 | HelpUtil.ht("/Dialog/OAuthAuthorisationWizard#FullyAutomaticProcessFailed")
|
---|
472 | );
|
---|
473 | }
|
---|
474 |
|
---|
475 | protected void alertInvalidLoginUrl() {
|
---|
476 | HelpAwareOptionPane.showOptionDialog(
|
---|
477 | FullyAutomaticAuthorizationUI.this,
|
---|
478 | tr("<html>"
|
---|
479 | + "The automatic process for retrieving an OAuth Access Token<br>"
|
---|
480 | + "from the OSM server failed because JOSM was not able to build<br>"
|
---|
481 | + "a valid login URL from the OAuth Authorize Endpoint URL ''{0}''.<br><br>"
|
---|
482 | + "Please check your advanced setting and try again."
|
---|
483 | + "</html>",
|
---|
484 | getAdvancedPropertiesPanel().getAdvancedParameters().getAuthoriseUrl()),
|
---|
485 | tr("OAuth authorization failed"),
|
---|
486 | JOptionPane.ERROR_MESSAGE,
|
---|
487 | HelpUtil.ht("/Dialog/OAuthAuthorisationWizard#FullyAutomaticProcessFailed")
|
---|
488 | );
|
---|
489 | }
|
---|
490 |
|
---|
491 | protected void alertLoginFailed(OsmLoginFailedException e) {
|
---|
492 | String loginUrl = null;
|
---|
493 | try {
|
---|
494 | loginUrl = authClient.buildOsmLoginUrl();
|
---|
495 | } catch(OsmOAuthAuthorizationException e1) {
|
---|
496 | alertInvalidLoginUrl();
|
---|
497 | return;
|
---|
498 | }
|
---|
499 | HelpAwareOptionPane.showOptionDialog(
|
---|
500 | FullyAutomaticAuthorizationUI.this,
|
---|
501 | tr("<html>"
|
---|
502 | + "The automatic process for retrieving an OAuth Access Token<br>"
|
---|
503 | + "from the OSM server failed. JOSM failed to log into {0}<br>"
|
---|
504 | + "for user {1}.<br><br>"
|
---|
505 | + "Please check username and password and try again."
|
---|
506 | +"</html>",
|
---|
507 | loginUrl,
|
---|
508 | getOsmUserName()),
|
---|
509 | tr("OAuth authorization failed"),
|
---|
510 | JOptionPane.ERROR_MESSAGE,
|
---|
511 | HelpUtil.ht("/Dialog/OAuthAuthorisationWizard#FullyAutomaticProcessFailed")
|
---|
512 | );
|
---|
513 | }
|
---|
514 |
|
---|
515 | protected void handleException(final OsmOAuthAuthorizationException e) {
|
---|
516 | Runnable r = new Runnable() {
|
---|
517 | @Override
|
---|
518 | public void run() {
|
---|
519 | if (e instanceof OsmLoginFailedException) {
|
---|
520 | alertLoginFailed((OsmLoginFailedException)e);
|
---|
521 | } else {
|
---|
522 | alertAuthorisationFailed(e);
|
---|
523 | }
|
---|
524 | }
|
---|
525 | };
|
---|
526 | Main.error(e);
|
---|
527 | GuiHelper.runInEDT(r);
|
---|
528 | }
|
---|
529 |
|
---|
530 | @Override
|
---|
531 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
532 | try {
|
---|
533 | getProgressMonitor().setTicksCount(3);
|
---|
534 | authClient = new OsmOAuthAuthorizationClient(
|
---|
535 | getAdvancedPropertiesPanel().getAdvancedParameters()
|
---|
536 | );
|
---|
537 | OAuthToken requestToken = authClient.getRequestToken(
|
---|
538 | getProgressMonitor().createSubTaskMonitor(1, false)
|
---|
539 | );
|
---|
540 | getProgressMonitor().worked(1);
|
---|
541 | if (canceled)return;
|
---|
542 | authClient.authorise(
|
---|
543 | requestToken,
|
---|
544 | getOsmUserName(),
|
---|
545 | getOsmPassword(),
|
---|
546 | pnlOsmPrivileges.getPrivileges(),
|
---|
547 | getProgressMonitor().createSubTaskMonitor(1, false)
|
---|
548 | );
|
---|
549 | getProgressMonitor().worked(1);
|
---|
550 | if (canceled)return;
|
---|
551 | final OAuthToken accessToken = authClient.getAccessToken(
|
---|
552 | getProgressMonitor().createSubTaskMonitor(1,false)
|
---|
553 | );
|
---|
554 | getProgressMonitor().worked(1);
|
---|
555 | if (canceled)return;
|
---|
556 | GuiHelper.runInEDT(new Runnable() {
|
---|
557 | @Override
|
---|
558 | public void run() {
|
---|
559 | prepareUIForResultDisplay();
|
---|
560 | setAccessToken(accessToken);
|
---|
561 | }
|
---|
562 | });
|
---|
563 | } catch(final OsmOAuthAuthorizationException e) {
|
---|
564 | handleException(e);
|
---|
565 | }
|
---|
566 | }
|
---|
567 | }
|
---|
568 | }
|
---|