Example usage for org.apache.http.client CredentialsProvider getCredentials

List of usage examples for org.apache.http.client CredentialsProvider getCredentials

Introduction

In this page you can find the example usage for org.apache.http.client CredentialsProvider getCredentials.

Prototype

Credentials getCredentials(AuthScope authscope);

Source Link

Document

Get the Credentials credentials for the given authentication scope.

Usage

From source file:org.apache.camel.component.http4.PreemptiveAuthInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
    // If no auth scheme avaialble yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(HttpClientContext.CREDS_PROVIDER);
        if (authScheme != null) {
            Credentials creds = credsProvider.getCredentials(AuthScope.ANY);
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }/*from w  ww .jav  a2s  . c  om*/
            authState.update(authScheme, creds);
        }
    }

}

From source file:org.springframework.cloud.dataflow.rest.util.HttpClientConfigurerTests.java

/**
 * Test ensuring that the {@link AuthScope} is set for the target host.
 *///from  w  w w .ja v a  2 s. co  m
@Test
public void testThatHttpClientWithProxyIsCreatedAndHasCorrectCredentialsProviders() throws Exception {
    final URI targetHost = new URI("http://test.com");
    final HttpClientConfigurer builder = HttpClientConfigurer.create(targetHost);
    builder.basicAuthCredentials("foo", "password");
    builder.withProxyCredentials(URI.create("https://spring.io"), null, null);

    final Field credentialsProviderField = ReflectionUtils.findField(HttpClientConfigurer.class,
            "credentialsProvider");
    ReflectionUtils.makeAccessible(credentialsProviderField);
    CredentialsProvider credentialsProvider = (CredentialsProvider) credentialsProviderField.get(builder);
    Assert.assertNotNull(credentialsProvider.getCredentials(new AuthScope("test.com", 80)));
    Assert.assertNull(credentialsProvider.getCredentials(new AuthScope("spring.io", 80)));
}

From source file:org.springframework.cloud.dataflow.rest.util.HttpClientConfigurerTests.java

/**
 * Test ensuring that the {@link AuthScope} is set for the target host and the proxy server.
 *///  ww w .  j  av a  2 s  . co m
@Test
public void testThatHttpClientWithProxyIsCreatedAndHasCorrectCredentialsProviders2() throws Exception {
    final URI targetHost = new URI("http://test.com");
    final HttpClientConfigurer builder = HttpClientConfigurer.create(targetHost);
    builder.basicAuthCredentials("foo", "password");
    builder.withProxyCredentials(URI.create("https://spring.io"), "proxyuser", "proxypassword");

    final Field credentialsProviderField = ReflectionUtils.findField(HttpClientConfigurer.class,
            "credentialsProvider");
    ReflectionUtils.makeAccessible(credentialsProviderField);
    CredentialsProvider credentialsProvider = (CredentialsProvider) credentialsProviderField.get(builder);
    Assert.assertNotNull(credentialsProvider.getCredentials(new AuthScope("test.com", 80)));
    Assert.assertNotNull(credentialsProvider.getCredentials(new AuthScope("spring.io", 80)));
}

From source file:org.janusgraph.diskstorage.es.rest.util.BasicAuthHttpClientConfigCallbackTest.java

@Test
public void testSetDefaultCredentialsProviderWithRealm() throws Exception {

    final CredentialsProvider cp = basicAuthTestBase(HTTP_REALM);

    // expected: will match any host in that specific realm
    final Credentials credentialsForRealm1 = cp.getCredentials(new AuthScope("dummyhost1", 1234, HTTP_REALM));
    assertEquals(HTTP_USER, credentialsForRealm1.getUserPrincipal().getName());
    assertEquals(HTTP_PASSWORD, credentialsForRealm1.getPassword());

    // ...but not in any other realms
    final Credentials credentialsForRealm3 = cp
            .getCredentials(new AuthScope("dummyhost1", 1234, "Not_" + HTTP_REALM));
    assertNull(credentialsForRealm3);//from  ww w . jav  a2  s .  c  om
}

From source file:com.microsoft.exchange.impl.http.PreemptiveAuthInterceptor.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (authState.getAuthScheme() == null) {
        AuthScheme preemptiveAuthScheme = (AuthScheme) context.getAttribute(PREEMPTIVE_AUTH);
        if (preemptiveAuthScheme != null) {
            CredentialsProvider credsProvider = (CredentialsProvider) context
                    .getAttribute(ClientContext.CREDS_PROVIDER);
            Credentials creds = credsProvider.getCredentials(authScope);
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }/*  ww w  .  j  av  a 2s. c o m*/
            authState.setAuthScheme(preemptiveAuthScheme);
            authState.setCredentials(creds);
            if (log.isTraceEnabled()) {
                log.trace("successfully set credentials " + creds + " and authScheme " + preemptiveAuthScheme
                        + " for request " + request);
            }
        } else {
            log.warn(PREEMPTIVE_AUTH
                    + " authScheme not found in context; make sure you are using the CustomHttpComponentsMessageSender and the preemptiveAuthEnabled property is set to true");
        }
    } else {
        log.warn("context's authState attribute (" + authState + ") has non-null AuthScheme for request "
                + request);
    }
}

