Http digest authentication on Android fails with 401

258 views Asked by At

I am trying to do http digest authentication from an android client. Here is a curl line that connects and does the digest successfully.

curl -i --digest --user '[email protected]:500a436e-2d5d-4a2e-be82-19651f7ea904' -v https://localhost:8080/v1/resources/debug

Here is one attempt in my android client that returns a 401.

AndroidHttpClient httpClient = AndroidHttpClient.newInstance("user agent");

String url = "http://localhost:8080/v1/resources/debug";
URL urlObj = new URL(url);
HttpHost host = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol());
AuthScope scope = new AuthScope(urlObj.getHost(), urlObj.getPort());
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("[email protected]", "500a436e-2d5d-4a2e-be82-19651f7ea904");

CredentialsProvider cp = new BasicCredentialsProvider();
cp.setCredentials(scope, creds);
HttpContext credContext = new BasicHttpContext();
credContext.setAttribute(ClientContext.CREDS_PROVIDER, cp);

HttpGet job = new HttpGet(url);
HttpResponse response = httpClient.execute(host,job,credContext);
StatusLine status = response.getStatusLine();
System.out.println("#### " + status.toString());
httpClient.close();

Here is a second attempt in the client, also returns a 401.

DefaultHttpClient httpclient = new DefaultHttpClient();
DefaultHttpClient httpclient2 = new DefaultHttpClient();
HttpGet httpget = new HttpGet("https://localhost:8080/v1/resources/debug");

HttpResponse response = httpclient.execute(httpget);
System.out.println(response.getStatusLine());

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
    Header authHeader = response.getFirstHeader(AUTH.WWW_AUTH);
    System.out.println("authHeader = " + authHeader);
    DigestScheme digestScheme = new DigestScheme(); 
    digestScheme.processChallenge(authHeader);   
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("[email protected]", "500a436e-2d5d-4a2e-be82-19651f7ea904
    httpget.addHeader(digestScheme.authenticate(creds, httpget));
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = httpclient2.execute(httpget, responseHandler);
    System.out.println("responseBody : " + responseBody);
}

Can anyone see what I'm missing to get the authentication to work? We know it's not a server problem since the curl line works great.

0

There are 0 answers