Search in sources :

Example 11 with Credentials

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);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ProxyClient(org.apache.hc.client5.http.impl.classic.ProxyClient) HttpHost(org.apache.hc.core5.http.HttpHost) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 12 with Credentials

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);
}
Also used : StringTokenizer(java.util.StringTokenizer) BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) AuthScope(org.apache.hc.client5.http.auth.AuthScope) URL(java.net.URL) Credentials(org.apache.hc.client5.http.auth.Credentials) NTCredentials(org.apache.hc.client5.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials) NTCredentials(org.apache.hc.client5.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 13 with Credentials

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;
}
Also used : NTCredentials(org.apache.hc.client5.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 14 with 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());
}
Also used : CallbackHandler(org.eclipse.ecf.core.security.CallbackHandler) NameCallback(org.eclipse.ecf.core.security.NameCallback) ObjectCallback(org.eclipse.ecf.core.security.ObjectCallback) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 15 with Credentials

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);
}
Also used : AuthScope(org.apache.hc.client5.http.auth.AuthScope)

Aggregations

UsernamePasswordCredentials (org.apache.hc.client5.http.auth.UsernamePasswordCredentials)13 AuthScope (org.apache.hc.client5.http.auth.AuthScope)8 Credentials (org.apache.hc.client5.http.auth.Credentials)8 BasicCredentialsProvider (org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider)6 IOException (java.io.IOException)5 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)5 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)5 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)5 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)5 NTCredentials (org.apache.hc.client5.http.auth.NTCredentials)4 HttpHost (org.apache.hc.core5.http.HttpHost)4 CredentialsStore (org.apache.hc.client5.http.auth.CredentialsStore)3 HttpEntity (org.apache.hc.core5.http.HttpEntity)3 AltiriaGwException (com.altiria.app.exception.AltiriaGwException)2 ConnectionException (com.altiria.app.exception.ConnectionException)2 GeneralAltiriaException (com.altiria.app.exception.GeneralAltiriaException)2 JsonException (com.altiria.app.exception.JsonException)2 JsonObject (com.google.gson.JsonObject)2 TypeToken (com.google.gson.reflect.TypeToken)2 SocketTimeoutException (java.net.SocketTimeoutException)2