mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/auth/BasicScheme.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 org.mozilla.apache.commons.codec.binary.Base64;
    32 import ch.boye.httpclientandroidlib.Header;
    33 import ch.boye.httpclientandroidlib.HttpRequest;
    34 import ch.boye.httpclientandroidlib.auth.AuthenticationException;
    35 import ch.boye.httpclientandroidlib.auth.Credentials;
    36 import ch.boye.httpclientandroidlib.auth.AUTH;
    37 import ch.boye.httpclientandroidlib.auth.InvalidCredentialsException;
    38 import ch.boye.httpclientandroidlib.auth.MalformedChallengeException;
    39 import ch.boye.httpclientandroidlib.auth.params.AuthParams;
    40 import ch.boye.httpclientandroidlib.message.BufferedHeader;
    41 import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
    42 import ch.boye.httpclientandroidlib.util.EncodingUtils;
    44 /**
    45  * Basic authentication scheme as defined in RFC 2617.
    46  * <p>
    47  * The following parameters can be used to customize the behavior of this
    48  * class:
    49  * <ul>
    50  *  <li>{@link ch.boye.httpclientandroidlib.auth.params.AuthPNames#CREDENTIAL_CHARSET}</li>
    51  * </ul>
    52  *
    53  * @since 4.0
    54  */
    55 @NotThreadSafe
    56 public class BasicScheme extends RFC2617Scheme {
    58     /** Whether the basic authentication process is complete */
    59     private boolean complete;
    61     /**
    62      * Default constructor for the basic authentication scheme.
    63      */
    64     public BasicScheme() {
    65         super();
    66         this.complete = false;
    67     }
    69     /**
    70      * Returns textual designation of the basic authentication scheme.
    71      *
    72      * @return <code>basic</code>
    73      */
    74     public String getSchemeName() {
    75         return "basic";
    76     }
    78     /**
    79      * Processes the Basic challenge.
    80      *
    81      * @param header the challenge header
    82      *
    83      * @throws MalformedChallengeException is thrown if the authentication challenge
    84      * is malformed
    85      */
    86     @Override
    87     public void processChallenge(
    88             final Header header) throws MalformedChallengeException {
    89         super.processChallenge(header);
    90         this.complete = true;
    91     }
    93     /**
    94      * Tests if the Basic authentication process has been completed.
    95      *
    96      * @return <tt>true</tt> if Basic authorization has been processed,
    97      *   <tt>false</tt> otherwise.
    98      */
    99     public boolean isComplete() {
   100         return this.complete;
   101     }
   103     /**
   104      * Returns <tt>false</tt>. Basic authentication scheme is request based.
   105      *
   106      * @return <tt>false</tt>.
   107      */
   108     public boolean isConnectionBased() {
   109         return false;
   110     }
   112     /**
   113      * Produces basic authorization header for the given set of {@link Credentials}.
   114      *
   115      * @param credentials The set of credentials to be used for authentication
   116      * @param request The request being authenticated
   117      * @throws InvalidCredentialsException if authentication credentials are not
   118      *   valid or not applicable for this authentication scheme
   119      * @throws AuthenticationException if authorization string cannot
   120      *   be generated due to an authentication failure
   121      *
   122      * @return a basic authorization string
   123      */
   124     public Header authenticate(
   125             final Credentials credentials,
   126             final HttpRequest request) throws AuthenticationException {
   128         if (credentials == null) {
   129             throw new IllegalArgumentException("Credentials may not be null");
   130         }
   131         if (request == null) {
   132             throw new IllegalArgumentException("HTTP request may not be null");
   133         }
   135         String charset = AuthParams.getCredentialCharset(request.getParams());
   136         return authenticate(credentials, charset, isProxy());
   137     }
   139     /**
   140      * Returns a basic <tt>Authorization</tt> header value for the given
   141      * {@link Credentials} and charset.
   142      *
   143      * @param credentials The credentials to encode.
   144      * @param charset The charset to use for encoding the credentials
   145      *
   146      * @return a basic authorization header
   147      */
   148     public static Header authenticate(
   149             final Credentials credentials,
   150             final String charset,
   151             boolean proxy) {
   152         if (credentials == null) {
   153             throw new IllegalArgumentException("Credentials may not be null");
   154         }
   155         if (charset == null) {
   156             throw new IllegalArgumentException("charset may not be null");
   157         }
   159         StringBuilder tmp = new StringBuilder();
   160         tmp.append(credentials.getUserPrincipal().getName());
   161         tmp.append(":");
   162         tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword());
   164         byte[] base64password = Base64.encodeBase64(
   165                 EncodingUtils.getBytes(tmp.toString(), charset));
   167         CharArrayBuffer buffer = new CharArrayBuffer(32);
   168         if (proxy) {
   169             buffer.append(AUTH.PROXY_AUTH_RESP);
   170         } else {
   171             buffer.append(AUTH.WWW_AUTH_RESP);
   172         }
   173         buffer.append(": Basic ");
   174         buffer.append(base64password, 0, base64password.length);
   176         return new BufferedHeader(buffer);
   177     }
   179 }

mercurial