1 /*
2  * ====================================================================
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  * ====================================================================
20  *
21  * This software consists of voluntary contributions made by many
22  * individuals on behalf of the Apache Software Foundation.  For more
23  * information on the Apache Software Foundation, please see
24  * <http://www.apache.org/>.
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  * Implementation of {@link CredentialsProvider} backed by standard
48  * JRE {@link Authenticator}.
49  *
50  * @since 4.3
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      * Default constructor.
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                 // Look for values given using http.proxyUser/http.proxyPassword or
121                 // https.proxyUser/https.proxyPassword. We cannot simply use the protocol from
122                 // the origin since a proxy retrieved from https.proxyHost/https.proxyPort will
123                 // still use http as protocol
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                                 // Domain may be specified in a fully qualified user name
139                                 ? new NTCredentials(systemcreds.getUserName(),
140                                                 new String(systemcreds.getPassword()), nullnull)
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 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 }
182