-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement HTTP Digest Access Authentication #2089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ | |
import static org.asynchttpclient.util.MiscUtils.isNonEmpty; | ||
import static org.asynchttpclient.util.StringUtils.appendBase16; | ||
import static org.asynchttpclient.util.StringUtils.toHexString; | ||
import org.asynchttpclient.util.MessageDigestUtils; | ||
|
||
/** | ||
* This class is required when authentication is needed. The class support | ||
|
@@ -452,62 +453,65 @@ public Builder parseProxyAuthenticateHeader(String headerLine) { | |
return this; | ||
} | ||
|
||
/** | ||
* Extracts the value of a token from a WWW-Authenticate or Proxy-Authenticate header line. | ||
* Example: match('Digest realm="test", nonce="abc"', "realm") returns "test" | ||
*/ | ||
private static @Nullable String match(String headerLine, String token) { | ||
if (headerLine == null || token == null) return null; | ||
String pattern = token + "=\""; | ||
int start = headerLine.indexOf(pattern); | ||
if (start == -1) return null; | ||
start += pattern.length(); | ||
int end = headerLine.indexOf('"', start); | ||
if (end == -1) return null; | ||
return headerLine.substring(start, end); | ||
} | ||
|
||
private void newCnonce(MessageDigest md) { | ||
byte[] b = new byte[8]; | ||
ThreadLocalRandom.current().nextBytes(b); | ||
b = md.digest(b); | ||
cnonce = toHexString(b); | ||
} | ||
|
||
/** | ||
* TODO: A Pattern/Matcher may be better. | ||
*/ | ||
private static @Nullable String match(String headerLine, String token) { | ||
if (headerLine == null) { | ||
return null; | ||
} | ||
|
||
int match = headerLine.indexOf(token); | ||
if (match <= 0) { | ||
return null; | ||
} | ||
|
||
// = to skip | ||
match += token.length() + 1; | ||
int trailingComa = headerLine.indexOf(',', match); | ||
String value = headerLine.substring(match, trailingComa > 0 ? trailingComa : headerLine.length()); | ||
value = value.length() > 0 && value.charAt(value.length() - 1) == '"' | ||
? value.substring(0, value.length() - 1) | ||
: value; | ||
return value.charAt(0) == '"' ? value.substring(1) : value; | ||
} | ||
|
||
private static byte[] md5FromRecycledStringBuilder(StringBuilder sb, MessageDigest md) { | ||
private static byte[] digestFromRecycledStringBuilder(StringBuilder sb, MessageDigest md) { | ||
md.update(StringUtils.charSequence2ByteBuffer(sb, ISO_8859_1)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
sb.setLength(0); | ||
return md.digest(); | ||
} | ||
|
||
private static MessageDigest getDigestInstance(String algorithm) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (algorithm == null || "MD5".equalsIgnoreCase(algorithm) || "MD5-sess".equalsIgnoreCase(algorithm)) { | ||
return MessageDigestUtils.pooledMd5MessageDigest(); | ||
} else if ("SHA-256".equalsIgnoreCase(algorithm) || "SHA-256-sess".equalsIgnoreCase(algorithm)) { | ||
return MessageDigestUtils.pooledSha256MessageDigest(); | ||
} else if ("SHA-512-256".equalsIgnoreCase(algorithm) || "SHA-512-256-sess".equalsIgnoreCase(algorithm)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will it handle "SHA-512/256" ?? |
||
return MessageDigestUtils.pooledSha512_256MessageDigest(); | ||
} else { | ||
throw new UnsupportedOperationException("Digest algorithm not supported: " + algorithm); | ||
} | ||
} | ||
|
||
private byte[] ha1(StringBuilder sb, MessageDigest md) { | ||
// if algorithm is "MD5" or is unspecified => A1 = username ":" realm-value ":" | ||
// passwd | ||
// if algorithm is "MD5-sess" => A1 = MD5( username-value ":" realm-value ":" | ||
// passwd ) ":" nonce-value ":" cnonce-value | ||
|
||
sb.append(principal).append(':').append(realmName).append(':').append(password); | ||
byte[] core = md5FromRecycledStringBuilder(sb, md); | ||
byte[] core = digestFromRecycledStringBuilder(sb, md); | ||
|
||
if (algorithm == null || "MD5".equals(algorithm)) { | ||
if (algorithm == null || "MD5".equalsIgnoreCase(algorithm) || "SHA-256".equalsIgnoreCase(algorithm) || "SHA-512-256".equalsIgnoreCase(algorithm)) { | ||
// A1 = username ":" realm-value ":" passwd | ||
return core; | ||
} | ||
if ("MD5-sess".equals(algorithm)) { | ||
// A1 = MD5(username ":" realm-value ":" passwd ) ":" nonce ":" cnonce | ||
if ("MD5-sess".equalsIgnoreCase(algorithm) || "SHA-256-sess".equalsIgnoreCase(algorithm) || "SHA-512-256-sess".equalsIgnoreCase(algorithm)) { | ||
// A1 = HASH(username ":" realm-value ":" passwd ) ":" nonce ":" cnonce | ||
appendBase16(sb, core); | ||
sb.append(':').append(nonce).append(':').append(cnonce); | ||
return md5FromRecycledStringBuilder(sb, md); | ||
return digestFromRecycledStringBuilder(sb, md); | ||
} | ||
|
||
throw new UnsupportedOperationException("Digest algorithm not supported: " + algorithm); | ||
} | ||
|
||
|
@@ -526,7 +530,7 @@ private byte[] ha2(StringBuilder sb, String digestUri, MessageDigest md) { | |
throw new UnsupportedOperationException("Digest qop not supported: " + qop); | ||
} | ||
|
||
return md5FromRecycledStringBuilder(sb, md); | ||
return digestFromRecycledStringBuilder(sb, md); | ||
} | ||
|
||
private void appendMiddlePart(StringBuilder sb) { | ||
|
@@ -553,7 +557,7 @@ private void newResponse(MessageDigest md) { | |
appendMiddlePart(sb); | ||
appendBase16(sb, ha2); | ||
|
||
byte[] responseDigest = md5FromRecycledStringBuilder(sb, md); | ||
byte[] responseDigest = digestFromRecycledStringBuilder(sb, md); | ||
response = toHexString(responseDigest); | ||
} | ||
} | ||
|
@@ -567,7 +571,9 @@ public Realm build() { | |
|
||
// Avoid generating | ||
if (isNonEmpty(nonce)) { | ||
MessageDigest md = pooledMd5MessageDigest(); | ||
// Defensive: if algorithm is null, default to MD5 | ||
String algo = (algorithm != null) ? algorithm : "MD5"; | ||
MessageDigest md = getDigestInstance(algo); | ||
newCnonce(md); | ||
newResponse(md); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MD5 =16 bytes; SHA-256 = 32 bytes;
rfc7616 doesn’t forbid long nonces... but wont the headers that big can be unwieldy, especially if you’re proxying or logging??