source: josm/trunk/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java@ 12928

Last change on this file since 12928 was 12928, checked in by bastiK, 8 years ago

see #15229 - do not copy the entire preferences list, just to set a custom server API in OAuth wizard

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