AuthScope cleanup

This commit is contained in:
Oleg Kalnichevski 2017-12-05 15:16:05 +01:00
parent 4a55a8cfbd
commit cca56bebe9
18 changed files with 185 additions and 301 deletions

View File

@ -106,9 +106,7 @@ public class Executor {
} }
public Executor auth(final HttpHost host, final Credentials creds) { public Executor auth(final HttpHost host, final Credentials creds) {
final AuthScope authScope = host != null ? return auth(new AuthScope(host), creds);
new AuthScope(host.getHostName(), host.getPort()) : AuthScope.ANY;
return auth(authScope, creds);
} }
/** /**
@ -174,19 +172,6 @@ public class Executor {
return authPreemptiveProxy(httpHost); return authPreemptiveProxy(httpHost);
} }
public Executor auth(final Credentials cred) {
return auth(AuthScope.ANY, cred);
}
public Executor auth(final String username, final char[] password) {
return auth(new UsernamePasswordCredentials(username, password));
}
public Executor auth(final String username, final char[] password,
final String workstation, final String domain) {
return auth(new NTCredentials(username, password, workstation, domain));
}
public Executor auth(final HttpHost host, public Executor auth(final HttpHost host,
final String username, final char[] password) { final String username, final char[] password) {
return auth(host, new UsernamePasswordCredentials(username, password)); return auth(host, new UsernamePasswordCredentials(username, password));

View File

@ -73,7 +73,7 @@ final class OSGiCredentialsProvider implements CredentialsStore {
// iterate over all active proxy configurations at the moment of getting the credential // iterate over all active proxy configurations at the moment of getting the credential
for (final ProxyConfiguration config : proxyConfigurations) { for (final ProxyConfiguration config : proxyConfigurations) {
if (config.isEnabled() && isSuitable(config, authScope)) { if (config.isEnabled() && isSuitable(config, authScope)) {
final String scheme = authScope.getScheme(); final String scheme = authScope.getAuthScheme();
if (BASIC_SCHEME_NAME.equals(scheme)) { if (BASIC_SCHEME_NAME.equals(scheme)) {
return new UsernamePasswordCredentials(config.getUsername(), config.getPassword().toCharArray()); return new UsernamePasswordCredentials(config.getUsername(), config.getPassword().toCharArray());
} else if (NTLM_SCHEME_NAME.equals(scheme)) { } else if (NTLM_SCHEME_NAME.equals(scheme)) {

View File

@ -54,7 +54,7 @@ public class OSGiCredentialsProviderTest {
@Test @Test
public void basicAuthentication() { public void basicAuthentication() {
final CredentialsProvider provider = credentialsProvider(proxy("user", "secret")); final CredentialsProvider provider = credentialsProvider(proxy("user", "secret"));
final Credentials credentials = provider.getCredentials(new AuthScope(HOST, PORT, null, "BASIC"), HTTP_CONTEXT); final Credentials credentials = provider.getCredentials(new AuthScope("http", HOST, PORT, null, "BASIC"), HTTP_CONTEXT);
assertThat(credentials, instanceOf(UsernamePasswordCredentials.class)); assertThat(credentials, instanceOf(UsernamePasswordCredentials.class));
assertCredentials((UsernamePasswordCredentials) credentials, "user", "secret"); assertCredentials((UsernamePasswordCredentials) credentials, "user", "secret");
} }
@ -62,7 +62,7 @@ public class OSGiCredentialsProviderTest {
@Test @Test
public void ntlmAuthenticationWithoutDomain() { public void ntlmAuthenticationWithoutDomain() {
final CredentialsProvider provider = credentialsProvider(proxy("user", "secret")); final CredentialsProvider provider = credentialsProvider(proxy("user", "secret"));
final Credentials credentials = provider.getCredentials(new AuthScope(HOST, PORT, null, "NTLM"), HTTP_CONTEXT); final Credentials credentials = provider.getCredentials(new AuthScope("http", HOST, PORT, null, "NTLM"), HTTP_CONTEXT);
assertThat(credentials, instanceOf(NTCredentials.class)); assertThat(credentials, instanceOf(NTCredentials.class));
assertCredentials((NTCredentials) credentials, "user", "secret", null); assertCredentials((NTCredentials) credentials, "user", "secret", null);
} }
@ -70,7 +70,7 @@ public class OSGiCredentialsProviderTest {
@Test @Test
public void ntlmAuthenticationWithDomain() { public void ntlmAuthenticationWithDomain() {
final CredentialsProvider provider = credentialsProvider(proxy("DOMAIN\\user", "secret")); final CredentialsProvider provider = credentialsProvider(proxy("DOMAIN\\user", "secret"));
final Credentials credentials = provider.getCredentials(new AuthScope(HOST, PORT, null, "NTLM"), HTTP_CONTEXT); final Credentials credentials = provider.getCredentials(new AuthScope("http", HOST, PORT, null, "NTLM"), HTTP_CONTEXT);
assertThat(credentials, instanceOf(NTCredentials.class)); assertThat(credentials, instanceOf(NTCredentials.class));
assertCredentials((NTCredentials) credentials, "user", "secret", "DOMAIN"); assertCredentials((NTCredentials) credentials, "user", "secret", "DOMAIN");
} }

View File

@ -338,7 +338,7 @@ public abstract class AbstractHttpAsyncClientAuthentication<T extends CloseableH
final HttpHost target = start(); final HttpHost target = start();
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, credsProvider.setCredentials(new AuthScope(null, null, -1, null ,null),
new UsernamePasswordCredentials("test", "test".toCharArray())); new UsernamePasswordCredentials("test", "test".toCharArray()));
final HttpClientContext context = HttpClientContext.create(); final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider); context.setCredentialsProvider(credsProvider);

View File

@ -214,7 +214,7 @@ public class HttpAsyncClientCompatibilityTest {
{ {
connManager.closeIdle(TimeValue.NEG_ONE_MILLISECONDS); connManager.closeIdle(TimeValue.NEG_ONE_MILLISECONDS);
credentialsProvider.setCredentials( credentialsProvider.setCredentials(
new AuthScope("otherhost", AuthScope.ANY_PORT, "Restricted Files"), new AuthScope("http", "otherhost", -1, "Restricted Files", null),
new UsernamePasswordCredentials("testuser", "nopassword".toCharArray())); new UsernamePasswordCredentials("testuser", "nopassword".toCharArray()));
final HttpClientContext context = HttpClientContext.create(); final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsProvider); context.setCredentialsProvider(credentialsProvider);

View File

@ -180,7 +180,7 @@ public class HttpClientCompatibilityTest {
{ {
connManager.closeIdle(TimeValue.NEG_ONE_MILLISECONDS); connManager.closeIdle(TimeValue.NEG_ONE_MILLISECONDS);
credentialsProvider.setCredentials( credentialsProvider.setCredentials(
new AuthScope("otherhost", AuthScope.ANY_PORT, "Restricted Files"), new AuthScope("http", "otherhost", -1, "Restricted Files", null),
new UsernamePasswordCredentials("testuser", "nopassword".toCharArray())); new UsernamePasswordCredentials("testuser", "nopassword".toCharArray()));
final HttpClientContext context = HttpClientContext.create(); final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsProvider); context.setCredentialsProvider(credentialsProvider);

View File

@ -321,7 +321,7 @@ public class TestClientAuthentication extends LocalServerTestBase {
final HttpClientContext context = HttpClientContext.create(); final HttpClientContext context = HttpClientContext.create();
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, credsProvider.setCredentials(new AuthScope(null, null, -1, null ,null),
new UsernamePasswordCredentials("test", "test".toCharArray())); new UsernamePasswordCredentials("test", "test".toCharArray()));
context.setCredentialsProvider(credsProvider); context.setCredentialsProvider(credsProvider);
@ -536,7 +536,7 @@ public class TestClientAuthentication extends LocalServerTestBase {
authCache.put(target, new BasicScheme()); authCache.put(target, new BasicScheme());
context.setAuthCache(authCache); context.setAuthCache(authCache);
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, credsProvider.setCredentials(new AuthScope(null, null, -1, null ,null),
new UsernamePasswordCredentials("test", "stuff".toCharArray())); new UsernamePasswordCredentials("test", "stuff".toCharArray()));
context.setCredentialsProvider(credsProvider); context.setCredentialsProvider(credsProvider);
@ -611,7 +611,7 @@ public class TestClientAuthentication extends LocalServerTestBase {
final HttpClientContext context = HttpClientContext.create(); final HttpClientContext context = HttpClientContext.create();
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, credsProvider.setCredentials(new AuthScope(null, null, -1, null ,null),
new UsernamePasswordCredentials("test", "test".toCharArray())); new UsernamePasswordCredentials("test", "test".toCharArray()));
context.setCredentialsProvider(credsProvider); context.setCredentialsProvider(credsProvider);

View File

@ -71,7 +71,7 @@ public class TestClientAuthenticationFakeNTLM extends LocalServerTestBase {
final HttpHost target = start(); final HttpHost target = start();
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, credsProvider.setCredentials(new AuthScope(null, null, -1, null ,null),
new NTCredentials("test", "test".toCharArray(), null, null)); new NTCredentials("test", "test".toCharArray(), null, null));
this.httpclient = HttpClients.custom() this.httpclient = HttpClients.custom()
@ -118,7 +118,7 @@ public class TestClientAuthenticationFakeNTLM extends LocalServerTestBase {
final HttpHost target = start(); final HttpHost target = start();
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, credsProvider.setCredentials(new AuthScope(null, null, -1, null ,null),
new NTCredentials("test", "test".toCharArray(), null, null)); new NTCredentials("test", "test".toCharArray(), null, null));
this.httpclient = HttpClients.custom() this.httpclient = HttpClients.custom()
@ -142,7 +142,7 @@ public class TestClientAuthenticationFakeNTLM extends LocalServerTestBase {
final HttpHost target = start(); final HttpHost target = start();
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, credsProvider.setCredentials(new AuthScope(null, null, -1, null ,null),
new NTCredentials("test", "test".toCharArray(), null, null)); new NTCredentials("test", "test".toCharArray(), null, null));
this.httpclient = HttpClients.custom() this.httpclient = HttpClients.custom()
@ -187,7 +187,7 @@ public class TestClientAuthenticationFakeNTLM extends LocalServerTestBase {
final HttpClientContext context = HttpClientContext.create(); final HttpClientContext context = HttpClientContext.create();
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, credsProvider.setCredentials(new AuthScope(null, null, -1, null ,null),
new NTCredentials("test", "test".toCharArray(), null, null)); new NTCredentials("test", "test".toCharArray(), null, null));
context.setCredentialsProvider(credsProvider); context.setCredentialsProvider(credsProvider);
final HttpGet httpget = new HttpGet("/"); final HttpGet httpget = new HttpGet("/");
@ -208,7 +208,7 @@ public class TestClientAuthenticationFakeNTLM extends LocalServerTestBase {
final HttpClientContext context = HttpClientContext.create(); final HttpClientContext context = HttpClientContext.create();
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, credsProvider.setCredentials(new AuthScope(null, null, -1, null ,null),
new NTCredentials("test", "test".toCharArray(), null, null)); new NTCredentials("test", "test".toCharArray(), null, null));
context.setCredentialsProvider(credsProvider); context.setCredentialsProvider(credsProvider);
final HttpGet httpget = new HttpGet("/"); final HttpGet httpget = new HttpGet("/");

View File

@ -157,7 +157,7 @@ public class TestSPNegoScheme extends LocalServerTestBase {
final AuthSchemeProvider nsf = new NegotiateSchemeProviderWithMockGssManager(); final AuthSchemeProvider nsf = new NegotiateSchemeProviderWithMockGssManager();
final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
final Credentials use_jaas_creds = new UseJaasCredentials(); final Credentials use_jaas_creds = new UseJaasCredentials();
credentialsProvider.setCredentials(new AuthScope(null, -1, null), use_jaas_creds); credentialsProvider.setCredentials(new AuthScope(null, null, -1, null, null), use_jaas_creds);
final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create() final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
.register(AuthSchemes.SPNEGO, nsf) .register(AuthSchemes.SPNEGO, nsf)
@ -188,7 +188,7 @@ public class TestSPNegoScheme extends LocalServerTestBase {
final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
final Credentials use_jaas_creds = new UseJaasCredentials(); final Credentials use_jaas_creds = new UseJaasCredentials();
credentialsProvider.setCredentials(new AuthScope(null, -1, null), use_jaas_creds); credentialsProvider.setCredentials(new AuthScope(null, null, -1, null, null), use_jaas_creds);
final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create() final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
.register(AuthSchemes.SPNEGO, nsf) .register(AuthSchemes.SPNEGO, nsf)

View File

@ -35,93 +35,57 @@ import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.LangUtils; import org.apache.hc.core5.util.LangUtils;
/** /**
* {@code AuthScope} represents an authentication scope consisting of a host name, * {@code AuthScope} represents an authentication scope consisting of
* a port number, a realm name and an authentication scheme name. * an application protocol, a host name, a port number, a realm name
* <p> * and an authentication scheme name.
* This class can also optionally contain a host of origin, if created in response
* to authentication challenge from a specific host.
* </p> * </p>
* @since 4.0 * @since 4.0
*/ */
@Contract(threading = ThreadingBehavior.IMMUTABLE) @Contract(threading = ThreadingBehavior.IMMUTABLE)
public class AuthScope { public class AuthScope {
/** private final String protocol;
* The {@code null} value represents any host. In the future versions of
* HttpClient the use of this parameter will be discontinued.
*/
public static final String ANY_HOST = null;
/**
* The {@code -1} value represents any port.
*/
public static final int ANY_PORT = -1;
/**
* The {@code null} value represents any realm.
*/
public static final String ANY_REALM = null;
/**
* The {@code null} value represents any authentication scheme.
*/
public static final String ANY_SCHEME = null;
/**
* Default scope matching any host, port, realm and authentication scheme.
* In the future versions of HttpClient the use of this parameter will be
* discontinued.
*/
public static final AuthScope ANY = new AuthScope(ANY_HOST, ANY_PORT, ANY_REALM, ANY_SCHEME);
/** The authentication scheme the credentials apply to. */
private final String scheme;
/** The realm the credentials apply to. */
private final String realm;
/** The host the credentials apply to. */
private final String host; private final String host;
/** The port the credentials apply to. */
private final int port; private final int port;
private final String realm;
/** The original host, if known */ private final String authScheme;
private final HttpHost origin;
/** /**
* Defines auth scope with the given {@code host}, {@code port}, {@code realm}, and * Defines auth scope with the given {@code protocol}, {@code host}, {@code port},
* {@code schemeName}. * {@code realm}, and {@code schemeName}.
* *
* @param host authentication host. May be {@link #ANY_HOST} if applies * @param protocol application protocol. May be {@code null} if applies
* to any protocol.
* @param host authentication host. May be {@code null} if applies
* to any host. * to any host.
* @param port authentication port. May be {@link #ANY_PORT} if applies * @param port authentication port. May be {@code -1} if applies
* to any port of the host. * to any port of the host.
* @param realm authentication realm. May be {@link #ANY_REALM} 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 scheme. May be {@link #ANY_SCHEME} if applies * @param authScheme authentication scheme. May be {@code null} if applies
* to any scheme supported by the host. * to any authScheme supported by the host.
*/ */
public AuthScope( public AuthScope(
final String protocol,
final String host, final String host,
final int port, final int port,
final String realm, final String realm,
final String schemeName) { final String authScheme) {
this.host = host == null ? ANY_HOST: host.toLowerCase(Locale.ROOT); this.protocol = protocol != null ? protocol.toLowerCase(Locale.ROOT) : null;
this.port = port < 0 ? ANY_PORT : port; this.host = host != null ? host.toLowerCase(Locale.ROOT) : null;
this.realm = realm == null ? ANY_REALM : realm; this.port = port >= 0 ? port: -1;
this.scheme = schemeName == null ? ANY_SCHEME : schemeName.toUpperCase(Locale.ROOT); this.realm = realm;
this.origin = null; this.authScheme = authScheme != null ? authScheme.toUpperCase(Locale.ROOT): null;
} }
/** /**
* Defines auth scope for a specific host of origin. * Defines auth scope for a specific host of origin.
* *
* @param origin host of origin * @param origin host of origin
* @param realm authentication realm. May be {@link #ANY_REALM} 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 scheme. May be {@link #ANY_SCHEME} if applies * @param schemeName authentication authScheme. May be {@code null} if applies
* to any scheme supported by the host. * to any authScheme supported by the host.
* *
* @since 4.2 * @since 4.2
*/ */
@ -130,11 +94,11 @@ public class AuthScope {
final String realm, final String realm,
final String schemeName) { final String schemeName) {
Args.notNull(origin, "Host"); Args.notNull(origin, "Host");
this.protocol = origin.getSchemeName().toLowerCase(Locale.ROOT);
this.host = origin.getHostName().toLowerCase(Locale.ROOT); this.host = origin.getHostName().toLowerCase(Locale.ROOT);
this.port = origin.getPort() < 0 ? ANY_PORT : origin.getPort(); this.port = origin.getPort() >= 0 ? origin.getPort() : -1;
this.realm = realm == null ? ANY_REALM : realm; this.realm = realm;
this.scheme = schemeName == null ? ANY_SCHEME : schemeName.toUpperCase(Locale.ROOT); this.authScheme = schemeName != null ? schemeName.toUpperCase(Locale.ROOT): null;
this.origin = origin;
} }
/** /**
@ -145,33 +109,19 @@ public class AuthScope {
* @since 4.2 * @since 4.2
*/ */
public AuthScope(final HttpHost origin) { public AuthScope(final HttpHost origin) {
this(origin, ANY_REALM, ANY_SCHEME); this(origin, null, null);
}
/**
* Defines auth scope with the given {@code host}, {@code port} and {@code realm}.
*
* @param host authentication host. May be {@link #ANY_HOST} if applies
* to any host.
* @param port authentication port. May be {@link #ANY_PORT} if applies
* to any port of the host.
* @param realm authentication realm. May be {@link #ANY_REALM} if applies
* to any realm on the host.
*/
public AuthScope(final String host, final int port, final String realm) {
this(host, port, realm, ANY_SCHEME);
} }
/** /**
* Defines auth scope with the given {@code host} and {@code port}. * Defines auth scope with the given {@code host} and {@code port}.
* *
* @param host authentication host. May be {@link #ANY_HOST} if applies * @param host authentication host. May be {@code null} if applies
* to any host. * to any host.
* @param port authentication port. May be {@link #ANY_PORT} if applies * @param port authentication port. May be {@code -1} if applies
* to any port of the host. * to any port of the host.
*/ */
public AuthScope(final String host, final int port) { public AuthScope(final String host, final int port) {
this(host, port, ANY_REALM, ANY_SCHEME); this(null, host, port, null, null);
} }
/** /**
@ -180,48 +130,31 @@ public class AuthScope {
public AuthScope(final AuthScope authscope) { public AuthScope(final AuthScope authscope) {
super(); super();
Args.notNull(authscope, "Scope"); Args.notNull(authscope, "Scope");
this.protocol = authscope.getProtocol();
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.scheme = authscope.getScheme(); this.authScheme = authscope.getAuthScheme();
this.origin = authscope.getOrigin();
} }
/** public String getProtocol() {
* @return host of origin. If unknown returns @null, return protocol;
*
* @since 4.4
*/
public HttpHost getOrigin() {
return this.origin;
} }
/**
* @return the host
*/
public String getHost() { public String getHost() {
return this.host; return this.host;
} }
/**
* @return the port
*/
public int getPort() { public int getPort() {
return this.port; return this.port;
} }
/**
* @return the realm name
*/
public String getRealm() { public String getRealm() {
return this.realm; return this.realm;
} }
/** public String getAuthScheme() {
* @return the scheme type return this.authScheme;
*/
public String getScheme() {
return this.scheme;
} }
/** /**
@ -233,69 +166,81 @@ 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.scheme, that.scheme)) { if (LangUtils.equals(this.authScheme, that.authScheme)) {
factor += 1; factor += 1;
} else { } else {
if (this.scheme != ANY_SCHEME && that.scheme != ANY_SCHEME) { if (this.authScheme != null && that.authScheme != null) {
return -1; return -1;
} }
} }
if (LangUtils.equals(this.realm, that.realm)) { if (LangUtils.equals(this.realm, that.realm)) {
factor += 2; factor += 2;
} else { } else {
if (this.realm != ANY_REALM && that.realm != ANY_REALM) { if (this.realm != null && that.realm != null) {
return -1; return -1;
} }
} }
if (this.port == that.port) { if (this.port == that.port) {
factor += 4; factor += 4;
} else { } else {
if (this.port != ANY_PORT && that.port != ANY_PORT) { if (this.port != -1 && that.port != -1) {
return -1;
}
}
if (LangUtils.equals(this.protocol, that.protocol)) {
factor += 8;
} else {
if (this.protocol != null && that.protocol != null) {
return -1; return -1;
} }
} }
if (LangUtils.equals(this.host, that.host)) { if (LangUtils.equals(this.host, that.host)) {
factor += 8; factor += 16;
} else { } else {
if (this.host != ANY_HOST && that.host != ANY_HOST) { if (this.host != null && that.host != null) {
return -1; return -1;
} }
} }
return factor; return factor;
} }
/**
* @see java.lang.Object#equals(Object)
*/
@Override @Override
public boolean equals(final Object o) { public boolean equals(final Object obj) {
if (o == null) { if (this == obj) {
return false;
}
if (o == this) {
return true; return true;
} }
if (!(o instanceof AuthScope)) { if (obj instanceof AuthScope) {
return super.equals(o); final AuthScope that = (AuthScope) obj;
} return LangUtils.equals(this.protocol, that.protocol)
final AuthScope that = (AuthScope) o; && LangUtils.equals(this.host, that.host)
return
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.scheme, that.scheme); && LangUtils.equals(this.authScheme, that.authScheme);
} else {
return false;
}
}
@Override
public int hashCode() {
int hash = LangUtils.HASH_SEED;
hash = LangUtils.hashCode(hash, this.protocol);
hash = LangUtils.hashCode(hash, this.host);
hash = LangUtils.hashCode(hash, this.port);
hash = LangUtils.hashCode(hash, this.realm);
hash = LangUtils.hashCode(hash, this.authScheme);
return hash;
} }
/**
* @see java.lang.Object#toString()
*/
@Override @Override
public String toString() { public String toString() {
final StringBuilder buffer = new StringBuilder(); final StringBuilder buffer = new StringBuilder();
if (this.scheme != null) { if (this.authScheme != null) {
buffer.append(this.scheme.toUpperCase(Locale.ROOT)); buffer.append(this.authScheme);
buffer.append(' '); } else {
buffer.append("<any auth scheme>");
} }
buffer.append(' ');
if (this.realm != null) { if (this.realm != null) {
buffer.append('\''); buffer.append('\'');
buffer.append(this.realm); buffer.append(this.realm);
@ -303,27 +248,25 @@ public class AuthScope {
} else { } else {
buffer.append("<any realm>"); buffer.append("<any realm>");
} }
if (this.host != null) { buffer.append(' ');
buffer.append('@'); if (this.protocol != null) {
buffer.append(this.host); buffer.append(this.protocol);
if (this.port >= 0) { } else {
buffer.append(':'); buffer.append("<any protocol>");
buffer.append(this.port);
} }
buffer.append("://");
if (this.host != null) {
buffer.append(this.host);
} else {
buffer.append("<any host>");
}
buffer.append(':');
if (this.port >= 0) {
buffer.append(this.port);
} else {
buffer.append("<any port>");
} }
return buffer.toString(); return buffer.toString();
} }
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
int hash = LangUtils.HASH_SEED;
hash = LangUtils.hashCode(hash, this.host);
hash = LangUtils.hashCode(hash, this.port);
hash = LangUtils.hashCode(hash, this.realm);
hash = LangUtils.hashCode(hash, this.scheme);
return hash;
}
} }

