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

Last change on this file since 17333 was 17333, checked in by Don-vip, 3 years ago

see #20129 - Fix typos and misspellings in the code (patch by gaben)

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