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

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

fix NPE seen in test if no password is set with basic auth (headless mode)

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