Normalize scheme name in AuthScope

This commit is contained in:
Oleg Kalnichevski 2022-11-26 23:12:59 +01:00
parent ff35df66a5
commit 2d77d1d579
3 changed files with 17 additions and 19 deletions

View File

@ -76,7 +76,7 @@ public class AuthScope {
this.host = host != null ? host.toLowerCase(Locale.ROOT) : null; this.host = host != null ? host.toLowerCase(Locale.ROOT) : null;
this.port = port >= 0 ? port: -1; this.port = port >= 0 ? port: -1;
this.realm = realm; this.realm = realm;
this.schemeName = schemeName; this.schemeName = schemeName != null ? schemeName.toUpperCase(Locale.ROOT) : null;
} }
/** /**
@ -99,7 +99,7 @@ public class AuthScope {
this.host = origin.getHostName().toLowerCase(Locale.ROOT); this.host = origin.getHostName().toLowerCase(Locale.ROOT);
this.port = origin.getPort() >= 0 ? origin.getPort() : -1; this.port = origin.getPort() >= 0 ? origin.getPort() : -1;
this.realm = realm; this.realm = realm;
this.schemeName = schemeName; this.schemeName = schemeName != null ? schemeName.toUpperCase(Locale.ROOT) : null;
} }
/** /**
@ -167,8 +167,7 @@ public class AuthScope {
*/ */
public int match(final AuthScope that) { public int match(final AuthScope that) {
int factor = 0; int factor = 0;
if (Objects.equals(toNullSafeLowerCase(this.schemeName), if (Objects.equals(this.schemeName, that.schemeName)) {
toNullSafeLowerCase(that.schemeName))) {
factor += 1; factor += 1;
} else { } else {
if (this.schemeName != null && that.schemeName != null) { if (this.schemeName != null && that.schemeName != null) {
@ -217,8 +216,7 @@ public class AuthScope {
&& Objects.equals(this.host, that.host) && Objects.equals(this.host, that.host)
&& this.port == that.port && this.port == that.port
&& Objects.equals(this.realm, that.realm) && Objects.equals(this.realm, that.realm)
&& Objects.equals(toNullSafeLowerCase(this.schemeName), && Objects.equals(this.schemeName, that.schemeName);
toNullSafeLowerCase(that.schemeName));
} }
return false; return false;
} }
@ -230,14 +228,10 @@ public class AuthScope {
hash = LangUtils.hashCode(hash, this.host); hash = LangUtils.hashCode(hash, this.host);
hash = LangUtils.hashCode(hash, this.port); hash = LangUtils.hashCode(hash, this.port);
hash = LangUtils.hashCode(hash, this.realm); hash = LangUtils.hashCode(hash, this.realm);
hash = LangUtils.hashCode(hash, toNullSafeLowerCase(this.schemeName)); hash = LangUtils.hashCode(hash, this.schemeName);
return hash; return hash;
} }
private String toNullSafeLowerCase(final String str) {
return str != null ? str.toLowerCase(Locale.ROOT) : null;
}
@Override @Override
public String toString() { public String toString() {
final StringBuilder buffer = new StringBuilder(); final StringBuilder buffer = new StringBuilder();

View File

@ -38,12 +38,12 @@ public class TestAuthScope {
@Test @Test
public void testBasics() { public void testBasics() {
final AuthScope authscope = new AuthScope("http", "somehost", 80, "somerealm", "SomeScheme"); final AuthScope authscope = new AuthScope("http", "somehost", 80, "somerealm", "SomeScheme");
Assertions.assertEquals("SomeScheme", authscope.getSchemeName()); Assertions.assertEquals("SOMESCHEME", authscope.getSchemeName());
Assertions.assertEquals("http", authscope.getProtocol()); Assertions.assertEquals("http", authscope.getProtocol());
Assertions.assertEquals("somehost", authscope.getHost()); Assertions.assertEquals("somehost", authscope.getHost());
Assertions.assertEquals(80, authscope.getPort()); Assertions.assertEquals(80, authscope.getPort());
Assertions.assertEquals("somerealm", authscope.getRealm()); Assertions.assertEquals("somerealm", authscope.getRealm());
Assertions.assertEquals("SomeScheme 'somerealm' http://somehost:80", authscope.toString()); Assertions.assertEquals("SOMESCHEME 'somerealm' http://somehost:80", authscope.toString());
} }
@Test @Test

View File

@ -31,6 +31,7 @@ import java.net.Authenticator.RequestorType;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.PasswordAuthentication; import java.net.PasswordAuthentication;
import java.net.URL; import java.net.URL;
import java.util.Locale;
import org.apache.hc.client5.http.auth.AuthScope; import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.Credentials; import org.apache.hc.client5.http.auth.Credentials;
@ -99,9 +100,11 @@ public class TestSystemDefaultCredentialsProvider {
final Credentials receivedCredentials = final Credentials receivedCredentials =
new SystemDefaultCredentialsProvider().getCredentials(authScope, coreContext); new SystemDefaultCredentialsProvider().getCredentials(authScope, coreContext);
Mockito.verify(authenticatorDelegate).getPasswordAuthentication(PROXY_HOST1, null, PROXY_PORT1, PROXY_PROTOCOL1, Mockito.verify(authenticatorDelegate).getPasswordAuthentication(
PROMPT1, StandardAuthScheme.BASIC, httpRequestUrl, PROXY_HOST1, null, PROXY_PORT1, PROXY_PROTOCOL1,
RequestorType.SERVER); PROMPT1, StandardAuthScheme.BASIC.toUpperCase(Locale.ROOT),
httpRequestUrl,
RequestorType.SERVER);
Assertions.assertNotNull(receivedCredentials); Assertions.assertNotNull(receivedCredentials);
Assertions.assertEquals(AUTH1.getUserName(), receivedCredentials.getUserPrincipal().getName()); Assertions.assertEquals(AUTH1.getUserName(), receivedCredentials.getUserPrincipal().getName());
} }
@ -116,9 +119,10 @@ public class TestSystemDefaultCredentialsProvider {
final Credentials receivedCredentials = final Credentials receivedCredentials =
new SystemDefaultCredentialsProvider().getCredentials(authScope, null); new SystemDefaultCredentialsProvider().getCredentials(authScope, null);
Mockito.verify(authenticatorDelegate).getPasswordAuthentication(PROXY_HOST1, null, PROXY_PORT1, PROXY_PROTOCOL1, Mockito.verify(authenticatorDelegate).getPasswordAuthentication(
PROMPT1, StandardAuthScheme.BASIC, null, PROXY_HOST1, null, PROXY_PORT1, PROXY_PROTOCOL1,
RequestorType.SERVER); PROMPT1, StandardAuthScheme.BASIC.toUpperCase(Locale.ROOT), null,
RequestorType.SERVER);
Assertions.assertNotNull(receivedCredentials); Assertions.assertNotNull(receivedCredentials);
Assertions.assertEquals(AUTH1.getUserName(), receivedCredentials.getUserPrincipal().getName()); Assertions.assertEquals(AUTH1.getUserName(), receivedCredentials.getUserPrincipal().getName());
} }