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 phonegap-facebook-plugin by Wizcorp.
the class HttpAuthenticator method getCredentials.
/**
* Returns the authorization credentials that may satisfy the challenge.
* Returns null if a challenge header was not provided or if credentials
* were not available.
*/
private static String getCredentials(RawHeaders responseHeaders, String challengeHeader, Proxy proxy, URL url) throws IOException {
List<Challenge> challenges = parseChallenges(responseHeaders, challengeHeader);
if (challenges.isEmpty()) {
return null;
}
for (Challenge challenge : challenges) {
// Use the global authenticator to get the password.
PasswordAuthentication auth;
if (responseHeaders.getResponseCode() == HTTP_PROXY_AUTH) {
InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
auth = Authenticator.requestPasswordAuthentication(proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(), url.getProtocol(), challenge.realm, challenge.scheme, url, Authenticator.RequestorType.PROXY);
} else {
auth = Authenticator.requestPasswordAuthentication(url.getHost(), getConnectToInetAddress(proxy, url), url.getPort(), url.getProtocol(), challenge.realm, challenge.scheme, url, Authenticator.RequestorType.SERVER);
}
if (auth == null) {
continue;
}
// Use base64 to encode the username and password.
String usernameAndPassword = auth.getUserName() + ":" + new String(auth.getPassword());
byte[] bytes = usernameAndPassword.getBytes("ISO-8859-1");
String encoded = Base64.encode(bytes);
return challenge.scheme + " " + encoded;
}
return null;
}
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 cordova-android-chromeview by thedracle.
the class HttpAuthenticator method getCredentials.
/**
* Returns the authorization credentials that may satisfy the challenge.
* Returns null if a challenge header was not provided or if credentials
* were not available.
*/
private static String getCredentials(RawHeaders responseHeaders, String challengeHeader, Proxy proxy, URL url) throws IOException {
List<Challenge> challenges = parseChallenges(responseHeaders, challengeHeader);
if (challenges.isEmpty()) {
return null;
}
for (Challenge challenge : challenges) {
// Use the global authenticator to get the password.
PasswordAuthentication auth;
if (responseHeaders.getResponseCode() == HTTP_PROXY_AUTH) {
InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
auth = Authenticator.requestPasswordAuthentication(proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(), url.getProtocol(), challenge.realm, challenge.scheme, url, Authenticator.RequestorType.PROXY);
} else {
auth = Authenticator.requestPasswordAuthentication(url.getHost(), getConnectToInetAddress(proxy, url), url.getPort(), url.getProtocol(), challenge.realm, challenge.scheme, url, Authenticator.RequestorType.SERVER);
}
if (auth == null) {
continue;
}
// Use base64 to encode the username and password.
String usernameAndPassword = auth.getUserName() + ":" + new String(auth.getPassword());
byte[] bytes = usernameAndPassword.getBytes("ISO-8859-1");
String encoded = Base64.encode(bytes);
return challenge.scheme + " " + encoded;
}
return null;
}
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