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

Last change on this file since 11043 was 10615, checked in by Don-vip, 8 years ago

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

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