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

View File

@ -38,12 +38,12 @@ public class TestAuthScope {
@Test
public void testBasics() {
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("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", authscope.toString());
}
@Test

View File

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