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

Last change on this file since 4690 was 4690, checked in by stoecker, 12 years ago

see #7086 - fix passing auth information to wrong server

File size: 4.0 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;
10
11abstract public class AbstractCredentialsAgent implements CredentialsAgent {
12
13 Map<RequestorType, PasswordAuthentication> memoryCredentialsCache = new HashMap<RequestorType, PasswordAuthentication>();
14
15 /**
16 * @see CredentialsAgent#getCredentials(RequestorType, boolean)
17 */
18 @Override
19 public CredentialsAgentResponse getCredentials(RequestorType requestorType, String host, boolean noSuccessWithLastResponse) throws CredentialsAgentException{
20 if (requestorType == null)
21 return null;
22 PasswordAuthentication credentials = lookup(requestorType, host);
23 String username = (credentials == null || credentials.getUserName() == null) ? "" : credentials.getUserName();
24 String password = (credentials == null || credentials.getPassword() == null) ? "" : String.valueOf(credentials.getPassword());
25
26 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.equals("") || password.equals("")) {
47 CredentialDialog dialog = null;
48 switch(requestorType) {
49 case SERVER: dialog = CredentialDialog.getOsmApiCredentialDialog(username, password, host, getSaveUsernameAndPasswordCheckboxText()); break;
50 case PROXY: dialog = CredentialDialog.getHttpProxyCredentialDialog(username, password, host, getSaveUsernameAndPasswordCheckboxText()); break;
51 }
52 dialog.setVisible(true);
53 response.setCanceled(dialog.isCanceled());
54 if (dialog.isCanceled())
55 return response;
56 response.setUsername(dialog.getUsername());
57 response.setPassword(dialog.getPassword());
58 if (dialog.isSaveCredentials()) {
59 store(requestorType, host, new PasswordAuthentication(
60 response.getUsername(),
61 response.getPassword()
62 ));
63 /*
64 * User decides not to save credentials to file. Keep it
65 * in memory so we don't have to ask over and over again.
66 */
67 } else {
68 PasswordAuthentication pa = new PasswordAuthentication(dialog.getUsername(), dialog.getPassword());
69 memoryCredentialsCache.put(requestorType, pa);
70 }
71 /*
72 * We got it from file.
73 */
74 } else {
75 response.setUsername(username);
76 response.setPassword(password.toCharArray());
77 response.setCanceled(false);
78 }
79 return response;
80 }
81
82 /**
83 * Provide the text for a checkbox that offers to save the
84 * username and password that has been entered by the user.
85 */
86 public abstract String getSaveUsernameAndPasswordCheckboxText();
87}
Note: See TracBrowser for help on using the repository browser.