View File

@ -43,6 +43,7 @@ import org.apache.hc.core5.util.Args;
public class AuthSupport { public class AuthSupport {
public static void extractFromAuthority( public static void extractFromAuthority(
final String scheme,
final URIAuthority authority, final URIAuthority authority,
final CredentialsStore credentialsStore) { final CredentialsStore credentialsStore) {
Args.notNull(credentialsStore, "Credentials store"); Args.notNull(credentialsStore, "Credentials store");
@ -64,7 +65,7 @@ public class AuthSupport {
password = null; password = null;
} }
credentialsStore.setCredentials( credentialsStore.setCredentials(
new AuthScope(authority.getHostName(), authority.getPort(), null, AuthSchemes.BASIC), new AuthScope(scheme, authority.getHostName(), authority.getPort(), null, AuthSchemes.BASIC),
new UsernamePasswordCredentials(userName, password)); new UsernamePasswordCredentials(userName, password));
} }

View File

@ -125,7 +125,7 @@ class AsyncProtocolExec implements AsyncExecChainHandler {
if (authority != null) { if (authority != null) {
final CredentialsProvider credsProvider = clientContext.getCredentialsProvider(); final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
if (credsProvider instanceof CredentialsStore) { if (credsProvider instanceof CredentialsStore) {
AuthSupport.extractFromAuthority(authority, (CredentialsStore) credsProvider); AuthSupport.extractFromAuthority(request.getScheme(), authority, (CredentialsStore) credsProvider);
} }
} }

View File

@ -45,7 +45,6 @@ import org.apache.hc.client5.http.config.AuthSchemes;
import org.apache.hc.client5.http.protocol.HttpClientContext; import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.annotation.Contract; import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior; import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest; import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.protocol.HttpContext; import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.hc.core5.util.Args; import org.apache.hc.core5.util.Args;
@ -113,7 +112,7 @@ public class SystemDefaultCredentialsProvider implements CredentialsStore {
authscope.getPort(), authscope.getPort(),
protocol, protocol,
authscope.getRealm(), authscope.getRealm(),
translateAuthScheme(authscope.getScheme()), translateAuthScheme(authscope.getAuthScheme()),
targetHostURL, targetHostURL,
requestorType); requestorType);
} }
@ -128,9 +127,7 @@ public class SystemDefaultCredentialsProvider implements CredentialsStore {
final String host = authscope.getHost(); final String host = authscope.getHost();
if (host != null) { if (host != null) {
final HttpClientContext clientContext = context != null ? HttpClientContext.adapt(context) : null; final HttpClientContext clientContext = context != null ? HttpClientContext.adapt(context) : null;
final int port = authscope.getPort(); final String protocol = authscope.getProtocol() != null ? authscope.getProtocol() : (authscope.getPort() == 443 ? "https" : "http");
final HttpHost origin = authscope.getOrigin();
final String protocol = origin != null ? origin.getSchemeName() : (port == 443 ? "https" : "http");
PasswordAuthentication systemcreds = getSystemCreds( PasswordAuthentication systemcreds = getSystemCreds(
protocol, authscope, Authenticator.RequestorType.SERVER, clientContext); protocol, authscope, Authenticator.RequestorType.SERVER, clientContext);
if (systemcreds == null) { if (systemcreds == null) {
@ -161,7 +158,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 (AuthSchemes.NTLM.equalsIgnoreCase(authscope.getScheme())) { if (AuthSchemes.NTLM.equalsIgnoreCase(authscope.getAuthScheme())) {
// 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

@ -130,7 +130,7 @@ final class ProtocolExec implements ExecChainHandler {
if (authority != null) { if (authority != null) {
final CredentialsProvider credsProvider = context.getCredentialsProvider(); final CredentialsProvider credsProvider = context.getCredentialsProvider();
if (credsProvider instanceof CredentialsStore) { if (credsProvider instanceof CredentialsStore) {
AuthSupport.extractFromAuthority(authority, (CredentialsStore) credsProvider); AuthSupport.extractFromAuthority(request.getScheme(), authority, (CredentialsStore) credsProvider);
} }
} }

View File

@ -37,149 +37,119 @@ public class TestAuthScope {
@Test @Test
public void testBasics() { public void testBasics() {
final AuthScope authscope = new AuthScope("somehost", 80, "somerealm", "somescheme"); final AuthScope authscope = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
Assert.assertEquals("SOMESCHEME", authscope.getScheme()); Assert.assertEquals("SOMESCHEME", authscope.getAuthScheme());
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'@somehost:80", authscope.toString()); Assert.assertEquals("SOMESCHEME 'somerealm' http://somehost:80", authscope.toString());
}
@Test
public void testBasicsOptionalRealm() {
final AuthScope authscope = new AuthScope("somehost", 80, AuthScope.ANY_REALM, "somescheme");
Assert.assertEquals("SOMESCHEME", authscope.getScheme());
Assert.assertEquals("somehost", authscope.getHost());
Assert.assertEquals(80, authscope.getPort());
Assert.assertEquals(null, authscope.getRealm());
Assert.assertEquals("SOMESCHEME <any realm>@somehost:80", authscope.toString());
}
@Test
public void testBasicsOptionalScheme() {
final AuthScope authscope = new AuthScope("somehost", 80, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME);
Assert.assertEquals(null, authscope.getScheme());
Assert.assertEquals("somehost", authscope.getHost());
Assert.assertEquals(80, authscope.getPort());
Assert.assertEquals(null, authscope.getRealm());
Assert.assertEquals("<any realm>@somehost:80", authscope.toString());
}
@Test
public void testBasicsOptionalPort() {
final AuthScope authscope = new AuthScope("somehost", AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME);
Assert.assertEquals(null, authscope.getScheme());
Assert.assertEquals("somehost", authscope.getHost());
Assert.assertEquals(-1, authscope.getPort());
Assert.assertEquals(null, authscope.getRealm());
Assert.assertEquals("<any realm>@somehost", authscope.toString());
} }
@Test @Test
public void testByOrigin() { public void testByOrigin() {
final HttpHost host = new HttpHost("somehost", 8080, "http"); final HttpHost host = new HttpHost("somehost", 8080, "http");
final AuthScope authscope = new AuthScope(host); final AuthScope authscope = new AuthScope(host);
Assert.assertEquals(null, authscope.getScheme()); Assert.assertEquals(null, authscope.getAuthScheme());
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());
Assert.assertEquals(host, authscope.getOrigin()); Assert.assertEquals("http", authscope.getProtocol());
Assert.assertEquals("<any realm>@somehost:8080", authscope.toString()); Assert.assertEquals("<any auth scheme> <any realm> http://somehost:8080", authscope.toString());
} }
@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.getScheme()); Assert.assertEquals(null, authscope.getAuthScheme());
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());
Assert.assertEquals("<any realm>@somehost:80", authscope.toString()); Assert.assertEquals("<any auth scheme> <any realm> <any protocol>://somehost:80", authscope.toString());
} }
@Test
public void testByOriginMixedCaseHostname() throws Exception { public void testByOriginMixedCaseHostname() throws Exception {
final HttpHost host = new HttpHost("SomeHost", 8080, "http"); final HttpHost host = new HttpHost("SomeHost", 8080, "http");
final AuthScope authscope = new AuthScope(host); final AuthScope authscope = new AuthScope(host);
Assert.assertEquals("somehost", authscope.getHost()); Assert.assertEquals("somehost", authscope.getHost());
Assert.assertEquals(host, authscope.getOrigin());
} }
@Test @Test
public void testBasicsOptionalHost() { public void testBasicsAllOptional() {
final AuthScope authscope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME); final AuthScope authscope = new AuthScope(null, null, -1, null, null);
Assert.assertEquals(null, authscope.getScheme()); Assert.assertEquals(null, authscope.getAuthScheme());
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());
Assert.assertEquals("<any realm>", authscope.toString()); Assert.assertEquals("<any auth scheme> <any realm> <any protocol>://<any host>:<any port>", authscope.toString());
} }
@Test @Test
public void testScopeMatching() { public void testScopeMatching() {
final AuthScope authscope1 = new AuthScope("somehost", 80, "somerealm", "somescheme"); final AuthScope authscope1 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
final AuthScope authscope2 = new AuthScope("someotherhost", 80, "somerealm", "somescheme"); final AuthScope authscope2 = new AuthScope("http", "someotherhost", 80, "somerealm", "somescheme");
Assert.assertTrue(authscope1.match(authscope2) < 0); Assert.assertTrue(authscope1.match(authscope2) < 0);
int m1 = authscope1.match( int m1 = authscope1.match(new AuthScope(null, null, -1, null, "somescheme"));
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme")); int m2 = authscope1.match(new AuthScope(null, null, -1, "somerealm", null));
int m2 = authscope1.match(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm", AuthScope.ANY_SCHEME));
Assert.assertTrue(m2 > m1); Assert.assertTrue(m2 > m1);
m1 = authscope1.match( m1 = authscope1.match(new AuthScope(null, null, -1, null, "somescheme"));
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme")); m2 = authscope1.match(new AuthScope(null, null, -1, "somerealm", null));
m2 = authscope1.match(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm", AuthScope.ANY_SCHEME));
Assert.assertTrue(m2 > m1); Assert.assertTrue(m2 > m1);
m1 = authscope1.match( m1 = authscope1.match(new AuthScope(null, null, -1, "somerealm", "somescheme"));
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm", "somescheme")); m2 = authscope1.match(new AuthScope(null, null, 80, null, null));
m2 = authscope1.match(
new AuthScope(AuthScope.ANY_HOST, 80, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME));
Assert.assertTrue(m2 > m1); Assert.assertTrue(m2 > m1);
m1 = authscope1.match( m1 = authscope1.match(new AuthScope(null, null, 80, "somerealm", "somescheme"));
new AuthScope(AuthScope.ANY_HOST, 80, "somerealm", "somescheme")); m2 = authscope1.match(new AuthScope(null, "somehost", -1, null, null));
m2 = authscope1.match(
new AuthScope("somehost", AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME));
Assert.assertTrue(m2 > m1); Assert.assertTrue(m2 > m1);
m1 = authscope1.match(AuthScope.ANY); m1 = authscope1.match(new AuthScope(null, null, 80, "somerealm", "somescheme"));
m2 = authscope1.match( m2 = authscope1.match(new AuthScope(null, "somehost", -1, null, null));
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme")); Assert.assertTrue(m2 > m1);
m1 = authscope1.match(new AuthScope(null, null, -1, null, null));
m2 = authscope1.match(new AuthScope(null, null, -1, null, "somescheme"));
Assert.assertTrue(m2 > m1); Assert.assertTrue(m2 > m1);
} }
@Test @Test
public void testEquals() { public void testEquals() {
final AuthScope authscope1 = new AuthScope("somehost", 80, "somerealm", "somescheme"); final AuthScope authscope1 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
final AuthScope authscope2 = new AuthScope("someotherhost", 80, "somerealm", "somescheme"); final AuthScope authscope2 = new AuthScope("http", "someotherhost", 80, "somerealm", "somescheme");
final AuthScope authscope3 = new AuthScope("somehost", 80, "somerealm", "somescheme"); final AuthScope authscope3 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
final AuthScope authscope4 = new AuthScope("somehost", 8080, "somerealm", "somescheme"); final AuthScope authscope4 = new AuthScope("http", "somehost", 8080, "somerealm", "somescheme");
final AuthScope authscope5 = new AuthScope("somehost", 80, "someotherrealm", "somescheme"); final AuthScope authscope5 = new AuthScope("http", "somehost", 80, "someotherrealm", "somescheme");
final AuthScope authscope6 = new AuthScope("somehost", 80, "somerealm", "someotherscheme"); final AuthScope authscope6 = new AuthScope("http", "somehost", 80, "somerealm", "someotherscheme");
final AuthScope authscope7 = 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));
Assert.assertFalse(authscope1.equals(authscope4)); Assert.assertFalse(authscope1.equals(authscope4));
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));
} }
@Test @Test
public void testHash() { public void testHash() {
final AuthScope authscope1 = new AuthScope("somehost", 80, "somerealm", "somescheme"); final AuthScope authscope1 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
final AuthScope authscope2 = new AuthScope("someotherhost", 80, "somerealm", "somescheme"); final AuthScope authscope2 = new AuthScope("http", "someotherhost", 80, "somerealm", "somescheme");
final AuthScope authscope3 = new AuthScope("somehost", 80, "somerealm", "somescheme"); final AuthScope authscope3 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
final AuthScope authscope4 = new AuthScope("somehost", 8080, "somerealm", "somescheme"); final AuthScope authscope4 = new AuthScope("http", "somehost", 8080, "somerealm", "somescheme");
final AuthScope authscope5 = new AuthScope("somehost", 80, "someotherrealm", "somescheme"); final AuthScope authscope5 = new AuthScope("http", "somehost", 80, "someotherrealm", "somescheme");
final AuthScope authscope6 = new AuthScope("somehost", 80, "somerealm", "someotherscheme"); final AuthScope authscope6 = new AuthScope("http", "somehost", 80, "somerealm", "someotherscheme");
final AuthScope authscope7 = 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());
Assert.assertFalse(authscope1.hashCode() == authscope4.hashCode()); Assert.assertFalse(authscope1.hashCode() == authscope4.hashCode());
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());
} }
} }

