mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/auth/AuthSchemeBase.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  *
     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.auth;
    29 import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
    31 import ch.boye.httpclientandroidlib.FormattedHeader;
    32 import ch.boye.httpclientandroidlib.Header;
    33 import ch.boye.httpclientandroidlib.HttpRequest;
    34 import ch.boye.httpclientandroidlib.auth.AUTH;
    35 import ch.boye.httpclientandroidlib.auth.AuthenticationException;
    36 import ch.boye.httpclientandroidlib.auth.ContextAwareAuthScheme;
    37 import ch.boye.httpclientandroidlib.auth.Credentials;
    38 import ch.boye.httpclientandroidlib.auth.MalformedChallengeException;
    39 import ch.boye.httpclientandroidlib.protocol.HTTP;
    40 import ch.boye.httpclientandroidlib.protocol.HttpContext;
    41 import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
    43 /**
    44  * Abstract authentication scheme class that serves as a basis
    45  * for all authentication schemes supported by HttpClient. This class
    46  * defines the generic way of parsing an authentication challenge. It
    47  * does not make any assumptions regarding the format of the challenge
    48  * nor does it impose any specific way of responding to that challenge.
    49  *
    50  *
    51  * @since 4.0
    52  */
    53 @NotThreadSafe // proxy
    54 public abstract class AuthSchemeBase implements ContextAwareAuthScheme {
    56     /**
    57      * Flag whether authenticating against a proxy.
    58      */
    59     private boolean proxy;
    61     public AuthSchemeBase() {
    62         super();
    63     }
    65     /**
    66      * Processes the given challenge token. Some authentication schemes
    67      * may involve multiple challenge-response exchanges. Such schemes must be able
    68      * to maintain the state information when dealing with sequential challenges
    69      *
    70      * @param header the challenge header
    71      *
    72      * @throws MalformedChallengeException is thrown if the authentication challenge
    73      * is malformed
    74      */
    75     public void processChallenge(final Header header) throws MalformedChallengeException {
    76         if (header == null) {
    77             throw new IllegalArgumentException("Header may not be null");
    78         }
    79         String authheader = header.getName();
    80         if (authheader.equalsIgnoreCase(AUTH.WWW_AUTH)) {
    81             this.proxy = false;
    82         } else if (authheader.equalsIgnoreCase(AUTH.PROXY_AUTH)) {
    83             this.proxy = true;
    84         } else {
    85             throw new MalformedChallengeException("Unexpected header name: " + authheader);
    86         }
    88         CharArrayBuffer buffer;
    89         int pos;
    90         if (header instanceof FormattedHeader) {
    91             buffer = ((FormattedHeader) header).getBuffer();
    92             pos = ((FormattedHeader) header).getValuePos();
    93         } else {
    94             String s = header.getValue();
    95             if (s == null) {
    96                 throw new MalformedChallengeException("Header value is null");
    97             }
    98             buffer = new CharArrayBuffer(s.length());
    99             buffer.append(s);
   100             pos = 0;
   101         }
   102         while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
   103             pos++;
   104         }
   105         int beginIndex = pos;
   106         while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
   107             pos++;
   108         }
   109         int endIndex = pos;
   110         String s = buffer.substring(beginIndex, endIndex);
   111         if (!s.equalsIgnoreCase(getSchemeName())) {
   112             throw new MalformedChallengeException("Invalid scheme identifier: " + s);
   113         }
   115         parseChallenge(buffer, pos, buffer.length());
   116     }
   119     @SuppressWarnings("deprecation")
   120     public Header authenticate(
   121             final Credentials credentials,
   122             final HttpRequest request,
   123             final HttpContext context) throws AuthenticationException {
   124         return authenticate(credentials, request);
   125     }
   127     protected abstract void parseChallenge(
   128             CharArrayBuffer buffer, int beginIndex, int endIndex) throws MalformedChallengeException;
   130     /**
   131      * Returns <code>true</code> if authenticating against a proxy, <code>false</code>
   132      * otherwise.
   133      *
   134      * @return <code>true</code> if authenticating against a proxy, <code>false</code>
   135      * otherwise
   136      */
   137     public boolean isProxy() {
   138         return this.proxy;
   139     }
   141     @Override
   142     public String toString() {
   143         return getSchemeName();
   144     }
   146 }

mercurial