Search in sources :

Example 81 with PasswordAuthentication

use of java.net.PasswordAuthentication in project knime-core by knime.

the class ThreadLocalHTTPAuthenticator method getPasswordAuthentication.

/**
 * {@inheritDoc}
 */
@Override
protected PasswordAuthentication getPasswordAuthentication() {
    if (SUPPRESS_POPUP.get() == Boolean.TRUE) {
        return null;
    }
    try {
        // write request values from this object into the delegate
        for (final Field f : Authenticator.class.getDeclaredFields()) {
            if (!Modifier.isStatic(f.getModifiers())) {
                f.setAccessible(true);
                Object o = f.get(this);
                f.set(m_delegate, o);
            }
        }
        final Method m = Authenticator.class.getDeclaredMethod("getPasswordAuthentication");
        m.setAccessible(true);
        return (PasswordAuthentication) m.invoke(m_delegate);
    } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
        NodeLogger.getLogger(getClass()).warn("Could not delegate HTTP authentication request: " + ex.getMessage(), ex);
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) PasswordAuthentication(java.net.PasswordAuthentication)

Example 82 with PasswordAuthentication

use of java.net.PasswordAuthentication in project cxf by apache.

the class CXFAuthenticatorCleanupTest method runCleanupTestWeakRef.

@Test
public void runCleanupTestWeakRef() throws Exception {
    InetAddress add = InetAddress.getLocalHost();
    final List<Integer> traceLengths = new ArrayList<>();
    // create a chain of CXFAuthenticators, strongly referenced to prevent cleanups
    Authenticator.setDefault(new Authenticator() {

        protected PasswordAuthentication getPasswordAuthentication() {
            traceLengths.add(Thread.currentThread().getStackTrace().length);
            return super.getPasswordAuthentication();
        }
    });
    Authenticator.requestPasswordAuthentication("localhost", add, 8080, "http", "hello", "http");
    for (int x = 0; x < 20; x++) {
        CXFAuthenticator.addAuthenticator();
        CXFAuthenticator.instance = null;
        System.gc();
    }
    CXFAuthenticator.addAuthenticator();
    System.gc();
    Authenticator.requestPasswordAuthentication("localhost", add, 8080, "http", "hello", "http");
    CXFAuthenticator.instance = null;
    for (int x = 0; x < 10; x++) {
        System.gc();
        Authenticator.requestPasswordAuthentication("localhost", add, 8080, "http", "hello", "http");
    }
    Assert.assertEquals(12, traceLengths.size());
    // first trace would be just the raw authenticator above
    int raw = traceLengths.get(0);
    // second trace should still have an Authenticator added
    int one = traceLengths.get(1);
    // after clear and gc's
    int none = traceLengths.get(traceLengths.size() - 1);
    /*stacktrace for one should be different with raw
         * but the stracktrace length in java 8 and java 9-plus
         * isn't identical
         * so previous assertion one < (raw + (20 * 2)
         * isn't applicable for java 9-plus
         */
    Assert.assertTrue(one != raw);
    Assert.assertTrue(one > raw);
    Assert.assertTrue(one > none);
    Assert.assertEquals(raw, none);
}
Also used : ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Example 83 with PasswordAuthentication

use of java.net.PasswordAuthentication in project cxf by apache.

the class CXFAuthenticatorCleanupTest method runCleanupTestStrongRef.

@Test
public void runCleanupTestStrongRef() throws Exception {
    final List<Integer> traceLengths = new ArrayList<>();
    // create a chain of CXFAuthenticators, strongly referenced to prevent cleanups
    Authenticator.setDefault(new Authenticator() {

        protected PasswordAuthentication getPasswordAuthentication() {
            traceLengths.add(Thread.currentThread().getStackTrace().length);
            return super.getPasswordAuthentication();
        }
    });
    InetAddress add = InetAddress.getLocalHost();
    Authenticator.requestPasswordAuthentication("localhost", add, 8080, "http", "hello", "http");
    List<CXFAuthenticator> list = new ArrayList<>();
    for (int x = 0; x < 20; x++) {
        CXFAuthenticator.addAuthenticator();
        list.add(CXFAuthenticator.instance);
        CXFAuthenticator.instance = null;
    }
    Authenticator.requestPasswordAuthentication("localhost", add, 8080, "http", "hello", "http");
    for (int x = 9; x > 0; x -= 2) {
        list.remove(x);
    }
    for (int x = 0; x < 10; x++) {
        System.gc();
        Authenticator.requestPasswordAuthentication("localhost", add, 8080, "http", "hello", "http");
    }
    list.clear();
    for (int x = 0; x < 10; x++) {
        System.gc();
        Authenticator.requestPasswordAuthentication("localhost", add, 8080, "http", "hello", "http");
    }
    Assert.assertEquals(22, traceLengths.size());
    // first trace would be just the raw authenticator above
    int raw = traceLengths.get(0);
    // second would be the trace with ALL the auths
    int all = traceLengths.get(1);
    // after remove of 5 and some gc's
    int some = traceLengths.get(11);
    // after clear and gc's
    int none = traceLengths.get(traceLengths.size() - 1);
    // System.out.println(traceLengths);
    // all should be A LOT above raw
    Assert.assertTrue(all > (raw + 20 * 3));
    Assert.assertTrue(all > raw);
    Assert.assertTrue(all > some);
    Assert.assertTrue(some > none);
    Assert.assertEquals(raw, none);
}
Also used : ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Example 84 with PasswordAuthentication

