mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/client/DefaultUserTokenHandler.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /*
     2  * ====================================================================
     3  *
     4  *  Licensed to the Apache Software Foundation (ASF) under one or more
     5  *  contributor license agreements.  See the NOTICE file distributed with
     6  *  this work for additional information regarding copyright ownership.
     7  *  The ASF licenses this file to You under the Apache License, Version 2.0
     8  *  (the "License"); you may not use this file except in compliance with
     9  *  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, software
    14  *  distributed under the License is distributed on an "AS IS" BASIS,
    15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  *  See the License for the specific language governing permissions and
    17  *  limitations under the License.
    18  * ====================================================================
    19  *
    20  * This software consists of voluntary contributions made by many
    21  * individuals on behalf of the Apache Software Foundation.  For more
    22  * information on the Apache Software Foundation, please see
    23  * <http://www.apache.org/>.
    24  *
    25  */
    27 package ch.boye.httpclientandroidlib.impl.client;
    29 import java.security.Principal;
    31 import javax.net.ssl.SSLSession;
    33 import ch.boye.httpclientandroidlib.annotation.Immutable;
    35 import ch.boye.httpclientandroidlib.auth.AuthScheme;
    36 import ch.boye.httpclientandroidlib.auth.AuthState;
    37 import ch.boye.httpclientandroidlib.auth.Credentials;
    38 import ch.boye.httpclientandroidlib.client.UserTokenHandler;
    39 import ch.boye.httpclientandroidlib.client.protocol.ClientContext;
    40 import ch.boye.httpclientandroidlib.conn.HttpRoutedConnection;
    41 import ch.boye.httpclientandroidlib.protocol.ExecutionContext;
    42 import ch.boye.httpclientandroidlib.protocol.HttpContext;
    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 principle 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</code> token will be returned.
    55  *
    56  * @since 4.0
    57  */
    58 @Immutable
    59 public class DefaultUserTokenHandler implements UserTokenHandler {
    61     public Object getUserToken(final HttpContext context) {
    63         Principal userPrincipal = null;
    65         AuthState targetAuthState = (AuthState) context.getAttribute(
    66                 ClientContext.TARGET_AUTH_STATE);
    67         if (targetAuthState != null) {
    68             userPrincipal = getAuthPrincipal(targetAuthState);
    69             if (userPrincipal == null) {
    70                 AuthState proxyAuthState = (AuthState) context.getAttribute(
    71                         ClientContext.PROXY_AUTH_STATE);
    72                 userPrincipal = getAuthPrincipal(proxyAuthState);
    73             }
    74         }
    76         if (userPrincipal == null) {
    77             HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
    78                     ExecutionContext.HTTP_CONNECTION);
    79             if (conn.isOpen()) {
    80                 SSLSession sslsession = conn.getSSLSession();
    81                 if (sslsession != null) {
    82                     userPrincipal = sslsession.getLocalPrincipal();
    83                 }
    84             }
    85         }
    87         return userPrincipal;
    88     }
    90     private static Principal getAuthPrincipal(final AuthState authState) {
    91         AuthScheme scheme = authState.getAuthScheme();
    92         if (scheme != null && scheme.isComplete() && scheme.isConnectionBased()) {
    93             Credentials creds = authState.getCredentials();
    94             if (creds != null) {
    95                 return creds.getUserPrincipal();
    96             }
    97         }
    98         return null;
    99     }
   101 }

mercurial