|
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/. */ |
|
4 |
|
5 package org.mozilla.gecko.sync.net; |
|
6 |
|
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; |
|
14 |
|
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; |
|
20 |
|
21 /** |
|
22 * Constructor. |
|
23 * |
|
24 * @param credentials string in form "user:pass". |
|
25 */ |
|
26 public BasicAuthHeaderProvider(String credentials) { |
|
27 this.credentials = credentials; |
|
28 } |
|
29 |
|
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 } |
|
39 |
|
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); |
|
47 |
|
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 } |