Search in sources :

Example 31 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project wildfly by wildfly.

the class TransportGuaranteeTestCase method checkGetURL.

/**
     * Check response on given url
     *
     * @param url
     * @param responseSubstring - if null we are checking response code only
     * @return
     * @throws Exception
     */
private boolean checkGetURL(String url, String responseSubstring, String user, String pass) throws Exception {
    log.trace("Checking URL=" + url);
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY), new UsernamePasswordCredentials(user, pass));
    CloseableHttpClient httpClient;
    if (url.startsWith("https")) {
        httpClient = TestHttpClientUtils.getHttpsClient(credentialsProvider);
    } else {
        httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
    }
    HttpGet get = new HttpGet(url);
    HttpResponse hr;
    try {
        try {
            hr = httpClient.execute(get);
        } catch (Exception e) {
            if (// in case substring is defined, rethrow exception so, we can easier analyze the cause
            responseSubstring == null) // in case substring is defined, rethrow exception so, we can easier analyze the cause
            {
                return false;
            } else {
                throw new Exception(e);
            }
        }
        int statusCode = hr.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            log.trace("statusCode not expected. statusCode=" + statusCode + ", URL=" + url);
            return false;
        }
        if (responseSubstring == null) {
            // this indicates that negative test had problems
            log.trace("statusCode==200 on URL=" + url);
            return true;
        }
        String response = EntityUtils.toString(hr.getEntity());
        if (response.indexOf(responseSubstring) != -1) {
            return true;
        } else {
            log.trace("Response doesn't contain expected substring (" + responseSubstring + ")");
            return false;
        }
    } finally {
        if (httpClient != null) {
            httpClient.close();
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpGet(org.apache.http.client.methods.HttpGet) AuthScope(org.apache.http.auth.AuthScope) HttpResponse(org.apache.http.HttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 32 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project wildfly by wildfly.

the class WebSecurityBASICTestCase method makeCall.

@Override
protected void makeCall(String user, String pass, int expectedStatusCode) throws Exception {
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort()), new UsernamePasswordCredentials(user, pass));
    try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build()) {
        HttpGet httpget = new HttpGet(url.toExternalForm() + "secured/");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        StatusLine statusLine = response.getStatusLine();
        if (entity != null) {
            log.trace("Response content length: " + entity.getContentLength());
        }
        assertEquals(expectedStatusCode, statusLine.getStatusCode());
        EntityUtils.consume(entity);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) AuthScope(org.apache.http.auth.AuthScope) HttpResponse(org.apache.http.HttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 33 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project undertow by undertow-io.

the class HttpTunnelingViaConnectTestCase method testConnectViaProxy.

@Test
public void testConnectViaProxy() throws Exception {
    final HttpHost proxy = new HttpHost(DefaultServer.getHostAddress("default"), DefaultServer.getHostPort("default") + 1, "http");
    final HttpHost target = new HttpHost(DefaultServer.getHostAddress("default"), DefaultServer.getHostPort("default"), "http");
    ProxyClient proxyClient = new ProxyClient();
    Socket socket = proxyClient.tunnel(proxy, target, new UsernamePasswordCredentials("a", "b"));
    try {
        Writer out = new OutputStreamWriter(socket.getOutputStream(), HTTP.DEF_CONTENT_CHARSET);
        out.write("GET / HTTP/1.1\r\n");
        out.write("Host: " + target.toHostString() + "\r\n");
        out.write("Connection: close\r\n");
        out.write("\r\n");
        out.flush();
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), HTTP.DEF_CONTENT_CHARSET));
        String line = null;
        boolean found = false;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
            if (line.equals("MyHeader: MyValue")) {
                found = true;
            }
        }
        Assert.assertTrue(found);
    } finally {
        socket.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) HttpHost(org.apache.http.HttpHost) ProxyClient(org.apache.http.impl.client.ProxyClient) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Example 34 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project ThinkAndroid by white-cat.

the class AsyncHttpClient method setBasicAuth.

/**
	 * Sets basic authentication for the request. You should pass in your
	 * AuthScope for security. It should be like this
	 * setBasicAuth("username","password", new
	 * AuthScope("host",port,AuthScope.ANY_REALM))
	 * 
	 * @param username
	 * @param password
	 * @param scope
	 *            - an AuthScope object
	 * 
	 */
public void setBasicAuth(String user, String pass, AuthScope scope) {
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, pass);
    this.httpClient.getCredentialsProvider().setCredentials(scope, credentials);
}
Also used : UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 35 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project intellij-community by JetBrains.

the class GithubConnectionBuilder method setupCredentialsProvider.

@NotNull
private CredentialsProvider setupCredentialsProvider(@NotNull HttpClientBuilder builder) {
    CredentialsProvider provider = new BasicCredentialsProvider();
    // Basic authentication
    GithubAuthData.BasicAuth basicAuth = myAuth.getBasicAuth();
    if (basicAuth != null) {
        AuthScope authScope = getBasicAuthScope();
        provider.setCredentials(authScope, new UsernamePasswordCredentials(basicAuth.getLogin(), basicAuth.getPassword()));
        builder.addInterceptorFirst(new PreemptiveBasicAuthInterceptor(authScope));
    }
    builder.setDefaultCredentialsProvider(provider);
    if (myAuth.isUseProxy()) {
        IdeHttpClientHelpers.ApacheHttpClient4.setProxyCredentialsForUrlIfEnabled(provider, myApiURL);
    }
    return provider;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) AuthScope(org.apache.http.auth.AuthScope) GithubAuthData(org.jetbrains.plugins.github.util.GithubAuthData) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)134 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)70 AuthScope (org.apache.http.auth.AuthScope)63 CredentialsProvider (org.apache.http.client.CredentialsProvider)55 HttpGet (org.apache.http.client.methods.HttpGet)28 HttpHost (org.apache.http.HttpHost)27 IOException (java.io.IOException)21 Test (org.junit.Test)21 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)20 HttpResponse (org.apache.http.HttpResponse)19 Credentials (org.apache.http.auth.Credentials)18 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)16 HttpClient (org.apache.http.client.HttpClient)14 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)14 BasicScheme (org.apache.http.impl.auth.BasicScheme)12 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)11 AuthCache (org.apache.http.client.AuthCache)10 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)10 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)10 BasicHeader (org.apache.http.message.BasicHeader)9