Changeset 18991 in josm for trunk/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java
- Timestamp:
- 2024-02-21T21:26:18+01:00 (3 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java
r18650 r18991 13 13 import java.awt.event.ActionEvent; 14 14 import java.io.IOException; 15 import java.net.Authenticator.RequestorType;16 import java.net.PasswordAuthentication;17 15 import java.util.concurrent.Executor; 18 16 … … 24 22 import javax.swing.JPanel; 25 23 import javax.swing.JTabbedPane; 26 import javax.swing.event.DocumentEvent;27 import javax.swing.event.DocumentListener;28 import javax.swing.text.JTextComponent;29 24 import javax.swing.text.html.HTMLEditorKit; 30 25 31 import org.openstreetmap.josm.data.oauth. OAuthParameters;32 import org.openstreetmap.josm.data.oauth.OAuth Token;26 import org.openstreetmap.josm.data.oauth.IOAuthToken; 27 import org.openstreetmap.josm.data.oauth.OAuthVersion; 33 28 import org.openstreetmap.josm.gui.HelpAwareOptionPane; 34 29 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 35 30 import org.openstreetmap.josm.gui.help.HelpUtil; 36 import org.openstreetmap.josm.gui.preferences.server.UserNameValidator;37 31 import org.openstreetmap.josm.gui.util.GuiHelper; 38 import org.openstreetmap.josm.gui.widgets.DefaultTextComponentValidator;39 32 import org.openstreetmap.josm.gui.widgets.HtmlPanel; 40 33 import org.openstreetmap.josm.gui.widgets.JMultilineLabel; 41 import org.openstreetmap.josm.gui.widgets.JosmPasswordField;42 import org.openstreetmap.josm.gui.widgets.JosmTextField;43 import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;44 34 import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel; 45 import org.openstreetmap.josm.io.OsmApi;46 35 import org.openstreetmap.josm.io.OsmTransferException; 47 import org.openstreetmap.josm.io.auth.CredentialsAgent;48 import org.openstreetmap.josm.io.auth.CredentialsAgentException;49 import org.openstreetmap.josm.io.auth.CredentialsManager;50 36 import org.openstreetmap.josm.tools.ImageProvider; 51 37 import org.openstreetmap.josm.tools.Logging; 52 import org.openstreetmap.josm.tools.Utils;53 38 import org.xml.sax.SAXException; 54 39 55 40 /** 56 * This is a nUI which supports a JOSM user to get an OAuth Access Token in a fully41 * This is a UI which supports a JOSM user to get an OAuth Access Token in a fully 57 42 * automatic process. 58 43 * … … 60 45 */ 61 46 public 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 47 private final AccessTokenInfoPanel pnlAccessTokenInfo = new AccessTokenInfoPanel(); 68 48 private OsmPrivilegesPanel pnlOsmPrivileges; … … 105 85 + tr("Please enter your OSM user name and password. The password will <strong>not</strong> be saved " 106 86 + "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.").replace All("\\. ", ".<br>")87 + "Subsequent data upload requests don''t use your password any more.").replace(". ", ".<br>") 108 88 + "</p>" 109 89 + "</body></html>"); … … 119 99 pnl.add(new JLabel(tr("Username: ")), gc); 120 100 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 101 // the password input field 129 102 gc.anchor = GridBagConstraints.NORTHWEST; … … 134 107 pnl.add(new JLabel(tr("Password:")), gc); 135 108 109 // filler - grab remaining space 136 110 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 space144 111 gc.gridy = 4; 145 112 gc.gridwidth = 2; … … 168 135 169 136 /** 170 * Initializes the panel with values from the preferences171 * @param paramApiUrl the API URL172 */173 @Override174 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 137 * Builds the panel with the action button for starting the authorisation 195 138 * … … 200 143 201 144 RunAuthorisationAction runAuthorisationAction = new RunAuthorisationAction(); 202 tfPassword.getDocument().addDocumentListener(runAuthorisationAction);203 tfUserName.getDocument().addDocumentListener(runAuthorisationAction);204 145 pnl.add(new JButton(runAuthorisationAction)); 205 146 return pnl; … … 289 230 } 290 231 291 protected String getOsmUserName() {292 return tfUserName.getText();293 }294 295 protected String getOsmPassword() {296 return String.valueOf(tfPassword.getPassword());297 }298 299 232 /** 300 233 * Constructs a new {@code FullyAutomaticAuthorizationUI} for the given API URL. … … 302 235 * @param executor the executor used for running the HTTP requests for the authorization 303 236 * @since 5422 304 */ 237 * @deprecated since 18991 238 */ 239 @Deprecated 305 240 public FullyAutomaticAuthorizationUI(String apiUrl, Executor executor) { 306 super(apiUrl); 241 this(apiUrl, executor, OAuthVersion.OAuth10a); 242 } 243 244 /** 245 * Constructs a new {@code FullyAutomaticAuthorizationUI} for the given API URL. 246 * @param apiUrl The OSM API URL 247 * @param executor the executor used for running the HTTP requests for the authorization 248 * @param oAuthVersion The OAuth version to use for this UI 249 * @since 18991 250 */ 251 public FullyAutomaticAuthorizationUI(String apiUrl, Executor executor, OAuthVersion oAuthVersion) { 252 super(apiUrl, oAuthVersion); 307 253 this.executor = executor; 308 254 build(); … … 315 261 316 262 @Override 317 protected void setAccessToken( OAuthToken accessToken) {263 protected void setAccessToken(IOAuthToken accessToken) { 318 264 super.setAccessToken(accessToken); 319 265 pnlAccessTokenInfo.setAccessToken(accessToken); … … 323 269 * Starts the authorisation process 324 270 */ 325 class RunAuthorisationAction extends AbstractAction implements DocumentListener{271 class RunAuthorisationAction extends AbstractAction { 326 272 RunAuthorisationAction() { 327 273 putValue(NAME, tr("Authorize now")); 328 274 new ImageProvider("oauth", "oauth-small").getResource().attachImageIcon(this); 329 275 putValue(SHORT_DESCRIPTION, tr("Click to redirect you to the authorization form on the JOSM web site")); 330 updateEnabledState();331 276 } 332 277 … … 334 279 public void actionPerformed(ActionEvent evt) { 335 280 executor.execute(new FullyAutomaticAuthorisationTask(FullyAutomaticAuthorizationUI.this)); 336 }337 338 protected final void updateEnabledState() {339 setEnabled(valPassword.isValid() && valUserName.isValid());340 }341 342 @Override343 public void changedUpdate(DocumentEvent e) {344 updateEnabledState();345 }346 347 @Override348 public void insertUpdate(DocumentEvent e) {349 updateEnabledState();350 }351 352 @Override353 public void removeUpdate(DocumentEvent e) {354 updateEnabledState();355 281 } 356 282 } … … 386 312 FullyAutomaticAuthorizationUI.this, 387 313 getApiUrl(), 388 (OAuthParameters) getAdvancedPropertiesPanel().getAdvancedParameters(),389 314 getAccessToken() 390 315 )); 391 }392 }393 394 static class PasswordValidator extends DefaultTextComponentValidator {395 PasswordValidator(JTextComponent tc) {396 super(tc, tr("Please enter your OSM password"), tr("The password cannot be empty. Please enter your OSM password"));397 316 } 398 317 } … … 439 358 + "Please check your advanced setting and try again." 440 359 + "</html>", 441 ((OAuthParameters) getAdvancedPropertiesPanel().getAdvancedParameters()).getAuthoriseUrl()),360 getAdvancedPropertiesPanel().getAdvancedParameters().getAuthorizationUrl()), 442 361 tr("OAuth authorization failed"), 443 362 JOptionPane.ERROR_MESSAGE, … … 446 365 } 447 366 448 protected void alertLoginFailed() {449 final String loginUrl = ((OAuthParameters) getAdvancedPropertiesPanel().getAdvancedParameters()).getOsmLoginUrl();450 HelpAwareOptionPane.showOptionDialog(451 FullyAutomaticAuthorizationUI.this,452 tr("<html>"453 + "The automatic process for retrieving an OAuth Access Token<br>"454 + "from the OSM server failed. JOSM failed to log into {0}<br>"455 + "for user {1}.<br><br>"456 + "Please check username and password and try again."457 +"</html>",458 loginUrl,459 Utils.escapeReservedCharactersHTML(getOsmUserName())),460 tr("OAuth authorization failed"),461 JOptionPane.ERROR_MESSAGE,462 HelpUtil.ht("/Dialog/OAuthAuthorisationWizard#FullyAutomaticProcessFailed")463 );464 }465 466 367 protected void handleException(final OsmOAuthAuthorizationException e) { 467 Runnable r = () -> { 468 if (e instanceof OsmLoginFailedException) { 469 alertLoginFailed(); 470 } else { 471 alertAuthorisationFailed(); 368 Logging.error(e); 369 GuiHelper.runInEDT(this::alertAuthorisationFailed); 370 } 371 372 @Override 373 protected void realRun() throws SAXException, IOException, OsmTransferException { 374 getProgressMonitor().setTicksCount(2); 375 OAuthAuthorizationWizard.authorize(true, token -> { 376 if (!canceled) { 377 getProgressMonitor().worked(1); 378 GuiHelper.runInEDT(() -> { 379 prepareUIForResultDisplay(); 380 setAccessToken(token.orElse(null)); 381 }); 472 382 } 473 }; 474 Logging.error(e); 475 GuiHelper.runInEDT(r); 476 } 477 478 @Override 479 protected void realRun() throws SAXException, IOException, OsmTransferException { 480 try { 481 getProgressMonitor().setTicksCount(3); 482 OsmOAuthAuthorizationClient authClient = new OsmOAuthAuthorizationClient( 483 (OAuthParameters) getAdvancedPropertiesPanel().getAdvancedParameters() 484 ); 485 OAuthToken requestToken = authClient.getRequestToken( 486 getProgressMonitor().createSubTaskMonitor(1, false) 487 ); 488 getProgressMonitor().worked(1); 489 if (canceled) return; 490 authClient.authorise( 491 requestToken, 492 getOsmUserName(), 493 getOsmPassword(), 494 pnlOsmPrivileges.getPrivileges(), 495 getProgressMonitor().createSubTaskMonitor(1, false) 496 ); 497 getProgressMonitor().worked(1); 498 if (canceled) return; 499 final OAuthToken accessToken = authClient.getAccessToken( 500 getProgressMonitor().createSubTaskMonitor(1, false) 501 ); 502 getProgressMonitor().worked(1); 503 if (canceled) return; 504 GuiHelper.runInEDT(() -> { 505 prepareUIForResultDisplay(); 506 setAccessToken(accessToken); 507 }); 508 } catch (final OsmOAuthAuthorizationException e) { 509 handleException(e); 510 } 383 }, getApiUrl(), getOAuthVersion()); 384 getProgressMonitor().worked(1); 511 385 } 512 386 }
Note:
See TracChangeset
for help on using the changeset viewer.