use of java.net.PasswordAuthentication in project cxf by apache.

the class ReferencingAuthenticator method tryWithInternal.

private PasswordAuthentication tryWithInternal(Authenticator a) throws Exception {
    if (a == null) {
        return null;
    }
    final Field[] fields;
    if (SKIPCHECK) {
        fields = Authenticator.class.getDeclaredFields();
    } else {
        fields = AccessController.doPrivileged((PrivilegedAction<Field[]>) () -> Authenticator.class.getDeclaredFields());
    }
    for (final Field f : fields) {
        if (!Modifier.isStatic(f.getModifiers())) {
            f.setAccessible(true);
            Object o = f.get(this);
            f.set(a, o);
        }
    }
    Method method;
    if (SKIPCHECK) {
        method = Authenticator.class.getDeclaredMethod("getPasswordAuthentication");
        method.setAccessible(true);
    } else {
        method = AccessController.doPrivileged((PrivilegedAction<Method>) () -> {
            try {
                return Authenticator.class.getDeclaredMethod("getPasswordAuthentication");
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        });
        AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
            method.setAccessible(true);
            return null;
        });
    }
    return (PasswordAuthentication) method.invoke(a);
}
Also used : Field(java.lang.reflect.Field) PrivilegedAction(java.security.PrivilegedAction) Method(java.lang.reflect.Method) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 85 with PasswordAuthentication

use of java.net.PasswordAuthentication in project sonarqube by SonarSource.

the class DefaultHttpDownloaderTest method configure_http_proxy_credentials.

@Test
public void configure_http_proxy_credentials() {
    DefaultHttpDownloader.AuthenticatorFacade system = mock(DefaultHttpDownloader.AuthenticatorFacade.class);
    MapSettings settings = new MapSettings();
    settings.setProperty("https.proxyHost", "1.2.3.4");
    settings.setProperty("http.proxyUser", "the_login");
    settings.setProperty("http.proxyPassword", "the_passwd");
    new DefaultHttpDownloader.BaseHttpDownloader(system, settings.asConfig(), null);
    verify(system).setDefaultAuthenticator(argThat(authenticator -> {
        DefaultHttpDownloader.ProxyAuthenticator a = (DefaultHttpDownloader.ProxyAuthenticator) authenticator;
        PasswordAuthentication authentication = a.getPasswordAuthentication();
        return authentication.getUserName().equals("the_login") && new String(authentication.getPassword()).equals("the_passwd");
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) SonarException(org.sonar.api.utils.SonarException) SocketAddress(java.net.SocketAddress) BeforeClass(org.junit.BeforeClass) TestRule(org.junit.rules.TestRule) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Server(org.sonar.api.platform.Server) MapSettings(org.sonar.api.config.internal.MapSettings) ProxySelector(java.net.ProxySelector) BaseMatcher(org.hamcrest.BaseMatcher) PasswordAuthentication(java.net.PasswordAuthentication) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Proxy(java.net.Proxy) SocketTimeoutException(java.net.SocketTimeoutException) Timeout(org.junit.rules.Timeout) Request(org.simpleframework.http.Request) URI(java.net.URI) Description(org.hamcrest.Description) AfterClass(org.junit.AfterClass) Properties(java.util.Properties) Container(org.simpleframework.http.core.Container) CoreProperties(org.sonar.api.CoreProperties) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) InetSocketAddress(java.net.InetSocketAddress) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Mockito.verify(org.mockito.Mockito.verify) Rule(org.junit.Rule) GZIPOutputStream(java.util.zip.GZIPOutputStream) DisableOnDebug(org.junit.rules.DisableOnDebug) Response(org.simpleframework.http.Response) TemporaryFolder(org.junit.rules.TemporaryFolder) InputStream(java.io.InputStream) SocketConnection(org.simpleframework.transport.connect.SocketConnection) Mockito.mock(org.mockito.Mockito.mock) MapSettings(org.sonar.api.config.internal.MapSettings) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Aggregations

PasswordAuthentication (java.net.PasswordAuthentication)108 Authenticator (java.net.Authenticator)52 URL (java.net.URL)27 InetSocketAddress (java.net.InetSocketAddress)22 Proxy (java.net.Proxy)19 Test (org.junit.Test)16 HttpURLConnection (java.net.HttpURLConnection)11 InetAddress (java.net.InetAddress)11 URI (java.net.URI)10 MalformedURLException (java.net.MalformedURLException)9 File (java.io.File)8 IOException (java.io.IOException)8 SocketAddress (java.net.SocketAddress)8 UnknownHostException (java.net.UnknownHostException)6 PrivilegedActionException (java.security.PrivilegedActionException)6 InputStream (java.io.InputStream)5 SocketTimeoutException (java.net.SocketTimeoutException)5 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)5 HttpRetryException (java.net.HttpRetryException)4 ProtocolException (java.net.ProtocolException)4