Home | Newer (Next) | Older (Prev)

HTTP Digest Access Authentication using MD5 and HttpClient 4

2010-05-04T20:30:00Z

Dealing with HTTP's Digest authentication mechanism isn't too bad once you have the basic building blocks in place.  Luckily HttpClient 4 can automatically solve many types of authentication challenges for you, if used correctly.  Using HttpClient 4, I built an app that authenticates against a SOAP based web-service requiring WWW-Authenticate Digest authentication.  In a nutshell, the fundamental principal behind HTTP Digest authentication is simple:


Here's a sample with a bit of pseudo code mixed in (so, you get the idea):

// A org.apache.http.impl.auth.DigestScheme instance is
// what will process the challenge from the web-server
final DigestScheme md5Auth = new DigestScheme();

// This should return an HTTP 401 Unauthorized with
// a challenge to solve.
final HttpResponse authResponse =
doPost(url, postBody, contentType);

// Validate that we got an HTTP 401 back
if(authResponse.getStatusLine().getStatusCode() ==
HttpStatus.SC_UNAUTHORIZED) {
if(authResponse.containsHeader("WWW-Authenticate")) {
// Get the challenge.
final Header challenge =
authResponse.getHeaders("WWW-Authenticate")[0];
// Solve it.
md5Auth.processChallenge(challenge);
// Generate a solution Authentication header using your
// username and password.
final Header solution = md5Auth.authenticate(
new UsernamePasswordCredentials(username, password),
new BasicHttpRequest(HttpPost.METHOD_NAME,
new URL(url).getPath()));
// Do another POST, but this time include the solution
// Authentication header as generated by HttpClient.
final HttpResponse goodResponse =
doPost(url, postBody, contentType, solution);
// ... do something useful with goodResponse, which assuming
// your credentials were valid, should contain the data you
// requested.
} else {
throw new Error("Web-service responded with Http 401, " +
"but didn't send us a usable WWW-Authenticate header.");
}
} else {
throw new Error("Didn't get an Http 401 " +
"like we were expecting.");
}

Enjoy.