View File

@ -43,14 +43,10 @@ public class TestBasicCredentialsProvider {
public final static Credentials CREDS2 = public final static Credentials CREDS2 =
new UsernamePasswordCredentials("user2", "pass2".toCharArray()); new UsernamePasswordCredentials("user2", "pass2".toCharArray());
public final static AuthScope SCOPE1 = public final static AuthScope SCOPE1 = new AuthScope(null, null, -1, "realm1", null);
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm1"); public final static AuthScope SCOPE2 = new AuthScope(null, null, -1, "realm2", null);
public final static AuthScope SCOPE2 = public final static AuthScope BOGUS = new AuthScope(null, null, -1, "bogus", null);
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm2"); public final static AuthScope DEFSCOPE = new AuthScope(null, "host", -1, "realm", null);
public final static AuthScope BOGUS =
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "bogus");
public final static AuthScope DEFSCOPE =
new AuthScope("host", AuthScope.ANY_PORT, "realm");
@Test @Test
public void testBasicCredentialsProviderCredentials() { public void testBasicCredentialsProviderCredentials() {
@ -70,7 +66,7 @@ public class TestBasicCredentialsProvider {
@Test @Test
public void testBasicCredentialsProviderDefaultCredentials() { public void testBasicCredentialsProviderDefaultCredentials() {
final BasicCredentialsProvider state = new BasicCredentialsProvider(); final BasicCredentialsProvider state = new BasicCredentialsProvider();
state.setCredentials(AuthScope.ANY, CREDS1); state.setCredentials(new AuthScope(null, null, -1, null ,null), CREDS1);
state.setCredentials(SCOPE2, CREDS2); state.setCredentials(SCOPE2, CREDS2);
Assert.assertEquals(CREDS1, state.getCredentials(BOGUS, null)); Assert.assertEquals(CREDS1, state.getCredentials(BOGUS, null));
} }
@ -79,7 +75,7 @@ public class TestBasicCredentialsProvider {
public void testDefaultCredentials() throws Exception { public void testDefaultCredentials() throws Exception {
final BasicCredentialsProvider state = new BasicCredentialsProvider(); final BasicCredentialsProvider state = new BasicCredentialsProvider();
final Credentials expected = new UsernamePasswordCredentials("name", "pass".toCharArray()); final Credentials expected = new UsernamePasswordCredentials("name", "pass".toCharArray());
state.setCredentials(AuthScope.ANY, expected); state.setCredentials(new AuthScope(null, null, -1, null ,null), expected);
final Credentials got = state.getCredentials(DEFSCOPE, null); final Credentials got = state.getCredentials(DEFSCOPE, null);
Assert.assertEquals(got, expected); Assert.assertEquals(got, expected);
} }
@ -97,8 +93,7 @@ public class TestBasicCredentialsProvider {
public void testHostCredentials() throws Exception { public void testHostCredentials() throws Exception {
final BasicCredentialsProvider state = new BasicCredentialsProvider(); final BasicCredentialsProvider state = new BasicCredentialsProvider();
final Credentials expected = new UsernamePasswordCredentials("name", "pass".toCharArray()); final Credentials expected = new UsernamePasswordCredentials("name", "pass".toCharArray());
state.setCredentials( state.setCredentials(new AuthScope(null, "host", -1, null, null), expected);
new AuthScope("host", AuthScope.ANY_PORT, AuthScope.ANY_REALM), expected);
final Credentials got = state.getCredentials(DEFSCOPE, null); final Credentials got = state.getCredentials(DEFSCOPE, null);
Assert.assertEquals(expected, got); Assert.assertEquals(expected, got);
} }
@ -107,10 +102,8 @@ public class TestBasicCredentialsProvider {
public void testWrongHostCredentials() throws Exception { public void testWrongHostCredentials() throws Exception {
final BasicCredentialsProvider state = new BasicCredentialsProvider(); final BasicCredentialsProvider state = new BasicCredentialsProvider();
final Credentials expected = new UsernamePasswordCredentials("name", "pass".toCharArray()); final Credentials expected = new UsernamePasswordCredentials("name", "pass".toCharArray());
state.setCredentials( state.setCredentials(new AuthScope(null, "host1", -1, "realm", null), expected);
new AuthScope("host1", AuthScope.ANY_PORT, "realm"), expected); final Credentials got = state.getCredentials(new AuthScope(null, "host2", -1, "realm", null), null);
final Credentials got = state.getCredentials(
new AuthScope("host2", AuthScope.ANY_PORT, "realm"), null);
Assert.assertNotSame(expected, got); Assert.assertNotSame(expected, got);
} }
@ -118,10 +111,8 @@ public class TestBasicCredentialsProvider {
public void testWrongRealmCredentials() throws Exception { public void testWrongRealmCredentials() throws Exception {
final BasicCredentialsProvider state = new BasicCredentialsProvider(); final BasicCredentialsProvider state = new BasicCredentialsProvider();
final Credentials cred = new UsernamePasswordCredentials("name", "pass".toCharArray()); final Credentials cred = new UsernamePasswordCredentials("name", "pass".toCharArray());
state.setCredentials( state.setCredentials(new AuthScope(null, "host", -1, "realm1", null), cred);
new AuthScope("host", AuthScope.ANY_PORT, "realm1"), cred); final Credentials got = state.getCredentials(new AuthScope(null, "host", -1, "realm2", null), null);
final Credentials got = state.getCredentials(
new AuthScope("host", AuthScope.ANY_PORT, "realm2"), null);
Assert.assertNotSame(cred, got); Assert.assertNotSame(cred, got);
} }
@ -141,27 +132,24 @@ public class TestBasicCredentialsProvider {
final Credentials creds2 = new UsernamePasswordCredentials("name2", "pass2".toCharArray()); final Credentials creds2 = new UsernamePasswordCredentials("name2", "pass2".toCharArray());
final Credentials creds3 = new UsernamePasswordCredentials("name3", "pass3".toCharArray()); final Credentials creds3 = new UsernamePasswordCredentials("name3", "pass3".toCharArray());
final AuthScope scope1 = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM); final AuthScope scope1 = new AuthScope(null, null, -1, null, null);
final AuthScope scope2 = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm"); final AuthScope scope2 = new AuthScope(null, null, -1, "somerealm", null);
final AuthScope scope3 = new AuthScope("somehost", AuthScope.ANY_PORT, AuthScope.ANY_REALM); final AuthScope scope3 = new AuthScope(null, "somehost", -1, null, null);
final BasicCredentialsProvider state = new BasicCredentialsProvider(); final BasicCredentialsProvider state = new BasicCredentialsProvider();
state.setCredentials(scope1, creds1); state.setCredentials(scope1, creds1);
state.setCredentials(scope2, creds2); state.setCredentials(scope2, creds2);
state.setCredentials(scope3, creds3); state.setCredentials(scope3, creds3);
Credentials got = state.getCredentials( Credentials got = state.getCredentials(new AuthScope("http", "someotherhost", 80, "someotherrealm", "basic"), null);
new AuthScope("someotherhost", 80, "someotherrealm", "basic"), null);
Credentials expected = creds1; Credentials expected = creds1;
Assert.assertEquals(expected, got); Assert.assertEquals(expected, got);
got = state.getCredentials( got = state.getCredentials(new AuthScope("http", "someotherhost", 80, "somerealm", "basic"), null);
new AuthScope("someotherhost", 80, "somerealm", "basic"), null);
expected = creds2; expected = creds2;
Assert.assertEquals(expected, got); Assert.assertEquals(expected, got);
got = state.getCredentials( got = state.getCredentials(new AuthScope("http", "somehost", 80, "someotherrealm", "basic"), null);
new AuthScope("somehost", 80, "someotherrealm", "basic"), null);
expected = creds3; expected = creds3;
Assert.assertEquals(expected, got); Assert.assertEquals(expected, got);
} }

View File

@ -90,7 +90,7 @@ public class TestSystemDefaultCredentialsProvider {
final AuthenticatorDelegate authenticatorDelegate = installAuthenticator(AUTH1); final AuthenticatorDelegate authenticatorDelegate = installAuthenticator(AUTH1);
final URL httpRequestUrl = new URL(TARGET_SCHEME1, TARGET_HOST1, TARGET_PORT1, "/"); final URL httpRequestUrl = new URL(TARGET_SCHEME1, TARGET_HOST1, TARGET_PORT1, "/");
final AuthScope authScope = new AuthScope(PROXY_HOST1, PROXY_PORT1, PROMPT1, "BASIC"); final AuthScope authScope = new AuthScope(PROXY_PROTOCOL1, PROXY_HOST1, PROXY_PORT1, PROMPT1, "BASIC");
final HttpCoreContext coreContext = new HttpCoreContext(); final HttpCoreContext coreContext = new HttpCoreContext();
coreContext.setAttribute(HttpCoreContext.HTTP_REQUEST, new HttpGet(httpRequestUrl.toURI())); coreContext.setAttribute(HttpCoreContext.HTTP_REQUEST, new HttpGet(httpRequestUrl.toURI()));
@ -110,7 +110,7 @@ public class TestSystemDefaultCredentialsProvider {
final AuthenticatorDelegate authenticatorDelegate = installAuthenticator(AUTH1); final AuthenticatorDelegate authenticatorDelegate = installAuthenticator(AUTH1);
final AuthScope authScope = new AuthScope(PROXY_HOST1, PROXY_PORT1, PROMPT1, "BASIC"); final AuthScope authScope = new AuthScope(PROXY_PROTOCOL1, PROXY_HOST1, PROXY_PORT1, PROMPT1, "BASIC");
final Credentials receivedCredentials = final Credentials receivedCredentials =
new SystemDefaultCredentialsProvider().getCredentials(authScope, null); new SystemDefaultCredentialsProvider().getCredentials(authScope, null);

View File

@ -138,7 +138,7 @@ public class TestProtocolExec {
protocolExec.execute(request, scope, chain); protocolExec.execute(request, scope, chain);
Assert.assertEquals(new URI("http://bar/test"), request.getUri()); Assert.assertEquals(new URI("http://bar/test"), request.getUri());
final CredentialsProvider credentialsProvider = context.getCredentialsProvider(); final CredentialsProvider credentialsProvider = context.getCredentialsProvider();
final Credentials creds = credentialsProvider.getCredentials(new AuthScope("bar", -1, null), null); final Credentials creds = credentialsProvider.getCredentials(new AuthScope(null, "bar", -1, null, null), null);
Assert.assertNotNull(creds); Assert.assertNotNull(creds);
Assert.assertEquals("somefella", creds.getUserPrincipal().getName()); Assert.assertEquals("somefella", creds.getUserPrincipal().getName());
} }