Properly distinguish between AuthScheme and auth scheme name

Throughout the code the terms 'authScheme' and 'scheme' have been used
synonymously for an AuthScheme instance and a string-based auth scheme
name. To avoid confusion, fields, methods and variable have been adapted
to distinguish both properly. If necessary, Javadoc has been modified to
denote the nature of the input.

Also an auth scheme name is retained as-is, but normalized to lowercase
if comparsion is required.

This closes #193
This commit is contained in:
Michael Osipov 2019-12-30 01:20:04 +01:00
parent 634886ab61
commit 3730b03a99
14 changed files with 91 additions and 82 deletions

View File

@ -44,8 +44,8 @@ public class BasicAuthTokenExtractor {
if (i == -1) { if (i == -1) {
throw new ProtocolException("Invalid challenge response: " + challengeResponse); throw new ProtocolException("Invalid challenge response: " + challengeResponse);
} }
final String authscheme = challengeResponse.substring(0, i); final String schemeName = challengeResponse.substring(0, i);
if (authscheme.equalsIgnoreCase(StandardAuthScheme.BASIC)) { if (schemeName.equalsIgnoreCase(StandardAuthScheme.BASIC)) {
final String s = challengeResponse.substring(i + 1).trim(); final String s = challengeResponse.substring(i + 1).trim();
try { try {
final byte[] credsRaw = s.getBytes(StandardCharsets.US_ASCII); final byte[] credsRaw = s.getBytes(StandardCharsets.US_ASCII);

View File

@ -32,8 +32,8 @@ import com.sun.jna.platform.win32.WinError;
public final class WindowsNegotiateSchemeGetTokenFail extends WindowsNegotiateScheme { public final class WindowsNegotiateSchemeGetTokenFail extends WindowsNegotiateScheme {
public WindowsNegotiateSchemeGetTokenFail(final String scheme, final String servicePrincipalName) { public WindowsNegotiateSchemeGetTokenFail(final String schemeName, final String servicePrincipalName) {
super(scheme, servicePrincipalName); super(schemeName, servicePrincipalName);
} }
@Override @Override

View File

@ -74,7 +74,7 @@ public class WindowsNegotiateScheme implements AuthScheme {
private final Logger log = LoggerFactory.getLogger(getClass()); private final Logger log = LoggerFactory.getLogger(getClass());
// NTLM or Negotiate // NTLM or Negotiate
private final String scheme; private final String schemeName;
private final String servicePrincipalName; private final String servicePrincipalName;
private ChallengeType challengeType; private ChallengeType challengeType;
@ -83,15 +83,15 @@ public class WindowsNegotiateScheme implements AuthScheme {
private CtxtHandle sspiContext; private CtxtHandle sspiContext;
private boolean continueNeeded; private boolean continueNeeded;
WindowsNegotiateScheme(final String scheme, final String servicePrincipalName) { WindowsNegotiateScheme(final String schemeName, final String servicePrincipalName) {
super(); super();
this.scheme = (scheme == null) ? StandardAuthScheme.SPNEGO : scheme; this.schemeName = (schemeName == null) ? StandardAuthScheme.SPNEGO : schemeName;
this.continueNeeded = true; this.continueNeeded = true;
this.servicePrincipalName = servicePrincipalName; this.servicePrincipalName = servicePrincipalName;
if (this.log.isDebugEnabled()) { if (this.log.isDebugEnabled()) {
this.log.debug("Created WindowsNegotiateScheme using " + this.scheme); this.log.debug("Created WindowsNegotiateScheme using " + this.schemeName);
} }
} }
@ -115,7 +115,7 @@ public class WindowsNegotiateScheme implements AuthScheme {
@Override @Override
public String getName() { public String getName() {
return scheme; return schemeName;
} }
@Override @Override
@ -186,7 +186,7 @@ public class WindowsNegotiateScheme implements AuthScheme {
clientCred = new CredHandle(); clientCred = new CredHandle();
final int rc = Secur32.INSTANCE.AcquireCredentialsHandle(username, final int rc = Secur32.INSTANCE.AcquireCredentialsHandle(username,
scheme, Sspi.SECPKG_CRED_OUTBOUND, null, null, null, null, schemeName, Sspi.SECPKG_CRED_OUTBOUND, null, null, null, null,
clientCred, lifetime); clientCred, lifetime);
if (WinError.SEC_E_OK != rc) { if (WinError.SEC_E_OK != rc) {
@ -220,7 +220,7 @@ public class WindowsNegotiateScheme implements AuthScheme {
throw ex; throw ex;
} }
} }
return scheme + " " + response; return schemeName + " " + response;
} }
private void failAuthCleanup() { private void failAuthCleanup() {

View File

@ -46,28 +46,28 @@ import org.apache.hc.core5.util.Args;
public final class AuthChallenge { public final class AuthChallenge {
private final ChallengeType challengeType; private final ChallengeType challengeType;
private final String scheme; private final String schemeName;
private final String value; private final String value;
private final List<NameValuePair> params; private final List<NameValuePair> params;
public AuthChallenge(final ChallengeType challengeType, final String scheme, final String value, final List<? extends NameValuePair> params) { public AuthChallenge(final ChallengeType challengeType, final String schemeName, final String value, final List<? extends NameValuePair> params) {
super(); super();
this.challengeType = Args.notNull(challengeType, "Challenge type"); this.challengeType = Args.notNull(challengeType, "Challenge type");
this.scheme = Args.notNull(scheme, "Auth scheme"); this.schemeName = Args.notNull(schemeName, "schemeName");
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 ChallengeType challengeType, final String scheme, final NameValuePair... params) { public AuthChallenge(final ChallengeType challengeType, final String schemeName, final NameValuePair... params) {
this(challengeType, scheme, null, Arrays.asList(params)); this(challengeType, schemeName, null, Arrays.asList(params));
} }
public ChallengeType getChallengeType() { public ChallengeType getChallengeType() {
return challengeType; return challengeType;
} }
public String getScheme() { public String getSchemeName() {
return scheme; return schemeName;
} }
public String getValue() { public String getValue() {
@ -81,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(schemeName).append(" ");
if (value != null) { if (value != null) {
buffer.append(value); buffer.append(value);
} else if (params != null) { } else if (params != null) {

View File

@ -48,7 +48,7 @@ public class AuthScope {
private final String host; private final String host;
private final int port; private final int port;
private final String realm; private final String realm;
private final String authScheme; private final String schemeName;
/** /**
* Defines auth scope with the given {@code protocol}, {@code host}, {@code port}, * Defines auth scope with the given {@code protocol}, {@code host}, {@code port},
@ -62,20 +62,20 @@ public class AuthScope {
* to any port of the host. * to any port of the host.
* @param realm authentication realm. May be {@code null} if applies * @param realm authentication realm. May be {@code null} if applies
* to any realm on the host. * to any realm on the host.
* @param authScheme authentication scheme. May be {@code null} if applies * @param schemeName authentication scheme name. May be {@code null} if applies
* to any authScheme supported by the host. * to any auth scheme supported by the host.
*/ */
public AuthScope( public AuthScope(
final String protocol, final String protocol,
final String host, final String host,
final int port, final int port,
final String realm, final String realm,
final String authScheme) { final String schemeName) {
this.protocol = protocol != null ? protocol.toLowerCase(Locale.ROOT) : null; this.protocol = protocol != null ? protocol.toLowerCase(Locale.ROOT) : null;
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.authScheme = authScheme != null ? authScheme.toUpperCase(Locale.ROOT): null; this.schemeName = schemeName != null ? schemeName : null;
} }
/** /**
@ -84,8 +84,8 @@ public class AuthScope {
* @param origin host of origin * @param origin host of origin
* @param realm authentication realm. May be {@code null} if applies * @param realm authentication realm. May be {@code null} if applies
* to any realm on the host. * to any realm on the host.
* @param schemeName authentication authScheme. May be {@code null} if applies * @param schemeName authentication scheme name. May be {@code null} if applies
* to any authScheme supported by the host. * to any auth scheme supported by the host.
* *
* @since 4.2 * @since 4.2
*/ */
@ -98,7 +98,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.authScheme = schemeName != null ? schemeName.toUpperCase(Locale.ROOT): null; this.schemeName = schemeName != null ? schemeName : null;
} }
/** /**
@ -134,7 +134,7 @@ public class AuthScope {
this.host = authScope.getHost(); this.host = authScope.getHost();
this.port = authScope.getPort(); this.port = authScope.getPort();
this.realm = authScope.getRealm(); this.realm = authScope.getRealm();
this.authScheme = authScope.getAuthScheme(); this.schemeName = authScope.getSchemeName();
} }
public String getProtocol() { public String getProtocol() {
@ -153,8 +153,8 @@ public class AuthScope {
return this.realm; return this.realm;
} }
public String getAuthScheme() { public String getSchemeName() {
return this.authScheme; return this.schemeName;
} }
/** /**
@ -166,10 +166,11 @@ public class AuthScope {
*/ */
public int match(final AuthScope that) { public int match(final AuthScope that) {
int factor = 0; int factor = 0;
if (LangUtils.equals(this.authScheme, that.authScheme)) { if (LangUtils.equals(toNullSafeLowerCase(this.schemeName),
toNullSafeLowerCase(that.schemeName))) {
factor += 1; factor += 1;
} else { } else {
if (this.authScheme != null && that.authScheme != null) { if (this.schemeName != null && that.schemeName != null) {
return -1; return -1;
} }
} }
@ -215,7 +216,8 @@ public class AuthScope {
&& LangUtils.equals(this.host, that.host) && LangUtils.equals(this.host, that.host)
&& this.port == that.port && this.port == that.port
&& LangUtils.equals(this.realm, that.realm) && LangUtils.equals(this.realm, that.realm)
&& LangUtils.equals(this.authScheme, that.authScheme); && LangUtils.equals(toNullSafeLowerCase(this.schemeName),
toNullSafeLowerCase(that.schemeName));
} }
return false; return false;
} }
@ -227,15 +229,19 @@ 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, this.authScheme); hash = LangUtils.hashCode(hash, toNullSafeLowerCase(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();
if (this.authScheme != null) { if (this.schemeName != null) {
buffer.append(this.authScheme); buffer.append(this.schemeName);
} else { } else {
buffer.append("<any auth scheme>"); buffer.append("<any auth scheme>");
} }

View File

@ -373,7 +373,7 @@ public class RequestConfig implements Cloneable {
/** /**
* Determines the order of preference for supported authentication schemes * Determines the order of preference for supported authentication schemes
* when authenticating with the target host. * by their names when authenticating with the target host.
* <p> * <p>
* Default: {@code null} * Default: {@code null}
* </p> * </p>
@ -385,7 +385,7 @@ public class RequestConfig implements Cloneable {
/** /**
* Determines the order of preference for supported authentication schemes * Determines the order of preference for supported authentication schemes
* when authenticating with the proxy host. * by their names when authenticating with the proxy host.
* <p> * <p>
* Default: {@code null} * Default: {@code null}
* </p> * </p>

View File

@ -97,13 +97,13 @@ public class DefaultAuthenticationStrategy implements AuthenticationStrategy {
this.log.debug("Authentication schemes in the order of preference: " + authPrefs); this.log.debug("Authentication schemes in the order of preference: " + authPrefs);
} }
for (final String id: authPrefs) { for (final String schemeName: authPrefs) {
final AuthChallenge challenge = challenges.get(id.toLowerCase(Locale.ROOT)); final AuthChallenge challenge = challenges.get(schemeName.toLowerCase(Locale.ROOT));
if (challenge != null) { if (challenge != null) {
final AuthSchemeFactory authSchemeFactory = registry.lookup(id); final AuthSchemeFactory authSchemeFactory = registry.lookup(schemeName);
if (authSchemeFactory == null) { if (authSchemeFactory == null) {
if (this.log.isWarnEnabled()) { if (this.log.isWarnEnabled()) {
this.log.warn("Authentication scheme " + id + " not supported"); this.log.warn("Authentication scheme " + schemeName + " not supported");
// Try again // Try again
} }
continue; continue;
@ -112,7 +112,7 @@ public class DefaultAuthenticationStrategy implements AuthenticationStrategy {
options.add(authScheme); options.add(authScheme);
} else { } else {
if (this.log.isDebugEnabled()) { if (this.log.isDebugEnabled()) {
this.log.debug("Challenge for " + id + " authentication scheme not available"); this.log.debug("Challenge for " + schemeName + " authentication scheme not available");
} }
} }
} }

View File

@ -88,42 +88,42 @@ public class AuthChallengeParser {
final ChallengeType challengeType, final CharSequence buffer, final ParserCursor cursor) throws ParseException { 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 schemeName = null;
final List<NameValuePair> params = new ArrayList<>(); final List<NameValuePair> params = new ArrayList<>();
while (!cursor.atEnd()) { while (!cursor.atEnd()) {
final NameValuePair tokenOrParameter = parseTokenOrParameter(buffer, cursor); final NameValuePair tokenOrParameter = parseTokenOrParameter(buffer, cursor);
if (tokenOrParameter.getValue() == null && !cursor.atEnd() && buffer.charAt(cursor.getPos()) != COMMA_CHAR) { if (tokenOrParameter.getValue() == null && !cursor.atEnd() && buffer.charAt(cursor.getPos()) != COMMA_CHAR) {
if (scheme != null) { if (schemeName != null) {
if (params.isEmpty()) { if (params.isEmpty()) {
throw new ParseException("Malformed auth challenge"); throw new ParseException("Malformed auth challenge");
} }
list.add(createAuthChallenge(challengeType, scheme, params)); list.add(createAuthChallenge(challengeType, schemeName, params));
params.clear(); params.clear();
} }
scheme = tokenOrParameter.getName(); schemeName = tokenOrParameter.getName();
} else { } else {
params.add(tokenOrParameter); params.add(tokenOrParameter);
if (!cursor.atEnd() && buffer.charAt(cursor.getPos()) != COMMA_CHAR) { if (!cursor.atEnd() && buffer.charAt(cursor.getPos()) != COMMA_CHAR) {
scheme = null; schemeName = null;
} }
} }
if (!cursor.atEnd() && buffer.charAt(cursor.getPos()) == COMMA_CHAR) { if (!cursor.atEnd() && buffer.charAt(cursor.getPos()) == COMMA_CHAR) {
cursor.updatePos(cursor.getPos() + 1); cursor.updatePos(cursor.getPos() + 1);
} }
} }
list.add(createAuthChallenge(challengeType, scheme, params)); list.add(createAuthChallenge(challengeType, schemeName, params));
return list; return list;
} }
private static AuthChallenge createAuthChallenge(final ChallengeType challengeType, final String scheme, final List<NameValuePair> params) throws ParseException { private static AuthChallenge createAuthChallenge(final ChallengeType challengeType, final String schemeName, final List<NameValuePair> params) throws ParseException {
if (scheme != null) { if (schemeName != 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(challengeType, scheme, nvp.getName(), null); return new AuthChallenge(challengeType, schemeName, nvp.getName(), null);
} }
} }
return new AuthChallenge(challengeType, scheme, null, params.size() > 0 ? params : null); return new AuthChallenge(challengeType, schemeName, 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);

View File

@ -195,9 +195,9 @@ public final class HttpAuthenticator {
continue; continue;
} }
for (final AuthChallenge authChallenge: authChallenges) { for (final AuthChallenge authChallenge: authChallenges) {
final String scheme = authChallenge.getScheme().toLowerCase(Locale.ROOT); final String schemeName = authChallenge.getSchemeName().toLowerCase(Locale.ROOT);
if (!challengeMap.containsKey(scheme)) { if (!challengeMap.containsKey(schemeName)) {
challengeMap.put(scheme, authChallenge); challengeMap.put(schemeName, authChallenge);
} }
} }
} }
@ -220,8 +220,8 @@ public final class HttpAuthenticator {
case UNCHALLENGED: case UNCHALLENGED:
final AuthScheme authScheme = authExchange.getAuthScheme(); final AuthScheme authScheme = authExchange.getAuthScheme();
if (authScheme != null) { if (authScheme != null) {
final String id = authScheme.getName(); final String schemeName = authScheme.getName();
final AuthChallenge challenge = challengeMap.get(id.toLowerCase(Locale.ROOT)); final AuthChallenge challenge = challengeMap.get(schemeName.toLowerCase(Locale.ROOT));
if (challenge != null) { if (challenge != null) {
this.log.debug("Authorization challenge processed"); this.log.debug("Authorization challenge processed");
try { try {
@ -259,8 +259,8 @@ public final class HttpAuthenticator {
final Queue<AuthScheme> authOptions = new LinkedList<>(); final Queue<AuthScheme> authOptions = new LinkedList<>();
for (final AuthScheme authScheme: preferredSchemes) { for (final AuthScheme authScheme: preferredSchemes) {
try { try {
final String id = authScheme.getName(); final String schemeName = authScheme.getName();
final AuthChallenge challenge = challengeMap.get(id.toLowerCase(Locale.ROOT)); final AuthChallenge challenge = challengeMap.get(schemeName.toLowerCase(Locale.ROOT));
authScheme.processChallenge(challenge, context); authScheme.processChallenge(challenge, context);
if (authScheme.isResponseReady(host, credsProvider, context)) { if (authScheme.isResponseReady(host, credsProvider, context)) {
authOptions.add(authScheme); authOptions.add(authScheme);

View File

@ -91,7 +91,7 @@ public class SystemDefaultCredentialsProvider implements CredentialsStore {
authScope.getPort(), authScope.getPort(),
protocol, protocol,
authScope.getRealm(), authScope.getRealm(),
authScope.getAuthScheme(), authScope.getSchemeName(),
targetHostURL, targetHostURL,
requestorType); requestorType);
} }
@ -128,7 +128,7 @@ public class SystemDefaultCredentialsProvider implements CredentialsStore {
if (domain != null) { if (domain != null) {
return new NTCredentials(systemcreds.getUserName(), systemcreds.getPassword(), null, domain); return new NTCredentials(systemcreds.getUserName(), systemcreds.getPassword(), null, domain);
} }
if (StandardAuthScheme.NTLM.equalsIgnoreCase(authScope.getAuthScheme())) { if (StandardAuthScheme.NTLM.equalsIgnoreCase(authScope.getSchemeName())) {
// Domain may be specified in a fully qualified user name // Domain may be specified in a fully qualified user name
return new NTCredentials( return new NTCredentials(
systemcreds.getUserName(), systemcreds.getPassword(), null, null); systemcreds.getUserName(), systemcreds.getPassword(), null, null);

View File

@ -38,7 +38,7 @@ public class TestAuthChallenge {
@Test @Test
public void testAuthChallengeWithValue() { public void testAuthChallengeWithValue() {
final AuthChallenge authChallenge = new AuthChallenge(ChallengeType.TARGET, StandardAuthScheme.BASIC, "blah", null); final AuthChallenge authChallenge = new AuthChallenge(ChallengeType.TARGET, StandardAuthScheme.BASIC, "blah", null);
Assert.assertEquals(StandardAuthScheme.BASIC, authChallenge.getScheme()); Assert.assertEquals(StandardAuthScheme.BASIC, authChallenge.getSchemeName());
Assert.assertEquals("blah", authChallenge.getValue()); Assert.assertEquals("blah", authChallenge.getValue());
Assert.assertEquals(null, authChallenge.getParams()); Assert.assertEquals(null, authChallenge.getParams());
Assert.assertEquals(StandardAuthScheme.BASIC + " blah", authChallenge.toString()); Assert.assertEquals(StandardAuthScheme.BASIC + " blah", authChallenge.toString());
@ -48,7 +48,7 @@ public class TestAuthChallenge {
public void testAuthChallengeWithParams() { public void testAuthChallengeWithParams() {
final AuthChallenge authChallenge = new AuthChallenge(ChallengeType.TARGET, StandardAuthScheme.BASIC, null, final AuthChallenge authChallenge = new AuthChallenge(ChallengeType.TARGET, StandardAuthScheme.BASIC, null,
Arrays.asList(new BasicNameValuePair("blah", "this"), new BasicNameValuePair("blah", "that"))); Arrays.asList(new BasicNameValuePair("blah", "this"), new BasicNameValuePair("blah", "that")));
Assert.assertEquals(StandardAuthScheme.BASIC, authChallenge.getScheme()); Assert.assertEquals(StandardAuthScheme.BASIC, authChallenge.getSchemeName());
Assert.assertEquals(null, authChallenge.getValue()); Assert.assertEquals(null, authChallenge.getValue());
Assert.assertNotNull(authChallenge.getParams()); Assert.assertNotNull(authChallenge.getParams());
Assert.assertEquals(StandardAuthScheme.BASIC + " [blah=this, blah=that]", authChallenge.toString()); Assert.assertEquals(StandardAuthScheme.BASIC + " [blah=this, blah=that]", authChallenge.toString());

View File

@ -37,20 +37,20 @@ 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");
Assert.assertEquals("SOMESCHEME", authscope.getAuthScheme()); Assert.assertEquals("SomeScheme", authscope.getSchemeName());
Assert.assertEquals("http", authscope.getProtocol()); Assert.assertEquals("http", authscope.getProtocol());
Assert.assertEquals("somehost", authscope.getHost()); Assert.assertEquals("somehost", authscope.getHost());
Assert.assertEquals(80, authscope.getPort()); Assert.assertEquals(80, authscope.getPort());
Assert.assertEquals("somerealm", authscope.getRealm()); Assert.assertEquals("somerealm", authscope.getRealm());
Assert.assertEquals("SOMESCHEME 'somerealm' http://somehost:80", authscope.toString()); Assert.assertEquals("SomeScheme 'somerealm' http://somehost:80", authscope.toString());
} }
@Test @Test
public void testByOrigin() { public void testByOrigin() {
final HttpHost host = new HttpHost("http", "somehost", 8080); final HttpHost host = new HttpHost("http", "somehost", 8080);
final AuthScope authscope = new AuthScope(host); final AuthScope authscope = new AuthScope(host);
Assert.assertEquals(null, authscope.getAuthScheme()); Assert.assertEquals(null, authscope.getSchemeName());
Assert.assertEquals("somehost", authscope.getHost()); Assert.assertEquals("somehost", authscope.getHost());
Assert.assertEquals(8080, authscope.getPort()); Assert.assertEquals(8080, authscope.getPort());
Assert.assertEquals(null, authscope.getRealm()); Assert.assertEquals(null, authscope.getRealm());
@ -61,7 +61,7 @@ public class TestAuthScope {
@Test @Test
public void testMixedCaseHostname() { public void testMixedCaseHostname() {
final AuthScope authscope = new AuthScope("SomeHost", 80); final AuthScope authscope = new AuthScope("SomeHost", 80);
Assert.assertEquals(null, authscope.getAuthScheme()); Assert.assertEquals(null, authscope.getSchemeName());
Assert.assertEquals("somehost", authscope.getHost()); Assert.assertEquals("somehost", authscope.getHost());
Assert.assertEquals(80, authscope.getPort()); Assert.assertEquals(80, authscope.getPort());
Assert.assertEquals(null, authscope.getRealm()); Assert.assertEquals(null, authscope.getRealm());
@ -78,7 +78,7 @@ public class TestAuthScope {
@Test @Test
public void testBasicsAllOptional() { public void testBasicsAllOptional() {
final AuthScope authscope = new AuthScope(null, null, -1, null, null); final AuthScope authscope = new AuthScope(null, null, -1, null, null);
Assert.assertEquals(null, authscope.getAuthScheme()); Assert.assertEquals(null, authscope.getSchemeName());
Assert.assertEquals(null, authscope.getHost()); Assert.assertEquals(null, authscope.getHost());
Assert.assertEquals(-1, authscope.getPort()); Assert.assertEquals(-1, authscope.getPort());
Assert.assertEquals(null, authscope.getRealm()); Assert.assertEquals(null, authscope.getRealm());
@ -125,6 +125,7 @@ public class TestAuthScope {
final AuthScope authscope5 = new AuthScope("http", "somehost", 80, "someotherrealm", "somescheme"); final AuthScope authscope5 = new AuthScope("http", "somehost", 80, "someotherrealm", "somescheme");
final AuthScope authscope6 = new AuthScope("http", "somehost", 80, "somerealm", "someotherscheme"); final AuthScope authscope6 = new AuthScope("http", "somehost", 80, "somerealm", "someotherscheme");
final AuthScope authscope7 = new AuthScope("https", "somehost", 80, "somerealm", "somescheme"); final AuthScope authscope7 = new AuthScope("https", "somehost", 80, "somerealm", "somescheme");
final AuthScope authscope8 = new AuthScope("https", "somehost", 80, "somerealm", "SomeScheme");
Assert.assertTrue(authscope1.equals(authscope1)); Assert.assertTrue(authscope1.equals(authscope1));
Assert.assertFalse(authscope1.equals(authscope2)); Assert.assertFalse(authscope1.equals(authscope2));
Assert.assertTrue(authscope1.equals(authscope3)); Assert.assertTrue(authscope1.equals(authscope3));
@ -132,6 +133,7 @@ public class TestAuthScope {
Assert.assertFalse(authscope1.equals(authscope5)); Assert.assertFalse(authscope1.equals(authscope5));
Assert.assertFalse(authscope1.equals(authscope6)); Assert.assertFalse(authscope1.equals(authscope6));
Assert.assertFalse(authscope1.equals(authscope7)); Assert.assertFalse(authscope1.equals(authscope7));
Assert.assertTrue(authscope7.equals(authscope8));
} }
@Test @Test
@ -143,6 +145,7 @@ public class TestAuthScope {
final AuthScope authscope5 = new AuthScope("http", "somehost", 80, "someotherrealm", "somescheme"); final AuthScope authscope5 = new AuthScope("http", "somehost", 80, "someotherrealm", "somescheme");
final AuthScope authscope6 = new AuthScope("http", "somehost", 80, "somerealm", "someotherscheme"); final AuthScope authscope6 = new AuthScope("http", "somehost", 80, "somerealm", "someotherscheme");
final AuthScope authscope7 = new AuthScope("https", "somehost", 80, "somerealm", "somescheme"); final AuthScope authscope7 = new AuthScope("https", "somehost", 80, "somerealm", "somescheme");
final AuthScope authscope8 = new AuthScope("https", "somehost", 80, "somerealm", "SomeScheme");
Assert.assertTrue(authscope1.hashCode() == authscope1.hashCode()); Assert.assertTrue(authscope1.hashCode() == authscope1.hashCode());
Assert.assertFalse(authscope1.hashCode() == authscope2.hashCode()); Assert.assertFalse(authscope1.hashCode() == authscope2.hashCode());
Assert.assertTrue(authscope1.hashCode() == authscope3.hashCode()); Assert.assertTrue(authscope1.hashCode() == authscope3.hashCode());
@ -150,6 +153,7 @@ public class TestAuthScope {
Assert.assertFalse(authscope1.hashCode() == authscope5.hashCode()); Assert.assertFalse(authscope1.hashCode() == authscope5.hashCode());
Assert.assertFalse(authscope1.hashCode() == authscope6.hashCode()); Assert.assertFalse(authscope1.hashCode() == authscope6.hashCode());
Assert.assertFalse(authscope1.hashCode() == authscope7.hashCode()); Assert.assertFalse(authscope1.hashCode() == authscope7.hashCode());
Assert.assertTrue(authscope7.hashCode() == authscope8.hashCode());
} }
} }

View File

@ -157,7 +157,7 @@ public class TestAuthChallengeParser {
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);
Assert.assertEquals(StandardAuthScheme.BASIC, challenge1.getScheme()); Assert.assertEquals(StandardAuthScheme.BASIC, challenge1.getSchemeName());
Assert.assertEquals(null, challenge1.getValue()); Assert.assertEquals(null, challenge1.getValue());
final List<NameValuePair> params = challenge1.getParams(); final List<NameValuePair> params = challenge1.getParams();
Assert.assertNotNull(params); Assert.assertNotNull(params);
@ -174,7 +174,7 @@ public class TestAuthChallengeParser {
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);
Assert.assertEquals(StandardAuthScheme.BASIC, challenge1.getScheme()); Assert.assertEquals(StandardAuthScheme.BASIC, challenge1.getSchemeName());
Assert.assertEquals(null, challenge1.getValue()); Assert.assertEquals(null, challenge1.getValue());
final List<NameValuePair> params = challenge1.getParams(); final List<NameValuePair> params = challenge1.getParams();
Assert.assertNotNull(params); Assert.assertNotNull(params);
@ -193,12 +193,12 @@ public class TestAuthChallengeParser {
Assert.assertEquals(2, challenges.size()); Assert.assertEquals(2, challenges.size());
final AuthChallenge challenge1 = challenges.get(0); final AuthChallenge challenge1 = challenges.get(0);
Assert.assertEquals("This", challenge1.getScheme()); Assert.assertEquals("This", challenge1.getSchemeName());
Assert.assertEquals("xxxxxxxxxxxxxxxxxxxxxx", challenge1.getValue()); Assert.assertEquals("xxxxxxxxxxxxxxxxxxxxxx", challenge1.getValue());
Assert.assertNull(challenge1.getParams()); Assert.assertNull(challenge1.getParams());
final AuthChallenge challenge2 = challenges.get(1); final AuthChallenge challenge2 = challenges.get(1);
Assert.assertEquals("That", challenge2.getScheme()); Assert.assertEquals("That", challenge2.getSchemeName());
Assert.assertEquals("yyyyyyyyyyyyyyyyyyyyyy", challenge2.getValue()); Assert.assertEquals("yyyyyyyyyyyyyyyyyyyyyy", challenge2.getValue());
Assert.assertNull(challenge2.getParams()); Assert.assertNull(challenge2.getParams());
} }
@ -214,7 +214,7 @@ public class TestAuthChallengeParser {
Assert.assertEquals(2, challenges.size()); Assert.assertEquals(2, challenges.size());
final AuthChallenge challenge1 = challenges.get(0); final AuthChallenge challenge1 = challenges.get(0);
Assert.assertEquals(StandardAuthScheme.BASIC, challenge1.getScheme()); Assert.assertEquals(StandardAuthScheme.BASIC, challenge1.getSchemeName());
Assert.assertEquals(null, challenge1.getValue()); Assert.assertEquals(null, challenge1.getValue());
final List<NameValuePair> params1 = challenge1.getParams(); final List<NameValuePair> params1 = challenge1.getParams();
Assert.assertNotNull(params1); Assert.assertNotNull(params1);
@ -224,7 +224,7 @@ public class TestAuthChallengeParser {
assertNameValuePair(new BasicNameValuePair("param2", "that"), params1.get(2)); assertNameValuePair(new BasicNameValuePair("param2", "that"), params1.get(2));
final AuthChallenge challenge2 = challenges.get(1); final AuthChallenge challenge2 = challenges.get(1);
Assert.assertEquals(StandardAuthScheme.BASIC, challenge2.getScheme()); Assert.assertEquals(StandardAuthScheme.BASIC, challenge2.getSchemeName());
Assert.assertEquals(null, challenge2.getValue()); Assert.assertEquals(null, challenge2.getValue());
final List<NameValuePair> params2 = challenge2.getParams(); final List<NameValuePair> params2 = challenge2.getParams();
Assert.assertNotNull(params2); Assert.assertNotNull(params2);
@ -245,7 +245,7 @@ public class TestAuthChallengeParser {
Assert.assertEquals(1, challenges.size()); Assert.assertEquals(1, challenges.size());
final AuthChallenge challenge1 = challenges.get(0); final AuthChallenge challenge1 = challenges.get(0);
Assert.assertEquals("This", challenge1.getScheme()); Assert.assertEquals("This", challenge1.getSchemeName());
Assert.assertEquals(null, challenge1.getValue()); Assert.assertEquals(null, challenge1.getValue());
Assert.assertNull(challenge1.getParams()); Assert.assertNull(challenge1.getParams());
} }
@ -284,7 +284,7 @@ public class TestAuthChallengeParser {
Assert.assertEquals(1, challenges.size()); Assert.assertEquals(1, challenges.size());
final AuthChallenge challenge1 = challenges.get(0); final AuthChallenge challenge1 = challenges.get(0);
Assert.assertEquals("blah", challenge1.getScheme()); Assert.assertEquals("blah", challenge1.getSchemeName());
Assert.assertEquals("blah", challenge1.getValue()); Assert.assertEquals("blah", challenge1.getValue());
Assert.assertNull(challenge1.getParams()); Assert.assertNull(challenge1.getParams());
} }
@ -299,7 +299,7 @@ public class TestAuthChallengeParser {
Assert.assertEquals(1, challenges.size()); Assert.assertEquals(1, challenges.size());
final AuthChallenge challenge1 = challenges.get(0); final AuthChallenge challenge1 = challenges.get(0);
Assert.assertEquals("blah", challenge1.getScheme()); Assert.assertEquals("blah", challenge1.getSchemeName());
Assert.assertEquals(null, challenge1.getValue()); Assert.assertEquals(null, challenge1.getValue());
final List<NameValuePair> params1 = challenge1.getParams(); final List<NameValuePair> params1 = challenge1.getParams();
Assert.assertNotNull(params1); Assert.assertNotNull(params1);

View File

@ -28,7 +28,6 @@ package org.apache.hc.client5.http.impl.auth;
import java.net.Authenticator; import java.net.Authenticator;
import java.net.Authenticator.RequestorType; import java.net.Authenticator.RequestorType;
import java.util.Locale;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.PasswordAuthentication; import java.net.PasswordAuthentication;
import java.net.URL; import java.net.URL;
@ -101,7 +100,7 @@ public class TestSystemDefaultCredentialsProvider {
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(PROXY_HOST1, null, PROXY_PORT1, PROXY_PROTOCOL1,
PROMPT1, StandardAuthScheme.BASIC.toUpperCase(Locale.ROOT), httpRequestUrl, PROMPT1, StandardAuthScheme.BASIC, httpRequestUrl,
RequestorType.SERVER); RequestorType.SERVER);
Assert.assertNotNull(receivedCredentials); Assert.assertNotNull(receivedCredentials);
Assert.assertEquals(AUTH1.getUserName(), receivedCredentials.getUserPrincipal().getName()); Assert.assertEquals(AUTH1.getUserName(), receivedCredentials.getUserPrincipal().getName());
@ -119,7 +118,7 @@ public class TestSystemDefaultCredentialsProvider {
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(PROXY_HOST1, null, PROXY_PORT1, PROXY_PROTOCOL1,
PROMPT1, StandardAuthScheme.BASIC.toUpperCase(Locale.ROOT), null, PROMPT1, StandardAuthScheme.BASIC, null,
RequestorType.SERVER); RequestorType.SERVER);
Assert.assertNotNull(receivedCredentials); Assert.assertNotNull(receivedCredentials);
Assert.assertEquals(AUTH1.getUserName(), receivedCredentials.getUserPrincipal().getName()); Assert.assertEquals(AUTH1.getUserName(), receivedCredentials.getUserPrincipal().getName());