source: josm/trunk/src/org/openstreetmap/josm/io/auth/DefaultAuthenticator.java@ 4690

Last change on this file since 4690 was 4690, checked in by stoecker, 12 years ago

see #7086 - fix passing auth information to wrong server

  • Property svn:eol-style set to native
File size: 2.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.auth;
3
4import java.net.Authenticator;
5import java.net.PasswordAuthentication;
6import java.util.HashMap;
7import java.util.Map;
8
9import org.openstreetmap.josm.Main;
10
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}.
15 *
16 */
17public class DefaultAuthenticator extends Authenticator {
18 private static DefaultAuthenticator instance;
19
20 public static DefaultAuthenticator getInstance() {
21 return instance;
22 }
23
24 public static void createInstance() {
25 instance = new DefaultAuthenticator();
26 }
27
28 private final Map<RequestorType, Boolean> credentialsTried = new HashMap<RequestorType, Boolean>();
29 private boolean enabled = true;
30
31 private DefaultAuthenticator() {
32 }
33
34 /**
35 * Called by the Java http stack when either the OSM API server or a proxy requires
36 * authentication.
37 *
38 */
39 @Override protected PasswordAuthentication getPasswordAuthentication() {
40 if (!enabled)
41 return null;
42 try {
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 }
50 boolean tried = credentialsTried.get(getRequestorType()) != null;
51 CredentialsAgentResponse response = CredentialsManager.getInstance().getCredentials(getRequestorType(), getRequestingHost(), tried);
52 if (response == null || response.isCanceled())
53 return null;
54 credentialsTried.put(getRequestorType(), true);
55 return new PasswordAuthentication(response.getUsername(), response.getPassword());
56 } catch(CredentialsAgentException e) {
57 e.printStackTrace();
58 return null;
59 }
60 }
61
62 public boolean isEnabled() {
63 return enabled;
64 }
65
66 public void setEnabled(boolean enabled) {
67 this.enabled = enabled;
68 }
69}
Note: See TracBrowser for help on using the repository browser.