Search in sources :

Example 21 with NTCredentials

use of org.apache.http.auth.NTCredentials in project htmlunit by HtmlUnit.

the class DefaultCredentialsProvider method addNTLMCredentials.

/**
 * Adds NTLM credentials for the specified username/password on the specified host/port.
 * @param username the username for the new credentials; should not include the domain to authenticate with;
 *        for example: <tt>"user"</tt> is correct whereas <tt>"DOMAIN\\user"</tt> is not
 * @param password the password for the new credentials
 * @param host the host to which to the new credentials apply ({@code null} if applicable to any host)
 * @param port the port to which to the new credentials apply (negative if applicable to any port)
 * @param workstation The workstation the authentication request is originating from.
 *        Essentially, the computer name for this machine.
 * @param domain the domain to authenticate within
 */
public void addNTLMCredentials(final String username, final String password, final String host, final int port, final String workstation, final String domain) {
    final AuthScope authscope = new AuthScope(host, port, ANY_REALM, ANY_SCHEME);
    final Credentials credentials = new NTCredentials(username, password, workstation, domain);
    setCredentials(authscope, credentials);
}
Also used : AuthScope(org.apache.http.auth.AuthScope) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) NTCredentials(org.apache.http.auth.NTCredentials) Credentials(org.apache.http.auth.Credentials) NTCredentials(org.apache.http.auth.NTCredentials)

Example 22 with NTCredentials

use of org.apache.http.auth.NTCredentials in project selenium_java by sergueik.

the class ProxyTest method testProxyCredentials.

@Test
public void testProxyCredentials() throws Exception {
    WebDriverManager.config().setProxy("myproxy:8081").setProxyUser("domain\\me").setProxyPass("pass");
    HttpClient wdmClient = new HttpClient();
    Field field = HttpClient.class.getDeclaredField("closeableHttpClient");
    field.setAccessible(true);
    CloseableHttpClient client = (CloseableHttpClient) field.get(wdmClient);
    field = client.getClass().getDeclaredField("credentialsProvider");
    field.setAccessible(true);
    BasicCredentialsProvider provider = (BasicCredentialsProvider) field.get(client);
    NTCredentials ntcreds = (NTCredentials) provider.getCredentials(new AuthScope(ANY_HOST, ANY_PORT, ANY_REALM, NTLM));
    assertThat(ntcreds.getDomain(), equalTo("DOMAIN"));
    assertThat(ntcreds.getUserName(), equalTo("me"));
    assertThat(ntcreds.getPassword(), equalTo("pass"));
    UsernamePasswordCredentials creds = (UsernamePasswordCredentials) provider.getCredentials(new AuthScope(ANY_HOST, ANY_PORT));
    assertThat(creds.getUserName(), equalTo("domain\\me"));
    assertThat(creds.getPassword(), equalTo("pass"));
}
Also used : Field(java.lang.reflect.Field) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClient(io.github.bonigarcia.wdm.HttpClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) AuthScope(org.apache.http.auth.AuthScope) NTCredentials(org.apache.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Example 23 with NTCredentials

use of org.apache.http.auth.NTCredentials 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.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 24 with NTCredentials

use of org.apache.http.auth.NTCredentials in project cs-actions by CloudSlang.

the class CredentialsProviderBuilder method buildCredentialsProvider.

public CredentialsProvider buildCredentialsProvider() {
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    if (!StringUtils.isEmpty(username)) {
        Credentials credentials;
        if (authTypes.contains(AuthTypes.NTLM)) {
            String[] domainAndUsername = getDomainUsername(username);
            credentials = new NTCredentials(domainAndUsername[1], password, host, domainAndUsername[0]);
        } else {
            credentials = new UsernamePasswordCredentials(username, password);
        }
        credentialsProvider.setCredentials(new AuthScope(host, Integer.parseInt(port)), credentials);
    } else if (authTypes.contains(AuthTypes.KERBEROS)) {
        credentialsProvider.setCredentials(new AuthScope(host, Integer.parseInt(port)), new Credentials() {

            @Override
            public Principal getUserPrincipal() {
                return null;
            }

            @Override
            public String getPassword() {
                return null;
            }
        });
    }
    if (!StringUtils.isEmpty(proxyUsername)) {
        int intProxyPort = 8080;
        if (!StringUtils.isEmpty(proxyPort)) {
            intProxyPort = Utils.validatePortNumber(proxyPort);
        }
        credentialsProvider.setCredentials(new AuthScope(proxyHost, intProxyPort), new UsernamePasswordCredentials(proxyUsername, proxyPassword));
    }
    return credentialsProvider;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) AuthScope(org.apache.http.auth.AuthScope) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) NTCredentials(org.apache.http.auth.NTCredentials) Credentials(org.apache.http.auth.Credentials) NTCredentials(org.apache.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 25 with NTCredentials

use of org.apache.http.auth.NTCredentials in project cs-actions by CloudSlang.

the class CredentialsProviderBuilderTest method createNtlmCredentialsProvider.

@Test
public void createNtlmCredentialsProvider() {
    CredentialsProvider credentialsProvider = getCredentialsProvider(AuthSchemes.NTLM);
    Credentials credentials = credentialsProvider.getCredentials(new AuthScope("host", 80));
    assertThat(credentials, instanceOf(NTCredentials.class));
    NTCredentials ntCredentials = (NTCredentials) credentials;
    assertEquals("DOMAIN", ntCredentials.getDomain());
    assertEquals("HOST", ntCredentials.getWorkstation());
    assertEquals("pass", ntCredentials.getPassword());
    Credentials proxyCredentials = credentialsProvider.getCredentials(new AuthScope("proxy", 8080));
    assertThat(proxyCredentials, instanceOf(UsernamePasswordCredentials.class));
    UsernamePasswordCredentials userCredentials = (UsernamePasswordCredentials) proxyCredentials;
    assertEquals("proxyUsername", userCredentials.getUserName());
}
Also used : AuthScope(org.apache.http.auth.AuthScope) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) NTCredentials(org.apache.http.auth.NTCredentials) Credentials(org.apache.http.auth.Credentials) NTCredentials(org.apache.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Aggregations

NTCredentials (org.apache.http.auth.NTCredentials)58 AuthScope (org.apache.http.auth.AuthScope)38 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)30 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)27 CredentialsProvider (org.apache.http.client.CredentialsProvider)24 HttpHost (org.apache.http.HttpHost)20 Credentials (org.apache.http.auth.Credentials)20 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)10 RequestConfig (org.apache.http.client.config.RequestConfig)9 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)9 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)8 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)8 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)5 LaxRedirectStrategy (org.apache.http.impl.client.LaxRedirectStrategy)5 HttpRequestExecutor (org.apache.http.protocol.HttpRequestExecutor)5 AuthSchemeProvider (org.apache.http.auth.AuthSchemeProvider)4 AuthenticationException (org.apache.http.auth.AuthenticationException)4 InvalidCredentialsException (org.apache.http.auth.InvalidCredentialsException)4 BufferedHeader (org.apache.http.message.BufferedHeader)4