1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultUserTokenHandler.java $
3 * $Revision: 659971 $
4 * $Date: 2008-05-25 05:01:22 -0700 (Sun, 25 May 2008) $
5 *
6 * ====================================================================
7 *
8 *  Licensed to the Apache Software Foundation (ASF) under one or more
9 *  contributor license agreements.  See the NOTICE file distributed with
10 *  this work for additional information regarding copyright ownership.
11 *  The ASF licenses this file to You under the Apache License, Version 2.0
12 *  (the "License"); you may not use this file except in compliance with
13 *  the License.  You may obtain a copy of the License at
14 *
15 *      http://www.apache.org/licenses/LICENSE-2.0
16 *
17 *  Unless required by applicable law or agreed to in writing, software
18 *  distributed under the License is distributed on an "AS IS" BASIS,
19 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 *  See the License for the specific language governing permissions and
21 *  limitations under the License.
22 * ====================================================================
23 *
24 * This software consists of voluntary contributions made by many
25 * individuals on behalf of the Apache Software Foundation.  For more
26 * information on the Apache Software Foundation, please see
27 * <http://www.apache.org/>.
28 *
29 */
30
31package org.apache.http.impl.client;
32
33import java.security.Principal;
34
35import javax.net.ssl.SSLSession;
36
37import org.apache.http.auth.AuthScheme;
38import org.apache.http.auth.AuthState;
39import org.apache.http.auth.Credentials;
40import org.apache.http.client.UserTokenHandler;
41import org.apache.http.client.protocol.ClientContext;
42import org.apache.http.conn.ManagedClientConnection;
43import org.apache.http.protocol.ExecutionContext;
44import org.apache.http.protocol.HttpContext;
45
46public class DefaultUserTokenHandler implements UserTokenHandler {
47
48    public Object getUserToken(final HttpContext context) {
49
50        Principal userPrincipal = null;
51
52        AuthState targetAuthState = (AuthState) context.getAttribute(
53                ClientContext.TARGET_AUTH_STATE);
54        if (targetAuthState != null) {
55            userPrincipal = getAuthPrincipal(targetAuthState);
56            if (userPrincipal == null) {
57                AuthState proxyAuthState = (AuthState) context.getAttribute(
58                        ClientContext.PROXY_AUTH_STATE);
59                userPrincipal = getAuthPrincipal(proxyAuthState);
60            }
61        }
62
63        if (userPrincipal == null) {
64            ManagedClientConnection conn = (ManagedClientConnection) context.getAttribute(
65                    ExecutionContext.HTTP_CONNECTION);
66            if (conn.isOpen()) {
67                SSLSession sslsession = conn.getSSLSession();
68                if (sslsession != null) {
69                    userPrincipal = sslsession.getLocalPrincipal();
70                }
71            }
72        }
73
74        return userPrincipal;
75    }
76
77    private static Principal getAuthPrincipal(final AuthState authState) {
78        AuthScheme scheme = authState.getAuthScheme();
79        if (scheme != null && scheme.isComplete() && scheme.isConnectionBased()) {
80            Credentials creds = authState.getCredentials();
81            if (creds != null) {
82                return creds.getUserPrincipal();
83            }
84        }
85        return null;
86    }
87
88}
89