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