Search in sources :

Example 61 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project tutorials by eugenp.

the class HttpClientAuthLiveTest method provider.

// UTILS
private CredentialsProvider provider() {
    final CredentialsProvider provider = new BasicCredentialsProvider();
    final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
    provider.setCredentials(AuthScope.ANY, credentials);
    return provider;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 62 with UsernamePasswordCredentials

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

the class RestConnection method login.

public boolean login(String username, String password) throws IOException, URISyntaxException {
    URI uri = getContextURI().path("auth").path(username).queryParam("password", password).build();
    // provider credentials
    provider.setCredentials(new AuthScope(HOST, PORT), new UsernamePasswordCredentials(username, password));
    provider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username, password));
    int code = -1;
    HttpGet httpget = new HttpGet(uri);
    try (CloseableHttpResponse response = httpclient.execute(httpget)) {
        Header header = response.getFirstHeader(RestSecurityHelper.SEC_TOKEN);
        if (header != null) {
            securityToken = header.getValue();
        }
        HttpEntity entity = response.getEntity();
        code = response.getStatusLine().getStatusCode();
        EntityUtils.consume(entity);
    } catch (IOException e) {
        log.error("", e);
    }
    return code == 200;
}
Also used : Header(org.apache.http.Header) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) AuthScope(org.apache.http.auth.AuthScope) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) URI(java.net.URI) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 63 with UsernamePasswordCredentials

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

the class MdsRestBundleIT method setUp.

@Before
public void setUp() throws IOException {
    if (getDataService() == null || getDataServiceForFilteredEntity() == null) {
        clearEntities();
        prepareEntity();
        prepareFilteredEntity();
        SchemaHolder schemaHolder = entityService.getSchema();
        jarGeneratorService.regenerateMdsDataBundle(schemaHolder);
    }
    getDataService().deleteAll();
    getDataServiceForFilteredEntity().deleteAll();
    if (!userService.hasActiveMotechAdmin()) {
        userService.registerMotechAdmin("motech", "motech", "motech@motechsuite.org", Locale.ENGLISH);
    }
    getHttpClient().getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthPolicy.BASIC), new UsernamePasswordCredentials("motech", "motech"));
}
Also used : AuthScope(org.apache.http.auth.AuthScope) SchemaHolder(org.motechproject.mds.dto.SchemaHolder) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Before(org.junit.Before)

Example 64 with UsernamePasswordCredentials

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

the class StandAloneTrustedHttpClientImpl method manuallyHandleDigestAuthentication.

/**
 * Handles the necessary handshake for digest authenticaion in the case where it isn't a GET operation.
 *
 * @param httpUriRequest
 *         The request location to get the digest authentication for.
 * @param httpClient
 *         The client to send the request through.
 * @throws org.opencastproject.security.api.TrustedHttpClientException
 *         Thrown if the client cannot be shutdown.
 */
private void manuallyHandleDigestAuthentication(HttpUriRequest httpUriRequest, HttpClient httpClient) throws TrustedHttpClientException {
    HttpRequestBase digestRequest;
    try {
        digestRequest = (HttpRequestBase) httpUriRequest.getClass().newInstance();
    } catch (Exception e) {
        throw new IllegalStateException("Can not create a new " + httpUriRequest.getClass().getName());
    }
    digestRequest.setURI(httpUriRequest.getURI());
    digestRequest.setHeader(REQUESTED_AUTH_HEADER, DIGEST_AUTH);
    String[] realmAndNonce = getRealmAndNonce(digestRequest);
    if (realmAndNonce != null) {
        // Set the user/pass
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, pass);
        // Set up the digest authentication with the required values
        DigestScheme digestAuth = new DigestScheme();
        digestAuth.overrideParamter("realm", realmAndNonce[0]);
        digestAuth.overrideParamter("nonce", realmAndNonce[1]);
        // Add the authentication header
        try {
            httpUriRequest.setHeader(digestAuth.authenticate(creds, httpUriRequest));
        } catch (Exception e) {
            // close the http connection(s)
            httpClient.getConnectionManager().shutdown();
            throw new TrustedHttpClientException(e);
        }
    }
}
Also used : DigestScheme(org.apache.http.impl.auth.DigestScheme) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 65 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project connect-sdk-java by Ingenico-ePayments.

the class DefaultConnectionTest method assertProxy.

@SuppressWarnings("resource")
private static void assertProxy(DefaultConnection connection, ProxyConfiguration proxyConfiguration) {
    CloseableHttpClient httpClient = ReflectionUtil.getField(connection, "httpClient", CloseableHttpClient.class);
    DefaultProxyRoutePlanner routePlanner = ReflectionUtil.getField(httpClient, "routePlanner", DefaultProxyRoutePlanner.class);
    HttpHost proxy = ReflectionUtil.getField(routePlanner, "proxy", HttpHost.class);
    Assert.assertEquals(proxyConfiguration.getScheme(), proxy.getSchemeName());
    Assert.assertEquals(proxyConfiguration.getPort(), proxy.getPort());
    BasicCredentialsProvider credentialsProvider = ReflectionUtil.getField(httpClient, "credentialsProvider", BasicCredentialsProvider.class);
    AuthScope authScope = new AuthScope(proxy);
    Credentials credentials = credentialsProvider.getCredentials(authScope);
    if (proxyConfiguration.getUsername() != null) {
        Assert.assertTrue(credentials instanceof UsernamePasswordCredentials);
        UsernamePasswordCredentials usernamePasswordCredentials = (UsernamePasswordCredentials) credentials;
        Assert.assertEquals(proxyConfiguration.getUsername(), usernamePasswordCredentials.getUserName());
        Assert.assertEquals(proxyConfiguration.getPassword(), usernamePasswordCredentials.getPassword());
    } else {
        Assert.assertNull(credentials);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) DefaultProxyRoutePlanner(org.apache.http.impl.conn.DefaultProxyRoutePlanner) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)337 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)212 CredentialsProvider (org.apache.http.client.CredentialsProvider)189 AuthScope (org.apache.http.auth.AuthScope)163 HttpHost (org.apache.http.HttpHost)95 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)78 HttpGet (org.apache.http.client.methods.HttpGet)73 IOException (java.io.IOException)54 BasicScheme (org.apache.http.impl.auth.BasicScheme)53 Test (org.junit.Test)53 HttpResponse (org.apache.http.HttpResponse)52 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)46 Credentials (org.apache.http.auth.Credentials)44 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)43 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)37 URI (java.net.URI)34 AuthCache (org.apache.http.client.AuthCache)32 HttpClient (org.apache.http.client.HttpClient)31 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)31 HttpPost (org.apache.http.client.methods.HttpPost)29