[4246] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io.auth;
|
---|
| 3 |
|
---|
[7075] | 4 | import java.awt.GraphicsEnvironment;
|
---|
[4246] | 5 | import java.net.Authenticator.RequestorType;
|
---|
| 6 | import java.net.PasswordAuthentication;
|
---|
| 7 | import java.util.HashMap;
|
---|
| 8 | import java.util.Map;
|
---|
| 9 |
|
---|
| 10 | import org.openstreetmap.josm.gui.io.CredentialDialog;
|
---|
[5692] | 11 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[4246] | 12 |
|
---|
[6883] | 13 | public abstract class AbstractCredentialsAgent implements CredentialsAgent {
|
---|
[4246] | 14 |
|
---|
[7005] | 15 | protected Map<RequestorType, PasswordAuthentication> memoryCredentialsCache = new HashMap<>();
|
---|
[4246] | 16 |
|
---|
| 17 | @Override
|
---|
[7067] | 18 | public CredentialsAgentResponse getCredentials(final RequestorType requestorType, final String host, boolean noSuccessWithLastResponse)
|
---|
| 19 | throws CredentialsAgentException {
|
---|
[4246] | 20 | if (requestorType == null)
|
---|
| 21 | return null;
|
---|
[4690] | 22 | PasswordAuthentication credentials = lookup(requestorType, host);
|
---|
[5692] | 23 | final String username = (credentials == null || credentials.getUserName() == null) ? "" : credentials.getUserName();
|
---|
| 24 | final String password = (credentials == null || credentials.getPassword() == null) ? "" : String.valueOf(credentials.getPassword());
|
---|
[4246] | 25 |
|
---|
[5692] | 26 | final CredentialsAgentResponse response = new CredentialsAgentResponse();
|
---|
[4246] | 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 | */
|
---|
[6087] | 46 | } else if (noSuccessWithLastResponse || username.isEmpty() || password.isEmpty()) {
|
---|
[7075] | 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 | }
|
---|
[7067] | 68 | if (response.isCanceled() || response.getUsername() == null || response.getPassword() == null) {
|
---|
[5692] | 69 | return response;
|
---|
[4246] | 70 | }
|
---|
[5692] | 71 | if (response.isSaveCredentials()) {
|
---|
[4690] | 72 | store(requestorType, host, new PasswordAuthentication(
|
---|
[4246] | 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 {
|
---|
[5692] | 81 | PasswordAuthentication pa = new PasswordAuthentication(response.getUsername(), response.getPassword());
|
---|
[4246] | 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 |
|
---|
[4249] | 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();
|
---|
[4246] | 100 | }
|
---|