hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1756293 - in /httpcomponents/httpclient/trunk: httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/ httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/ httpclient5/src/main/java/org/apache/hc/client5/http/au...
Date Sat, 13 Aug 2016 19:55:43 GMT
Author: olegk
Date: Sat Aug 13 19:55:43 2016
New Revision: 1756293

URL: http://svn.apache.org/viewvc?rev=1756293&view=rev
Log:
HTTPCLIENT-1748: Improved compatibility with system authentication API in applets

Added:
    httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestSystemDefaultCredentialsProvider.java
  (with props)
Modified:
    httpcomponents/httpclient/trunk/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Executor.java
    httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiCredentialsProvider.java
    httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/auth/CredentialsProvider.java
    httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/auth/CredentialsStore.java
    httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java
    httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java
    httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/GGSSchemeBase.java
    httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/NTLMScheme.java
    httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/SystemDefaultCredentialsProvider.java
    httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/routing/SystemDefaultRoutePlanner.java
    httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/BasicCredentialsProvider.java
    httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestBasicCredentialsProvider.java
    httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java
    httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/integration/TestClientAuthentication.java
    httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/integration/TestClientAuthenticationFallBack.java
    httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/integration/TestClientReauthentication.java
    httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/sync/TestProtocolExec.java

