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;
}
}
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);
}
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);
}
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);
}
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");
}));
}
Aggregations