HTTPCLIENT-1855: Update DIGEST nonce counter in auth cache after auth challenge

This commit is contained in:
alessandro.gherardi 2017-10-02 10:06:07 -06:00 committed by Oleg Kalnichevski
parent 74514b65ff
commit 7e44b9635e
2 changed files with 35 additions and 1 deletions

View File

@ -44,6 +44,7 @@ import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.conn.routing.RouteInfo;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.Args;
@ -135,7 +136,8 @@ public class RequestAuthCache implements HttpRequestInterceptor {
final Credentials creds = credsProvider.getCredentials(authScope);
if (creds != null) {
if ("BASIC".equalsIgnoreCase(authScheme.getSchemeName())) {
if (AuthSchemes.BASIC.equalsIgnoreCase(authScheme.getSchemeName())
|| AuthSchemes.DIGEST.equalsIgnoreCase(authScheme.getSchemeName())) {
authState.setState(AuthProtocolState.CHALLENGED);
} else {
authState.setState(AuthProtocolState.SUCCESS);

View File

@ -37,6 +37,7 @@ import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.auth.DigestScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.message.BasicHttpRequest;
@ -55,6 +56,8 @@ public class TestRequestAuthCache {
private AuthScope authscope2;
private BasicScheme authscheme1;
private BasicScheme authscheme2;
private DigestScheme digestAuthscheme1;
private DigestScheme digestAuthscheme2;
private BasicCredentialsProvider credProvider;
private AuthState targetState;
private AuthState proxyState;
@ -71,6 +74,8 @@ public class TestRequestAuthCache {
this.authscope2 = new AuthScope(this.proxy);
this.authscheme1 = new BasicScheme();
this.authscheme2 = new BasicScheme();
this.digestAuthscheme1 = new DigestScheme();
this.digestAuthscheme2 = new DigestScheme();
this.credProvider.setCredentials(this.authscope1, this.creds1);
this.credProvider.setCredentials(this.authscope2, this.creds2);
@ -118,6 +123,33 @@ public class TestRequestAuthCache {
Assert.assertSame(this.creds2, this.proxyState.getCredentials());
}
@Test
public void testPreemptiveTargetAndProxyAuthDigest() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
final AuthCache authCache = new BasicAuthCache();
authCache.put(this.target, this.digestAuthscheme1);
authCache.put(this.proxy, this.digestAuthscheme2);
context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
final HttpRequestInterceptor interceptor = new RequestAuthCache();
interceptor.process(request, context);
Assert.assertNotNull(this.targetState.getAuthScheme());
Assert.assertSame(this.targetState.getState(), AuthProtocolState.CHALLENGED);
Assert.assertSame(this.creds1, this.targetState.getCredentials());
Assert.assertNotNull(this.proxyState.getAuthScheme());
Assert.assertSame(this.proxyState.getState(), AuthProtocolState.CHALLENGED);
Assert.assertSame(this.creds2, this.proxyState.getCredentials());
}
@Test
public void testCredentialsProviderNotSet() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", "/");