Skip to content

Commit 5a1a2ce

Browse files
author
Stephane Landelle
committed
NTLM WWW-Authenticate header is not always the first one, close #736
1 parent ed6586a commit 5a1a2ce

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

src/main/java/com/ning/http/client/providers/netty/handler/HttpProtocol.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
package com.ning.http.client.providers.netty.handler;
1515

16-
import static com.ning.http.client.providers.netty.util.HttpUtils.isNTLM;
16+
import static com.ning.http.client.providers.netty.util.HttpUtils.getNTLM;
1717
import static com.ning.http.util.AsyncHttpProviderUtils.getDefaultPort;
1818
import static com.ning.http.util.MiscUtils.isNonEmpty;
1919
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.CONTINUE;
@@ -90,8 +90,9 @@ private Realm kerberosChallenge(Channel channel,//
9090
.build();
9191

9292
} catch (Throwable throwable) {
93-
if (isNTLM(proxyAuth)) {
94-
return ntlmChallenge(proxyAuth.get(0), request, proxyServer, headers, realm, future, proxyInd);
93+
String ntlmAuthenticate = getNTLM(proxyAuth);
94+
if (ntlmAuthenticate != null) {
95+
return ntlmChallenge(ntlmAuthenticate, request, proxyServer, headers, realm, future, proxyInd);
9596
}
9697
requestSender.abort(channel, future, throwable);
9798
return null;
@@ -218,9 +219,10 @@ private boolean exitAfterHandling401(//
218219
Realm newRealm = null;
219220

220221
boolean negociate = wwwAuthHeaders.contains("Negotiate");
221-
if (!wwwAuthHeaders.contains("Kerberos") && (isNTLM(wwwAuthHeaders) || negociate)) {
222+
String ntlmAuthenticate = getNTLM(wwwAuthHeaders);
223+
if (!wwwAuthHeaders.contains("Kerberos") && ntlmAuthenticate != null) {
222224
// NTLM
223-
newRealm = ntlmChallenge(wwwAuthHeaders.get(0), request, proxyServer, request.getHeaders(), realm, future, false);
225+
newRealm = ntlmChallenge(ntlmAuthenticate, request, proxyServer, request.getHeaders(), realm, future, false);
224226

225227
// don't forget to reuse channel: NTLM authenticates a connection
226228
future.setReuseChannel(true);
@@ -304,8 +306,9 @@ private boolean exitAfterHandling407(//
304306
FluentCaseInsensitiveStringsMap requestHeaders = request.getHeaders();
305307

306308
boolean negociate = proxyAuthHeaders.contains("Negotiate");
307-
if (!proxyAuthHeaders.contains("Kerberos") && (isNTLM(proxyAuthHeaders) || negociate)) {
308-
newRealm = ntlmProxyChallenge(proxyAuthHeaders.get(0), request, proxyServer, requestHeaders, realm, future, true);
309+
String ntlmAuthenticate = getNTLM(proxyAuthHeaders);
310+
if (!proxyAuthHeaders.contains("Kerberos") && ntlmAuthenticate != null) {
311+
newRealm = ntlmProxyChallenge(ntlmAuthenticate, request, proxyServer, requestHeaders, realm, future, true);
309312
// SPNEGO KERBEROS
310313
} else if (negociate) {
311314
newRealm = kerberosChallenge(channel, proxyAuthHeaders, request, proxyServer, requestHeaders, realm, future, true);

src/main/java/com/ning/http/client/providers/netty/request/NettyRequestFactory.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
package com.ning.http.client.providers.netty.request;
1515

16-
import static com.ning.http.client.providers.netty.util.HttpUtils.isNTLM;
16+
import static com.ning.http.client.providers.netty.util.HttpUtils.getNTLM;
1717
import static com.ning.http.client.providers.netty.util.HttpUtils.isSecure;
1818
import static com.ning.http.client.providers.netty.util.HttpUtils.isWebSocket;
1919
import static com.ning.http.client.providers.netty.util.HttpUtils.useProxyConnect;
@@ -157,13 +157,14 @@ public String firstRequestOnlyProxyAuthorizationHeader(Request request, ProxySer
157157

158158
if (method == HttpMethod.CONNECT) {
159159
List<String> auth = request.getHeaders().get(HttpHeaders.Names.PROXY_AUTHORIZATION);
160-
if (isNTLM(auth)) {
161-
proxyAuthorization = auth.get(0);
160+
String ntlmHeader = getNTLM(auth);
161+
if (ntlmHeader != null) {
162+
proxyAuthorization = ntlmHeader;
162163
}
163164

164165
} else if (proxyServer != null && proxyServer.getPrincipal() != null && isNonEmpty(proxyServer.getNtlmDomain())) {
165166
List<String> auth = request.getHeaders().get(HttpHeaders.Names.PROXY_AUTHORIZATION);
166-
if (!isNTLM(auth)) {
167+
if (getNTLM(auth) == null) {
167168
String msg = NTLMEngine.INSTANCE.generateType1Msg();
168169
proxyAuthorization = "NTLM " + msg;
169170
}

src/main/java/com/ning/http/client/providers/netty/util/HttpUtils.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
*/
1414
package com.ning.http.client.providers.netty.util;
1515

16-
import static com.ning.http.util.MiscUtils.isNonEmpty;
17-
1816
import org.jboss.netty.handler.codec.http.HttpHeaders;
1917

2018
import com.ning.http.client.uri.Uri;
@@ -33,8 +31,15 @@ public final class HttpUtils {
3331
private HttpUtils() {
3432
}
3533

36-
public static boolean isNTLM(List<String> auth) {
37-
return isNonEmpty(auth) && auth.get(0).startsWith("NTLM");
34+
public static String getNTLM(List<String> authenticateHeaders) {
35+
if (authenticateHeaders != null) {
36+
for (String authenticateHeader: authenticateHeaders) {
37+
if (authenticateHeader.startsWith("NTLM"))
38+
return authenticateHeader;
39+
}
40+
}
41+
42+
return null;
3843
}
3944

4045
public static List<String> getNettyHeaderValuesByCaseInsensitiveName(HttpHeaders headers, String name) {

0 commit comments

Comments
 (0)