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      /**
63       * Default instance of {@link DefaultUserTokenHandler}.
64       */
65      public static final DefaultUserTokenHandler INSTANCE = new DefaultUserTokenHandler();
66  
67      @Override
68      public Object getUserToken(final HttpRoute route, final HttpContext context) {
69          return getUserToken(route, null, context);
70      }
71  
72      @Override
73      public Object getUserToken(final HttpRoute route, final HttpRequest request, final HttpContext context) {
74  
75          final HttpClientContext clientContext = HttpClientContext.cast(context);
76  
77          final HttpHost target = request != null ? new HttpHost(request.getScheme(), request.getAuthority()) : route.getTargetHost();
78  
79          final AuthExchange targetAuthExchange = clientContext.getAuthExchange(target);
80          if (targetAuthExchange != null) {
81              final Principal authPrincipal = getAuthPrincipal(targetAuthExchange);
82              if (authPrincipal != null) {
83                  return authPrincipal;
84              }
85          }
86          final HttpHost proxy = route.getProxyHost();
87          if (proxy != null) {
88              final AuthExchange proxyAuthExchange = clientContext.getAuthExchange(proxy);
89              if (proxyAuthExchange != null) {
90                  final Principal authPrincipal = getAuthPrincipal(proxyAuthExchange);
91                  if (authPrincipal != null) {
92                      return authPrincipal;
93                  }
94              }
95          }
96          final SSLSession sslSession = clientContext.getSSLSession();
97          if (sslSession != null) {
98              return sslSession.getLocalPrincipal();
99          }
100         return null;
101     }
102 
103     private static Principal getAuthPrincipal(final AuthExchange authExchange) {
104         final AuthScheme scheme = authExchange.getAuthScheme();
105         if (scheme != null && scheme.isConnectionBased()) {
106             return scheme.getPrincipal();
107         }
108         return null;
109     }
110 
111 }