1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.auth;
|
---|
3 |
|
---|
4 | import java.awt.GraphicsEnvironment;
|
---|
5 | import java.net.Authenticator.RequestorType;
|
---|
6 | import java.net.PasswordAuthentication;
|
---|
7 | import java.util.EnumMap;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.gui.io.CredentialDialog;
|
---|
11 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
12 |
|
---|
13 | public 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(new Runnable() {
|
---|
49 | @Override
|
---|
50 | public void run() {
|
---|
51 | CredentialDialog dialog;
|
---|
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 | * @return checkbox text
|
---|
99 | */
|
---|
100 | public abstract String getSaveUsernameAndPasswordCheckboxText();
|
---|
101 | }
|
---|