Skip to content

Commit b906a06

Browse files
author
Stephane Landelle
committed
Merge pull request AsyncHttpClient#408 from taer/authfix
Backport old digest auth to 1.7
2 parents 06203e3 + 80667db commit b906a06

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

src/main/java/com/ning/http/client/Realm.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,12 +526,23 @@ private void newResponse() throws UnsupportedEncodingException {
526526
byte[] ha1 = md.digest();
527527

528528
md.reset();
529+
530+
//HA2 if qop is auth-int is methodName:url:md5(entityBody)
529531
md.update(new StringBuilder(methodName)
530532
.append(':')
531533
.append(uri).toString().getBytes("ISO-8859-1"));
532534
byte[] ha2 = md.digest();
533535

534-
md.update(new StringBuilder(toBase16(ha1))
536+
if(qop==null || qop.equals("")) {
537+
md.update(new StringBuilder(toBase16(ha1))
538+
.append(':')
539+
.append(nonce)
540+
.append(':')
541+
.append(toBase16(ha2)).toString().getBytes("ISO-8859-1"));
542+
543+
} else {
544+
//qop ="auth" or "auth-int"
545+
md.update(new StringBuilder(toBase16(ha1))
535546
.append(':')
536547
.append(nonce)
537548
.append(':')
@@ -542,6 +553,8 @@ private void newResponse() throws UnsupportedEncodingException {
542553
.append(qop)
543554
.append(':')
544555
.append(toBase16(ha2)).toString().getBytes("ISO-8859-1"));
556+
}
557+
545558
byte[] digest = md.digest();
546559

547560
response = toHexString(digest);

src/test/java/com/ning/http/client/RealmTest.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import com.ning.http.client.Realm.AuthScheme;
1616
import com.ning.http.client.Realm.RealmBuilder;
1717
import org.testng.Assert;
18+
import java.math.BigInteger;
19+
import java.security.MessageDigest;
20+
import java.security.NoSuchAlgorithmException;
1821
import org.testng.annotations.Test;
1922

2023
public class RealmTest {
@@ -36,4 +39,82 @@ public void testClone() {
3639
Assert.assertEquals( clone.getAlgorithm(), orig.getAlgorithm() );
3740
Assert.assertEquals( clone.getAuthScheme(), orig.getAuthScheme() );
3841
}
42+
@Test(groups = "fast")
43+
public void testOldDigestEmptyString() {
44+
String qop="";
45+
testOldDigest(qop);
46+
}
47+
@Test(groups = "fast")
48+
public void testOldDigestNull() {
49+
String qop=null;
50+
testOldDigest(qop);
51+
}
52+
53+
private void testOldDigest(String qop){
54+
String user="user";
55+
String pass="pass";
56+
String realm="realm";
57+
String nonce="nonce";
58+
String method="GET";
59+
String uri="/foo";
60+
RealmBuilder builder = new RealmBuilder();
61+
builder.setPrincipal( user ).setPassword( pass );
62+
builder.setNonce( nonce );
63+
builder.setUri( uri );
64+
builder.setMethodName(method);
65+
builder.setRealmName( realm );
66+
builder.setQop(qop);
67+
builder.setScheme( AuthScheme.DIGEST );
68+
Realm orig = builder.build();
69+
70+
String ha1=getMd5(user +":" + realm +":"+pass);
71+
String ha2=getMd5(method +":"+ uri);
72+
String expectedResponse=getMd5(ha1 +":" + nonce +":" + ha2);
73+
74+
Assert.assertEquals(expectedResponse,orig.getResponse());
75+
}
76+
77+
@Test(groups = "fast")
78+
public void testStrongDigest() {
79+
String user="user";
80+
String pass="pass";
81+
String realm="realm";
82+
String nonce="nonce";
83+
String method="GET";
84+
String uri="/foo";
85+
String qop="auth";
86+
RealmBuilder builder = new RealmBuilder();
87+
builder.setPrincipal( user ).setPassword( pass );
88+
builder.setNonce( nonce );
89+
builder.setUri( uri );
90+
builder.setMethodName(method);
91+
builder.setRealmName( realm );
92+
builder.setQop(qop);
93+
builder.setScheme( AuthScheme.DIGEST );
94+
Realm orig = builder.build();
95+
96+
String nc = orig.getNc();
97+
String cnonce = orig.getCnonce();
98+
String ha1=getMd5(user +":" + realm +":"+pass);
99+
String ha2=getMd5(method +":"+ uri);
100+
String expectedResponse=getMd5(ha1 +":" + nonce +":" + nc + ":" + cnonce +":" + qop + ":" + ha2);
101+
102+
Assert.assertEquals(expectedResponse,orig.getResponse());
103+
}
104+
105+
private String getMd5(String what){
106+
try {
107+
MessageDigest md = MessageDigest.getInstance("MD5");
108+
md.update(what.getBytes("ISO-8859-1"));
109+
byte[] hash = md.digest();
110+
BigInteger bi = new BigInteger(1, hash);
111+
String result = bi.toString(16);
112+
if (result.length() % 2 != 0) {
113+
return "0" + result;
114+
}
115+
return result;
116+
} catch (Exception e) {
117+
throw new RuntimeException(e);
118+
}
119+
}
39120
}

0 commit comments

Comments
 (0)