Search in sources :

Example 36 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project apex-core by apache.

the class WebServicesClientTest method checkUserCredentials.

public static void checkUserCredentials(String username, String password, AuthScheme authScheme) throws NoSuchFieldException, IllegalAccessException {
    CredentialsProvider provider = getCredentialsProvider();
    String httpScheme = AuthScope.ANY_SCHEME;
    if (authScheme == AuthScheme.BASIC) {
        httpScheme = AuthSchemes.BASIC;
    } else if (authScheme == AuthScheme.DIGEST) {
        httpScheme = AuthSchemes.DIGEST;
    }
    AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, httpScheme);
    Credentials credentials = provider.getCredentials(authScope);
    Assert.assertNotNull("Credentials", credentials);
    Assert.assertTrue("Credentials type is user", UsernamePasswordCredentials.class.isAssignableFrom(credentials.getClass()));
    UsernamePasswordCredentials pwdCredentials = (UsernamePasswordCredentials) credentials;
    Assert.assertEquals("Username", username, pwdCredentials.getUserName());
    Assert.assertEquals("Password", password, pwdCredentials.getPassword());
}
Also used : AuthScope(org.apache.http.auth.AuthScope) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 37 with UsernamePasswordCredentials

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

the class WebDAVTest method setUp.

protected void setUp() throws Exception {
    this.uri = URI.create(System.getProperty("webdav.test.url", "http://localhost:8080/repository/default/"));
    this.root = this.uri.toASCIIString();
    if (!this.root.endsWith("/")) {
        this.root += "/";
    }
    this.username = System.getProperty("webdav.test.username", "admin");
    this.password = System.getProperty("webdav.test.password", "admin");
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(this.username, this.password));
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    // Add AuthCache to the execution context
    this.context = HttpClientContext.create();
    this.context.setCredentialsProvider(credsProvider);
    this.context.setAuthCache(authCache);
    this.client = HttpClients.custom().setConnectionManager(cm).build();
    super.setUp();
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) AuthCache(org.apache.http.client.AuthCache) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 38 with UsernamePasswordCredentials

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

the class TestAuth method update_with_auth_11.

@Test
public void update_with_auth_11() {
    UpdateRequest updates = UpdateFactory.create("CREATE SILENT GRAPH <http://graph>");
    UpdateProcessRemoteBase ue = (UpdateProcessRemoteBase) UpdateExecutionFactory.createRemote(updates, authServiceUpdate);
    // Auth credentials for valid user with correct password scoped to correct URI
    // Also using pre-emptive auth
    BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
    URI scope = URI.create(authServiceUpdate);
    credsProv.setCredentials(new AuthScope(scope.getHost(), scope.getPort()), new UsernamePasswordCredentials("allowed", "password"));
    // 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(new HttpHost(scope.getHost()), basicAuth);
    // Add AuthCache to the execution context
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProv);
    context.setAuthCache(authCache);
    HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProv).build();
    ue.setClient(client);
    ue.setHttpContext(context);
    ue.execute();
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) UpdateRequest(org.apache.jena.update.UpdateRequest) HttpHost(org.apache.http.HttpHost) HttpClient(org.apache.http.client.HttpClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) AuthScope(org.apache.http.auth.AuthScope) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) UpdateProcessRemoteBase(org.apache.jena.sparql.modify.UpdateProcessRemoteBase) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) URI(java.net.URI) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Example 39 with UsernamePasswordCredentials

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

the class TestFusekiTestAuth method testServer_auth_bad_password.

@Test(expected = HttpException.class)
public void testServer_auth_bad_password() {
    BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
    credsProv.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USER, "WRONG"));
    HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProv).build();
    try (TypedInputStream in = HttpOp.execHttpGet(FusekiTestAuth.urlDataset(), "*/*", client, null)) {
    } catch (HttpException ex) {
        throw assertAuthHttpException(ex);
    }
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClient(org.apache.http.client.HttpClient) HttpException(org.apache.jena.atlas.web.HttpException) FusekiTestAuth.assertAuthHttpException(org.apache.jena.fuseki.embedded.FusekiTestAuth.assertAuthHttpException) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Example 40 with UsernamePasswordCredentials

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

the class TestFusekiTestServer method testServer_2.

@Test
public void testServer_2() {
    BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
    credsProv.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("USER", "PASSWORD"));
    HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProv).build();
    // No auth set - should work.
    try (TypedInputStream in = HttpOp.execHttpGet(FusekiTestServer.urlDataset(), "*/*")) {
    } catch (HttpException ex) {
        Assert.assertTrue(ex.getResponseCode() == HttpSC.FORBIDDEN_403 || ex.getResponseCode() == HttpSC.UNAUTHORIZED_401);
        throw ex;
    }
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClient(org.apache.http.client.HttpClient) HttpException(org.apache.jena.atlas.web.HttpException) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

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