[2711] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io.auth;
|
---|
| 3 |
|
---|
| 4 | import java.net.Authenticator;
|
---|
| 5 | import java.net.PasswordAuthentication;
|
---|
| 6 | import java.util.HashMap;
|
---|
| 7 | import java.util.Map;
|
---|
| 8 |
|
---|
[2748] | 9 | import org.openstreetmap.josm.Main;
|
---|
| 10 |
|
---|
[2711] | 11 | /**
|
---|
| 12 | * This is the default authenticator used in JOSM. It delegates lookup of credentials
|
---|
| 13 | * for the OSM API and an optional proxy server to the currently configured
|
---|
| 14 | * {@see CredentialsManager}.
|
---|
[2801] | 15 | *
|
---|
[2711] | 16 | */
|
---|
| 17 | public class DefaultAuthenticator extends Authenticator {
|
---|
[2748] | 18 | private static DefaultAuthenticator instance;
|
---|
[2711] | 19 |
|
---|
[2748] | 20 | public static DefaultAuthenticator getInstance() {
|
---|
| 21 | return instance;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
[4249] | 24 | public static void createInstance() {
|
---|
| 25 | instance = new DefaultAuthenticator();
|
---|
[2748] | 26 | }
|
---|
| 27 |
|
---|
[2711] | 28 | private final Map<RequestorType, Boolean> credentialsTried = new HashMap<RequestorType, Boolean>();
|
---|
[2748] | 29 | private boolean enabled = true;
|
---|
[2711] | 30 |
|
---|
[4249] | 31 | private DefaultAuthenticator() {
|
---|
[2711] | 32 | }
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * Called by the Java http stack when either the OSM API server or a proxy requires
|
---|
| 36 | * authentication.
|
---|
[2801] | 37 | *
|
---|
[2711] | 38 | */
|
---|
| 39 | @Override protected PasswordAuthentication getPasswordAuthentication() {
|
---|
[2748] | 40 | if (!enabled)
|
---|
| 41 | return null;
|
---|
[2711] | 42 | try {
|
---|
[2748] | 43 | if (getRequestorType().equals(Authenticator.RequestorType.SERVER)) {
|
---|
| 44 | // if we are working with OAuth we don't prompt for a password
|
---|
| 45 | //
|
---|
| 46 | String authMethod = Main.pref.get("osm-server.auth-method", "basic");
|
---|
| 47 | if (authMethod.equals("oauth"))
|
---|
| 48 | return null;
|
---|
| 49 | }
|
---|
[2711] | 50 | boolean tried = credentialsTried.get(getRequestorType()) != null;
|
---|
[4690] | 51 | CredentialsAgentResponse response = CredentialsManager.getInstance().getCredentials(getRequestorType(), getRequestingHost(), tried);
|
---|
[2711] | 52 | if (response == null || response.isCanceled())
|
---|
| 53 | return null;
|
---|
| 54 | credentialsTried.put(getRequestorType(), true);
|
---|
| 55 | return new PasswordAuthentication(response.getUsername(), response.getPassword());
|
---|
[4245] | 56 | } catch(CredentialsAgentException e) {
|
---|
[2711] | 57 | e.printStackTrace();
|
---|
| 58 | return null;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
[2748] | 61 |
|
---|
| 62 | public boolean isEnabled() {
|
---|
| 63 | return enabled;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | public void setEnabled(boolean enabled) {
|
---|
| 67 | this.enabled = enabled;
|
---|
| 68 | }
|
---|
[2711] | 69 | }
|
---|