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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/client/DefaultUserTokenHandler.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,101 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + *
     1.7 + *  Licensed to the Apache Software Foundation (ASF) under one or more
     1.8 + *  contributor license agreements.  See the NOTICE file distributed with
     1.9 + *  this work for additional information regarding copyright ownership.
    1.10 + *  The ASF licenses this file to You under the Apache License, Version 2.0
    1.11 + *  (the "License"); you may not use this file except in compliance with
    1.12 + *  the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + *  Unless required by applicable law or agreed to in writing, software
    1.17 + *  distributed under the License is distributed on an "AS IS" BASIS,
    1.18 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.19 + *  See the License for the specific language governing permissions and
    1.20 + *  limitations under the License.
    1.21 + * ====================================================================
    1.22 + *
    1.23 + * This software consists of voluntary contributions made by many
    1.24 + * individuals on behalf of the Apache Software Foundation.  For more
    1.25 + * information on the Apache Software Foundation, please see
    1.26 + * <http://www.apache.org/>.
    1.27 + *
    1.28 + */
    1.29 +
    1.30 +package ch.boye.httpclientandroidlib.impl.client;
    1.31 +
    1.32 +import java.security.Principal;
    1.33 +
    1.34 +import javax.net.ssl.SSLSession;
    1.35 +
    1.36 +import ch.boye.httpclientandroidlib.annotation.Immutable;
    1.37 +
    1.38 +import ch.boye.httpclientandroidlib.auth.AuthScheme;
    1.39 +import ch.boye.httpclientandroidlib.auth.AuthState;
    1.40 +import ch.boye.httpclientandroidlib.auth.Credentials;
    1.41 +import ch.boye.httpclientandroidlib.client.UserTokenHandler;
    1.42 +import ch.boye.httpclientandroidlib.client.protocol.ClientContext;
    1.43 +import ch.boye.httpclientandroidlib.conn.HttpRoutedConnection;
    1.44 +import ch.boye.httpclientandroidlib.protocol.ExecutionContext;
    1.45 +import ch.boye.httpclientandroidlib.protocol.HttpContext;
    1.46 +
    1.47 +/**
    1.48 + * Default implementation of {@link UserTokenHandler}. This class will use
    1.49 + * an instance of {@link Principal} as a state object for HTTP connections,
    1.50 + * if it can be obtained from the given execution context. This helps ensure
    1.51 + * persistent connections created with a particular user identity within
    1.52 + * a particular security context can be reused by the same user only.
    1.53 + * <p>
    1.54 + * DefaultUserTokenHandler will use the user principle of connection
    1.55 + * based authentication schemes such as NTLM or that of the SSL session
    1.56 + * with the client authentication turned on. If both are unavailable,
    1.57 + * <code>null</code> token will be returned.
    1.58 + *
    1.59 + * @since 4.0
    1.60 + */
    1.61 +@Immutable
    1.62 +public class DefaultUserTokenHandler implements UserTokenHandler {
    1.63 +
    1.64 +    public Object getUserToken(final HttpContext context) {
    1.65 +
    1.66 +        Principal userPrincipal = null;
    1.67 +
    1.68 +        AuthState targetAuthState = (AuthState) context.getAttribute(
    1.69 +                ClientContext.TARGET_AUTH_STATE);
    1.70 +        if (targetAuthState != null) {
    1.71 +            userPrincipal = getAuthPrincipal(targetAuthState);
    1.72 +            if (userPrincipal == null) {
    1.73 +                AuthState proxyAuthState = (AuthState) context.getAttribute(
    1.74 +                        ClientContext.PROXY_AUTH_STATE);
    1.75 +                userPrincipal = getAuthPrincipal(proxyAuthState);
    1.76 +            }
    1.77 +        }
    1.78 +
    1.79 +        if (userPrincipal == null) {
    1.80 +            HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
    1.81 +                    ExecutionContext.HTTP_CONNECTION);
    1.82 +            if (conn.isOpen()) {
    1.83 +                SSLSession sslsession = conn.getSSLSession();
    1.84 +                if (sslsession != null) {
    1.85 +                    userPrincipal = sslsession.getLocalPrincipal();
    1.86 +                }
    1.87 +            }
    1.88 +        }
    1.89 +
    1.90 +        return userPrincipal;
    1.91 +    }
    1.92 +
    1.93 +    private static Principal getAuthPrincipal(final AuthState authState) {
    1.94 +        AuthScheme scheme = authState.getAuthScheme();
    1.95 +        if (scheme != null && scheme.isComplete() && scheme.isConnectionBased()) {
    1.96 +            Credentials creds = authState.getCredentials();
    1.97 +            if (creds != null) {
    1.98 +                return creds.getUserPrincipal();
    1.99 +            }
   1.100 +        }
   1.101 +        return null;
   1.102 +    }
   1.103 +
   1.104 +}

mercurial