mobile/android/base/sync/net/BasicAuthHeaderProvider.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/sync/net/BasicAuthHeaderProvider.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,51 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.sync.net;
     1.9 +
    1.10 +import ch.boye.httpclientandroidlib.Header;
    1.11 +import ch.boye.httpclientandroidlib.auth.Credentials;
    1.12 +import ch.boye.httpclientandroidlib.auth.UsernamePasswordCredentials;
    1.13 +import ch.boye.httpclientandroidlib.client.methods.HttpRequestBase;
    1.14 +import ch.boye.httpclientandroidlib.impl.auth.BasicScheme;
    1.15 +import ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient;
    1.16 +import ch.boye.httpclientandroidlib.protocol.BasicHttpContext;
    1.17 +
    1.18 +/**
    1.19 + * An <code>AuthHeaderProvider</code> that returns an HTTP Basic auth header.
    1.20 + */
    1.21 +public class BasicAuthHeaderProvider implements AuthHeaderProvider {
    1.22 +  protected final String credentials;
    1.23 +
    1.24 +  /**
    1.25 +   * Constructor.
    1.26 +   *
    1.27 +   * @param credentials string in form "user:pass".
    1.28 +   */
    1.29 +  public BasicAuthHeaderProvider(String credentials) {
    1.30 +    this.credentials = credentials;
    1.31 +  }
    1.32 +
    1.33 +  /**
    1.34 +   * Constructor.
    1.35 +   *
    1.36 +   * @param user username.
    1.37 +   * @param pass password.
    1.38 +   */
    1.39 +  public BasicAuthHeaderProvider(String user, String pass) {
    1.40 +    this(user + ":" + pass);
    1.41 +  }
    1.42 +
    1.43 +  /**
    1.44 +   * Return a Header object representing an Authentication header for HTTP
    1.45 +   * Basic.
    1.46 +   */
    1.47 +  @Override
    1.48 +  public Header getAuthHeader(HttpRequestBase request, BasicHttpContext context, DefaultHttpClient client) {
    1.49 +    Credentials creds = new UsernamePasswordCredentials(credentials);
    1.50 +
    1.51 +    // This must be UTF-8 to generate the same Basic Auth headers as desktop for non-ASCII passwords.
    1.52 +    return BasicScheme.authenticate(creds, "UTF-8", false);
    1.53 +  }
    1.54 +}

mercurial