Modified: httpcomponents/httpclient/trunk/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Executor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Executor.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Executor.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Executor.java
Sat Aug 13 19:55:43 2016
@@ -146,7 +146,7 @@ public class Executor {
 
     public Executor authPreemptive(final HttpHost host) {
         if (this.credentialsStore != null) {
-            final Credentials credentials = this.credentialsStore.getCredentials(new AuthScope(host));
+            final Credentials credentials = this.credentialsStore.getCredentials(new AuthScope(host),
null);
             if (credentials == null) {
                 final BasicScheme basicScheme = new BasicScheme();
                 basicScheme.initPreemptive(credentials);
@@ -165,7 +165,7 @@ public class Executor {
 
     public Executor authPreemptiveProxy(final HttpHost proxy) {
         if (this.credentialsStore != null) {
-            final Credentials credentials = this.credentialsStore.getCredentials(new AuthScope(proxy));
+            final Credentials credentials = this.credentialsStore.getCredentials(new AuthScope(proxy),
null);
             if (credentials == null) {
                 final BasicScheme basicScheme = new BasicScheme();
                 basicScheme.initPreemptive(credentials);

Modified: httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiCredentialsProvider.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiCredentialsProvider.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiCredentialsProvider.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiCredentialsProvider.java
Sat Aug 13 19:55:43 2016
@@ -33,6 +33,7 @@ import org.apache.hc.client5.http.auth.C
 import org.apache.hc.client5.http.auth.CredentialsStore;
 import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
 import org.apache.hc.client5.http.osgi.services.ProxyConfiguration;
+import org.apache.hc.core5.http.protocol.HttpContext;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
 
@@ -64,7 +65,7 @@ final class OSGiCredentialsProvider impl
      * {@inheritDoc}
      */
     @Override
-    public Credentials getCredentials(final AuthScope authscope) {
+    public Credentials getCredentials(final AuthScope authscope, final HttpContext context)
{
         // iterate over all active proxy configurations at the moment of getting the credential
         for (final ServiceRegistration<ProxyConfiguration> registration : registeredConfigurations.values())
{
             final ProxyConfiguration proxyConfiguration = bundleContext.getService(registration.getReference());

Modified: httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/auth/CredentialsProvider.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/auth/CredentialsProvider.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/auth/CredentialsProvider.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/auth/CredentialsProvider.java
Sat Aug 13 19:55:43 2016
@@ -26,6 +26,8 @@
  */
 package org.apache.hc.client5.http.auth;
 
+import org.apache.hc.core5.http.protocol.HttpContext;
+
 /**
  * Provider of authentication credentials.
  * <p>
@@ -42,8 +44,10 @@ public interface CredentialsProvider {
      * if available.
      *
      * @param authscope the {@link AuthScope authentication scope}
+     * @param context the {@link HttpContext http context}
      * @return the credentials
+     * @since 5.0
      */
-    Credentials getCredentials(AuthScope authscope);
+    Credentials getCredentials(AuthScope authscope, HttpContext context);
 
 }

Modified: httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/auth/CredentialsStore.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/auth/CredentialsStore.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/auth/CredentialsStore.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/auth/CredentialsStore.java
Sat Aug 13 19:55:43 2016
@@ -45,7 +45,7 @@ public interface CredentialsStore extend
      * @param credentials the authentication {@link Credentials credentials}
      * for the given scope.
      *
-     * @see #getCredentials(AuthScope)
+     * @see #getCredentials(AuthScope, HttpContext)
      */
     void setCredentials(AuthScope authscope, Credentials credentials);
 

Modified: httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java
Sat Aug 13 19:55:43 2016
@@ -140,7 +140,8 @@ public class BasicScheme implements Auth
         Args.notNull(host, "Auth host");
         Args.notNull(credentialsProvider, "CredentialsProvider");
 
-        final Credentials credentials = credentialsProvider.getCredentials(new AuthScope(host,
getRealm(), getName()));
+        final Credentials credentials = credentialsProvider.getCredentials(
+                new AuthScope(host, getRealm(), getName()), context);
         if (credentials != null) {
             this.username = credentials.getUserPrincipal().getName();
             this.password = credentials.getPassword();

Modified: httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java
Sat Aug 13 19:55:43 2016
@@ -172,7 +172,8 @@ public class DigestScheme implements Aut
         Args.notNull(host, "Auth host");
         Args.notNull(credentialsProvider, "CredentialsProvider");
 
-        final Credentials credentials = credentialsProvider.getCredentials(new AuthScope(host,
getRealm(), getName()));
+        final Credentials credentials = credentialsProvider.getCredentials(
+                new AuthScope(host, getRealm(), getName()), context);
         if (credentials != null) {
             this.username = credentials.getUserPrincipal().getName();
             this.password = credentials.getPassword();

Modified: httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/GGSSchemeBase.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/GGSSchemeBase.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/GGSSchemeBase.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/GGSSchemeBase.java
Sat Aug 13 19:55:43 2016
@@ -175,7 +175,8 @@ public abstract class GGSSchemeBase impl
         Args.notNull(host, "Auth host");
         Args.notNull(credentialsProvider, "CredentialsProvider");
 
-        final Credentials credentials = credentialsProvider.getCredentials(new AuthScope(host,
null, getName()));
+        final Credentials credentials = credentialsProvider.getCredentials(
+                new AuthScope(host, null, getName()), context);
         if (credentials instanceof KerberosCredentials) {
             this.gssCredential = ((KerberosCredentials) credentials).getGSSCredential();
         } else {

Modified: httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/NTLMScheme.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/NTLMScheme.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/NTLMScheme.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/NTLMScheme.java
Sat Aug 13 19:55:43 2016
@@ -129,7 +129,8 @@ public class NTLMScheme implements AuthS
         Args.notNull(host, "Auth host");
         Args.notNull(credentialsProvider, "CredentialsProvider");
 
-        final Credentials credentials = credentialsProvider.getCredentials(new AuthScope(host,
null, getName()));
+        final Credentials credentials = credentialsProvider.getCredentials(
+                new AuthScope(host, null, getName()), context);
         if (credentials instanceof NTCredentials) {
             this.credentials = (NTCredentials) credentials;
             return true;

Modified: httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/SystemDefaultCredentialsProvider.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/SystemDefaultCredentialsProvider.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/SystemDefaultCredentialsProvider.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/SystemDefaultCredentialsProvider.java
Sat Aug 13 19:55:43 2016
@@ -27,7 +27,9 @@
 package org.apache.hc.client5.http.impl.auth;
 
 import java.net.Authenticator;
+import java.net.MalformedURLException;
 import java.net.PasswordAuthentication;
+import java.net.URL;
 import java.util.Locale;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -41,6 +43,9 @@ import org.apache.hc.client5.http.config
 import org.apache.hc.client5.http.impl.sync.BasicCredentialsProvider;
 import org.apache.hc.core5.annotation.ThreadSafe;
 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.http.protocol.HttpCoreContext;
 import org.apache.hc.core5.util.Args;
 
 /**
@@ -88,35 +93,52 @@ public class SystemDefaultCredentialsPro
 
     private static PasswordAuthentication getSystemCreds(
             final AuthScope authscope,
-            final Authenticator.RequestorType requestorType) {
+            final Authenticator.RequestorType requestorType,
+            final HttpContext context) {
         final String hostname = authscope.getHost();
         final int port = authscope.getPort();
         final HttpHost origin = authscope.getOrigin();
         final String protocol = origin != null ? origin.getSchemeName() :
                 (port == 443 ? "https" : "http");
+
+        final URL targetHostURL = getTargetHostURL(context);
+        // use null addr, because the authentication fails if it does not exactly match the
expected realm's host
         return Authenticator.requestPasswordAuthentication(
                 hostname,
                 null,
                 port,
                 protocol,
-                null,
+                authscope.getRealm(),
                 translateScheme(authscope.getScheme()),
-                null,
+                targetHostURL,
                 requestorType);
     }
 
+    private static URL getTargetHostURL(final HttpContext context) {
+        if (context == null) {
+            return null;
+        }
+        final HttpRequest httpRequest = (HttpRequest)context.getAttribute(HttpCoreContext.HTTP_REQUEST);
+        final String uri = httpRequest.getRequestLine().getUri();
+        try {
+            return new URL(uri);
+        } catch (final MalformedURLException e) {
+            return null;
+        }
+    }
+
     @Override
-    public Credentials getCredentials(final AuthScope authscope) {
+    public Credentials getCredentials(final AuthScope authscope, final HttpContext context)
{
         Args.notNull(authscope, "Auth scope");
-        final Credentials localcreds = internal.getCredentials(authscope);
+        final Credentials localcreds = internal.getCredentials(authscope, context);
         if (localcreds != null) {
             return localcreds;
         }
         final String host = authscope.getHost();
         if (host != null) {
-            PasswordAuthentication systemcreds = getSystemCreds(authscope, Authenticator.RequestorType.SERVER);
+            PasswordAuthentication systemcreds = getSystemCreds(authscope, Authenticator.RequestorType.SERVER,
context);
             if (systemcreds == null) {
-                systemcreds = getSystemCreds(authscope, Authenticator.RequestorType.PROXY);
+                systemcreds = getSystemCreds(authscope, Authenticator.RequestorType.PROXY,
context);
             }
             if (systemcreds == null) {
                 final String proxyHost = System.getProperty("http.proxyHost");
@@ -143,7 +165,7 @@ public class SystemDefaultCredentialsPro
                     return new NTCredentials(systemcreds.getUserName(), systemcreds.getPassword(),
null, domain);
                 }
                 if (AuthSchemes.NTLM.equalsIgnoreCase(authscope.getScheme())) {
-                    // Domian may be specified in a fully qualified user name
+                    // Domain may be specified in a fully qualified user name
                     return new NTCredentials(
                             systemcreds.getUserName(), systemcreds.getPassword(), null, null);
                 }

Modified: httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/routing/SystemDefaultRoutePlanner.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/routing/SystemDefaultRoutePlanner.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/routing/SystemDefaultRoutePlanner.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/routing/SystemDefaultRoutePlanner.java
Sat Aug 13 19:55:43 2016
@@ -86,22 +86,12 @@ public class SystemDefaultRoutePlanner e
             }
             final InetSocketAddress isa = (InetSocketAddress) p.address();
             // assume default scheme (http)
-            result = new HttpHost(getHost(isa), isa.getPort());
+            result = new HttpHost(isa.getAddress(), isa.getHostString(), isa.getPort(), null);
         }
 
         return result;
     }
 
-    private String getHost(final InetSocketAddress isa) {
-
-        //@@@ Will this work with literal IPv6 addresses, or do we
-        //@@@ need to wrap these in [] for the string representation?
-        //@@@ Having it in this method at least allows for easy workarounds.
-       return isa.isUnresolved() ?
-            isa.getHostName() : isa.getAddress().getHostAddress();
-
-    }
-
     private Proxy chooseProxy(final List<Proxy> proxies) {
         Proxy result = null;
         // check the list for one we can use

Modified: httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/BasicCredentialsProvider.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/BasicCredentialsProvider.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/BasicCredentialsProvider.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/BasicCredentialsProvider.java
Sat Aug 13 19:55:43 2016
@@ -33,6 +33,7 @@ import org.apache.hc.client5.http.auth.A
 import org.apache.hc.client5.http.auth.Credentials;
 import org.apache.hc.client5.http.auth.CredentialsStore;
 import org.apache.hc.core5.annotation.ThreadSafe;
+import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.util.Args;
 
 /**
@@ -94,7 +95,8 @@ public class BasicCredentialsProvider im
     }
 
     @Override
-    public Credentials getCredentials(final AuthScope authscope) {
+    public Credentials getCredentials(final AuthScope authscope,
+                                      final HttpContext httpContext) {
         Args.notNull(authscope, "Authentication scope");
         return matchCredentials(this.credMap, authscope);
     }

Modified: httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestBasicCredentialsProvider.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestBasicCredentialsProvider.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestBasicCredentialsProvider.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestBasicCredentialsProvider.java
Sat Aug 13 19:55:43 2016
@@ -58,14 +58,14 @@ public class TestBasicCredentialsProvide
         final BasicCredentialsProvider state = new BasicCredentialsProvider();
         state.setCredentials(SCOPE1, CREDS1);
         state.setCredentials(SCOPE2, CREDS2);
-        Assert.assertEquals(CREDS1, state.getCredentials(SCOPE1));
-        Assert.assertEquals(CREDS2, state.getCredentials(SCOPE2));
+        Assert.assertEquals(CREDS1, state.getCredentials(SCOPE1, null));
+        Assert.assertEquals(CREDS2, state.getCredentials(SCOPE2, null));
     }
 
     @Test
     public void testBasicCredentialsProviderNoCredentials() {
         final BasicCredentialsProvider state = new BasicCredentialsProvider();
-        Assert.assertEquals(null, state.getCredentials(BOGUS));
+        Assert.assertEquals(null, state.getCredentials(BOGUS, null));
     }
 
     @Test
@@ -73,7 +73,7 @@ public class TestBasicCredentialsProvide
         final BasicCredentialsProvider state = new BasicCredentialsProvider();
         state.setCredentials(AuthScope.ANY, CREDS1);
         state.setCredentials(SCOPE2, CREDS2);
-        Assert.assertEquals(CREDS1, state.getCredentials(BOGUS));
+        Assert.assertEquals(CREDS1, state.getCredentials(BOGUS, null));
     }
 
     @Test
@@ -81,7 +81,7 @@ public class TestBasicCredentialsProvide
         final BasicCredentialsProvider state = new BasicCredentialsProvider();
         final Credentials expected = new UsernamePasswordCredentials("name", "pass".toCharArray());
         state.setCredentials(AuthScope.ANY, expected);
-        final Credentials got = state.getCredentials(DEFSCOPE);
+        final Credentials got = state.getCredentials(DEFSCOPE, null);
         Assert.assertEquals(got, expected);
     }
 
@@ -90,7 +90,7 @@ public class TestBasicCredentialsProvide
         final BasicCredentialsProvider state = new BasicCredentialsProvider();
         final Credentials expected = new UsernamePasswordCredentials("name", "pass".toCharArray());
         state.setCredentials(DEFSCOPE, expected);
-        final Credentials got = state.getCredentials(DEFSCOPE);
+        final Credentials got = state.getCredentials(DEFSCOPE, null);
         Assert.assertEquals(expected, got);
     }
 
@@ -100,7 +100,7 @@ public class TestBasicCredentialsProvide
         final Credentials expected = new UsernamePasswordCredentials("name", "pass".toCharArray());
         state.setCredentials(
             new AuthScope("host", AuthScope.ANY_PORT, AuthScope.ANY_REALM), expected);
-        final Credentials got = state.getCredentials(DEFSCOPE);
+        final Credentials got = state.getCredentials(DEFSCOPE, null);
         Assert.assertEquals(expected, got);
     }
 
@@ -111,7 +111,7 @@ public class TestBasicCredentialsProvide
         state.setCredentials(
             new AuthScope("host1", AuthScope.ANY_PORT, "realm"), expected);
         final Credentials got = state.getCredentials(
-            new AuthScope("host2", AuthScope.ANY_PORT, "realm"));
+            new AuthScope("host2", AuthScope.ANY_PORT, "realm"), null);
         Assert.assertNotSame(expected, got);
     }
 
@@ -122,7 +122,7 @@ public class TestBasicCredentialsProvide
         state.setCredentials(
             new AuthScope("host", AuthScope.ANY_PORT, "realm1"), cred);
         final Credentials got = state.getCredentials(
-            new AuthScope("host", AuthScope.ANY_PORT, "realm2"));
+            new AuthScope("host", AuthScope.ANY_PORT, "realm2"), null);
         Assert.assertNotSame(cred, got);
     }
 
@@ -132,7 +132,7 @@ public class TestBasicCredentialsProvide
         final BasicCredentialsProvider state = new BasicCredentialsProvider();
         final Credentials expected = new UsernamePasswordCredentials("name", "pass".toCharArray());
         state.setCredentials(new AuthScope(httpHost), expected);
-        final Credentials got = state.getCredentials(DEFSCOPE);
+        final Credentials got = state.getCredentials(DEFSCOPE, null);
         Assert.assertEquals(expected, got);
     }
 
@@ -152,17 +152,17 @@ public class TestBasicCredentialsProvide
         state.setCredentials(scope3, creds3);
 
         Credentials got = state.getCredentials(
-            new AuthScope("someotherhost", 80, "someotherrealm", "basic"));
+            new AuthScope("someotherhost", 80, "someotherrealm", "basic"), null);
         Credentials expected = creds1;
         Assert.assertEquals(expected, got);
 
         got = state.getCredentials(
-            new AuthScope("someotherhost", 80, "somerealm", "basic"));
+            new AuthScope("someotherhost", 80, "somerealm", "basic"), null);
         expected = creds2;
         Assert.assertEquals(expected, got);
 
         got = state.getCredentials(
-            new AuthScope("somehost", 80, "someotherrealm", "basic"));
+            new AuthScope("somehost", 80, "someotherrealm", "basic"), null);
         expected = creds3;
         Assert.assertEquals(expected, got);
     }

Modified: httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java
Sat Aug 13 19:55:43 2016
@@ -160,12 +160,13 @@ public class TestHttpAuthenticator {
         response.addHeader(new BasicHeader(HttpHeaders.WWW_AUTHENTICATE, "whatever realm=\"realm1\",
stuff=\"1234\""));
 
         final Credentials credentials = new UsernamePasswordCredentials("user", "pass".toCharArray());
-        Mockito.when(this.credentialsProvider.getCredentials(Mockito.<AuthScope>any())).thenReturn(credentials);
+        Mockito.when(this.credentialsProvider.getCredentials(Mockito.<AuthScope>any(),
+                                                             Mockito.<HttpContext>any())).thenReturn(credentials);
 
         final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy();
 
-        Assert.assertTrue(this.httpAuthenticator.prepareAuthResponse(
-                host, ChallengeType.TARGET, response, authStrategy, this.authExchange, this.context));
+        Assert.assertTrue(this.httpAuthenticator.prepareAuthResponse(host, ChallengeType.TARGET,
response, authStrategy,
+                                                                     this.authExchange, this.context));
         Assert.assertEquals(AuthExchange.State.CHALLENGED, this.authExchange.getState());
 
         final Queue<AuthScheme> options = this.authExchange.getAuthOptions();
@@ -182,17 +183,19 @@ public class TestHttpAuthenticator {
     @Test
     public void testAuthenticationCredentialsForBasic() throws Exception {
         final HttpHost host = new HttpHost("somehost", 80);
-        final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED,
"UNAUTHORIZED");
+        final HttpResponse response =
+            new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
         response.addHeader(new BasicHeader(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"test\""));
         response.addHeader(new BasicHeader(HttpHeaders.WWW_AUTHENTICATE, "Digest realm=\"realm1\",
nonce=\"1234\""));
 
         final Credentials credentials = new UsernamePasswordCredentials("user", "pass".toCharArray());
-        Mockito.when(this.credentialsProvider.getCredentials(new AuthScope(host, "test",
"basic"))).thenReturn(credentials);
+        Mockito.when(this.credentialsProvider.getCredentials(Mockito.eq(new AuthScope(host,
"test", "basic")),
+                                                             Mockito.<HttpContext>any())).thenReturn(credentials);
 
         final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy();
 
-        Assert.assertTrue(this.httpAuthenticator.prepareAuthResponse(
-                host, ChallengeType.TARGET, response, authStrategy, this.authExchange, this.context));
+        Assert.assertTrue(this.httpAuthenticator.prepareAuthResponse(host, ChallengeType.TARGET,
response, authStrategy,
+                                                                     this.authExchange, this.context));
         Assert.assertEquals(AuthExchange.State.CHALLENGED, this.authExchange.getState());
 
         final Queue<AuthScheme> options = this.authExchange.getAuthOptions();
@@ -322,7 +325,8 @@ public class TestHttpAuthenticator {
         response.addHeader(new BasicHeader(HttpHeaders.WWW_AUTHENTICATE, "whatever realm=\"realm1\",
stuff=\"1234\""));
 
         final Credentials credentials = new UsernamePasswordCredentials("user", "pass".toCharArray());
-        Mockito.when(this.credentialsProvider.getCredentials(new AuthScope(host, "realm1",
"digest"))).thenReturn(credentials);
+        Mockito.when(this.credentialsProvider.getCredentials(Mockito.eq(new AuthScope(host,
"realm1", "digest")),
+                                                             Mockito.<HttpContext>any())).thenReturn(credentials);
 
         final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy();
 

Added: httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestSystemDefaultCredentialsProvider.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestSystemDefaultCredentialsProvider.java?rev=1756293&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestSystemDefaultCredentialsProvider.java
(added)
+++ httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestSystemDefaultCredentialsProvider.java
Sat Aug 13 19:55:43 2016
@@ -0,0 +1,142 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.client5.http.impl.auth;
+
+import java.net.Authenticator;
+import java.net.Authenticator.RequestorType;
+import java.net.InetAddress;
+import java.net.MalformedURLException;
+import java.net.PasswordAuthentication;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+import org.apache.hc.client5.http.auth.AuthScope;
+import org.apache.hc.client5.http.auth.Credentials;
+import org.apache.hc.client5.http.methods.HttpGet;
+import org.apache.hc.core5.http.HttpHost;
+import org.apache.hc.core5.http.protocol.HttpCoreContext;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+/**
+ * Simple tests for {@link SystemDefaultCredentialsProvider}.
+ */
+public class TestSystemDefaultCredentialsProvider {
+
+    private final static String PROXY_PROTOCOL1 = "http";
+    private final static String PROXY_HOST1 = "proxyhost1";
+    private final static int PROXY_PORT1 = 3128;
+    private final static String PROMPT1 = "HttpClient authentication test prompt";
+    private final static String TARGET_SCHEME1 = "https";
+    private final static String TARGET_HOST1 = "targethost1";
+    private final static int TARGET_PORT1 = 80;
+    private final static PasswordAuthentication AUTH1 =
+        new PasswordAuthentication("testUser", "testPassword".toCharArray());
+
+    // It's not possible to mock static Authenticator methods. So we mock a delegate
+    private final class DelegatedAuthenticator extends Authenticator {
+        private final AuthenticatorDelegate authenticatorDelegate;
+
+        private DelegatedAuthenticator(final AuthenticatorDelegate authenticatorDelegate)
{
+            this.authenticatorDelegate = authenticatorDelegate;
+        }
+
+        @Override
+        protected PasswordAuthentication getPasswordAuthentication() {
+            return authenticatorDelegate.getPasswordAuthentication(getRequestingHost(), getRequestingSite(),
+                                                                   getRequestingPort(), getRequestingProtocol(),
+                                                                   getRequestingPrompt(),
getRequestingScheme(),
+                                                                   getRequestingURL(), getRequestorType());
+        }
+    }
+
+    private interface AuthenticatorDelegate {
+        PasswordAuthentication getPasswordAuthentication(
+            String host,
+            InetAddress addr,
+            int port,
+            String protocol,
+            String prompt,
+            String scheme,
+            URL url,
+            RequestorType reqType);
+    }
+
+    @Test
+    public void testSystemCredentialsProviderCredentials() throws MalformedURLException,
+        URISyntaxException {
+
+        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 HttpCoreContext coreContext = new HttpCoreContext();
+        coreContext.setTargetHost(new HttpHost(TARGET_HOST1, TARGET_PORT1, TARGET_SCHEME1));
+        coreContext.setAttribute(HttpCoreContext.HTTP_REQUEST, new HttpGet(httpRequestUrl.toURI()));
+
+        final Credentials receivedCredentials =
+            new SystemDefaultCredentialsProvider().getCredentials(authScope, coreContext);
+
+        Mockito.verify(authenticatorDelegate).getPasswordAuthentication(PROXY_HOST1, null,
PROXY_PORT1, PROXY_PROTOCOL1,
+                                                                        PROMPT1, "Basic",
httpRequestUrl,
+                                                                        RequestorType.SERVER);
+        Assert.assertNotNull(receivedCredentials);
+        Assert.assertEquals(AUTH1.getUserName(), receivedCredentials.getUserPrincipal().getName());
+        Assert.assertEquals(AUTH1.getPassword(), receivedCredentials.getPassword());
+    }
+
+    @Test
+    public void testSystemCredentialsProviderNoContext() throws MalformedURLException,
+        URISyntaxException {
+
+        final AuthenticatorDelegate authenticatorDelegate = installAuthenticator(AUTH1);
+
+        final AuthScope authScope = new AuthScope(PROXY_HOST1, PROXY_PORT1, PROMPT1, "BASIC");
+
+        final Credentials receivedCredentials =
+            new SystemDefaultCredentialsProvider().getCredentials(authScope, null);
+
+        Mockito.verify(authenticatorDelegate).getPasswordAuthentication(PROXY_HOST1, null,
PROXY_PORT1, PROXY_PROTOCOL1,
+                                                                        PROMPT1, "Basic",
null,
+                                                                        RequestorType.SERVER);
+        Assert.assertNotNull(receivedCredentials);
+        Assert.assertEquals(AUTH1.getUserName(), receivedCredentials.getUserPrincipal().getName());
+        Assert.assertEquals(AUTH1.getPassword(), receivedCredentials.getPassword());
+    }
+
+    private AuthenticatorDelegate installAuthenticator(final PasswordAuthentication returedAuthentication)
{
+        final AuthenticatorDelegate authenticatorDelegate = Mockito.mock(AuthenticatorDelegate.class);
+        Mockito.when(authenticatorDelegate.getPasswordAuthentication(Mockito.anyString(),
+                                                                     Mockito.any(InetAddress.class),
Mockito.anyInt(),
+                                                                     Mockito.anyString(),
Mockito.anyString(),
+                                                                     Mockito.anyString(),
Mockito.any(URL.class),
+                                                                     Mockito.any(RequestorType.class))).thenReturn(returedAuthentication);
+        Authenticator.setDefault(new DelegatedAuthenticator(authenticatorDelegate));
+        return authenticatorDelegate;
+    }
+}

Propchange: httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestSystemDefaultCredentialsProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestSystemDefaultCredentialsProvider.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestSystemDefaultCredentialsProvider.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/integration/TestClientAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/integration/TestClientAuthentication.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/integration/TestClientAuthentication.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/integration/TestClientAuthentication.java
Sat Aug 13 19:55:43 2016
@@ -156,7 +156,7 @@ public class TestClientAuthentication ex
         }
 
         @Override
-        public Credentials getCredentials(final AuthScope authscope) {
+        public Credentials getCredentials(final AuthScope authscope, final HttpContext context)
{
             this.authscope = authscope;
             return this.creds;
         }

Modified: httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/integration/TestClientAuthenticationFallBack.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/integration/TestClientAuthenticationFallBack.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/integration/TestClientAuthenticationFallBack.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/integration/TestClientAuthenticationFallBack.java
Sat Aug 13 19:55:43 2016
@@ -118,7 +118,7 @@ public class TestClientAuthenticationFal
         }
 
         @Override
-        public Credentials getCredentials(final AuthScope authscope) {
+        public Credentials getCredentials(final AuthScope authscope, final HttpContext context)
{
             this.authscope = authscope;
             return this.creds;
         }

Modified: httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/integration/TestClientReauthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/integration/TestClientReauthentication.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/integration/TestClientReauthentication.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/integration/TestClientReauthentication.java
Sat Aug 13 19:55:43 2016
@@ -134,7 +134,7 @@ public class TestClientReauthentication
         }
 
         @Override
-        public Credentials getCredentials(final AuthScope authscope) {
+        public Credentials getCredentials(final AuthScope authscope, final HttpContext context)
{
             this.authscope = authscope;
             return this.creds;
         }

Modified: httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/sync/TestProtocolExec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/sync/TestProtocolExec.java?rev=1756293&r1=1756292&r2=1756293&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/sync/TestProtocolExec.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5/src/test/java/org/apache/hc/client5/http/impl/sync/TestProtocolExec.java
Sat Aug 13 19:55:43 2016
@@ -190,7 +190,7 @@ public class TestProtocolExec {
         Assert.assertEquals(new URI("/test"), request.getURI());
         Assert.assertEquals(new HttpHost("bar", -1), context.getTargetHost());
         final CredentialsProvider credentialsProvider = context.getCredentialsProvider();
-        final Credentials creds = credentialsProvider.getCredentials(new AuthScope("bar",
-1, null));
+        final Credentials creds = credentialsProvider.getCredentials(new AuthScope("bar",
-1, null), null);
         Assert.assertNotNull(creds);
         Assert.assertEquals("somefella", creds.getUserPrincipal().getName());
     }




Mime
View raw message