You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by ja...@apache.org on 2017/06/19 01:16:02 UTC

ant-ivy git commit: IVY-1557 Pass along the requesting URL and type to the underlying authenticator, when IvyAuthenticator can't handle the authentication request

Repository: ant-ivy
Updated Branches:
  refs/heads/master 1c23268d7 -> aed63ae84


IVY-1557 Pass along the requesting URL and type to the underlying authenticator, when IvyAuthenticator can't handle the authentication request


Project: http://git-wip-us.apache.org/repos/asf/ant-ivy/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant-ivy/commit/aed63ae8
Tree: http://git-wip-us.apache.org/repos/asf/ant-ivy/tree/aed63ae8
Diff: http://git-wip-us.apache.org/repos/asf/ant-ivy/diff/aed63ae8

Branch: refs/heads/master
Commit: aed63ae84f10aa758a905d5827994068edb71e0d
Parents: 1c23268
Author: Jaikiran Pai <ja...@apache.org>
Authored: Sun Jun 18 12:30:44 2017 +0530
Committer: Jaikiran Pai <ja...@apache.org>
Committed: Sun Jun 18 19:38:15 2017 +0530

----------------------------------------------------------------------
 .../apache/ivy/util/url/IvyAuthenticator.java   |   2 +-
 test/java/org/apache/ivy/TestHelper.java        |  24 ++++
 .../ivy/util/url/IvyAuthenticatorTest.java      | 119 +++++++++++++++++++
 3 files changed, 144 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/aed63ae8/src/java/org/apache/ivy/util/url/IvyAuthenticator.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/ivy/util/url/IvyAuthenticator.java b/src/java/org/apache/ivy/util/url/IvyAuthenticator.java
index 905da4e..8df0150 100644
--- a/src/java/org/apache/ivy/util/url/IvyAuthenticator.java
+++ b/src/java/org/apache/ivy/util/url/IvyAuthenticator.java
@@ -103,7 +103,7 @@ public final class IvyAuthenticator extends Authenticator {
             try {
                 result = Authenticator.requestPasswordAuthentication(getRequestingHost(),
                     getRequestingSite(), getRequestingPort(), getRequestingProtocol(),
-                    getRequestingPrompt(), getRequestingScheme());
+                    getRequestingPrompt(), getRequestingScheme(), getRequestingURL(), getRequestorType());
             } finally {
                 Authenticator.setDefault(this);
             }

http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/aed63ae8/test/java/org/apache/ivy/TestHelper.java
----------------------------------------------------------------------
diff --git a/test/java/org/apache/ivy/TestHelper.java b/test/java/org/apache/ivy/TestHelper.java
index 20fdfc3..ac8b669 100644
--- a/test/java/org/apache/ivy/TestHelper.java
+++ b/test/java/org/apache/ivy/TestHelper.java
@@ -19,6 +19,8 @@ package org.apache.ivy;
 
 import java.io.File;
 import java.io.IOException;
+import java.lang.reflect.Field;
+import java.net.Authenticator;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
@@ -345,4 +347,26 @@ public class TestHelper {
         del.setDir(cache);
         del.execute();
     }
