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 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                 final String proxyHost = System.getProperty(protocol + ".proxyHost");
121                 if (proxyHost != null) {
122                     final String proxyPort = System.getProperty(protocol + ".proxyPort");
123                     if (proxyPort != null) {
124                         try {
125                             final AuthScope systemScope = new AuthScope(proxyHost, Integer.parseInt(proxyPort));
126                             if (authscope.match(systemScope) >= 0) {
127                                 final String proxyUser = System.getProperty(protocol + ".proxyUser");
128                                 if (proxyUser != null) {
129                                     final String proxyPassword = System.getProperty(protocol + ".proxyPassword");
130                                     systemcreds = new PasswordAuthentication(proxyUser, proxyPassword != null ? proxyPassword.toCharArray() : new char[] {});
131                                 }
132                             }
133                         } catch (final NumberFormatException ex) {
134                         }
135                     }
136                 }
137             }
138             if (systemcreds != null) {
139                 final String domain = System.getProperty("http.auth.ntlm.domain");
140                 if (domain != null) {
141                     return new NTCredentials(
142                             systemcreds.getUserName(),
143                             new String(systemcreds.getPassword()),
144                             null, domain);
145                 } else {
146                     if (AuthSchemes.NTLM.equalsIgnoreCase(authscope.getScheme())) {
147                         // Domain may be specified in a fully qualified user name
148                         return new NTCredentials(
149                                 systemcreds.getUserName(),
150                                 new String(systemcreds.getPassword()),
151                                 null, null);
152                     } else {
153                         return new UsernamePasswordCredentials(
154                                 systemcreds.getUserName(),
155                                 new String(systemcreds.getPassword()));
156                     }
157                 }
158             }
159         }
160         return null;
161     }
162 
163     @Override
164     public void clear() {
165         internal.clear();
166     }
167 
168 }