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

Last change on this file since 3734 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

  • Property svn:eol-style set to native
File size: 2.6 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;
8import java.util.logging.Logger;
9
10import 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 */
18public 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}
Note: See TracBrowser for help on using the repository browser.