mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/protocol/RequestAuthCache.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     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  */
    28 package ch.boye.httpclientandroidlib.client.protocol;
    30 import java.io.IOException;
    32 import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog;
    33 /* LogFactory removed by HttpClient for Android script. */
    34 import ch.boye.httpclientandroidlib.HttpException;
    35 import ch.boye.httpclientandroidlib.HttpHost;
    36 import ch.boye.httpclientandroidlib.HttpRequest;
    37 import ch.boye.httpclientandroidlib.HttpRequestInterceptor;
    38 import ch.boye.httpclientandroidlib.annotation.Immutable;
    39 import ch.boye.httpclientandroidlib.auth.AuthScheme;
    40 import ch.boye.httpclientandroidlib.auth.AuthScope;
    41 import ch.boye.httpclientandroidlib.auth.AuthState;
    42 import ch.boye.httpclientandroidlib.auth.Credentials;
    43 import ch.boye.httpclientandroidlib.client.AuthCache;
    44 import ch.boye.httpclientandroidlib.client.CredentialsProvider;
    45 import ch.boye.httpclientandroidlib.protocol.ExecutionContext;
    46 import ch.boye.httpclientandroidlib.protocol.HttpContext;
    48 /**
    49  * Request interceptor that can preemptively authenticate against known hosts,
    50  * if there is a cached {@link AuthScheme} instance in the local
    51  * {@link AuthCache} associated with the given target or proxy host.
    52  *
    53  * @since 4.1
    54  */
    55 @Immutable
    56 public class RequestAuthCache implements HttpRequestInterceptor {
    58     public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass());
    60     public RequestAuthCache() {
    61         super();
    62     }
    64     public void process(final HttpRequest request, final HttpContext context)
    65             throws HttpException, IOException {
    66         if (request == null) {
    67             throw new IllegalArgumentException("HTTP request may not be null");
    68         }
    69         if (context == null) {
    70             throw new IllegalArgumentException("HTTP context may not be null");
    71         }
    73         AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
    74         if (authCache == null) {
    75             this.log.debug("Auth cache not set in the context");
    76             return;
    77         }
    79         CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
    80                 ClientContext.CREDS_PROVIDER);
    81         if (credsProvider == null) {
    82             this.log.debug("Credentials provider not set in the context");
    83             return;
    84         }
    86         HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    87         AuthState targetState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    88         if (target != null && targetState != null && targetState.getAuthScheme() == null) {
    89             AuthScheme authScheme = authCache.get(target);
    90             if (authScheme != null) {
    91                 doPreemptiveAuth(target, authScheme, targetState, credsProvider);
    92             }
    93         }
    95         HttpHost proxy = (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
    96         AuthState proxyState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
    97         if (proxy != null && proxyState != null && proxyState.getAuthScheme() == null) {
    98             AuthScheme authScheme = authCache.get(proxy);
    99             if (authScheme != null) {
   100                 doPreemptiveAuth(proxy, authScheme, proxyState, credsProvider);
   101             }
   102         }
   103     }
   105     private void doPreemptiveAuth(
   106             final HttpHost host,
   107             final AuthScheme authScheme,
   108             final AuthState authState,
   109             final CredentialsProvider credsProvider) {
   110         String schemeName = authScheme.getSchemeName();
   111         if (this.log.isDebugEnabled()) {
   112             this.log.debug("Re-using cached '" + schemeName + "' auth scheme for " + host);
   113         }
   115         AuthScope authScope = new AuthScope(host.getHostName(), host.getPort(),
   116                 AuthScope.ANY_REALM, schemeName);
   117         Credentials creds = credsProvider.getCredentials(authScope);
   119         if (creds != null) {
   120             authState.setAuthScheme(authScheme);
   121             authState.setCredentials(creds);
   122         } else {
   123             this.log.debug("No credentials for preemptive authentication");
   124         }
   125     }
   127 }

mercurial