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.nio.client;
28  
29  import org.apache.http.HttpConnection;
30  import org.apache.http.annotation.Contract;
31  import org.apache.http.annotation.ThreadingBehavior;
32  import org.apache.http.auth.AuthScheme;
33  import org.apache.http.auth.AuthState;
34  import org.apache.http.auth.Credentials;
35  import org.apache.http.client.UserTokenHandler;
36  import org.apache.http.client.protocol.HttpClientContext;
37  import org.apache.http.nio.conn.ManagedNHttpClientConnection;
38  import org.apache.http.protocol.HttpContext;
39  
40  import javax.net.ssl.SSLSession;
41  import java.security.Principal;
42  
43  /**
44   * Default implementation of {@link org.apache.http.client.UserTokenHandler}
45   * for asynchrounous HTTP client communication. This class will use
46   * an instance of {@link java.security.Principal} as a state object for
47   * non-blocking HTTP connections, if it can be obtained from the given
48   * execution context. This helps ensure persistent connections created with
49   * a particular user identity within a particular security context can be
50   * reused by the same user only.
51   * <p>
52   * This implementation will use the user principle of connection based
53   * authentication schemes such as NTLM or that of the SSL session with
54   * the client authentication turned on. If both are unavailable,
55   * {@code null} token will be returned.
56   *
57   * @since 4.0
58   */
59  @Contract(threading = ThreadingBehavior.IMMUTABLE)
60  public class DefaultAsyncUserTokenHandler implements UserTokenHandler {
61  
62      public static final DefaultAsyncUserTokenHandler INSTANCE = new DefaultAsyncUserTokenHandler();
63  
64      @Override
65      public Object getUserToken(final HttpContext context) {
66  
67          final HttpClientContext clientContext = HttpClientContext.adapt(context);
68  
69          Principal userPrincipal = null;
70  
71          final AuthState targetAuthState = clientContext.getTargetAuthState();
72          if (targetAuthState != null) {
73              userPrincipal = getAuthPrincipal(targetAuthState);
74              if (userPrincipal == null) {
75                  final AuthState proxyAuthState = clientContext.getProxyAuthState();
76                  userPrincipal = getAuthPrincipal(proxyAuthState);
77              }
78          }
79  
80          if (userPrincipal == null) {
81              final HttpConnection conn = clientContext.getConnection();
82              if (conn.isOpen() && conn instanceof ManagedNHttpClientConnection) {
83                  final SSLSession sslsession = ((ManagedNHttpClientConnection) conn).getSSLSession();
84                  if (sslsession != null) {
85                      userPrincipal = sslsession.getLocalPrincipal();
86                  }
87              }
88          }
89  
90          return userPrincipal;
91      }
92  
93      private static Principal getAuthPrincipal(final AuthState authState) {
94          final AuthScheme scheme = authState.getAuthScheme();
95          if (scheme != null && scheme.isComplete() && scheme.isConnectionBased()) {
96              final Credentials creds = authState.getCredentials();
97              if (creds != null) {
98                  return creds.getUserPrincipal();
99              }
100         }
101         return null;
102     }
103 
104 }