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.annotation.ThreadSafe;
36  import org.apache.http.auth.AuthScope;
37  import org.apache.http.auth.Credentials;
38  import org.apache.http.auth.NTCredentials;
39  import org.apache.http.auth.UsernamePasswordCredentials;
40  import org.apache.http.client.CredentialsProvider;
41  import org.apache.http.client.config.AuthSchemes;
42  import org.apache.http.util.Args;
43  
44  /**
45   * Implementation of {@link CredentialsProvider} backed by standard
46   * JRE {@link Authenticator}.
47   *
48   * @since 4.3
49   */
50  @ThreadSafe
51  public class SystemDefaultCredentialsProvider implements CredentialsProvider {
52  
53      private static final Map<String, String> SCHEME_MAP;
54  
55      static {
56          SCHEME_MAP = new ConcurrentHashMap<String, String>();
57          SCHEME_MAP.put(AuthSchemes.BASIC.toUpperCase(Locale.ENGLISH), "Basic");
58          SCHEME_MAP.put(AuthSchemes.DIGEST.toUpperCase(Locale.ENGLISH), "Digest");
59          SCHEME_MAP.put(AuthSchemes.NTLM.toUpperCase(Locale.ENGLISH), "NTLM");
60          SCHEME_MAP.put(AuthSchemes.SPNEGO.toUpperCase(Locale.ENGLISH), "SPNEGO");
61          SCHEME_MAP.put(AuthSchemes.KERBEROS.toUpperCase(Locale.ENGLISH), "Kerberos");
62      }
63  
64      private static String translateScheme(final String key) {
65          if (key == null) {
66              return null;
67          }
68          final String s = SCHEME_MAP.get(key);
69          return s != null ? s : key;
70      }
71  
72      private final BasicCredentialsProvider internal;
73  
74      /**
75       * Default constructor.
76       */
77      public SystemDefaultCredentialsProvider() {
78          super();
79          this.internal = new BasicCredentialsProvider();
80      }
81  
82      public void setCredentials(final AuthScope authscope, final Credentials credentials) {
83          internal.setCredentials(authscope, credentials);
84      }
85  
86      private static PasswordAuthentication getSystemCreds(
87              final AuthScope authscope,
88              final Authenticator.RequestorType requestorType) {
89          final String hostname = authscope.getHost();
90          final int port = authscope.getPort();
91          final String protocol = port == 443 ? "https" : "http";
92          return Authenticator.requestPasswordAuthentication(
93                  hostname,
94                  null,
95                  port,
96                  protocol,
97                  null,
98                  translateScheme(authscope.getScheme()),
99                  null,
100                 requestorType);
101     }
102 
103     public Credentials getCredentials(final AuthScope authscope) {
104         Args.notNull(authscope, "Auth scope");
105         final Credentials localcreds = internal.getCredentials(authscope);
106         if (localcreds != null) {
107             return localcreds;
108         }
109         if (authscope.getHost() != null) {
110             PasswordAuthentication systemcreds = getSystemCreds(
111                     authscope, Authenticator.RequestorType.SERVER);
112             if (systemcreds == null) {
113                 systemcreds = getSystemCreds(
114                         authscope, Authenticator.RequestorType.PROXY);
115             }
116             if (systemcreds != null) {
117                 final String domain = System.getProperty("http.auth.ntlm.domain");
118                 if (domain != null) {
119                     return new NTCredentials(
120                             systemcreds.getUserName(),
121                             new String(systemcreds.getPassword()),
122                             null, domain);
123                 } else {
124                     if (AuthSchemes.NTLM.equalsIgnoreCase(authscope.getScheme())) {
125                         // Domian may be specified in a fully qualified user name
126                         return new NTCredentials(
127                                 systemcreds.getUserName(),
128                                 new String(systemcreds.getPassword()),
129                                 null, null);
130                     } else {
131                         return new UsernamePasswordCredentials(
132                                 systemcreds.getUserName(),
133                                 new String(systemcreds.getPassword()));
134                     }
135                 }
136             }
137         }
138         return null;
139     }
140 
141     public void clear() {
142         internal.clear();
143     }
144 
145 }