Digest authentication scheme as defined in RFC 2617.
Both MD5 (default) and MD5-sess are supported.
Currently only qop=auth or no qop is supported. qop=auth-int
is unsupported. If auth and auth-int are provided, auth is
used.
| Method from org.apache.commons.httpclient.auth.DigestScheme Detail: |
public String authenticate(Credentials credentials,
HttpMethod method) throws AuthenticationException {
LOG.trace("enter DigestScheme.authenticate(Credentials, HttpMethod)");
UsernamePasswordCredentials usernamepassword = null;
try {
usernamepassword = (UsernamePasswordCredentials) credentials;
} catch (ClassCastException e) {
throw new InvalidCredentialsException(
"Credentials cannot be used for digest authentication: "
+ credentials.getClass().getName());
}
getParameters().put("methodname", method.getName());
StringBuffer buffer = new StringBuffer(method.getPath());
String query = method.getQueryString();
if (query != null) {
if (query.indexOf("?") != 0) {
buffer.append("?");
}
buffer.append(method.getQueryString());
}
getParameters().put("uri", buffer.toString());
String charset = getParameter("charset");
if (charset == null) {
getParameters().put("charset", method.getParams().getCredentialCharset());
}
String digest = createDigest(
usernamepassword.getUserName(),
usernamepassword.getPassword());
return "Digest " + createDigestHeader(usernamepassword.getUserName(),
digest);
}
Produces a digest authorization string for the given set of
Credentials , method name and URI. |
public String authenticate(Credentials credentials,
String method,
String uri) throws AuthenticationException {
LOG.trace("enter DigestScheme.authenticate(Credentials, String, String)");
UsernamePasswordCredentials usernamepassword = null;
try {
usernamepassword = (UsernamePasswordCredentials) credentials;
} catch (ClassCastException e) {
throw new InvalidCredentialsException(
"Credentials cannot be used for digest authentication: "
+ credentials.getClass().getName());
}
getParameters().put("methodname", method);
getParameters().put("uri", uri);
String digest = createDigest(
usernamepassword.getUserName(),
usernamepassword.getPassword());
return "Digest " + createDigestHeader(usernamepassword.getUserName(), digest);
} Deprecated! Use - #authenticate(Credentials, HttpMethod)
Produces a digest authorization string for the given set of
Credentials , method name and URI. |
public static String createCnonce() {
LOG.trace("enter DigestScheme.createCnonce()");
String cnonce;
final String digAlg = "MD5";
MessageDigest md5Helper;
try {
md5Helper = MessageDigest.getInstance(digAlg);
} catch (NoSuchAlgorithmException e) {
throw new HttpClientError(
"Unsupported algorithm in HTTP Digest authentication: "
+ digAlg);
}
cnonce = Long.toString(System.currentTimeMillis());
cnonce = encode(md5Helper.digest(EncodingUtil.getAsciiBytes(cnonce)));
return cnonce;
}
Creates a random cnonce value based on the current time. |
public String getID() {
String id = getRealm();
String nonce = getParameter("nonce");
if (nonce != null) {
id += "-" + nonce;
}
return id;
} Deprecated! no - longer used
Gets an ID based upon the realm and the nonce value. This ensures that requests
to the same realm with different nonce values will succeed. This differentiation
allows servers to request re-authentication using a fresh nonce value. |
public String getSchemeName() {
return "digest";
}
Returns textual designation of the digest authentication scheme. |
public boolean isComplete() {
String s = getParameter("stale");
if ("true".equalsIgnoreCase(s)) {
return false;
} else {
return this.complete;
}
}
Tests if the Digest authentication process has been completed. |
public boolean isConnectionBased() {
return false;
}
Returns false. Digest authentication scheme is request based. |
public void processChallenge(String challenge) throws MalformedChallengeException {
super.processChallenge(challenge);
if (getParameter("realm") == null) {
throw new MalformedChallengeException("missing realm in challange");
}
if (getParameter("nonce") == null) {
throw new MalformedChallengeException("missing nonce in challange");
}
boolean unsupportedQop = false;
// qop parsing
String qop = getParameter("qop");
if (qop != null) {
StringTokenizer tok = new StringTokenizer(qop,",");
while (tok.hasMoreTokens()) {
String variant = tok.nextToken().trim();
if (variant.equals("auth")) {
qopVariant = QOP_AUTH;
break; //that's our favourite, because auth-int is unsupported
} else if (variant.equals("auth-int")) {
qopVariant = QOP_AUTH_INT;
} else {
unsupportedQop = true;
LOG.warn("Unsupported qop detected: "+ variant);
}
}
}
if (unsupportedQop && (qopVariant == QOP_MISSING)) {
throw new MalformedChallengeException("None of the qop methods is supported");
}
cnonce = createCnonce();
this.complete = true;
}
Processes the Digest challenge. |