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 | import java.util.logging.Logger;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * This is the default authenticator used in JOSM. It delegates lookup of credentials
|
---|
14 | * for the OSM API and an optional proxy server to the currently configured
|
---|
15 | * {@see CredentialsManager}.
|
---|
16 | *
|
---|
17 | */
|
---|
18 | public class DefaultAuthenticator extends Authenticator {
|
---|
19 | @SuppressWarnings("unused")
|
---|
20 | private static final Logger logger = Logger.getLogger(DefaultAuthenticator.class.getName());
|
---|
21 | private static DefaultAuthenticator instance;
|
---|
22 |
|
---|
23 | public static DefaultAuthenticator getInstance() {
|
---|
24 | return instance;
|
---|
25 | }
|
---|
26 |
|
---|
27 | public static void createInstance(CredentialsManager credentialManager) {
|
---|
28 | instance = new DefaultAuthenticator(credentialManager);
|
---|
29 | }
|
---|
30 |
|
---|
31 | private CredentialsManager credentialManager;
|
---|
32 | private final Map<RequestorType, Boolean> credentialsTried = new HashMap<RequestorType, Boolean>();
|
---|
33 | private boolean enabled = true;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | *
|
---|
37 | * @param credentialManager the credential manager
|
---|
38 | */
|
---|
39 | private DefaultAuthenticator(CredentialsManager credentialManager) {
|
---|
40 | this.credentialManager = credentialManager;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Called by the Java http stack when either the OSM API server or a proxy requires
|
---|
45 | * authentication.
|
---|
46 | *
|
---|
47 | */
|
---|
48 | @Override protected PasswordAuthentication getPasswordAuthentication() {
|
---|
49 | if (!enabled)
|
---|
50 | return null;
|
---|
51 | try {
|
---|
52 | if (getRequestorType().equals(Authenticator.RequestorType.SERVER)) {
|
---|
53 | // if we are working with OAuth we don't prompt for a password
|
---|
54 | //
|
---|
55 | String authMethod = Main.pref.get("osm-server.auth-method", "basic");
|
---|
56 | if (authMethod.equals("oauth"))
|
---|
57 | return null;
|
---|
58 | }
|
---|
59 | boolean tried = credentialsTried.get(getRequestorType()) != null;
|
---|
60 | CredentialsManagerResponse response = credentialManager.getCredentials(getRequestorType(), tried);
|
---|
61 | if (response == null || response.isCanceled())
|
---|
62 | return null;
|
---|
63 | credentialsTried.put(getRequestorType(), true);
|
---|
64 | return new PasswordAuthentication(response.getUsername(), response.getPassword());
|
---|
65 | } catch(CredentialsManagerException e) {
|
---|
66 | e.printStackTrace();
|
---|
67 | return null;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public boolean isEnabled() {
|
---|
72 | return enabled;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public void setEnabled(boolean enabled) {
|
---|
76 | this.enabled = enabled;
|
---|
77 | }
|
---|
78 | }
|
---|