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) {
final AuthScope authScope = host != null ?
new AuthScope(host.getHostName(), host.getPort()) : AuthScope.ANY;
return auth(authScope, creds);
return auth(new AuthScope(host), creds);
}
/**
@ -174,19 +172,6 @@ public class Executor {
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,
final String username, final char[] 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
for (final ProxyConfiguration config : proxyConfigurations) {
if (config.isEnabled() && isSuitable(config, authScope)) {
final String scheme = authScope.getScheme();
final String scheme = authScope.getAuthScheme();
if (BASIC_SCHEME_NAME.equals(scheme)) {
return new UsernamePasswordCredentials(config.getUsername(), config.getPassword().toCharArray());
} else if (NTLM_SCHEME_NAME.equals(scheme)) {

View File

@ -54,7 +54,7 @@ public class OSGiCredentialsProviderTest {
@Test
public void basicAuthentication() {
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));
assertCredentials((UsernamePasswordCredentials) credentials, "user", "secret");
}
@ -62,7 +62,7 @@ public class OSGiCredentialsProviderTest {
@Test
public void ntlmAuthenticationWithoutDomain() {
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));
assertCredentials((NTCredentials) credentials, "user", "secret", null);
}
@ -70,7 +70,7 @@ public class OSGiCredentialsProviderTest {
@Test
public void ntlmAuthenticationWithDomain() {
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));
assertCredentials((NTCredentials) credentials, "user", "secret", "DOMAIN");
}

View File

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

View File

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

View File

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

View File

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

View File

@ -71,7 +71,7 @@ public class TestClientAuthenticationFakeNTLM extends LocalServerTestBase {
final HttpHost target = start();
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));
this.httpclient = HttpClients.custom()
@ -118,7 +118,7 @@ public class TestClientAuthenticationFakeNTLM extends LocalServerTestBase {
final HttpHost target = start();
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));
this.httpclient = HttpClients.custom()
@ -142,7 +142,7 @@ public class TestClientAuthenticationFakeNTLM extends LocalServerTestBase {
final HttpHost target = start();
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));
this.httpclient = HttpClients.custom()
@ -187,7 +187,7 @@ public class TestClientAuthenticationFakeNTLM extends LocalServerTestBase {
final HttpClientContext context = HttpClientContext.create();
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));
context.setCredentialsProvider(credsProvider);
final HttpGet httpget = new HttpGet("/");
@ -208,7 +208,7 @@ public class TestClientAuthenticationFakeNTLM extends LocalServerTestBase {
final HttpClientContext context = HttpClientContext.create();
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));
context.setCredentialsProvider(credsProvider);
final HttpGet httpget = new HttpGet("/");

View File

@ -157,7 +157,7 @@ public class TestSPNegoScheme extends LocalServerTestBase {
final AuthSchemeProvider nsf = new NegotiateSchemeProviderWithMockGssManager();
final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
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()
.register(AuthSchemes.SPNEGO, nsf)
@ -188,7 +188,7 @@ public class TestSPNegoScheme extends LocalServerTestBase {
final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
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()
.register(AuthSchemes.SPNEGO, nsf)

View File

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

View File

@ -125,7 +125,7 @@ class AsyncProtocolExec implements AsyncExecChainHandler {
if (authority != null) {
final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
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.core5.annotation.Contract;
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.protocol.HttpContext;
import org.apache.hc.core5.util.Args;
@ -113,7 +112,7 @@ public class SystemDefaultCredentialsProvider implements CredentialsStore {
authscope.getPort(),
protocol,
authscope.getRealm(),
translateAuthScheme(authscope.getScheme()),
translateAuthScheme(authscope.getAuthScheme()),
targetHostURL,
requestorType);
}
@ -128,9 +127,7 @@ public class SystemDefaultCredentialsProvider implements CredentialsStore {
final String host = authscope.getHost();
if (host != null) {
final HttpClientContext clientContext = context != null ? HttpClientContext.adapt(context) : null;
final int port = authscope.getPort();
final HttpHost origin = authscope.getOrigin();
final String protocol = origin != null ? origin.getSchemeName() : (port == 443 ? "https" : "http");
final String protocol = authscope.getProtocol() != null ? authscope.getProtocol() : (authscope.getPort() == 443 ? "https" : "http");
PasswordAuthentication systemcreds = getSystemCreds(
protocol, authscope, Authenticator.RequestorType.SERVER, clientContext);
if (systemcreds == null) {
@ -161,7 +158,7 @@ public class SystemDefaultCredentialsProvider implements CredentialsStore {
if (domain != null) {
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
return new NTCredentials(
systemcreds.getUserName(), systemcreds.getPassword(), null, null);

View File

@ -130,7 +130,7 @@ final class ProtocolExec implements ExecChainHandler {
if (authority != null) {
final CredentialsProvider credsProvider = context.getCredentialsProvider();
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
public void testBasics() {
final AuthScope authscope = new AuthScope("somehost", 80, "somerealm", "somescheme");
Assert.assertEquals("SOMESCHEME", authscope.getScheme());
final AuthScope authscope = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
Assert.assertEquals("SOMESCHEME", authscope.getAuthScheme());
Assert.assertEquals("http", authscope.getProtocol());
Assert.assertEquals("somehost", authscope.getHost());
Assert.assertEquals(80, authscope.getPort());
Assert.assertEquals("somerealm", authscope.getRealm());
Assert.assertEquals("SOMESCHEME 'somerealm'@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());
Assert.assertEquals("SOMESCHEME 'somerealm' http://somehost:80", authscope.toString());
}
@Test
public void testByOrigin() {
final HttpHost host = new HttpHost("somehost", 8080, "http");
final AuthScope authscope = new AuthScope(host);
Assert.assertEquals(null, authscope.getScheme());
Assert.assertEquals(null, authscope.getAuthScheme());
Assert.assertEquals("somehost", authscope.getHost());
Assert.assertEquals(8080, authscope.getPort());
Assert.assertEquals(null, authscope.getRealm());
Assert.assertEquals(host, authscope.getOrigin());
Assert.assertEquals("<any realm>@somehost:8080", authscope.toString());
Assert.assertEquals("http", authscope.getProtocol());
Assert.assertEquals("<any auth scheme> <any realm> http://somehost:8080", authscope.toString());
}
@Test
public void testMixedCaseHostname() {
final AuthScope authscope = new AuthScope("SomeHost", 80);
Assert.assertEquals(null, authscope.getScheme());
Assert.assertEquals(null, authscope.getAuthScheme());
Assert.assertEquals("somehost", authscope.getHost());
Assert.assertEquals(80, authscope.getPort());
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 {
final HttpHost host = new HttpHost("SomeHost", 8080, "http");
final AuthScope authscope = new AuthScope(host);
Assert.assertEquals("somehost", authscope.getHost());
Assert.assertEquals(host, authscope.getOrigin());
}
@Test
public void testBasicsOptionalHost() {
final AuthScope authscope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME);
Assert.assertEquals(null, authscope.getScheme());
public void testBasicsAllOptional() {
final AuthScope authscope = new AuthScope(null, null, -1, null, null);
Assert.assertEquals(null, authscope.getAuthScheme());
Assert.assertEquals(null, authscope.getHost());
Assert.assertEquals(-1, authscope.getPort());
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
public void testScopeMatching() {
final AuthScope authscope1 = new AuthScope("somehost", 80, "somerealm", "somescheme");
final AuthScope authscope2 = new AuthScope("someotherhost", 80, "somerealm", "somescheme");
final AuthScope authscope1 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
final AuthScope authscope2 = new AuthScope("http", "someotherhost", 80, "somerealm", "somescheme");
Assert.assertTrue(authscope1.match(authscope2) < 0);
int m1 = authscope1.match(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
int m2 = authscope1.match(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm", AuthScope.ANY_SCHEME));
int m1 = authscope1.match(new AuthScope(null, null, -1, null, "somescheme"));
int m2 = authscope1.match(new AuthScope(null, null, -1, "somerealm", null));
Assert.assertTrue(m2 > m1);
m1 = authscope1.match(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
m2 = authscope1.match(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm", AuthScope.ANY_SCHEME));
m1 = authscope1.match(new AuthScope(null, null, -1, null, "somescheme"));
m2 = authscope1.match(new AuthScope(null, null, -1, "somerealm", null));
Assert.assertTrue(m2 > m1);
m1 = authscope1.match(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm", "somescheme"));
m2 = authscope1.match(
new AuthScope(AuthScope.ANY_HOST, 80, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME));
m1 = authscope1.match(new AuthScope(null, null, -1, "somerealm", "somescheme"));
m2 = authscope1.match(new AuthScope(null, null, 80, null, null));
Assert.assertTrue(m2 > m1);
m1 = authscope1.match(
new AuthScope(AuthScope.ANY_HOST, 80, "somerealm", "somescheme"));
m2 = authscope1.match(
new AuthScope("somehost", AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME));
m1 = authscope1.match(new AuthScope(null, null, 80, "somerealm", "somescheme"));
m2 = authscope1.match(new AuthScope(null, "somehost", -1, null, null));
Assert.assertTrue(m2 > m1);
m1 = authscope1.match(AuthScope.ANY);
m2 = authscope1.match(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
m1 = authscope1.match(new AuthScope(null, null, 80, "somerealm", "somescheme"));
m2 = authscope1.match(new AuthScope(null, "somehost", -1, null, null));
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);
}
@Test
public void testEquals() {
final AuthScope authscope1 = new AuthScope("somehost", 80, "somerealm", "somescheme");
final AuthScope authscope2 = new AuthScope("someotherhost", 80, "somerealm", "somescheme");
final AuthScope authscope3 = new AuthScope("somehost", 80, "somerealm", "somescheme");
final AuthScope authscope4 = new AuthScope("somehost", 8080, "somerealm", "somescheme");
final AuthScope authscope5 = new AuthScope("somehost", 80, "someotherrealm", "somescheme");
final AuthScope authscope6 = new AuthScope("somehost", 80, "somerealm", "someotherscheme");
final AuthScope authscope1 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
final AuthScope authscope2 = new AuthScope("http", "someotherhost", 80, "somerealm", "somescheme");
final AuthScope authscope3 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
final AuthScope authscope4 = new AuthScope("http", "somehost", 8080, "somerealm", "somescheme");
final AuthScope authscope5 = new AuthScope("http", "somehost", 80, "someotherrealm", "somescheme");
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.assertFalse(authscope1.equals(authscope2));
Assert.assertTrue(authscope1.equals(authscope3));
Assert.assertFalse(authscope1.equals(authscope4));
Assert.assertFalse(authscope1.equals(authscope5));
Assert.assertFalse(authscope1.equals(authscope6));
Assert.assertFalse(authscope1.equals(authscope7));
}
@Test
public void testHash() {
final AuthScope authscope1 = new AuthScope("somehost", 80, "somerealm", "somescheme");
final AuthScope authscope2 = new AuthScope("someotherhost", 80, "somerealm", "somescheme");
final AuthScope authscope3 = new AuthScope("somehost", 80, "somerealm", "somescheme");
final AuthScope authscope4 = new AuthScope("somehost", 8080, "somerealm", "somescheme");
final AuthScope authscope5 = new AuthScope("somehost", 80, "someotherrealm", "somescheme");
final AuthScope authscope6 = new AuthScope("somehost", 80, "somerealm", "someotherscheme");
final AuthScope authscope1 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
final AuthScope authscope2 = new AuthScope("http", "someotherhost", 80, "somerealm", "somescheme");
final AuthScope authscope3 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
final AuthScope authscope4 = new AuthScope("http", "somehost", 8080, "somerealm", "somescheme");
final AuthScope authscope5 = new AuthScope("http", "somehost", 80, "someotherrealm", "somescheme");
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.assertFalse(authscope1.hashCode() == authscope2.hashCode());
Assert.assertTrue(authscope1.hashCode() == authscope3.hashCode());
Assert.assertFalse(authscope1.hashCode() == authscope4.hashCode());
Assert.assertFalse(authscope1.hashCode() == authscope5.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 =
new UsernamePasswordCredentials("user2", "pass2".toCharArray());
public final static AuthScope SCOPE1 =
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm1");
public final static AuthScope SCOPE2 =
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm2");
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");
public final static AuthScope SCOPE1 = new AuthScope(null, null, -1, "realm1", null);
public final static AuthScope SCOPE2 = new AuthScope(null, null, -1, "realm2", null);
public final static AuthScope BOGUS = new AuthScope(null, null, -1, "bogus", null);
public final static AuthScope DEFSCOPE = new AuthScope(null, "host", -1, "realm", null);
@Test
public void testBasicCredentialsProviderCredentials() {
@ -70,7 +66,7 @@ public class TestBasicCredentialsProvider {
@Test
public void testBasicCredentialsProviderDefaultCredentials() {
final BasicCredentialsProvider state = new BasicCredentialsProvider();
state.setCredentials(AuthScope.ANY, CREDS1);
state.setCredentials(new AuthScope(null, null, -1, null ,null), CREDS1);
state.setCredentials(SCOPE2, CREDS2);
Assert.assertEquals(CREDS1, state.getCredentials(BOGUS, null));
}
@ -79,7 +75,7 @@ public class TestBasicCredentialsProvider {
public void testDefaultCredentials() throws Exception {
final BasicCredentialsProvider state = new BasicCredentialsProvider();
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);
Assert.assertEquals(got, expected);
}
@ -97,8 +93,7 @@ public class TestBasicCredentialsProvider {
public void testHostCredentials() throws Exception {
final BasicCredentialsProvider state = new BasicCredentialsProvider();
final Credentials expected = new UsernamePasswordCredentials("name", "pass".toCharArray());
state.setCredentials(
new AuthScope("host", AuthScope.ANY_PORT, AuthScope.ANY_REALM), expected);
state.setCredentials(new AuthScope(null, "host", -1, null, null), expected);
final Credentials got = state.getCredentials(DEFSCOPE, null);
Assert.assertEquals(expected, got);
}
@ -107,10 +102,8 @@ public class TestBasicCredentialsProvider {
public void testWrongHostCredentials() throws Exception {
final BasicCredentialsProvider state = new BasicCredentialsProvider();
final Credentials expected = new UsernamePasswordCredentials("name", "pass".toCharArray());
state.setCredentials(
new AuthScope("host1", AuthScope.ANY_PORT, "realm"), expected);
final Credentials got = state.getCredentials(
new AuthScope("host2", AuthScope.ANY_PORT, "realm"), null);
state.setCredentials(new AuthScope(null, "host1", -1, "realm", null), expected);
final Credentials got = state.getCredentials(new AuthScope(null, "host2", -1, "realm", null), null);
Assert.assertNotSame(expected, got);
}
@ -118,10 +111,8 @@ public class TestBasicCredentialsProvider {
public void testWrongRealmCredentials() throws Exception {
final BasicCredentialsProvider state = new BasicCredentialsProvider();
final Credentials cred = new UsernamePasswordCredentials("name", "pass".toCharArray());
state.setCredentials(
new AuthScope("host", AuthScope.ANY_PORT, "realm1"), cred);
final Credentials got = state.getCredentials(
new AuthScope("host", AuthScope.ANY_PORT, "realm2"), null);
state.setCredentials(new AuthScope(null, "host", -1, "realm1", null), cred);
final Credentials got = state.getCredentials(new AuthScope(null, "host", -1, "realm2", null), null);
Assert.assertNotSame(cred, got);
}
@ -141,27 +132,24 @@ public class TestBasicCredentialsProvider {
final Credentials creds2 = new UsernamePasswordCredentials("name2", "pass2".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 scope2 = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm");
final AuthScope scope3 = new AuthScope("somehost", AuthScope.ANY_PORT, AuthScope.ANY_REALM);
final AuthScope scope1 = new AuthScope(null, null, -1, null, null);
final AuthScope scope2 = new AuthScope(null, null, -1, "somerealm", null);
final AuthScope scope3 = new AuthScope(null, "somehost", -1, null, null);
final BasicCredentialsProvider state = new BasicCredentialsProvider();
state.setCredentials(scope1, creds1);
state.setCredentials(scope2, creds2);
state.setCredentials(scope3, creds3);
Credentials got = state.getCredentials(
new AuthScope("someotherhost", 80, "someotherrealm", "basic"), null);
Credentials got = state.getCredentials(new AuthScope("http", "someotherhost", 80, "someotherrealm", "basic"), null);
Credentials expected = creds1;
Assert.assertEquals(expected, got);
got = state.getCredentials(
new AuthScope("someotherhost", 80, "somerealm", "basic"), null);
got = state.getCredentials(new AuthScope("http", "someotherhost", 80, "somerealm", "basic"), null);
expected = creds2;
Assert.assertEquals(expected, got);
got = state.getCredentials(
new AuthScope("somehost", 80, "someotherrealm", "basic"), null);
got = state.getCredentials(new AuthScope("http", "somehost", 80, "someotherrealm", "basic"), null);
expected = creds3;
Assert.assertEquals(expected, got);
}

View File

@ -90,7 +90,7 @@ public class TestSystemDefaultCredentialsProvider {
final AuthenticatorDelegate authenticatorDelegate = installAuthenticator(AUTH1);
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();
coreContext.setAttribute(HttpCoreContext.HTTP_REQUEST, new HttpGet(httpRequestUrl.toURI()));
@ -110,7 +110,7 @@ public class TestSystemDefaultCredentialsProvider {
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 =
new SystemDefaultCredentialsProvider().getCredentials(authScope, null);

View File

@ -138,7 +138,7 @@ public class TestProtocolExec {
protocolExec.execute(request, scope, chain);
Assert.assertEquals(new URI("http://bar/test"), request.getUri());
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.assertEquals("somefella", creds.getUserPrincipal().getName());
}