+
+    /**
+     * The {@link Authenticator} doesn't have API to get hold of the current system level {@link Authenticator}.
+     * This method does a best-effort attempt to try and get hold of the current {@link Authenticator} in a way that's
+     * specific to the implementation of this method. There's no guarantee that this method will return the current
+     * authenticator.
+     *
+     * @return Returns the currently setup system level {@link Authenticator}. In cases where this method isn't able to get
+     * the current authenticator, this method returns null
+     */
+    public static Authenticator getCurrentAuthenticator() {
+        // we use reflection to try and get hold of the "current" authenticator
+        // since there's no getter available on the Authenticator.
+        try {
+            Field f = Authenticator.class.getDeclaredField("theAuthenticator");
+            f.setAccessible(true);
+            return (Authenticator) f.get(null);
+        } catch (Throwable t) {
+            // ignore and return null
+            return null;
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/aed63ae8/test/java/org/apache/ivy/util/url/IvyAuthenticatorTest.java
----------------------------------------------------------------------
diff --git a/test/java/org/apache/ivy/util/url/IvyAuthenticatorTest.java b/test/java/org/apache/ivy/util/url/IvyAuthenticatorTest.java
new file mode 100644
index 0000000..2d826fa
--- /dev/null
+++ b/test/java/org/apache/ivy/util/url/IvyAuthenticatorTest.java
@@ -0,0 +1,119 @@
+/*
+ * 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.
+ */
+
+package org.apache.ivy.util.url;
+
+import org.apache.ivy.TestHelper;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.net.Authenticator;
+import java.net.InetAddress;
+import java.net.PasswordAuthentication;
+import java.net.URL;
+import java.util.Arrays;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests {@link IvyAuthenticator}
+ */
+public class IvyAuthenticatorTest {
+
+    private Authenticator previousAuthenticator;
+
+    private TestAuthenticator testAuthenticator;
+
+    @Before
+    public void before() {
+        previousAuthenticator = TestHelper.getCurrentAuthenticator();
+        this.setupTestAuthenticator();
+    }
+
+    private void setupTestAuthenticator() {
+        this.testAuthenticator = new TestAuthenticator();
+        // first setup our TestAuthenticator
+        Authenticator.setDefault(this.testAuthenticator);
+        // now install IvyAuthenticator on top of it
+        IvyAuthenticator.install();
+    }
+
+    @After
+    public void after() {
+        // reset to the authenticator that was around before the test was run
+        Authenticator.setDefault(previousAuthenticator);
+    }
+
+    /**
+     * Tests that when {@link IvyAuthenticator} can't handle a authentication request and falls back on an authenticator
+     * that was previously set, before IvyAuthenticator installed on top of it, the other authenticator gets passed all the
+     * relevant requesting information, including the {@link Authenticator#getRequestingURL() requesting URL} and
+     * {@link Authenticator#getRequestorType() request type}
+     *
+     * @throws Exception
+     * @see <a href="https://issues.apache.org/jira/browse/IVY-1557">IVY-1557</a>
+     */
+    @Test
+    public void testRequestURLAndType() throws Exception {
+        testAuthenticator.expectedHost = "localhost";
+        testAuthenticator.expectedPort = 12345;
+        testAuthenticator.expectedPrompt = "Test prompt - testRequestURLAndType";
+        testAuthenticator.expectedProtocol = "HTTP/1.1";
+        testAuthenticator.expectedURL = new URL("http", "localhost", 12345, "/a/b/c");
+        testAuthenticator.expectedType = Authenticator.RequestorType.PROXY;
+        testAuthenticator.expectedScheme = "BASIC";
+        testAuthenticator.expectedSite = InetAddress.getLoopbackAddress();
+
+        // trigger the authentication
+        final PasswordAuthentication auth = Authenticator.requestPasswordAuthentication(testAuthenticator.expectedHost, testAuthenticator.expectedSite,
+                testAuthenticator.expectedPort, testAuthenticator.expectedProtocol, testAuthenticator.expectedPrompt,
+                testAuthenticator.expectedScheme, testAuthenticator.expectedURL, testAuthenticator.expectedType);
+
+        assertNotNull("Expected a password authentication, but got none", auth);
+        assertEquals("Unexpected username", "dummy", auth.getUserName());
+        assertTrue("Unexpected password", Arrays.equals("dummy".toCharArray(), auth.getPassword()));
+    }
+
+
+    private class TestAuthenticator extends Authenticator {
+
+        private String expectedHost;
+        private int expectedPort = -1;
+        private String expectedPrompt;
+        private String expectedProtocol;
+        private String expectedScheme;
+        private URL expectedURL;
+        private RequestorType expectedType;
+        private InetAddress expectedSite;
+
+        @Override
+        protected PasswordAuthentication getPasswordAuthentication() {
+            assertEquals("Unexpected requesting host", expectedHost, getRequestingHost());
+            assertEquals("Unexpected requesting port", expectedPort, getRequestingPort());
+            assertEquals("Unexpected prompt", expectedPrompt, getRequestingPrompt());
+            assertEquals("Unexpected protocol", expectedProtocol, getRequestingProtocol());
+            assertEquals("Unexpected scheme", expectedScheme, getRequestingScheme());
+            assertEquals("Unexpected requesting URL", expectedURL, getRequestingURL());
+            assertEquals("Unexpected requesting type", expectedType, getRequestorType());
+            assertEquals("Unexpected requesting site", expectedSite, getRequestingSite());
+            return new PasswordAuthentication("dummy", "dummy".toCharArray());
+        }
+    }
+}