diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthChallenge.java b/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthChallenge.java index c5a968624..a9f6638de 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthChallenge.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthChallenge.java @@ -45,20 +45,25 @@ import org.apache.hc.core5.util.Args; @Contract(threading = ThreadingBehavior.IMMUTABLE) public final class AuthChallenge { + private final ChallengeType challengeType; private final String scheme; private final String value; private final List params; - public AuthChallenge(final String scheme, final String value, final List params) { + public AuthChallenge(final ChallengeType challengeType, final String scheme, final String value, final List params) { super(); - Args.notNull(scheme, "Auth scheme"); - this.scheme = scheme; + this.challengeType = Args.notNull(challengeType, "Challenge type"); + this.scheme = Args.notNull(scheme, "Auth scheme"); this.value = value; this.params = params != null ? Collections.unmodifiableList(new ArrayList<>(params)) : null; } - public AuthChallenge(final String scheme, final NameValuePair... params) { - this(scheme, null, Arrays.asList(params)); + public AuthChallenge(final ChallengeType challengeType, final String scheme, final NameValuePair... params) { + this(challengeType, scheme, null, Arrays.asList(params)); + } + + public ChallengeType getChallengeType() { + return challengeType; } public String getScheme() { @@ -76,7 +81,7 @@ public final class AuthChallenge { @Override public String toString() { final StringBuilder buffer = new StringBuilder(); - buffer.append(scheme).append(" "); + buffer.append(challengeType).append(" ").append(scheme).append(" "); if (value != null) { buffer.append(value); } else if (params != null) { diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/AuthChallengeParser.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/AuthChallengeParser.java index 83203608c..2ddc27dae 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/AuthChallengeParser.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/AuthChallengeParser.java @@ -32,6 +32,7 @@ import java.util.BitSet; import java.util.List; 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.ParseException; import org.apache.hc.core5.http.message.BasicNameValuePair; @@ -70,7 +71,7 @@ public class AuthChallengeParser { return new BasicNameValuePair(token, null); } - public List parse(final CharSequence buffer, final ParserCursor cursor) throws ParseException { + public List parse(final ChallengeType challengeType, final CharSequence buffer, final ParserCursor cursor) throws ParseException { final List list = new ArrayList<>(); String scheme = null; @@ -82,7 +83,7 @@ public class AuthChallengeParser { if (params.isEmpty()) { throw new ParseException("Malformed auth challenge"); } - list.add(createAuthChallenge(scheme, params)); + list.add(createAuthChallenge(challengeType, scheme, params)); params.clear(); } scheme = tokenOrParameter.getName(); @@ -96,24 +97,24 @@ public class AuthChallengeParser { cursor.updatePos(cursor.getPos() + 1); } } - list.add(createAuthChallenge(scheme, params)); + list.add(createAuthChallenge(challengeType, scheme, params)); return list; } - private static AuthChallenge createAuthChallenge(final String scheme, final List params) throws ParseException { + private static AuthChallenge createAuthChallenge(final ChallengeType challengeType, final String scheme, final List params) throws ParseException { if (scheme != null) { if (params.size() == 1) { final NameValuePair nvp = params.get(0); 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) { final NameValuePair nvp = params.get(0); 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"); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/HttpAuthenticator.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/HttpAuthenticator.java index 4237b389d..c475c4a44 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/HttpAuthenticator.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/HttpAuthenticator.java @@ -156,7 +156,7 @@ public class HttpAuthenticator { final ParserCursor cursor = new ParserCursor(pos, buffer.length()); final List authChallenges; try { - authChallenges = parser.parse(buffer, cursor); + authChallenges = parser.parse(challengeType, buffer, cursor); } catch (final ParseException ex) { if (this.log.isWarnEnabled()) { this.log.warn("Malformed challenge: " + header.getValue()); diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/auth/TestAuthChallenge.java b/httpclient5/src/test/java/org/apache/hc/client5/http/auth/TestAuthChallenge.java index 3ae2d6413..68b2845ec 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/auth/TestAuthChallenge.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/auth/TestAuthChallenge.java @@ -37,7 +37,7 @@ public class TestAuthChallenge { @Test 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("blah", authChallenge.getValue()); Assert.assertEquals(null, authChallenge.getParams()); @@ -46,7 +46,7 @@ public class TestAuthChallenge { @Test 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"))); Assert.assertEquals("Basic", authChallenge.getScheme()); Assert.assertEquals(null, authChallenge.getValue()); diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestAuthChallengeParser.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestAuthChallengeParser.java index 3407b11ff..04b2d2d59 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestAuthChallengeParser.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestAuthChallengeParser.java @@ -29,6 +29,7 @@ package org.apache.hc.client5.http.impl.auth; import java.util.List; 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.ParseException; import org.apache.hc.core5.http.message.BasicNameValuePair; @@ -151,7 +152,7 @@ public class TestAuthChallengeParser { final CharArrayBuffer buffer = new CharArrayBuffer(64); buffer.append("Basic realm=blah"); final ParserCursor cursor = new ParserCursor(0, buffer.length()); - final List challenges = parser.parse(buffer, cursor); + final List challenges = parser.parse(ChallengeType.TARGET, buffer, cursor); Assert.assertNotNull(challenges); Assert.assertEquals(1, challenges.size()); final AuthChallenge challenge1 = challenges.get(0); @@ -168,7 +169,7 @@ public class TestAuthChallengeParser { final CharArrayBuffer buffer = new CharArrayBuffer(64); buffer.append(" Basic realm = blah "); final ParserCursor cursor = new ParserCursor(0, buffer.length()); - final List challenges = parser.parse(buffer, cursor); + final List challenges = parser.parse(ChallengeType.TARGET, buffer, cursor); Assert.assertNotNull(challenges); Assert.assertEquals(1, challenges.size()); final AuthChallenge challenge1 = challenges.get(0); @@ -186,7 +187,7 @@ public class TestAuthChallengeParser { buffer.append("This xxxxxxxxxxxxxxxxxxxxxx, " + "That yyyyyyyyyyyyyyyyyyyyyy "); final ParserCursor cursor = new ParserCursor(0, buffer.length()); - final List challenges = parser.parse(buffer, cursor); + final List challenges = parser.parse(ChallengeType.TARGET, buffer, cursor); Assert.assertNotNull(challenges); Assert.assertEquals(2, challenges.size()); @@ -207,7 +208,7 @@ public class TestAuthChallengeParser { buffer.append("Basic realm=blah, param1 = this, param2=that, " + "Basic realm=\"\\\"yada\\\"\", this, that=,this-and-that "); final ParserCursor cursor = new ParserCursor(0, buffer.length()); - final List challenges = parser.parse(buffer, cursor); + final List challenges = parser.parse(ChallengeType.TARGET, buffer, cursor); Assert.assertNotNull(challenges); Assert.assertEquals(2, challenges.size()); @@ -238,7 +239,7 @@ public class TestAuthChallengeParser { final CharArrayBuffer buffer = new CharArrayBuffer(64); buffer.append("This"); final ParserCursor cursor = new ParserCursor(0, buffer.length()); - final List challenges = parser.parse(buffer, cursor); + final List challenges = parser.parse(ChallengeType.TARGET, buffer, cursor); Assert.assertNotNull(challenges); Assert.assertEquals(1, challenges.size()); @@ -253,7 +254,7 @@ public class TestAuthChallengeParser { final CharArrayBuffer buffer = new CharArrayBuffer(64); buffer.append("This , "); final ParserCursor cursor = new ParserCursor(0, buffer.length()); - parser.parse(buffer, cursor); + parser.parse(ChallengeType.TARGET, buffer, cursor); } @Test(expected = ParseException.class) @@ -261,7 +262,7 @@ public class TestAuthChallengeParser { final CharArrayBuffer buffer = new CharArrayBuffer(64); buffer.append("This = that"); final ParserCursor cursor = new ParserCursor(0, buffer.length()); - parser.parse(buffer, cursor); + parser.parse(ChallengeType.TARGET, buffer, cursor); } @Test(expected = ParseException.class) @@ -269,7 +270,7 @@ public class TestAuthChallengeParser { final CharArrayBuffer buffer = new CharArrayBuffer(64); buffer.append("blah blah blah"); final ParserCursor cursor = new ParserCursor(0, buffer.length()); - parser.parse(buffer, cursor); + parser.parse(ChallengeType.TARGET, buffer, cursor); } @Test @@ -277,7 +278,7 @@ public class TestAuthChallengeParser { final CharArrayBuffer buffer = new CharArrayBuffer(64); buffer.append("blah blah"); final ParserCursor cursor = new ParserCursor(0, buffer.length()); - final List challenges = parser.parse(buffer, cursor); + final List challenges = parser.parse(ChallengeType.TARGET, buffer, cursor); Assert.assertNotNull(challenges); Assert.assertEquals(1, challenges.size()); @@ -292,7 +293,7 @@ public class TestAuthChallengeParser { final CharArrayBuffer buffer = new CharArrayBuffer(64); buffer.append("blah blah, blah"); final ParserCursor cursor = new ParserCursor(0, buffer.length()); - final List challenges = parser.parse(buffer, cursor); + final List challenges = parser.parse(ChallengeType.TARGET, buffer, cursor); Assert.assertNotNull(challenges); Assert.assertEquals(1, challenges.size()); diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestBasicScheme.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestBasicScheme.java index 48d7786c1..4df6697a1 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestBasicScheme.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestBasicScheme.java @@ -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.AuthScheme; 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.impl.sync.BasicCredentialsProvider; import org.apache.hc.core5.http.HttpHost; @@ -57,7 +58,7 @@ public class TestBasicScheme { final CharArrayBuffer buffer = new CharArrayBuffer(s.length()); buffer.append(s); final ParserCursor cursor = new ParserCursor(0, buffer.length()); - final List authChallenges = AuthChallengeParser.INSTANCE.parse(buffer, cursor); + final List authChallenges = AuthChallengeParser.INSTANCE.parse(ChallengeType.TARGET, buffer, cursor); Assert.assertEquals(1, authChallenges.size()); return authChallenges.get(0); } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestDigestScheme.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestDigestScheme.java index e64b9e0d3..8d52c71b7 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestDigestScheme.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestDigestScheme.java @@ -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.AuthScope; 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.MalformedChallengeException; import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; @@ -69,7 +70,7 @@ public class TestDigestScheme { final CharArrayBuffer buffer = new CharArrayBuffer(s.length()); buffer.append(s); final ParserCursor cursor = new ParserCursor(0, buffer.length()); - final List authChallenges = AuthChallengeParser.INSTANCE.parse(buffer, cursor); + final List authChallenges = AuthChallengeParser.INSTANCE.parse(ChallengeType.TARGET, buffer, cursor); Assert.assertEquals(1, authChallenges.size()); return authChallenges.get(0); } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/protocol/TestAuthenticationStrategy.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/protocol/TestAuthenticationStrategy.java index 75a2413bf..35de8330a 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/protocol/TestAuthenticationStrategy.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/protocol/TestAuthenticationStrategy.java @@ -85,9 +85,9 @@ public class TestAuthenticationStrategy { final HttpClientContext context = HttpClientContext.create(); final Map challenges = new HashMap<>(); - challenges.put("basic", new AuthChallenge("Basic", + challenges.put("basic", new AuthChallenge(ChallengeType.TARGET, "Basic", 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"))); final List authSchemes = authStrategy.select(ChallengeType.TARGET, challenges, context); @@ -101,11 +101,11 @@ public class TestAuthenticationStrategy { final HttpClientContext context = HttpClientContext.create(); final Map challenges = new HashMap<>(); - challenges.put("basic", new AuthChallenge("Basic", + challenges.put("basic", new AuthChallenge(ChallengeType.TARGET, "Basic", 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"))); - challenges.put("whatever", new AuthChallenge("Whatever", + challenges.put("whatever", new AuthChallenge(ChallengeType.TARGET, "Whatever", new BasicNameValuePair("realm", "realm3"))); final Registry authSchemeRegistry = RegistryBuilder.create() @@ -137,9 +137,9 @@ public class TestAuthenticationStrategy { final HttpClientContext context = HttpClientContext.create(); final Map challenges = new HashMap<>(); - challenges.put("basic", new AuthChallenge("Basic", + challenges.put("basic", new AuthChallenge(ChallengeType.TARGET, "Basic", 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"))); final Registry authSchemeRegistry = RegistryBuilder.create()