Skip to content

Commit c7b4d6c

Browse files
author
Stephane Landelle
committed
Merge pull request AsyncHttpClient#512 from elakito/master
511: GrizzlyAsyncHttpProvider not honoring usePreemtiveAuth
2 parents 595a061 + 2ae3bcf commit c7b4d6c

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

api/src/test/java/org/asynchttpclient/async/BasicAuthTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.asynchttpclient.HttpResponseHeaders;
3535
import org.asynchttpclient.HttpResponseStatus;
3636
import org.asynchttpclient.Realm;
37+
import org.asynchttpclient.Realm.AuthScheme;
3738
import org.asynchttpclient.Response;
3839
import org.asynchttpclient.SimpleAsyncHttpClient;
3940
import org.asynchttpclient.consumers.AppendableBodyConsumer;
@@ -64,6 +65,8 @@ public abstract class BasicAuthTest extends AbstractBasicTest {
6465
protected static final String MY_MESSAGE = "my message";
6566

6667
private Server server2;
68+
private Server serverNoAuth;
69+
private int portNoAuth;
6770

6871
public abstract String getProviderClass();
6972

@@ -72,6 +75,7 @@ public abstract class BasicAuthTest extends AbstractBasicTest {
7275
public void setUpGlobal() throws Exception {
7376
port1 = findFreePort();
7477
port2 = findFreePort();
78+
portNoAuth = findFreePort();
7579

7680
server = newJettyHttpServer(port1);
7781
addBasicAuthHandler(server, false, configureHandler());
@@ -81,13 +85,19 @@ public void setUpGlobal() throws Exception {
8185
addDigestAuthHandler(server2, true, new RedirectHandler());
8286
server2.start();
8387

88+
// need noAuth server to verify the preemptive auth mode (see basicAuthTetPreemtiveTest)
89+
serverNoAuth = newJettyHttpServer(portNoAuth);
90+
serverNoAuth.setHandler(new SimpleHandler());
91+
serverNoAuth.start();
92+
8493
logger.info("Local HTTP server started successfully");
8594
}
8695

8796
@AfterClass(alwaysRun = true)
8897
public void tearDownGlobal() throws Exception {
8998
super.tearDownGlobal();
9099
server2.stop();
100+
serverNoAuth.stop();
91101
}
92102

93103
@Override
@@ -100,6 +110,10 @@ protected String getTargetUrl2() {
100110
return "http://127.0.0.1:" + port2 + "/uff";
101111
}
102112

113+
protected String getTargetUrlNoAuth() {
114+
return "http://127.0.0.1:" + portNoAuth + "/";
115+
}
116+
103117
@Override
104118
public AbstractHandler configureHandler() throws Exception {
105119
return new SimpleHandler();
@@ -247,8 +261,10 @@ public Integer onCompleted() throws Exception {
247261
public void basicAuthTestPreemtiveTest() throws IOException, ExecutionException, TimeoutException, InterruptedException {
248262
AsyncHttpClient client = getAsyncHttpClient(null);
249263
try {
250-
Future<Response> f = client.prepareGet(getTargetUrl())//
251-
.setRealm((new Realm.RealmBuilder()).setPrincipal(USER).setPassword(ADMIN).setUsePreemptiveAuth(true).build())//
264+
// send the request to the no-auth endpoint to be able to verify the auth header is
265+
// really sent preemptively for the initial call.
266+
Future<Response> f = client.prepareGet(getTargetUrlNoAuth())//
267+
.setRealm((new Realm.RealmBuilder()).setScheme(AuthScheme.BASIC).setPrincipal(USER).setPassword(ADMIN).setUsePreemptiveAuth(true).build())//
252268
.execute();
253269

254270
Response resp = f.get(3, TimeUnit.SECONDS);

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/filters/AsyncHttpClientFilter.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,20 @@
1414
package org.asynchttpclient.providers.grizzly.filters;
1515

1616
import static org.asynchttpclient.providers.grizzly.filters.SwitchingSSLFilter.getHandshakeError;
17+
import static org.asynchttpclient.providers.grizzly.GrizzlyAsyncHttpProvider.NTLM_ENGINE;
1718
import static org.asynchttpclient.util.AsyncHttpProviderUtils.getAuthority;
19+
import static org.asynchttpclient.util.AuthenticatorUtils.computeBasicAuthentication;
20+
import static org.asynchttpclient.util.AuthenticatorUtils.computeDigestAuthentication;
1821
import static org.asynchttpclient.util.MiscUtil.isNonEmpty;
1922

23+
2024
import org.asynchttpclient.AsyncCompletionHandler;
2125
import org.asynchttpclient.AsyncHandler;
2226
import org.asynchttpclient.AsyncHttpClientConfig;
2327
import org.asynchttpclient.FluentCaseInsensitiveStringsMap;
2428
import org.asynchttpclient.FluentStringsMap;
2529
import org.asynchttpclient.ProxyServer;
30+
import org.asynchttpclient.Realm;
2631
import org.asynchttpclient.Request;
2732
import org.asynchttpclient.RequestBuilder;
2833
import org.asynchttpclient.Response;
@@ -262,6 +267,7 @@ private boolean sendAsGrizzlyRequest(final RequestInfoHolder requestInfoHolder,
262267
addHostHeader(request, uri, requestPacket);
263268
addGeneralHeaders(request, requestPacket);
264269
addCookies(request, requestPacket);
270+
addAuthorizationHeader(request, requestPacket);
265271

266272
initTransferCompletionHandler(request, httpTxContext.getHandler());
267273

@@ -398,6 +404,36 @@ private static void addHostHeader(final Request request, final URI uri, final Ht
398404
}
399405
}
400406

407+
private void addAuthorizationHeader(final Request request, final HttpRequestPacket requestPacket) {
408+
Realm realm = request.getRealm();
409+
if (realm == null) {
410+
realm = config.getRealm();
411+
}
412+
if (realm != null && realm.getUsePreemptiveAuth()) {
413+
final String authHeaderValue = generateAuthHeader(realm);
414+
if (authHeaderValue != null) {
415+
requestPacket.addHeader(Header.Authorization, authHeaderValue);
416+
}
417+
}
418+
}
419+
420+
private String generateAuthHeader(final Realm realm) {
421+
try {
422+
switch (realm.getAuthScheme()) {
423+
case BASIC:
424+
return computeBasicAuthentication(realm);
425+
case DIGEST:
426+
return computeDigestAuthentication(realm);
427+
case NTLM:
428+
return NTLM_ENGINE.generateType1Msg("NTLM " + realm.getNtlmDomain(), realm.getNtlmHost());
429+
default:
430+
return null;
431+
}
432+
} catch (Exception e) {
433+
throw new RuntimeException(e);
434+
}
435+
}
436+
401437
private static boolean isUpgradeRequest(final AsyncHandler handler) {
402438
return (handler instanceof UpgradeHandler);
403439
}

0 commit comments

Comments
 (0)