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