AuthChallenge to include challenge type (target or proxy)

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1787548 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2017-03-18 12:06:32 +00:00
parent 26cfea101f
commit 8a54c1b8f7
8 changed files with 44 additions and 35 deletions

View File

@ -45,20 +45,25 @@ import org.apache.hc.core5.util.Args;
@Contract(threading = ThreadingBehavior.IMMUTABLE) @Contract(threading = ThreadingBehavior.IMMUTABLE)
public final class AuthChallenge { public final class AuthChallenge {
private final ChallengeType challengeType;
private final String scheme; private final String scheme;
private final String value; private final String value;
private final List<NameValuePair> params; private final List<NameValuePair> params;
public AuthChallenge(final String scheme, final String value, final List<? extends NameValuePair> params) { public AuthChallenge(final ChallengeType challengeType, final String scheme, final String value, final List<? extends NameValuePair> params) {
super(); super();
Args.notNull(scheme, "Auth scheme"); this.challengeType = Args.notNull(challengeType, "Challenge type");
this.scheme = scheme; this.scheme = Args.notNull(scheme, "Auth scheme");
this.value = value; this.value = value;
this.params = params != null ? Collections.unmodifiableList(new ArrayList<>(params)) : null; this.params = params != null ? Collections.unmodifiableList(new ArrayList<>(params)) : null;
} }
public AuthChallenge(final String scheme, final NameValuePair... params) { public AuthChallenge(final ChallengeType challengeType, final String scheme, final NameValuePair... params) {
this(scheme, null, Arrays.asList(params)); this(challengeType, scheme, null, Arrays.asList(params));
}
public ChallengeType getChallengeType() {
return challengeType;
} }
public String getScheme() { public String getScheme() {
@ -76,7 +81,7 @@ public final class AuthChallenge {
@Override @Override
public String toString() { public String toString() {
final StringBuilder buffer = new StringBuilder(); final StringBuilder buffer = new StringBuilder();
buffer.append(scheme).append(" "); buffer.append(challengeType).append(" ").append(scheme).append(" ");
if (value != null) { if (value != null) {
buffer.append(value); buffer.append(value);
} else if (params != null) { } else if (params != null) {

View File

@ -32,6 +32,7 @@ import java.util.BitSet;
import java.util.List; import java.util.List;
import org.apache.hc.client5.http.auth.AuthChallenge; import org.apache.hc.client5.http.auth.AuthChallenge;
import org.apache.hc.client5.http.auth.ChallengeType;
import org.apache.hc.core5.http.NameValuePair; import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.ParseException; import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.message.BasicNameValuePair; import org.apache.hc.core5.http.message.BasicNameValuePair;
@ -70,7 +71,7 @@ public class AuthChallengeParser {
return new BasicNameValuePair(token, null); return new BasicNameValuePair(token, null);
} }
public List<AuthChallenge> parse(final CharSequence buffer, final ParserCursor cursor) throws ParseException { public List<AuthChallenge> parse(final ChallengeType challengeType, final CharSequence buffer, final ParserCursor cursor) throws ParseException {
final List<AuthChallenge> list = new ArrayList<>(); final List<AuthChallenge> list = new ArrayList<>();
String scheme = null; String scheme = null;
@ -82,7 +83,7 @@ public class AuthChallengeParser {
if (params.isEmpty()) { if (params.isEmpty()) {
throw new ParseException("Malformed auth challenge"); throw new ParseException("Malformed auth challenge");
} }
list.add(createAuthChallenge(scheme, params)); list.add(createAuthChallenge(challengeType, scheme, params));
params.clear(); params.clear();
} }
scheme = tokenOrParameter.getName(); scheme = tokenOrParameter.getName();
@ -96,24 +97,24 @@ public class AuthChallengeParser {
cursor.updatePos(cursor.getPos() + 1); cursor.updatePos(cursor.getPos() + 1);
} }
} }
list.add(createAuthChallenge(scheme, params)); list.add(createAuthChallenge(challengeType, scheme, params));
return list; return list;
} }
private static AuthChallenge createAuthChallenge(final String scheme, final List<NameValuePair> params) throws ParseException { private static AuthChallenge createAuthChallenge(final ChallengeType challengeType, final String scheme, final List<NameValuePair> params) throws ParseException {
if (scheme != null) { if (scheme != null) {
if (params.size() == 1) { if (params.size() == 1) {
final NameValuePair nvp = params.get(0); final NameValuePair nvp = params.get(0);
if (nvp.getValue() == null) { if (nvp.getValue() == null) {
return new AuthChallenge(scheme, nvp.getName(), null); return new AuthChallenge(challengeType, scheme, nvp.getName(), null);
} }
} }
return new AuthChallenge(scheme, null, params.size() > 0 ? params : null); return new AuthChallenge(challengeType, scheme, null, params.size() > 0 ? params : null);
} }
if (params.size() == 1) { if (params.size() == 1) {
final NameValuePair nvp = params.get(0); final NameValuePair nvp = params.get(0);
if (nvp.getValue() == null) { if (nvp.getValue() == null) {
return new AuthChallenge(nvp.getName(), null, null); return new AuthChallenge(challengeType, nvp.getName(), null, null);
} }
} }
throw new ParseException("Malformed auth challenge"); throw new ParseException("Malformed auth challenge");

View File

@ -156,7 +156,7 @@ public class HttpAuthenticator {
final ParserCursor cursor = new ParserCursor(pos, buffer.length()); final ParserCursor cursor = new ParserCursor(pos, buffer.length());
final List<AuthChallenge> authChallenges; final List<AuthChallenge> authChallenges;
try { try {
authChallenges = parser.parse(buffer, cursor); authChallenges = parser.parse(challengeType, buffer, cursor);
} catch (final ParseException ex) { } catch (final ParseException ex) {
if (this.log.isWarnEnabled()) { if (this.log.isWarnEnabled()) {
this.log.warn("Malformed challenge: " + header.getValue()); this.log.warn("Malformed challenge: " + header.getValue());

View File

@ -37,7 +37,7 @@ public class TestAuthChallenge {
@Test @Test
public void testAuthChallengeWithValue() { public void testAuthChallengeWithValue() {
final AuthChallenge authChallenge = new AuthChallenge("Basic", "blah", null); final AuthChallenge authChallenge = new AuthChallenge(ChallengeType.TARGET, "Basic", "blah", null);
Assert.assertEquals("Basic", authChallenge.getScheme()); Assert.assertEquals("Basic", authChallenge.getScheme());
Assert.assertEquals("blah", authChallenge.getValue()); Assert.assertEquals("blah", authChallenge.getValue());
Assert.assertEquals(null, authChallenge.getParams()); Assert.assertEquals(null, authChallenge.getParams());
@ -46,7 +46,7 @@ public class TestAuthChallenge {
@Test @Test
public void testAuthChallengeWithParams() { public void testAuthChallengeWithParams() {
final AuthChallenge authChallenge = new AuthChallenge("Basic", null, final AuthChallenge authChallenge = new AuthChallenge(ChallengeType.TARGET, "Basic", null,
Arrays.asList(new BasicNameValuePair("blah", "this"), new BasicNameValuePair("blah", "that"))); Arrays.asList(new BasicNameValuePair("blah", "this"), new BasicNameValuePair("blah", "that")));
Assert.assertEquals("Basic", authChallenge.getScheme()); Assert.assertEquals("Basic", authChallenge.getScheme());
Assert.assertEquals(null, authChallenge.getValue()); Assert.assertEquals(null, authChallenge.getValue());

View File

@ -29,6 +29,7 @@ package org.apache.hc.client5.http.impl.auth;
import java.util.List; import java.util.List;
import org.apache.hc.client5.http.auth.AuthChallenge; import org.apache.hc.client5.http.auth.AuthChallenge;
import org.apache.hc.client5.http.auth.ChallengeType;
import org.apache.hc.core5.http.NameValuePair; import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.ParseException; import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.message.BasicNameValuePair; import org.apache.hc.core5.http.message.BasicNameValuePair;
@ -151,7 +152,7 @@ public class TestAuthChallengeParser {
final CharArrayBuffer buffer = new CharArrayBuffer(64); final CharArrayBuffer buffer = new CharArrayBuffer(64);
buffer.append("Basic realm=blah"); buffer.append("Basic realm=blah");
final ParserCursor cursor = new ParserCursor(0, buffer.length()); final ParserCursor cursor = new ParserCursor(0, buffer.length());
final List<AuthChallenge> challenges = parser.parse(buffer, cursor); final List<AuthChallenge> challenges = parser.parse(ChallengeType.TARGET, buffer, cursor);
Assert.assertNotNull(challenges); Assert.assertNotNull(challenges);
Assert.assertEquals(1, challenges.size()); Assert.assertEquals(1, challenges.size());
final AuthChallenge challenge1 = challenges.get(0); final AuthChallenge challenge1 = challenges.get(0);
@ -168,7 +169,7 @@ public class TestAuthChallengeParser {
final CharArrayBuffer buffer = new CharArrayBuffer(64); final CharArrayBuffer buffer = new CharArrayBuffer(64);
buffer.append(" Basic realm = blah "); buffer.append(" Basic realm = blah ");
final ParserCursor cursor = new ParserCursor(0, buffer.length()); final ParserCursor cursor = new ParserCursor(0, buffer.length());
final List<AuthChallenge> challenges = parser.parse(buffer, cursor); final List<AuthChallenge> challenges = parser.parse(ChallengeType.TARGET, buffer, cursor);
Assert.assertNotNull(challenges); Assert.assertNotNull(challenges);
Assert.assertEquals(1, challenges.size()); Assert.assertEquals(1, challenges.size());
final AuthChallenge challenge1 = challenges.get(0); final AuthChallenge challenge1 = challenges.get(0);
@ -186,7 +187,7 @@ public class TestAuthChallengeParser {
buffer.append("This xxxxxxxxxxxxxxxxxxxxxx, " + buffer.append("This xxxxxxxxxxxxxxxxxxxxxx, " +
"That yyyyyyyyyyyyyyyyyyyyyy "); "That yyyyyyyyyyyyyyyyyyyyyy ");
final ParserCursor cursor = new ParserCursor(0, buffer.length()); final ParserCursor cursor = new ParserCursor(0, buffer.length());
final List<AuthChallenge> challenges = parser.parse(buffer, cursor); final List<AuthChallenge> challenges = parser.parse(ChallengeType.TARGET, buffer, cursor);
Assert.assertNotNull(challenges); Assert.assertNotNull(challenges);
Assert.assertEquals(2, challenges.size()); Assert.assertEquals(2, challenges.size());
@ -207,7 +208,7 @@ public class TestAuthChallengeParser {
buffer.append("Basic realm=blah, param1 = this, param2=that, " + buffer.append("Basic realm=blah, param1 = this, param2=that, " +
"Basic realm=\"\\\"yada\\\"\", this, that=,this-and-that "); "Basic realm=\"\\\"yada\\\"\", this, that=,this-and-that ");
final ParserCursor cursor = new ParserCursor(0, buffer.length()); final ParserCursor cursor = new ParserCursor(0, buffer.length());
final List<AuthChallenge> challenges = parser.parse(buffer, cursor); final List<AuthChallenge> challenges = parser.parse(ChallengeType.TARGET, buffer, cursor);
Assert.assertNotNull(challenges); Assert.assertNotNull(challenges);
Assert.assertEquals(2, challenges.size()); Assert.assertEquals(2, challenges.size());
@ -238,7 +239,7 @@ public class TestAuthChallengeParser {
final CharArrayBuffer buffer = new CharArrayBuffer(64); final CharArrayBuffer buffer = new CharArrayBuffer(64);
buffer.append("This"); buffer.append("This");
final ParserCursor cursor = new ParserCursor(0, buffer.length()); final ParserCursor cursor = new ParserCursor(0, buffer.length());
final List<AuthChallenge> challenges = parser.parse(buffer, cursor); final List<AuthChallenge> challenges = parser.parse(ChallengeType.TARGET, buffer, cursor);
Assert.assertNotNull(challenges); Assert.assertNotNull(challenges);
Assert.assertEquals(1, challenges.size()); Assert.assertEquals(1, challenges.size());
@ -253,7 +254,7 @@ public class TestAuthChallengeParser {
final CharArrayBuffer buffer = new CharArrayBuffer(64); final CharArrayBuffer buffer = new CharArrayBuffer(64);
buffer.append("This , "); buffer.append("This , ");
final ParserCursor cursor = new ParserCursor(0, buffer.length()); final ParserCursor cursor = new ParserCursor(0, buffer.length());
parser.parse(buffer, cursor); parser.parse(ChallengeType.TARGET, buffer, cursor);
} }
@Test(expected = ParseException.class) @Test(expected = ParseException.class)
@ -261,7 +262,7 @@ public class TestAuthChallengeParser {
final CharArrayBuffer buffer = new CharArrayBuffer(64); final CharArrayBuffer buffer = new CharArrayBuffer(64);
buffer.append("This = that"); buffer.append("This = that");
final ParserCursor cursor = new ParserCursor(0, buffer.length()); final ParserCursor cursor = new ParserCursor(0, buffer.length());
parser.parse(buffer, cursor); parser.parse(ChallengeType.TARGET, buffer, cursor);
} }
@Test(expected = ParseException.class) @Test(expected = ParseException.class)
@ -269,7 +270,7 @@ public class TestAuthChallengeParser {
final CharArrayBuffer buffer = new CharArrayBuffer(64); final CharArrayBuffer buffer = new CharArrayBuffer(64);
buffer.append("blah blah blah"); buffer.append("blah blah blah");
final ParserCursor cursor = new ParserCursor(0, buffer.length()); final ParserCursor cursor = new ParserCursor(0, buffer.length());
parser.parse(buffer, cursor); parser.parse(ChallengeType.TARGET, buffer, cursor);
} }
@Test @Test
@ -277,7 +278,7 @@ public class TestAuthChallengeParser {
final CharArrayBuffer buffer = new CharArrayBuffer(64); final CharArrayBuffer buffer = new CharArrayBuffer(64);
buffer.append("blah blah"); buffer.append("blah blah");
final ParserCursor cursor = new ParserCursor(0, buffer.length()); final ParserCursor cursor = new ParserCursor(0, buffer.length());
final List<AuthChallenge> challenges = parser.parse(buffer, cursor); final List<AuthChallenge> challenges = parser.parse(ChallengeType.TARGET, buffer, cursor);
Assert.assertNotNull(challenges); Assert.assertNotNull(challenges);
Assert.assertEquals(1, challenges.size()); Assert.assertEquals(1, challenges.size());
@ -292,7 +293,7 @@ public class TestAuthChallengeParser {
final CharArrayBuffer buffer = new CharArrayBuffer(64); final CharArrayBuffer buffer = new CharArrayBuffer(64);
buffer.append("blah blah, blah"); buffer.append("blah blah, blah");
final ParserCursor cursor = new ParserCursor(0, buffer.length()); final ParserCursor cursor = new ParserCursor(0, buffer.length());
final List<AuthChallenge> challenges = parser.parse(buffer, cursor); final List<AuthChallenge> challenges = parser.parse(ChallengeType.TARGET, buffer, cursor);
Assert.assertNotNull(challenges); Assert.assertNotNull(challenges);
Assert.assertEquals(1, challenges.size()); Assert.assertEquals(1, challenges.size());

View File

@ -37,6 +37,7 @@ import org.apache.commons.codec.binary.Base64;
import org.apache.hc.client5.http.auth.AuthChallenge; import org.apache.hc.client5.http.auth.AuthChallenge;
import org.apache.hc.client5.http.auth.AuthScheme; import org.apache.hc.client5.http.auth.AuthScheme;
import org.apache.hc.client5.http.auth.AuthScope; import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.ChallengeType;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.impl.sync.BasicCredentialsProvider; import org.apache.hc.client5.http.impl.sync.BasicCredentialsProvider;
import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpHost;
@ -57,7 +58,7 @@ public class TestBasicScheme {
final CharArrayBuffer buffer = new CharArrayBuffer(s.length()); final CharArrayBuffer buffer = new CharArrayBuffer(s.length());
buffer.append(s); buffer.append(s);
final ParserCursor cursor = new ParserCursor(0, buffer.length()); final ParserCursor cursor = new ParserCursor(0, buffer.length());
final List<AuthChallenge> authChallenges = AuthChallengeParser.INSTANCE.parse(buffer, cursor); final List<AuthChallenge> authChallenges = AuthChallengeParser.INSTANCE.parse(ChallengeType.TARGET, buffer, cursor);
Assert.assertEquals(1, authChallenges.size()); Assert.assertEquals(1, authChallenges.size());
return authChallenges.get(0); return authChallenges.get(0);
} }

View File

@ -41,6 +41,7 @@ import org.apache.hc.client5.http.auth.AuthChallenge;
import org.apache.hc.client5.http.auth.AuthScheme; import org.apache.hc.client5.http.auth.AuthScheme;
import org.apache.hc.client5.http.auth.AuthScope; import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.AuthenticationException; import org.apache.hc.client5.http.auth.AuthenticationException;
import org.apache.hc.client5.http.auth.ChallengeType;
import org.apache.hc.client5.http.auth.Credentials; import org.apache.hc.client5.http.auth.Credentials;
import org.apache.hc.client5.http.auth.MalformedChallengeException; import org.apache.hc.client5.http.auth.MalformedChallengeException;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
@ -69,7 +70,7 @@ public class TestDigestScheme {
final CharArrayBuffer buffer = new CharArrayBuffer(s.length()); final CharArrayBuffer buffer = new CharArrayBuffer(s.length());
buffer.append(s); buffer.append(s);
final ParserCursor cursor = new ParserCursor(0, buffer.length()); final ParserCursor cursor = new ParserCursor(0, buffer.length());
final List<AuthChallenge> authChallenges = AuthChallengeParser.INSTANCE.parse(buffer, cursor); final List<AuthChallenge> authChallenges = AuthChallengeParser.INSTANCE.parse(ChallengeType.TARGET, buffer, cursor);
Assert.assertEquals(1, authChallenges.size()); Assert.assertEquals(1, authChallenges.size());
return authChallenges.get(0); return authChallenges.get(0);
} }

View File

@ -85,9 +85,9 @@ public class TestAuthenticationStrategy {
final HttpClientContext context = HttpClientContext.create(); final HttpClientContext context = HttpClientContext.create();
final Map<String, AuthChallenge> challenges = new HashMap<>(); final Map<String, AuthChallenge> challenges = new HashMap<>();
challenges.put("basic", new AuthChallenge("Basic", challenges.put("basic", new AuthChallenge(ChallengeType.TARGET, "Basic",
new BasicNameValuePair("realm", "test"))); new BasicNameValuePair("realm", "test")));
challenges.put("digest", new AuthChallenge("Digest", challenges.put("digest", new AuthChallenge(ChallengeType.TARGET, "Digest",
new BasicNameValuePair("realm", "test"), new BasicNameValuePair("nonce", "1234"))); new BasicNameValuePair("realm", "test"), new BasicNameValuePair("nonce", "1234")));
final List<AuthScheme> authSchemes = authStrategy.select(ChallengeType.TARGET, challenges, context); final List<AuthScheme> authSchemes = authStrategy.select(ChallengeType.TARGET, challenges, context);
@ -101,11 +101,11 @@ public class TestAuthenticationStrategy {
final HttpClientContext context = HttpClientContext.create(); final HttpClientContext context = HttpClientContext.create();
final Map<String, AuthChallenge> challenges = new HashMap<>(); final Map<String, AuthChallenge> challenges = new HashMap<>();
challenges.put("basic", new AuthChallenge("Basic", challenges.put("basic", new AuthChallenge(ChallengeType.TARGET, "Basic",
new BasicNameValuePair("realm", "realm1"))); new BasicNameValuePair("realm", "realm1")));
challenges.put("digest", new AuthChallenge("Digest", challenges.put("digest", new AuthChallenge(ChallengeType.TARGET, "Digest",
new BasicNameValuePair("realm", "realm2"), new BasicNameValuePair("nonce", "1234"))); new BasicNameValuePair("realm", "realm2"), new BasicNameValuePair("nonce", "1234")));
challenges.put("whatever", new AuthChallenge("Whatever", challenges.put("whatever", new AuthChallenge(ChallengeType.TARGET, "Whatever",
new BasicNameValuePair("realm", "realm3"))); new BasicNameValuePair("realm", "realm3")));
final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create() final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
@ -137,9 +137,9 @@ public class TestAuthenticationStrategy {
final HttpClientContext context = HttpClientContext.create(); final HttpClientContext context = HttpClientContext.create();
final Map<String, AuthChallenge> challenges = new HashMap<>(); final Map<String, AuthChallenge> challenges = new HashMap<>();
challenges.put("basic", new AuthChallenge("Basic", challenges.put("basic", new AuthChallenge(ChallengeType.TARGET, "Basic",
new BasicNameValuePair("realm", "realm1"))); new BasicNameValuePair("realm", "realm1")));
challenges.put("digest", new AuthChallenge("Digest", challenges.put("digest", new AuthChallenge(ChallengeType.TARGET, "Digest",
new BasicNameValuePair("realm", "realm2"), new BasicNameValuePair("nonce", "1234"))); new BasicNameValuePair("realm", "realm2"), new BasicNameValuePair("nonce", "1234")));
final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create() final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()