Search in sources :

Example 6 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project orientdb by orientechnologies.

the class BaseHttpTest method exec.

protected BaseHttpTest exec() throws IOException {
    final HttpHost targetHost = new HttpHost(getHost(), getPort(), getProtocol());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost), new UsernamePasswordCredentials(getUserName(), getUserPassword()));
    // 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(targetHost, basicAuth);
    // Add AuthCache to the execution context
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);
    if (keepAlive != null)
        request.addHeader("Connection", keepAlive ? "Keep-Alive" : "Close");
    if (payload != null && request instanceof HttpEntityEnclosingRequestBase)
        ((HttpEntityEnclosingRequestBase) request).setEntity(payload);
    final CloseableHttpClient httpClient = HttpClients.createDefault();
    // DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(retry, false);
    // context.setAttribute(HttpMethodParams.RETRY_HANDLER, retryhandler);
    response = httpClient.execute(targetHost, request, context);
    return this;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) 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) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) AuthCache(org.apache.http.client.AuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 7 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project ats-framework by Axway.

the class HttpClient method addRequestHeaders.

private void addRequestHeaders(HttpRequestBase httpMethod) throws FileTransferException {
    // pass user credentials with the very first headers
    if (preemptiveBasicAuthentication) {
        if (this.username == null) {
            throw new FileTransferException("We cannot set user credentials as the user name is not set");
        }
        try {
            BasicScheme schema = new BasicScheme(Charset.forName("US-ASCII"));
            Header authenticationHeader = schema.authenticate(// here we make 'empty' http request, just so we could authenticate the credentials
            new UsernamePasswordCredentials(this.username, this.userpass), new HttpGet(), httpContext);
            httpMethod.addHeader(authenticationHeader);
        } catch (AuthenticationException ae) {
            throw new FileTransferException("Unable to add Basic Authentication header", ae);
        }
    }
    // Add the rest of the request headers
    for (Header header : requestHeaders) {
        httpMethod.setHeader(header);
    }
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) BasicScheme(org.apache.http.impl.auth.BasicScheme) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) AuthenticationException(org.apache.http.auth.AuthenticationException) HttpGet(org.apache.http.client.methods.HttpGet) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 8 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project jackrabbit by apache.

the class RepositoryServiceImpl method getContext.

protected HttpContext getContext(SessionInfo sessionInfo) throws RepositoryException {
    HttpClientContext result = HttpClientContext.create();
    if (sessionInfo != null) {
        checkSessionInfo(sessionInfo);
        org.apache.http.auth.Credentials creds = ((SessionInfoImpl) sessionInfo).getCredentials().getHttpCredentials();
        if (creds != null) {
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new org.apache.http.auth.AuthScope(httpHost.getHostName(), httpHost.getPort()), creds);
            BasicScheme basicAuth = new BasicScheme();
            AuthCache authCache = new BasicAuthCache();
            authCache.put(httpHost, basicAuth);
            result.setCredentialsProvider(credsProvider);
            result.setAuthCache(authCache);
        }
    }
    return result;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache)

Example 9 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme 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 10 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme 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)

Aggregations

BasicScheme (org.apache.http.impl.auth.BasicScheme)19 HttpHost (org.apache.http.HttpHost)13 AuthCache (org.apache.http.client.AuthCache)12 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)12 AuthScope (org.apache.http.auth.AuthScope)11 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)10 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)10 CredentialsProvider (org.apache.http.client.CredentialsProvider)9 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)9 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 Credentials (org.apache.http.auth.Credentials)4 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)4 IOException (java.io.IOException)3 AuthState (org.apache.http.auth.AuthState)3 HttpGet (org.apache.http.client.methods.HttpGet)3 URI (java.net.URI)2 URL (java.net.URL)2 Header (org.apache.http.Header)2 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)2 HttpContext (org.apache.http.protocol.HttpContext)2