diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncClientAuthenticationTest.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncClientAuthenticationTest.java index 6e4544f9ed..6a0641ce48 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncClientAuthenticationTest.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncClientAuthenticationTest.java @@ -44,6 +44,7 @@ import org.apache.hc.client5.http.auth.AuthSchemeFactory; import org.apache.hc.client5.http.auth.AuthScope; import org.apache.hc.client5.http.auth.BearerToken; +import org.apache.hc.client5.http.auth.ChallengeType; import org.apache.hc.client5.http.auth.CredentialsProvider; import org.apache.hc.client5.http.auth.StandardAuthScheme; import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; @@ -116,7 +117,7 @@ void testBasicAuthenticationNoCreds() throws Exception { Assertions.assertNotNull(response); Assertions.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getCode()); Mockito.verify(credsProvider).getCredentials( - Mockito.eq(new AuthScope(target, "test realm", "basic")), Mockito.any()); + Mockito.eq(new AuthScope(target, "test realm", "basic", ChallengeType.TARGET)), Mockito.any()); } @Test @@ -141,7 +142,8 @@ void testBasicAuthenticationFailure() throws Exception { Assertions.assertNotNull(response); Assertions.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getCode()); Mockito.verify(credsProvider).getCredentials( - Mockito.eq(new AuthScope(target, "test realm", "basic")), Mockito.any()); + Mockito.eq(new AuthScope(target, "test realm", "basic", ChallengeType.TARGET)), + Mockito.any()); } @Test @@ -167,7 +169,8 @@ void testBasicAuthenticationSuccess() throws Exception { Assertions.assertNotNull(response); Assertions.assertEquals(HttpStatus.SC_OK, response.getCode()); Mockito.verify(credsProvider).getCredentials( - Mockito.eq(new AuthScope(target, "test realm", "basic")), Mockito.any()); + Mockito.eq(new AuthScope(target, "test realm", "basic", ChallengeType.TARGET)), + Mockito.any()); } @Test @@ -193,7 +196,8 @@ void testBasicAuthenticationWithEntitySuccess() throws Exception { Assertions.assertNotNull(response); Assertions.assertEquals(HttpStatus.SC_OK, response.getCode()); Mockito.verify(credsProvider).getCredentials( - Mockito.eq(new AuthScope(target, "test realm", "basic")), Mockito.any()); + Mockito.eq(new AuthScope(target, "test realm", "basic", ChallengeType.TARGET)), + Mockito.any()); } @Test @@ -245,7 +249,8 @@ void testBasicAuthenticationExpectationSuccess() throws Exception { Assertions.assertNotNull(response); Assertions.assertEquals(HttpStatus.SC_OK, response.getCode()); Mockito.verify(credsProvider).getCredentials( - Mockito.eq(new AuthScope(target, "test realm", "basic")), Mockito.any()); + Mockito.eq(new AuthScope(target, "test realm", "basic", ChallengeType.TARGET)), + Mockito.any()); } @Test @@ -453,7 +458,8 @@ protected void customizeUnauthorizedResponse(final HttpResponse unauthorized) { Assertions.assertNotNull(response); Assertions.assertEquals(HttpStatus.SC_OK, response.getCode()); Mockito.verify(credsProvider).getCredentials( - Mockito.eq(new AuthScope(target, "test realm", "basic")), Mockito.any()); + Mockito.eq(new AuthScope(target, "test realm", "basic", ChallengeType.TARGET)), + Mockito.any()); } private final static String CHARS = "0123456789abcdef"; @@ -491,7 +497,8 @@ void testBearerTokenAuthentication() throws Exception { Assertions.assertNotNull(response1); Assertions.assertEquals(HttpStatus.SC_UNAUTHORIZED, response1.getCode()); Mockito.verify(credsProvider).getCredentials( - Mockito.eq(new AuthScope(target, "test realm", "bearer")), Mockito.any()); + Mockito.eq(new AuthScope(target, "test realm", "bearer", ChallengeType.TARGET)), + Mockito.any()); final HttpClientContext context2 = HttpClientContext.create(); Mockito.when(credsProvider.getCredentials(Mockito.any(), Mockito.any())) diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1ClientAuthentication.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1ClientAuthentication.java index 8cd4bcf2c6..5089213bb7 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1ClientAuthentication.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1ClientAuthentication.java @@ -32,6 +32,7 @@ import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder; import org.apache.hc.client5.http.auth.AuthScope; +import org.apache.hc.client5.http.auth.ChallengeType; import org.apache.hc.client5.http.auth.CredentialsProvider; import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; import org.apache.hc.client5.http.protocol.HttpClientContext; @@ -87,7 +88,7 @@ protected void customizeUnauthorizedResponse(final HttpResponse unauthorized) { Assertions.assertNotNull(response); Assertions.assertEquals(HttpStatus.SC_OK, response.getCode()); Mockito.verify(credsProvider).getCredentials( - Mockito.eq(new AuthScope(target, "test realm", "basic")), Mockito.any()); + Mockito.eq(new AuthScope(target, "test realm", "basic", ChallengeType.TARGET)), Mockito.any()); } } \ No newline at end of file diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestClientAuthentication.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestClientAuthentication.java index 7b8bf7900f..f2652e6b67 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestClientAuthentication.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestClientAuthentication.java @@ -47,6 +47,7 @@ import org.apache.hc.client5.http.auth.AuthSchemeFactory; import org.apache.hc.client5.http.auth.AuthScope; import org.apache.hc.client5.http.auth.BearerToken; +import org.apache.hc.client5.http.auth.ChallengeType; import org.apache.hc.client5.http.auth.CredentialsProvider; import org.apache.hc.client5.http.auth.StandardAuthScheme; import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; @@ -137,7 +138,8 @@ void testBasicAuthenticationNoCreds() throws Exception { return null; }); Mockito.verify(credsProvider).getCredentials( - Mockito.eq(new AuthScope(target, "test realm", "basic")), Mockito.any()); + Mockito.eq(new AuthScope(target, "test realm", "basic", ChallengeType.TARGET)), + Mockito.any()); } @Test @@ -163,7 +165,8 @@ void testBasicAuthenticationFailure() throws Exception { return null; }); Mockito.verify(credsProvider).getCredentials( - Mockito.eq(new AuthScope(target, "test realm", "basic")), Mockito.any()); + Mockito.eq(new AuthScope(target, "test realm", "basic", ChallengeType.TARGET)), + Mockito.any()); } @Test @@ -188,7 +191,8 @@ void testBasicAuthenticationSuccess() throws Exception { return null; }); Mockito.verify(credsProvider).getCredentials( - Mockito.eq(new AuthScope(target, "test realm", "basic")), Mockito.any()); + Mockito.eq(new AuthScope(target, "test realm", "basic", ChallengeType.TARGET)), + Mockito.any()); } @Test @@ -278,7 +282,8 @@ void testBasicAuthenticationSuccessOnRepeatablePost() throws Exception { return null; }); Mockito.verify(credsProvider).getCredentials( - Mockito.eq(new AuthScope(target, "test realm", "basic")), Mockito.any()); + Mockito.eq(new AuthScope(target, "test realm", "basic", ChallengeType.TARGET)), + Mockito.any()); } @Test @@ -754,7 +759,8 @@ protected void customizeUnauthorizedResponse(final ClassicHttpResponse unauthori return null; }); Mockito.verify(credsProvider).getCredentials( - Mockito.eq(new AuthScope(target, "test realm", "basic")), Mockito.any()); + Mockito.eq(new AuthScope(target, "test realm", "basic", ChallengeType.TARGET)), + Mockito.any()); } private final static String CHARS = "0123456789abcdef"; @@ -794,7 +800,8 @@ void testBearerTokenAuthentication() throws Exception { return null; }); Mockito.verify(credsProvider).getCredentials( - Mockito.eq(new AuthScope(target, "test realm", "bearer")), Mockito.any()); + Mockito.eq(new AuthScope(target, "test realm", "bearer", ChallengeType.TARGET)), + Mockito.any()); final HttpClientContext context2 = HttpClientContext.create(); Mockito.when(credsProvider.getCredentials(Mockito.any(), Mockito.any())) diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthScheme.java b/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthScheme.java index 075b5f93e1..f3b50d54de 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthScheme.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthScheme.java @@ -214,6 +214,32 @@ boolean isResponseReady( CredentialsProvider credentialsProvider, HttpContext context) throws AuthenticationException; + /** + * Determines whether or not an authorization response can be generated based on + * the actual authentication state. Generally the outcome of this method will depend + * upon availability of user credentials necessary to produce an authorization + * response. + * + * @param host The opposite endpoint + * @param challengeType the challenge type + * @param credentialsProvider The credentials to be used for authentication + * @param context HTTP context + * @throws AuthenticationException if authorization string cannot + * be generated due to an authentication failure + * + * @return {@code true} if an authorization response can be generated and + * the authentication handshake can proceed, {@code false} otherwise. + * + * @since 5.7 + */ + default boolean isResponseReady( + final HttpHost host, + final ChallengeType challengeType, + final CredentialsProvider credentialsProvider, + final HttpContext context) throws AuthenticationException { + return isResponseReady(host, credentialsProvider, context); + } + /** * Returns {@link Principal} whose credentials are used to generate * an authentication response. Connection based schemes are required diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthScope.java b/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthScope.java index cd4593f297..2f462f1383 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthScope.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthScope.java @@ -50,6 +50,7 @@ public class AuthScope { private final int port; private final String realm; private final String schemeName; + private final ChallengeType challengeType; /** * Defines auth scope with the given {@code protocol}, {@code host}, {@code port}, @@ -65,18 +66,38 @@ public class AuthScope { * to any realm on the host. * @param schemeName authentication scheme name. May be {@code null} if applies * to any auth scheme supported by the host. + * + * @since 5.7 */ public AuthScope( final String protocol, final String host, final int port, final String realm, - final String schemeName) { + final String schemeName, + final ChallengeType challengeType) { this.protocol = protocol != null ? protocol.toLowerCase(Locale.ROOT) : null; this.host = host != null ? host.toLowerCase(Locale.ROOT) : null; this.port = port >= 0 ? port : -1; this.realm = realm; this.schemeName = schemeName != null ? schemeName.toUpperCase(Locale.ROOT) : null; + this.challengeType = challengeType; + } + + public AuthScope( + final String protocol, + final String host, + final int port, + final String realm, + final String schemeName) { + this(protocol, host, port, realm, schemeName, null); + } + + public AuthScope( + final HttpHost origin, + final String realm, + final String schemeName) { + this(origin, realm, schemeName, null); } /** @@ -88,18 +109,20 @@ public AuthScope( * @param schemeName authentication scheme name. May be {@code null} if applies * to any auth scheme supported by the host. * - * @since 4.2 + * @since 5.7 */ public AuthScope( final HttpHost origin, final String realm, - final String schemeName) { + final String schemeName, + final ChallengeType challengeType) { Args.notNull(origin, "Host"); this.protocol = origin.getSchemeName().toLowerCase(Locale.ROOT); this.host = origin.getHostName().toLowerCase(Locale.ROOT); this.port = origin.getPort() >= 0 ? origin.getPort() : -1; this.realm = realm; this.schemeName = schemeName != null ? schemeName.toUpperCase(Locale.ROOT) : null; + this.challengeType = challengeType; } /** @@ -122,7 +145,7 @@ public AuthScope(final HttpHost origin) { * to any port of the host. */ public AuthScope(final String host, final int port) { - this(null, host, port, null, null); + this(null, host, port, null, null, null); } /** @@ -136,6 +159,7 @@ public AuthScope(final AuthScope authScope) { this.port = authScope.getPort(); this.realm = authScope.getRealm(); this.schemeName = authScope.getSchemeName(); + this.challengeType = authScope.getChallengeType(); } public String getProtocol() { @@ -158,6 +182,13 @@ public String getSchemeName() { return this.schemeName; } + /** + * @since 5.7 + */ + public ChallengeType getChallengeType() { + return challengeType; + } + /** * Tests if the authentication scopes match. * @@ -167,36 +198,43 @@ public String getSchemeName() { */ public int match(final AuthScope that) { int factor = 0; - if (Objects.equals(this.schemeName, that.schemeName)) { + if (Objects.equals(this.challengeType, that.challengeType)) { factor += 1; + } else { + if (this.challengeType != null && that.challengeType != null) { + return -1; + } + } + if (Objects.equals(this.schemeName, that.schemeName)) { + factor += 2; } else { if (this.schemeName != null && that.schemeName != null) { return -1; } } if (Objects.equals(this.realm, that.realm)) { - factor += 2; + factor += 4; } else { if (this.realm != null && that.realm != null) { return -1; } } if (this.port == that.port) { - factor += 4; + factor += 8; } else { if (this.port != -1 && that.port != -1) { return -1; } } if (Objects.equals(this.protocol, that.protocol)) { - factor += 8; + factor += 16; } else { if (this.protocol != null && that.protocol != null) { return -1; } } if (Objects.equals(this.host, that.host)) { - factor += 16; + factor += 32; } else { if (this.host != null && that.host != null) { return -1; @@ -216,7 +254,8 @@ public boolean equals(final Object obj) { && Objects.equals(this.host, that.host) && this.port == that.port && Objects.equals(this.realm, that.realm) - && Objects.equals(this.schemeName, that.schemeName); + && Objects.equals(this.schemeName, that.schemeName) + && Objects.equals(this.challengeType, that.challengeType); } return false; } @@ -229,6 +268,7 @@ public int hashCode() { hash = LangUtils.hashCode(hash, this.port); hash = LangUtils.hashCode(hash, this.realm); hash = LangUtils.hashCode(hash, this.schemeName); + hash = LangUtils.hashCode(hash, this.challengeType); return hash; } @@ -266,6 +306,12 @@ public String toString() { } else { buffer.append(""); } + buffer.append(' '); + if (this.challengeType != null) { + buffer.append(this.challengeType); + } else { + buffer.append(""); + } return buffer.toString(); } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/AuthenticationHandler.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/AuthenticationHandler.java index 1e74d23311..04e34466e8 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/AuthenticationHandler.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/AuthenticationHandler.java @@ -352,7 +352,7 @@ public boolean handleResponse( final String schemeName = authScheme.getName(); final AuthChallenge challenge = challengeMap.get(schemeName.toLowerCase(Locale.ROOT)); authScheme.processChallenge(host, challenged, challenge, clientContext); - if (authScheme.isResponseReady(host, credsProvider, clientContext)) { + if (authScheme.isResponseReady(host, challengeType, credsProvider, clientContext)) { authOptions.add(authScheme); } } catch (final AuthenticationException | MalformedChallengeException ex) { diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java index 15caa3ef71..8f30064dd5 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java @@ -40,6 +40,7 @@ import org.apache.hc.client5.http.auth.AuthScope; import org.apache.hc.client5.http.auth.AuthStateCacheable; 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.CredentialsProvider; import org.apache.hc.client5.http.auth.MalformedChallengeException; @@ -149,13 +150,21 @@ public boolean isResponseReady( final HttpHost host, final CredentialsProvider credentialsProvider, final HttpContext context) throws AuthenticationException { + return isResponseReady(host, null, credentialsProvider, context); + } + + @Override + public boolean isResponseReady( + final HttpHost host, + final ChallengeType challengeType, + final CredentialsProvider credentialsProvider, + final HttpContext context) throws AuthenticationException { Args.notNull(host, "Auth host"); Args.notNull(credentialsProvider, "CredentialsProvider"); - final AuthScope authScope = new AuthScope(host, getRealm(), getName()); - final Credentials credentials = credentialsProvider.getCredentials( - authScope, context); + final AuthScope authScope = new AuthScope(host, getRealm(), getName(), challengeType); + final Credentials credentials = credentialsProvider.getCredentials(authScope, context); if (credentials instanceof UsernamePasswordCredentials) { this.credentials = (UsernamePasswordCredentials) credentials; return true; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BearerScheme.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BearerScheme.java index 7dc105498f..ab79485a05 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BearerScheme.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BearerScheme.java @@ -39,6 +39,7 @@ import org.apache.hc.client5.http.auth.AuthStateCacheable; import org.apache.hc.client5.http.auth.AuthenticationException; import org.apache.hc.client5.http.auth.BearerToken; +import org.apache.hc.client5.http.auth.ChallengeType; import org.apache.hc.client5.http.auth.Credentials; import org.apache.hc.client5.http.auth.CredentialsProvider; import org.apache.hc.client5.http.auth.MalformedChallengeException; @@ -129,11 +130,20 @@ public boolean isResponseReady( final HttpHost host, final CredentialsProvider credentialsProvider, final HttpContext context) throws AuthenticationException { + return isResponseReady(host, null, credentialsProvider, context); + } + + @Override + public boolean isResponseReady( + final HttpHost host, + final ChallengeType challengeType, + final CredentialsProvider credentialsProvider, + final HttpContext context) throws AuthenticationException { Args.notNull(host, "Auth host"); Args.notNull(credentialsProvider, "Credentials provider"); - final AuthScope authScope = new AuthScope(host, getRealm(), getName()); + final AuthScope authScope = new AuthScope(host, getRealm(), getName(), challengeType); final Credentials credentials = credentialsProvider.getCredentials(authScope, context); if (credentials instanceof BearerToken) { this.bearerToken = (BearerToken) credentials; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java index d2484d1245..2ced3b1464 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java @@ -50,6 +50,7 @@ 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.CredentialsProvider; import org.apache.hc.client5.http.auth.MalformedChallengeException; @@ -338,13 +339,21 @@ public boolean isResponseReady( final HttpHost host, final CredentialsProvider credentialsProvider, final HttpContext context) throws AuthenticationException { + return isResponseReady(host, null, credentialsProvider, context); + } + + @Override + public boolean isResponseReady( + final HttpHost host, + final ChallengeType challengeType, + final CredentialsProvider credentialsProvider, + final HttpContext context) throws AuthenticationException { Args.notNull(host, "Auth host"); Args.notNull(credentialsProvider, "CredentialsProvider"); - final AuthScope authScope = new AuthScope(host, getRealm(), getName()); - final Credentials credentials = credentialsProvider.getCredentials( - authScope, context); + final AuthScope authScope = new AuthScope(host, getRealm(), getName(), challengeType); + final Credentials credentials = credentialsProvider.getCredentials(authScope, context); if (credentials instanceof UsernamePasswordCredentials) { this.credentials = (UsernamePasswordCredentials) credentials; return true; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/ScramScheme.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/ScramScheme.java index dc4aeec09a..1154477860 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/ScramScheme.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/ScramScheme.java @@ -52,6 +52,7 @@ 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.CredentialsProvider; import org.apache.hc.client5.http.auth.MalformedChallengeException; @@ -435,22 +436,33 @@ public String getRealm() { return this.realm; } + @Override + public boolean isResponseReady( + final HttpHost host, + final CredentialsProvider credentialsProvider, + final HttpContext context) throws AuthenticationException { + return isResponseReady(host, null, credentialsProvider, context); + } + /** * Allow response when: * - INIT (preemptive client-first) — only if creds have been prepared * - ANNOUNCED (401 without data) * - SERVER_FIRST_RCVD (ready to send client-final) + * + * @since 5.7 */ @Override public boolean isResponseReady( final HttpHost host, + final ChallengeType challengeType, final CredentialsProvider credentialsProvider, final HttpContext context) throws AuthenticationException { Args.notNull(credentialsProvider, "Credentials provider"); final HttpClientContext clientContext = HttpClientContext.cast(context); - final AuthScope scope = new AuthScope(host, this.realm, getName()); + final AuthScope scope = new AuthScope(host, this.realm, getName(), challengeType); final Credentials creds = credentialsProvider.getCredentials(scope, clientContext); if (!(creds instanceof UsernamePasswordCredentials)) { return false; diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/auth/TestAuthScope.java b/httpclient5/src/test/java/org/apache/hc/client5/http/auth/TestAuthScope.java index 531d0119b7..c3956c3465 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/auth/TestAuthScope.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/auth/TestAuthScope.java @@ -37,13 +37,13 @@ class TestAuthScope { @Test void testBasics() { - final AuthScope authscope = new AuthScope("http", "somehost", 80, "somerealm", "SomeScheme"); + final AuthScope authscope = new AuthScope("http", "somehost", 80, "somerealm", "SomeScheme", ChallengeType.TARGET); Assertions.assertEquals("SOMESCHEME", authscope.getSchemeName()); Assertions.assertEquals("http", authscope.getProtocol()); Assertions.assertEquals("somehost", authscope.getHost()); Assertions.assertEquals(80, authscope.getPort()); Assertions.assertEquals("somerealm", authscope.getRealm()); - Assertions.assertEquals("SOMESCHEME 'somerealm' http://somehost:80", authscope.toString()); + Assertions.assertEquals("SOMESCHEME 'somerealm' http://somehost:80 TARGET", authscope.toString()); } @Test @@ -55,7 +55,7 @@ void testByOrigin() { Assertions.assertEquals(8080, authscope.getPort()); Assertions.assertNull(authscope.getRealm()); Assertions.assertEquals("http", authscope.getProtocol()); - Assertions.assertEquals(" http://somehost:8080", authscope.toString()); + Assertions.assertEquals(" http://somehost:8080 ", authscope.toString()); } @Test @@ -65,7 +65,7 @@ void testMixedCaseHostname() { Assertions.assertEquals("somehost", authscope.getHost()); Assertions.assertEquals(80, authscope.getPort()); Assertions.assertNull(authscope.getRealm()); - Assertions.assertEquals(" ://somehost:80", authscope.toString()); + Assertions.assertEquals(" ://somehost:80 ", authscope.toString()); } @Test @@ -82,13 +82,13 @@ void testBasicsAllOptional() { Assertions.assertNull(authscope.getHost()); Assertions.assertEquals(-1, authscope.getPort()); Assertions.assertNull(authscope.getRealm()); - Assertions.assertEquals(" ://:", authscope.toString()); + Assertions.assertEquals(" ://: ", authscope.toString()); } @Test void testScopeMatching() { - final AuthScope authscope1 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme"); - final AuthScope authscope2 = new AuthScope("http", "someotherhost", 80, "somerealm", "somescheme"); + final AuthScope authscope1 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme", ChallengeType.TARGET); + final AuthScope authscope2 = new AuthScope("http", "someotherhost", 80, "somerealm", "somescheme", ChallengeType.TARGET); Assertions.assertTrue(authscope1.match(authscope2) < 0); int m1 = authscope1.match(new AuthScope(null, null, -1, null, "somescheme")); @@ -114,6 +114,10 @@ void testScopeMatching() { m1 = authscope1.match(new AuthScope(null, null, -1, null, null)); m2 = authscope1.match(new AuthScope(null, null, -1, null, "somescheme")); Assertions.assertTrue(m2 > m1); + + m1 = authscope1.match(new AuthScope(null, null, -1, null, "somescheme", null)); + m2 = authscope1.match(new AuthScope(null, null, -1, null, "somescheme", ChallengeType.TARGET)); + Assertions.assertTrue(m2 > m1); } @Test diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestAuthenticationHandler.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestAuthenticationHandler.java index 0d3015557b..db2a0015f1 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestAuthenticationHandler.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestAuthenticationHandler.java @@ -190,8 +190,9 @@ void testAuthenticationCredentialsForBasic() throws AuthenticationException, Mal response.addHeader(new BasicHeader(HttpHeaders.WWW_AUTHENTICATE, StandardAuthScheme.DIGEST + " realm=\"realm1\", nonce=\"1234\"")); final Credentials credentials = new UsernamePasswordCredentials("user", "pass".toCharArray()); - Mockito.when(this.credentialsProvider.getCredentials(Mockito.eq(new AuthScope(host, "test", StandardAuthScheme.BASIC)), - Mockito.any())).thenReturn(credentials); + Mockito.when(this.credentialsProvider.getCredentials( + Mockito.eq(new AuthScope(host, "test", StandardAuthScheme.BASIC,ChallengeType.TARGET)), Mockito.any())) + .thenReturn(credentials); final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy(); @@ -324,8 +325,9 @@ void testAuthenticationNoMatchingChallenge() throws AuthenticationException, Mal response.addHeader(new BasicHeader(HttpHeaders.WWW_AUTHENTICATE, "whatever realm=\"realm1\", stuff=\"1234\"")); final Credentials credentials = new UsernamePasswordCredentials("user", "pass".toCharArray()); - Mockito.when(this.credentialsProvider.getCredentials(Mockito.eq(new AuthScope(host, "realm1", StandardAuthScheme.DIGEST)), - Mockito.any())).thenReturn(credentials); + Mockito.when(this.credentialsProvider.getCredentials( + Mockito.eq(new AuthScope(host, "realm1",StandardAuthScheme.DIGEST, ChallengeType.TARGET)), Mockito.any())) + .thenReturn(credentials); final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy();