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

Last change on this file since 7937 was 7937, checked in by bastiK, 9 years ago

add subversion property svn:eol=native

  • Property svn:eol-style set to native
File size: 4.6 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.HashMap;
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 HashMap<>();
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(new Runnable() {
49 @Override
50 public void run() {
51 CredentialDialog dialog = null;
52 if (requestorType.equals(RequestorType.PROXY))
53 dialog = CredentialDialog.getHttpProxyCredentialDialog(
54 username, password, host, getSaveUsernameAndPasswordCheckboxText());
55 else
56 dialog = CredentialDialog.getOsmApiCredentialDialog(
57 username, password, host, getSaveUsernameAndPasswordCheckboxText());
58 dialog.setVisible(true);
59 response.setCanceled(dialog.isCanceled());
60 if (dialog.isCanceled())
61 return;
62 response.setUsername(dialog.getUsername());
63 response.setPassword(dialog.getPassword());
64 response.setSaveCredentials(dialog.isSaveCredentials());
65 }
66 });
67 }
68 if (response.isCanceled() || response.getUsername() == null || response.getPassword() == null) {
69 return response;
70 }
71 if (response.isSaveCredentials()) {
72 store(requestorType, host, new PasswordAuthentication(
73 response.getUsername(),
74 response.getPassword()
75 ));
76 /*
77 * User decides not to save credentials to file. Keep it
78 * in memory so we don't have to ask over and over again.
79 */
80 } else {
81 PasswordAuthentication pa = new PasswordAuthentication(response.getUsername(), response.getPassword());
82 memoryCredentialsCache.put(requestorType, pa);
83 }
84 /*
85 * We got it from file.
86 */
87 } else {
88 response.setUsername(username);
89 response.setPassword(password.toCharArray());
90 response.setCanceled(false);
91 }
92 return response;
93 }
94
95 /**
96 * Provide the text for a checkbox that offers to save the
97 * username and password that has been entered by the user.
98 */
99 public abstract String getSaveUsernameAndPasswordCheckboxText();
100}
Note: See TracBrowser for help on using the repository browser.