Search in sources :

Example 36 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();
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    result.setCredentialsProvider(credsProvider);
    // take over default credentials (e.g. for proxy)
    for (Map.Entry<AuthScope, org.apache.http.auth.Credentials> entry : commonCredentials.entrySet()) {
        credsProvider.setCredentials(entry.getKey(), entry.getValue());
    }
    if (sessionInfo != null) {
        checkSessionInfo(sessionInfo);
        org.apache.http.auth.Credentials creds = ((SessionInfoImpl) sessionInfo).getCredentials().getHttpCredentials();
        if (creds != null) {
            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.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) AuthScope(org.apache.http.auth.AuthScope) AuthScope(org.apache.http.auth.AuthScope) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashMap(java.util.HashMap) Credentials(javax.jcr.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 37 with BasicScheme

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

the class ConstraintDrivenAuthModeTestCase method testSecuredResourceWithValidCredential.

@Test
public void testSecuredResourceWithValidCredential() throws Exception {
    HttpGet request = new HttpGet(new URI(url.toExternalForm() + "secure.jsp"));
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "password1");
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, credentials);
    request.addHeader(new BasicScheme().authenticate(credentials, "UTF-8", false));
    try (CloseableHttpClient httpClient = HttpClients.custom().build()) {
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            int statusCode = response.getStatusLine().getStatusCode();
            assertEquals("Unexpected status code in HTTP response.", SC_OK, statusCode);
            assertEquals("Unexpected content of HTTP response.", "user1", EntityUtils.toString(response.getEntity()));
        }
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) URI(java.net.URI) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Example 38 with BasicScheme

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

the class ConstraintDrivenAuthModeTestCase method testUnsecureResourceWithInvalidCredential.

@Test
public void testUnsecureResourceWithInvalidCredential() throws Exception {
    HttpGet request = new HttpGet(new URI(url.toExternalForm() + "unsecure.jsp"));
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "password2");
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, credentials);
    request.addHeader(new BasicScheme().authenticate(credentials, request));
    try (CloseableHttpClient httpClient = HttpClients.custom().build()) {
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            int statusCode = response.getStatusLine().getStatusCode();
            assertEquals("Unexpected status code in HTTP response.", SC_OK, statusCode);
            assertEquals("Unexpected content of HTTP response.", "", EntityUtils.toString(response.getEntity()));
        }
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) URI(java.net.URI) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Example 39 with BasicScheme

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

the class ProactiveAuthModeTestCase method testSecuredResourceWithValidCredential.

@Test
public void testSecuredResourceWithValidCredential() throws Exception {
    HttpGet request = new HttpGet(new URI(url.toExternalForm() + "secure.jsp"));
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "password1");
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, credentials);
    request.addHeader(new BasicScheme().authenticate(credentials, "UTF-8", false));
    try (CloseableHttpClient httpClient = HttpClients.custom().build()) {
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            int statusCode = response.getStatusLine().getStatusCode();
            assertEquals("Unexpected status code in HTTP response.", SC_OK, statusCode);
            assertEquals("Unexpected content of HTTP response.", "user1", EntityUtils.toString(response.getEntity()));
        }
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) URI(java.net.URI) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Example 40 with BasicScheme

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

the class ProactiveAuthModeTestCase method testSecuredResourceWithInvalidCredential.

@Test
public void testSecuredResourceWithInvalidCredential() throws Exception {
    HttpGet request = new HttpGet(new URI(url.toExternalForm() + "secure.jsp"));
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "password2");
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, credentials);
    request.addHeader(new BasicScheme().authenticate(credentials, request));
    try (CloseableHttpClient httpClient = HttpClients.custom().build()) {
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            int statusCode = response.getStatusLine().getStatusCode();
            assertEquals("Unexpected status code in HTTP response.", SC_UNAUTHORIZED, statusCode);
        }
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) URI(java.net.URI) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Aggregations

BasicScheme (org.apache.http.impl.auth.BasicScheme)83 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)58 CredentialsProvider (org.apache.http.client.CredentialsProvider)49 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)48 AuthCache (org.apache.http.client.AuthCache)47 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)47 HttpHost (org.apache.http.HttpHost)45 AuthScope (org.apache.http.auth.AuthScope)39 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)32 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)25 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)24 HttpGet (org.apache.http.client.methods.HttpGet)23 URI (java.net.URI)21 Test (org.junit.Test)18 IOException (java.io.IOException)13 Credentials (org.apache.http.auth.Credentials)13 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)10 HttpResponse (org.apache.http.HttpResponse)9 HttpPost (org.apache.http.client.methods.HttpPost)9 Header (org.apache.http.Header)8