mobile/android/base/sync/net/BasicAuthHeaderProvider.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 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 package org.mozilla.gecko.sync.net;
     7 import ch.boye.httpclientandroidlib.Header;
     8 import ch.boye.httpclientandroidlib.auth.Credentials;
     9 import ch.boye.httpclientandroidlib.auth.UsernamePasswordCredentials;
    10 import ch.boye.httpclientandroidlib.client.methods.HttpRequestBase;
    11 import ch.boye.httpclientandroidlib.impl.auth.BasicScheme;
    12 import ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient;
    13 import ch.boye.httpclientandroidlib.protocol.BasicHttpContext;
    15 /**
    16  * An <code>AuthHeaderProvider</code> that returns an HTTP Basic auth header.
    17  */
    18 public class BasicAuthHeaderProvider implements AuthHeaderProvider {
    19   protected final String credentials;
    21   /**
    22    * Constructor.
    23    *
    24    * @param credentials string in form "user:pass".
    25    */
    26   public BasicAuthHeaderProvider(String credentials) {
    27     this.credentials = credentials;
    28   }
    30   /**
    31    * Constructor.
    32    *
    33    * @param user username.
    34    * @param pass password.
    35    */
    36   public BasicAuthHeaderProvider(String user, String pass) {
    37     this(user + ":" + pass);
    38   }
    40   /**
    41    * Return a Header object representing an Authentication header for HTTP
    42    * Basic.
    43    */
    44   @Override
    45   public Header getAuthHeader(HttpRequestBase request, BasicHttpContext context, DefaultHttpClient client) {
    46     Credentials creds = new UsernamePasswordCredentials(credentials);
    48     // This must be UTF-8 to generate the same Basic Auth headers as desktop for non-ASCII passwords.
    49     return BasicScheme.authenticate(creds, "UTF-8", false);
    50   }
    51 }

mercurial