Search in sources :

Example 86 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project vcell by virtualcell.

the class VCellApiClient method authenticate.

public AccessTokenRepresentation authenticate(String userid, String password, boolean alreadyDigested) throws ClientProtocolException, IOException {
    // hash the password
    String digestedPassword = (alreadyDigested) ? (password) : createdDigestPassword(password);
    HttpGet httpget = new HttpGet("https://" + httpHost.getHostName() + ":" + httpHost.getPort() + "/access_token?user_id=" + userid + "&user_password=" + digestedPassword + "&client_id=" + clientID);
    if (lg.isLoggable(Level.INFO)) {
        lg.info("Executing request to retrieve access_token " + httpget.getRequestLine());
    }
    String responseBody = httpclient.execute(httpget, responseHandler);
    String accessTokenJson = responseBody;
    if (lg.isLoggable(Level.INFO)) {
        lg.info("returned: " + accessTokenJson);
    }
    Gson gson = new Gson();
    AccessTokenRepresentation accessTokenRep = gson.fromJson(accessTokenJson, AccessTokenRepresentation.class);
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(httpHost.getHostName(), httpHost.getPort()), new UsernamePasswordCredentials("access_token", accessTokenRep.getToken()));
    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(httpHost, basicAuth);
    // Add AuthCache to the execution context
    httpClientContext = HttpClientContext.create();
    httpClientContext.setCredentialsProvider(credsProvider);
    httpClientContext.setAuthCache(authCache);
    return accessTokenRep;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpGet(org.apache.http.client.methods.HttpGet) AuthScope(org.apache.http.auth.AuthScope) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) Gson(com.google.gson.Gson) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) AccessTokenRepresentation(org.vcell.api.common.AccessTokenRepresentation) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 87 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project Lucee by lucee.

the class HTTPEngine4Impl method setProxy.

public static void setProxy(HttpClientBuilder builder, HttpUriRequest request, ProxyData proxy) {
    // set Proxy
    if (ProxyDataImpl.isValid(proxy)) {
        HttpHost host = new HttpHost(proxy.getServer(), proxy.getPort() == -1 ? 80 : proxy.getPort());
        builder.setProxy(host);
        // username/password
        if (!StringUtil.isEmpty(proxy.getUsername())) {
            CredentialsProvider cp = new BasicCredentialsProvider();
            builder.setDefaultCredentialsProvider(cp);
            cp.setCredentials(new AuthScope(proxy.getServer(), proxy.getPort()), new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()));
        }
    }
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) 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)

Example 88 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project CzechIdMng by bcvsolutions.

the class TestAppAuthenticationFilter method getSigningKey.

private Optional<String> getSigningKey() {
    CredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("idm", "");
    provider.setCredentials(AuthScope.ANY, credentials);
    HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(1 * 1000).setSocketTimeout(1 * 1000).build()).build();
    try {
        HttpResponse response = client.execute(new HttpGet("http://localhost:9080/oauth/token_key"));
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("Status: " + statusCode);
        if (statusCode < 400) {
            ObjectMapper om = new ObjectMapper();
            KeyAlg key = om.readValue(response.getEntity().getContent(), KeyAlg.class);
            return Optional.of(key.getValue());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return Optional.empty();
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ValidationException(javax.validation.ValidationException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 89 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project janusgraph by JanusGraph.

the class RestClientSetupTest method testHttpBasicAuthConfiguration.

@Test
public void testHttpBasicAuthConfiguration() throws Exception {
    // testing that the appropriate values are passed to the client builder via credentials provider
    final String testRealm = "testRealm";
    final String testUser = "testUser";
    final String testPassword = "testPassword";
    final CredentialsProvider cp = basicAuthTestBase(ImmutableMap.<String, String>builder().build(), testRealm, testUser, testPassword);
    final Credentials credentials = cp.getCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, testRealm));
    assertNotNull(credentials);
    assertEquals(testUser, credentials.getUserPrincipal().getName());
    assertEquals(testPassword, credentials.getPassword());
}
Also used : AuthScope(org.apache.http.auth.AuthScope) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 90 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project janusgraph by JanusGraph.

the class RestClientSetupTest method basicAuthTestBase.

private CredentialsProvider basicAuthTestBase(final Map<String, String> extraConfigValues, final String realm, final String username, final String password) throws Exception {
    final HttpClientConfigCallback hccc = authTestBase(ImmutableMap.<String, String>builder().put("index." + INDEX_NAME + ".elasticsearch.interface", "REST_CLIENT").put("index." + INDEX_NAME + ".elasticsearch.http.auth.type", HttpAuthTypes.BASIC.toString()).put("index." + INDEX_NAME + ".elasticsearch.http.auth.basic.username", username).put("index." + INDEX_NAME + ".elasticsearch.http.auth.basic.password", password).put("index." + INDEX_NAME + ".elasticsearch.http.auth.basic.realm", realm).putAll(extraConfigValues).build());
    final HttpAsyncClientBuilder hacb = PowerMockito.mock(HttpAsyncClientBuilder.class);
    doReturn(hacb).when(hacb).setDefaultCredentialsProvider(anyObject());
    hccc.customizeHttpClient(hacb);
    final ArgumentCaptor<BasicCredentialsProvider> cpCaptor = ArgumentCaptor.forClass(BasicCredentialsProvider.class);
    verify(hacb).setDefaultCredentialsProvider(cpCaptor.capture());
    final CredentialsProvider cp = cpCaptor.getValue();
    assertNotNull(cp);
    return cp;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClientConfigCallback(org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder)

Aggregations

CredentialsProvider (org.apache.http.client.CredentialsProvider)92 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)65 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)57 AuthScope (org.apache.http.auth.AuthScope)51 HttpHost (org.apache.http.HttpHost)28 HttpGet (org.apache.http.client.methods.HttpGet)19 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)17 Test (org.junit.Test)17 HttpResponse (org.apache.http.HttpResponse)16 Credentials (org.apache.http.auth.Credentials)16 IOException (java.io.IOException)13 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)12 BasicScheme (org.apache.http.impl.auth.BasicScheme)12 Header (org.apache.http.Header)11 HttpRequest (org.apache.http.HttpRequest)10 AuthCache (org.apache.http.client.AuthCache)10 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)10 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)10 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)9 HttpEntity (org.apache.http.HttpEntity)8