1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.auth;
|
---|
3 |
|
---|
4 | import java.net.Authenticator.RequestorType;
|
---|
5 | import java.net.PasswordAuthentication;
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.Map;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.gui.io.CredentialDialog;
|
---|
10 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
11 |
|
---|
12 | abstract public class AbstractCredentialsAgent implements CredentialsAgent {
|
---|
13 |
|
---|
14 | protected Map<RequestorType, PasswordAuthentication> memoryCredentialsCache = new HashMap<RequestorType, PasswordAuthentication>();
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * @see CredentialsAgent#getCredentials
|
---|
18 | */
|
---|
19 | @Override
|
---|
20 | public CredentialsAgentResponse getCredentials(final RequestorType requestorType, final String host, boolean noSuccessWithLastResponse) throws CredentialsAgentException{
|
---|
21 | if (requestorType == null)
|
---|
22 | return null;
|
---|
23 | PasswordAuthentication credentials = lookup(requestorType, host);
|
---|
24 | final String username = (credentials == null || credentials.getUserName() == null) ? "" : credentials.getUserName();
|
---|
25 | final String password = (credentials == null || credentials.getPassword() == null) ? "" : String.valueOf(credentials.getPassword());
|
---|
26 |
|
---|
27 | final CredentialsAgentResponse response = new CredentialsAgentResponse();
|
---|
28 |
|
---|
29 | /*
|
---|
30 | * Last request was successful and there was no credentials stored
|
---|
31 | * in file (or only the username is stored).
|
---|
32 | * -> Try to recall credentials that have been entered
|
---|
33 | * manually in this session.
|
---|
34 | */
|
---|
35 | if (!noSuccessWithLastResponse && memoryCredentialsCache.containsKey(requestorType) &&
|
---|
36 | (credentials == null || credentials.getPassword() == null || credentials.getPassword().length == 0)) {
|
---|
37 | PasswordAuthentication pa = memoryCredentialsCache.get(requestorType);
|
---|
38 | response.setUsername(pa.getUserName());
|
---|
39 | response.setPassword(pa.getPassword());
|
---|
40 | response.setCanceled(false);
|
---|
41 | /*
|
---|
42 | * Prompt the user for credentials. This happens the first time each
|
---|
43 | * josm start if the user does not save the credentials to preference
|
---|
44 | * file (username=="") and each time after authentication failed
|
---|
45 | * (noSuccessWithLastResponse == true).
|
---|
46 | */
|
---|
47 | } else if (noSuccessWithLastResponse || username.isEmpty() || password.isEmpty()) {
|
---|
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(username, password, host, getSaveUsernameAndPasswordCheckboxText());
|
---|
54 | else
|
---|
55 | dialog = CredentialDialog.getOsmApiCredentialDialog(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()) {
|
---|
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 | */
|
---|
96 | public abstract String getSaveUsernameAndPasswordCheckboxText();
|
---|
97 | }
|
---|