use of java.net.PasswordAuthentication in project buck by facebook.
the class HttpDownloaderTest method shouldReturnFalseIfTryingBasicAuthOverHttp.
@Test
public void shouldReturnFalseIfTryingBasicAuthOverHttp() throws IOException, URISyntaxException {
final HttpURLConnection connection = EasyMock.createNiceMock(HttpURLConnection.class);
EasyMock.replay(connection);
HttpDownloader downloader = getDownloader(connection);
PasswordAuthentication credentials = new PasswordAuthentication("not", "used".toCharArray());
boolean result = downloader.fetch(eventBus, new URI("http://example.com"), Optional.of(credentials), neverUsed);
assertFalse(result);
EasyMock.verify(connection);
}
use of java.net.PasswordAuthentication in project buck by facebook.
the class HttpDownloaderTest method shouldAddAuthenticationHeader.
@Test
public void shouldAddAuthenticationHeader() throws IOException, URISyntaxException {
Capture<String> capturedAuth = EasyMock.newCapture();
final HttpURLConnection connection = EasyMock.createNiceMock(HttpsURLConnection.class);
EasyMock.expect(connection.getResponseCode()).andStubReturn(HTTP_FORBIDDEN);
connection.addRequestProperty(eq("Authorization"), capture(capturedAuth));
EasyMock.expectLastCall();
EasyMock.replay(connection);
HttpDownloader downloader = getDownloader(connection);
PasswordAuthentication credentials = new PasswordAuthentication("foo", "bar".toCharArray());
boolean result = downloader.fetch(eventBus, new URI("https://example.com"), Optional.of(credentials), neverUsed);
assertFalse(result);
EasyMock.verify(connection);
Matcher m = Pattern.compile("^Basic (.*)$").matcher(capturedAuth.getValue());
assertTrue(m.matches());
assertEquals("foo:bar", new String(BaseEncoding.base64().decode(m.group(1)), StandardCharsets.UTF_8));
}
use of java.net.PasswordAuthentication in project okhttp by square.
the class JavaNetAuthenticator method authenticate.
@Override
public Request authenticate(Route route, Response response) throws IOException {
List<Challenge> challenges = response.challenges();
Request request = response.request();
HttpUrl url = request.url();
boolean proxyAuthorization = response.code() == 407;
Proxy proxy = route.proxy();
for (int i = 0, size = challenges.size(); i < size; i++) {
Challenge challenge = challenges.get(i);
if (!"Basic".equalsIgnoreCase(challenge.scheme()))
continue;
PasswordAuthentication auth;
if (proxyAuthorization) {
InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
auth = java.net.Authenticator.requestPasswordAuthentication(proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(), url.scheme(), challenge.realm(), challenge.scheme(), url.url(), RequestorType.PROXY);
} else {
auth = java.net.Authenticator.requestPasswordAuthentication(url.host(), getConnectToInetAddress(proxy, url), url.port(), url.scheme(), challenge.realm(), challenge.scheme(), url.url(), RequestorType.SERVER);
}
if (auth != null) {
String credential = Credentials.basic(auth.getUserName(), new String(auth.getPassword()));
return request.newBuilder().header(proxyAuthorization ? "Proxy-Authorization" : "Authorization", credential).build();
}
}
// No challenges were satisfied!
return null;
}
use of java.net.PasswordAuthentication in project robovm by robovm.
the class OldPasswordAuthenticationTest method test_ConstructorLjava_lang_String$C.
public void test_ConstructorLjava_lang_String$C() {
String name = "name";
char[] password = "hunter2".toCharArray();
try {
new PasswordAuthentication(name, null);
fail("NullPointerException was not thrown.");
} catch (NullPointerException npe) {
//expected
}
PasswordAuthentication pa = new PasswordAuthentication(null, password);
assertNull(pa.getUserName());
assertEquals(password.length, pa.getPassword().length);
}
use of java.net.PasswordAuthentication in project XobotOS by xamarin.
the class HttpURLConnectionImpl method getAuthorizationCredentials.
/**
* Returns the authorization credentials on the base of provided challenge.
*/
private String getAuthorizationCredentials(String challenge) throws IOException {
int idx = challenge.indexOf(" ");
if (idx == -1) {
return null;
}
String scheme = challenge.substring(0, idx);
int realm = challenge.indexOf("realm=\"") + 7;
String prompt = null;
if (realm != -1) {
int end = challenge.indexOf('"', realm);
if (end != -1) {
prompt = challenge.substring(realm, end);
}
}
// use the global authenticator to get the password
PasswordAuthentication pa = Authenticator.requestPasswordAuthentication(getConnectToInetAddress(), getConnectToPort(), url.getProtocol(), prompt, scheme);
if (pa == null) {
return null;
}
// base64 encode the username and password
String usernameAndPassword = pa.getUserName() + ":" + new String(pa.getPassword());
byte[] bytes = usernameAndPassword.getBytes(Charsets.ISO_8859_1);
String encoded = Base64.encode(bytes);
return scheme + " " + encoded;
}
Aggregations