View Javadoc
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 AuthScope authscope,
91              final Authenticator.RequestorType requestorType) {
92          final String hostname = authscope.getHost();
93          final int port = authscope.getPort();
94          final HttpHost origin = authscope.getOrigin();
95          final String protocol = origin != null ? origin.getSchemeName() :
96                  (port == 443 ? "https" : "http");
97          return Authenticator.requestPasswordAuthentication(
98                  hostname,
99                  null,
100                 port,
101                 protocol,
102                 null,
103                 translateScheme(authscope.getScheme()),
104                 null,
105                 requestorType);
106     }
107 
108     @Override
109     public Credentials getCredentials(final AuthScope authscope) {
110         Args.notNull(authscope, "Auth scope");
111         final Credentials localcreds = internal.getCredentials(authscope);
112         if (localcreds != null) {
113             return localcreds;
114         }
115         final String host = authscope.getHost();
116         if (host != null) {
117             PasswordAuthentication systemcreds = getSystemCreds(authscope, Authenticator.RequestorType.SERVER);
118             if (systemcreds == null) {
119                 systemcreds = getSystemCreds(authscope, Authenticator.RequestorType.PROXY);
120             }
121             if (systemcreds == null) {
122                 final String proxyHost = System.getProperty("http.proxyHost");
123                 if (proxyHost != null) {
124                     final String proxyPort = System.getProperty("http.proxyPort");
125                     if (proxyPort != null) {
126                         try {
127                             final AuthScope systemScope = new AuthScope(proxyHost, Integer.parseInt(proxyPort));
128                             if (authscope.match(systemScope) >= 0) {
129                                 final String proxyUser = System.getProperty("http.proxyUser");
130                                 if (proxyUser != null) {
131                                     final String proxyPassword = System.getProperty("http.proxyPassword");
132                                     systemcreds = new PasswordAuthentication(proxyUser, proxyPassword != null ? proxyPassword.toCharArray() : new char[] {});
133                                 }
134                             }
135                         } catch (NumberFormatException ex) {
136                         }
137                     }
138                 }
139             }
140             if (systemcreds != null) {
141                 final String domain = System.getProperty("http.auth.ntlm.domain");
142                 if (domain != null) {
143                     return new NTCredentials(
144                             systemcreds.getUserName(),
145                             new String(systemcreds.getPassword()),
146                             null, domain);
147                 } else {
148                     if (AuthSchemes.NTLM.equalsIgnoreCase(authscope.getScheme())) {
149                         // Domain may be specified in a fully qualified user name
150                         return new NTCredentials(
151                                 systemcreds.getUserName(),
152                                 new String(systemcreds.getPassword()),
153                                 null, null);
154                     } else {
155                         return new UsernamePasswordCredentials(
156                                 systemcreds.getUserName(),
157                                 new String(systemcreds.getPassword()));
158                     }
159                 }
160             }
161         }
162         return null;
163     }
164 
165     @Override
166     public void clear() {
167         internal.clear();
168     }
169 
170 }