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.hc.client5.http.impl;
28  
29  import java.security.Principal;
30  
31  import javax.net.ssl.SSLSession;
32  
33  import org.apache.hc.client5.http.HttpRoute;
34  import org.apache.hc.client5.http.UserTokenHandler;
35  import org.apache.hc.client5.http.auth.AuthExchange;
36  import org.apache.hc.client5.http.auth.AuthScheme;
37  import org.apache.hc.client5.http.protocol.HttpClientContext;
38  import org.apache.hc.core5.annotation.Contract;
39  import org.apache.hc.core5.annotation.ThreadingBehavior;
40  import org.apache.hc.core5.http.HttpHost;
41  import org.apache.hc.core5.http.HttpRequest;
42  import org.apache.hc.core5.http.protocol.HttpContext;
43  
44  /**
45   * Default implementation of {@link UserTokenHandler}. This class will use
46   * an instance of {@link Principal} as a state object for HTTP connections,
47   * if it can be obtained from the given execution context. This helps ensure
48   * persistent connections created with a particular user identity within
49   * a particular security context can be reused by the same user only.
50   * <p>
51   * DefaultUserTokenHandler will use the user principal of connection
52   * based authentication schemes such as NTLM or that of the SSL session
53   * with the client authentication turned on. If both are unavailable,
54   * {@code null} token will be returned.
55   * </p>
56   *
57   * @since 4.0
58   */
59  @Contract(threading = ThreadingBehavior.STATELESS)
60  public class DefaultUserTokenHandler implements UserTokenHandler {
61  
62      public static final DefaultUserTokenHandlererTokenHandler.html#DefaultUserTokenHandler">DefaultUserTokenHandler INSTANCE = new DefaultUserTokenHandler();
63  
64      @Override
65      public Object getUserToken(final HttpRoute route, final HttpContext context) {
66  
67          final HttpClientContext clientContext = HttpClientContext.adapt(context);
68  
69          final HttpRequest request = clientContext.getRequest();
70          final HttpHost target = request != null ? new HttpHost(request.getScheme(), request.getAuthority()) : route.getTargetHost();
71  
72          final AuthExchange targetAuthExchange = clientContext.getAuthExchange(target);
73          if (targetAuthExchange != null) {
74              final Principal authPrincipal = getAuthPrincipal(targetAuthExchange);
75              if (authPrincipal != null) {
76                  return authPrincipal;
77              }
78          }
79          final HttpHost proxy = route.getProxyHost();
80          if (proxy != null) {
81              final AuthExchange proxyAuthExchange = clientContext.getAuthExchange(proxy);
82              if (proxyAuthExchange != null) {
83                  final Principal authPrincipal = getAuthPrincipal(proxyAuthExchange);
84                  if (authPrincipal != null) {
85                      return authPrincipal;
86                  }
87              }
88          }
89          final SSLSession sslSession = clientContext.getSSLSession();
90          if (sslSession != null) {
91              return sslSession.getLocalPrincipal();
92          }
93          return null;
94      }
95  
96      private static Principal getAuthPrincipal(final AuthExchange authExchange) {
97          final AuthScheme scheme = authExchange.getAuthScheme();
98          if (scheme != null && scheme.isConnectionBased()) {
99              return scheme.getPrincipal();
100         }
101         return null;
102     }
103 
104 }