1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.http.impl.client;
28
29 import java.net.Authenticator;
30 import java.net.PasswordAuthentication;
31 import java.util.Locale;
32 import java.util.Map;
33 import java.util.concurrent.ConcurrentHashMap;
34
35 import org.apache.http.HttpHost;
36 import org.apache.http.annotation.Contract;
37 import org.apache.http.annotation.ThreadingBehavior;
38 import org.apache.http.auth.AuthScope;
39 import org.apache.http.auth.Credentials;
40 import org.apache.http.auth.NTCredentials;
41 import org.apache.http.auth.UsernamePasswordCredentials;
42 import org.apache.http.client.CredentialsProvider;
43 import org.apache.http.client.config.AuthSchemes;
44 import org.apache.http.util.Args;
45
46
47
48
49
50
51
52 @Contract(threading = ThreadingBehavior.SAFE)
53 public class SystemDefaultCredentialsProvider implements CredentialsProvider {
54
55 private static final Map<String, String> SCHEME_MAP;
56
57 static {
58 SCHEME_MAP = new ConcurrentHashMap<String, String>();
59 SCHEME_MAP.put(AuthSchemes.BASIC.toUpperCase(Locale.ROOT), "Basic");
60 SCHEME_MAP.put(AuthSchemes.DIGEST.toUpperCase(Locale.ROOT), "Digest");
61 SCHEME_MAP.put(AuthSchemes.NTLM.toUpperCase(Locale.ROOT), "NTLM");
62 SCHEME_MAP.put(AuthSchemes.SPNEGO.toUpperCase(Locale.ROOT), "SPNEGO");
63 SCHEME_MAP.put(AuthSchemes.KERBEROS.toUpperCase(Locale.ROOT), "Kerberos");
64 }
65
66 private static String translateScheme(final String key) {
67 if (key == null) {
68 return null;
69 }
70 final String s = SCHEME_MAP.get(key);
71 return s != null ? s : key;
72 }
73
74 private final BasicCredentialsProvider internal;
75
76
77
78
79 public SystemDefaultCredentialsProvider() {
80 super();
81 this.internal = new BasicCredentialsProvider();
82 }
83
84 @Override
85 public void setCredentials(final AuthScope authscope, final Credentials credentials) {
86 internal.setCredentials(authscope, credentials);
87 }
88
89 private static PasswordAuthentication getSystemCreds(
90 final String protocol,
91 final AuthScope authscope,
92 final Authenticator.RequestorType requestorType) {
93 return Authenticator.requestPasswordAuthentication(
94 authscope.getHost(),
95 null,
96 authscope.getPort(),
97 protocol,
98 null,
99 translateScheme(authscope.getScheme()),
100 null,
101 requestorType);
102 }
103
104 @Override
105 public Credentials getCredentials(final AuthScope authscope) {
106 Args.notNull(authscope, "Auth scope");
107 final Credentials localcreds = internal.getCredentials(authscope);
108 if (localcreds != null) {
109 return localcreds;
110 }
111 final String host = authscope.getHost();
112 if (host != null) {
113 final HttpHost origin = authscope.getOrigin();
114 final String protocol = origin != null ? origin.getSchemeName() : (authscope.getPort() == 443 ? "https" : "http");
115 PasswordAuthentication systemcreds = getSystemCreds(protocol, authscope, Authenticator.RequestorType.SERVER);
116 if (systemcreds == null) {
117 systemcreds = getSystemCreds(protocol, authscope, Authenticator.RequestorType.PROXY);
118 }
119 if (systemcreds == null) {
120
121
122
123
124 systemcreds = getProxyCredentials("http", authscope);
125 if (systemcreds == null) {
126 systemcreds = getProxyCredentials("https", authscope);
127 }
128 }
129 if (systemcreds != null) {
130 final String domain = System.getProperty("http.auth.ntlm.domain");
131 if (domain != null) {
132 return new NTCredentials(
133 systemcreds.getUserName(),
134 new String(systemcreds.getPassword()),
135 null, domain);
136 }
137 return AuthSchemes.NTLM.equalsIgnoreCase(authscope.getScheme())
138
139 ? new NTCredentials(systemcreds.getUserName(),
140 new String(systemcreds.getPassword()), null, null)
141 : new UsernamePasswordCredentials(systemcreds.getUserName(),
142 new String(systemcreds.getPassword()));
143 }
144 }
145 return null;
146 }
147
148 private static PasswordAuthentication getProxyCredentials(final String protocol, final AuthScope authscope) {
149 final String proxyHost = System.getProperty(protocol + ".proxyHost");
150 if (proxyHost == null) {
151 return null;
152 }
153 final String proxyPort = System.getProperty(protocol + ".proxyPort");
154 if (proxyPort == null) {
155 return null;
156 }
157
158 try {
159 final AuthScopeml#AuthScope">AuthScope systemScope = new AuthScope(proxyHost, Integer.parseInt(proxyPort));
160 if (authscope.match(systemScope) >= 0) {
161 final String proxyUser = System.getProperty(protocol + ".proxyUser");
162 if (proxyUser == null) {
163 return null;
164 }
165 final String proxyPassword = System.getProperty(protocol + ".proxyPassword");
166
167 return new PasswordAuthentication(proxyUser,
168 proxyPassword != null ? proxyPassword.toCharArray() : new char[] {});
169 }
170 } catch (final NumberFormatException ex) {
171 }
172
173 return null;
174 }
175
176 @Override
177 public void clear() {
178 internal.clear();
179 }
180
181 }