michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.sync.net; michael@0: michael@0: import ch.boye.httpclientandroidlib.Header; michael@0: import ch.boye.httpclientandroidlib.auth.Credentials; michael@0: import ch.boye.httpclientandroidlib.auth.UsernamePasswordCredentials; michael@0: import ch.boye.httpclientandroidlib.client.methods.HttpRequestBase; michael@0: import ch.boye.httpclientandroidlib.impl.auth.BasicScheme; michael@0: import ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient; michael@0: import ch.boye.httpclientandroidlib.protocol.BasicHttpContext; michael@0: michael@0: /** michael@0: * An AuthHeaderProvider that returns an HTTP Basic auth header. michael@0: */ michael@0: public class BasicAuthHeaderProvider implements AuthHeaderProvider { michael@0: protected final String credentials; michael@0: michael@0: /** michael@0: * Constructor. michael@0: * michael@0: * @param credentials string in form "user:pass". michael@0: */ michael@0: public BasicAuthHeaderProvider(String credentials) { michael@0: this.credentials = credentials; michael@0: } michael@0: michael@0: /** michael@0: * Constructor. michael@0: * michael@0: * @param user username. michael@0: * @param pass password. michael@0: */ michael@0: public BasicAuthHeaderProvider(String user, String pass) { michael@0: this(user + ":" + pass); michael@0: } michael@0: michael@0: /** michael@0: * Return a Header object representing an Authentication header for HTTP michael@0: * Basic. michael@0: */ michael@0: @Override michael@0: public Header getAuthHeader(HttpRequestBase request, BasicHttpContext context, DefaultHttpClient client) { michael@0: Credentials creds = new UsernamePasswordCredentials(credentials); michael@0: michael@0: // This must be UTF-8 to generate the same Basic Auth headers as desktop for non-ASCII passwords. michael@0: return BasicScheme.authenticate(creds, "UTF-8", false); michael@0: } michael@0: }