source: josm/trunk/src/org/openstreetmap/josm/io/auth/AbstractCredentialsAgent.java@ 12646

Last change on this file since 12646 was 12646, checked in by bastiK, 7 years ago

see #14794 - javadoc

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.auth;
3
4import java.awt.GraphicsEnvironment;
5import java.net.Authenticator.RequestorType;
6import java.net.PasswordAuthentication;
7import java.util.EnumMap;
8import java.util.Map;
9
10import org.openstreetmap.josm.gui.io.CredentialDialog;
11import org.openstreetmap.josm.gui.util.GuiHelper;
12
13/**
14 * Partial implementation of the {@link CredentialsAgent} interface.
15 * <p>
16 * Provides a memory cache for the credentials and means to query the information
17 * from the user.
18 */
19public abstract class AbstractCredentialsAgent implements CredentialsAgent {
20
21 protected Map<RequestorType, PasswordAuthentication> memoryCredentialsCache = new EnumMap<>(RequestorType.class);
22
23 @Override
24 public CredentialsAgentResponse getCredentials(final RequestorType requestorType, final String host, boolean noSuccessWithLastResponse)
25 throws CredentialsAgentException {
26 if (requestorType == null)
27 return null;
28 PasswordAuthentication credentials = lookup(requestorType, host);
29 final String username = (credentials == null || credentials.getUserName() == null) ? "" : credentials.getUserName();
30 final String password = (credentials == null || credentials.getPassword() == null) ? "" : String.valueOf(credentials.getPassword());
31
32 final CredentialsAgentResponse response = new CredentialsAgentResponse();
33
34 /*
35 * Last request was successful and there was no credentials stored
36 * in file (or only the username is stored).
37 * -> Try to recall credentials that have been entered
38 * manually in this session.
39 */
40 if (!noSuccessWithLastResponse && memoryCredentialsCache.containsKey(requestorType) &&
41 (credentials == null || credentials.getPassword() == null || credentials.getPassword().length == 0)) {
42 PasswordAuthentication pa = memoryCredentialsCache.get(requestorType);
43 response.setUsername(pa.getUserName());
44 response.setPassword(pa.getPassword());
45 response.setCanceled(false);
46 /*
47 * Prompt the user for credentials. This happens the first time each
48 * josm start if the user does not save the credentials to preference
49 * file (username=="") and each time after authentication failed
50 * (noSuccessWithLastResponse == true).
51 */
52 } else if (noSuccessWithLastResponse || username.isEmpty() || password.isEmpty()) {
53 if (!GraphicsEnvironment.isHeadless()) {
54 GuiHelper.runInEDTAndWait(() -> {
55 CredentialDialog dialog;
56 if (requestorType.equals(RequestorType.PROXY))
57 dialog = CredentialDialog.getHttpProxyCredentialDialog(
58 username, password, host, getSaveUsernameAndPasswordCheckboxText());
59 else
60 dialog = CredentialDialog.getOsmApiCredentialDialog(
61 username, password, host, getSaveUsernameAndPasswordCheckboxText());
62 dialog.setVisible(true);
63 response.setCanceled(dialog.isCanceled());
64 if (dialog.isCanceled())
65 return;
66 response.setUsername(dialog.getUsername());
67 response.setPassword(dialog.getPassword());
68 response.setSaveCredentials(dialog.isSaveCredentials());
69 });
70 }
71 if (response.isCanceled() || response.getUsername() == null || response.getPassword() == null) {
72 return response;
73 }
74 if (response.isSaveCredentials()) {
75 store(requestorType, host, new PasswordAuthentication(
76 response.getUsername(),
77 response.getPassword()
78 ));
79 /*
80 * User decides not to save credentials to file. Keep it
81 * in memory so we don't have to ask over and over again.
82 */
83 } else {
84 PasswordAuthentication pa = new PasswordAuthentication(response.getUsername(), response.getPassword());
85 memoryCredentialsCache.put(requestorType, pa);
86 }
87 /*
88 * We got it from file.
89 */
90 } else {
91 response.setUsername(username);
92 response.setPassword(password.toCharArray());
93 response.setCanceled(false);
94 }
95 return response;
96 }
97
98 /**
99 * Provide the text for a checkbox that offers to save the
100 * username and password that has been entered by the user.
101 * @return checkbox text
102 */
103 public abstract String getSaveUsernameAndPasswordCheckboxText();
104}
Note: See TracBrowser for help on using the repository browser.