use of org.apache.hc.client5.http.auth.Credentials in project mercury by yellow013.
the class ProxyTunnelDemo method main.
public static final void main(final String[] args) throws Exception {
final ProxyClient proxyClient = new ProxyClient();
final HttpHost target = new HttpHost("www.yahoo.com", 80);
final HttpHost proxy = new HttpHost("localhost", 8888);
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "pwd".toCharArray());
try (final Socket socket = proxyClient.tunnel(proxy, target, credentials)) {
final Writer out = new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.ISO_8859_1);
out.write("GET / HTTP/1.1\r\n");
out.write("Host: " + target.toHostString() + "\r\n");
out.write("Agent: whatever\r\n");
out.write("Connection: close\r\n");
out.write("\r\n");
out.flush();
final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.ISO_8859_1));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
}
}
use of org.apache.hc.client5.http.auth.Credentials in project webdrivermanager by bonigarcia.
the class HttpClient method createBasicCredentialsProvider.
private final Optional<BasicCredentialsProvider> createBasicCredentialsProvider(String proxy, String proxyUser, String proxyPass, HttpHost proxyHost) throws MalformedURLException, UnsupportedEncodingException {
Optional<URL> proxyUrl = determineProxyUrl(proxy);
if (!proxyUrl.isPresent()) {
return empty();
}
String username = null;
String password = null;
// apply env value
String userInfo = proxyUrl.get().getUserInfo();
if (userInfo != null) {
StringTokenizer st = new StringTokenizer(userInfo, ":");
username = st.hasMoreTokens() ? decode(st.nextToken(), UTF_8.name()) : null;
password = st.hasMoreTokens() ? decode(st.nextToken(), UTF_8.name()) : null;
}
String envProxyUser = getenv("HTTPS_PROXY_USER");
String envProxyPass = getenv("HTTPS_PROXY_PASS");
username = (envProxyUser != null) ? envProxyUser : username;
password = (envProxyPass != null) ? envProxyPass : password;
// apply option value
username = isNullOrEmpty(proxyUser) ? username : proxyUser;
password = isNullOrEmpty(proxyPass) ? password : proxyPass;
if (username == null) {
return empty();
}
String ntlmUsername = username;
String ntlmDomain = null;
int index = username.indexOf('\\');
if (index > 0) {
ntlmDomain = username.substring(0, index);
ntlmUsername = username.substring(index + 1);
}
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
AuthScope authScope = new AuthScope(proxyHost, null, NTLM);
char[] passwd = (password == null) ? new char[0] : password.toCharArray();
Credentials creds = new NTCredentials(ntlmUsername, passwd, getWorkstation(), ntlmDomain);
credentialsProvider.setCredentials(authScope, creds);
authScope = new AuthScope(proxyHost.getHostName(), proxyHost.getPort());
creds = new UsernamePasswordCredentials(username, passwd);
credentialsProvider.setCredentials(authScope, creds);
return Optional.of(credentialsProvider);
}
use of org.apache.hc.client5.http.auth.Credentials in project ecf by eclipse.
the class HttpClientProxyCredentialProvider method fixNTCredentials.
public static Credentials fixNTCredentials(Credentials credentials) {
if (credentials == null) {
return null;
}
if (credentials instanceof NTCredentials) {
NTCredentials ntCreds = (NTCredentials) credentials;
String userName = ntCreds.getUserName();
String domainUser = getNTLMUserName(userName);
if (ntCreds.getDomain() == null || domainUser != userName) {
String domain = getNTLMDomainName(userName);
String workstation = getNTLMWorkstation();
return new NTCredentials(domainUser, ntCreds.getPassword(), workstation, domain);
}
} else if (credentials instanceof UsernamePasswordCredentials) {
UsernamePasswordCredentials basicCredentials = (UsernamePasswordCredentials) credentials;
return createNTLMCredentials(basicCredentials.getUserName(), basicCredentials.getPassword());
}
return credentials;
}
use of org.apache.hc.client5.http.auth.Credentials in project ecf by eclipse.
the class HttpClientRetrieveFileTransfer method getFileRequestCredentials.
/**
* @return Credentials file request credentials
* @throws UnsupportedCallbackException if some problem
* @throws IOException if some problem
* @since 5.0
*/
protected Credentials getFileRequestCredentials() throws UnsupportedCallbackException, IOException {
if (connectContext == null)
return null;
final CallbackHandler callbackHandler = connectContext.getCallbackHandler();
if (callbackHandler == null)
return null;
final NameCallback usernameCallback = new NameCallback(USERNAME_PREFIX);
final ObjectCallback passwordCallback = new ObjectCallback();
callbackHandler.handle(new Callback[] { usernameCallback, passwordCallback });
username = usernameCallback.getName();
password = (String) passwordCallback.getObject();
return new UsernamePasswordCredentials(username, password == null ? null : password.toCharArray());
}
use of org.apache.hc.client5.http.auth.Credentials in project geo-platform by geosdi.
the class AbstractSecurityConnector method bindCredentials.
/**
* Bind Credentials for {@link CredentialsStore} class
*
* @param credentialsStore
* @param targetURI
*/
protected void bindCredentials(@Nonnull(when = NEVER) CredentialsStore credentialsStore, @Nonnull(when = NEVER) URI targetURI) {
checkArgument(credentialsStore != null, "The Parameter credentialsProvider must not be null.");
checkArgument(targetURI != null, "The Parameter targetURI must not be null.");
if (this.authScope == null) {
this.authScope = new AuthScope(targetURI.getHost(), targetURI.getPort());
}
credentialsStore.setCredentials(authScope, this.usernamePasswordCredentials);
}
Aggregations