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