From source file:com.xebialabs.deployit.ci.server.PreemptiveAuthenticationInterceptor.java

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    if (request.getFirstHeader("Authorization") == null) {
        LOGGER.trace("No 'Authorization' header found for request: {}", request.getRequestLine());
        HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        CredentialsProvider credentialsProvider = (CredentialsProvider) context
                .getAttribute(ClientContext.CREDS_PROVIDER);
        if (credentialsProvider != null) {
            Credentials credentials = credentialsProvider
                    .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (credentials != null) {
                request.setHeader(new BasicScheme().authenticate(credentials, request, context));
                LOGGER.trace("Set 'Authorization' header {} for request: {}", credentials.getUserPrincipal(),
                        request.getRequestLine());
            }/*from   w ww  .java2s.  c  om*/
        }
    }
}

From source file:org.jasig.schedassist.impl.caldav.PreemptiveAuthInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute(PREEMPTIVE_AUTH);
        CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(ClientContext.CREDS_PROVIDER);
        if (authScheme != null) {
            Credentials creds = credsProvider.getCredentials(caldavAdminAuthScope);
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }/* ww w . j  a va2  s.co  m*/
            authState.setAuthScheme(authScheme);
            authState.setCredentials(creds);
            if (log.isTraceEnabled()) {
                log.trace("successfully set credentials " + creds + " and authScheme " + authScheme
                        + " for request " + request);
            }
        } else {
            log.warn(PREEMPTIVE_AUTH
                    + " authScheme not found in context, failed to set scheme and credentials for " + request);
        }
    } else {
        log.warn("context's authState attribute (" + authState + ") has non-null AuthScheme for request "
                + request);
    }

}

From source file:com.microsoft.alm.plugin.context.ServerContextTest.java

@Test
public void getClientConfig() {
    AuthenticationInfo info = new AuthenticationInfo("user1", "pass", "server1", "4display");
    final ClientConfig config = ServerContext.getClientConfig(ServerContext.Type.TFS, info, false);

    final Map<String, Object> properties = config.getProperties();
    Assert.assertEquals(3, properties.size());

    Assert.assertEquals(false, properties.get(ApacheClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION));
    Assert.assertEquals(RequestEntityProcessing.BUFFERED,
            properties.get(ClientProperties.REQUEST_ENTITY_PROCESSING));

    final CredentialsProvider cp = (CredentialsProvider) properties
            .get(ApacheClientProperties.CREDENTIALS_PROVIDER);
    final Credentials credentials = cp.getCredentials(AuthScope.ANY);
    Assert.assertEquals(info.getPassword(), credentials.getPassword());
    Assert.assertEquals(info.getUserName(), credentials.getUserPrincipal().getName());

    // Make sure Fiddler properties get set if property is on
    final ClientConfig config2 = ServerContext.getClientConfig(ServerContext.Type.TFS, info, true);
    final Map<String, Object> properties2 = config2.getProperties();
    //proxy setting doesn't automatically mean we need to setup ssl trust store anymore
    Assert.assertEquals(4, properties2.size());
    Assert.assertNotNull(properties2.get(ClientProperties.PROXY_URI));
    Assert.assertNull(properties2.get(ApacheClientProperties.SSL_CONFIG));

    info = new AuthenticationInfo("users1", "pass", "https://tfsonprem.test", "4display");
    final ClientConfig config3 = ServerContext.getClientConfig(ServerContext.Type.TFS, info, false);
    final Map<String, Object> properties3 = config3.getProperties();
    Assert.assertEquals(4, properties3.size());
    Assert.assertNull(properties3.get(ClientProperties.PROXY_URI));
    Assert.assertNotNull(properties3.get(ApacheClientProperties.SSL_CONFIG));
}

From source file:kuona.client.PreemptiveAuth.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(ClientContext.CREDS_PROVIDER);
        HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (authScheme != null) {
            Credentials creds = credsProvider
                    .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }/*from w w  w  .j av  a 2 s  .  c o m*/
            authState.update(authScheme, creds);
        }
    }
}

From source file:org.opendatakit.dwc.server.GreetingServiceImpl.java

private static HttpRequestInterceptor getPreemptiveAuth() {
    return new HttpRequestInterceptor() {
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
            CredentialsProvider credsProvider = (CredentialsProvider) context
                    .getAttribute(ClientContext.CREDS_PROVIDER);
            HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

            if (authState.getAuthScheme() == null) {
                AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                Credentials creds = credsProvider.getCredentials(authScope);
                if (creds != null) {
                    authState.setAuthScheme(new BasicScheme());
                    authState.setCredentials(creds);
                }/*from   w ww .  ja v  a2s . c o m*/
            }
        }
    };
}