use of org.apache.http.client.CredentialsProvider 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;
}
use of org.apache.http.client.CredentialsProvider 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();
}
use of org.apache.http.client.CredentialsProvider in project wildfly by wildfly.
the class SAML2AttributeMappingTestCase method testPassUserPrincipalToAttributeManager.
/**
* Tests IDP attribute mapping when passUserPrincipalToAttributeManager is set to "true". Automatic handling of redirections
* is enabled for HTTP client used.
*
* @throws Exception
*/
@Test
public void testPassUserPrincipalToAttributeManager() throws Exception {
Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new JBossNegotiateSchemeFactory(true)).build();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(null, -1, null), new NullHCCredentials());
try (final CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultAuthSchemeRegistry(authSchemeRegistry).setDefaultCredentialsProvider(credentialsProvider).setRedirectStrategy(Utils.REDIRECT_STRATEGY).build()) {
String response = PicketLinkTestBase.makeCallWithKerberosAuthn(spUrl.toURI(), httpClient, "jduke", "theduke", 200);
assertEquals("SP index page was not reached", SP_RESPONSE_BODY, response);
response = PicketLinkTestBase.makeCall(new URL(spUrl.toString() + PrintAttributeServlet.SERVLET_PATH.substring(1)), httpClient, 200);
assertEquals("cn attribute not stored", "Java Duke", response);
}
}
use of org.apache.http.client.CredentialsProvider in project wildfly by wildfly.
the class BasicAuthenticationWebFailoverTestCase method test.
@Test
public void test(@ArquillianResource(SecureServlet.class) @OperateOnDeployment(DEPLOYMENT_1) URL baseURL1, @ArquillianResource(SecureServlet.class) @OperateOnDeployment(DEPLOYMENT_2) URL baseURL2) throws IOException, URISyntaxException {
CredentialsProvider provider = new BasicCredentialsProvider();
HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
URI uri1 = SecureServlet.createURI(baseURL1);
URI uri2 = SecureServlet.createURI(baseURL2);
try {
// Valid login, invalid role
setCredentials(provider, "forbidden", "password", baseURL1, baseURL2);
HttpResponse response = client.execute(new HttpGet(uri1));
try {
Assert.assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getStatusLine().getStatusCode());
} finally {
HttpClientUtils.closeQuietly(response);
}
// Invalid login, valid role
setCredentials(provider, "allowed", "bad", baseURL1, baseURL2);
response = client.execute(new HttpGet(uri1));
try {
Assert.assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
} finally {
HttpClientUtils.closeQuietly(response);
}
// Valid login, valid role
setCredentials(provider, "allowed", "password", baseURL1, baseURL2);
String sessionId = null;
response = client.execute(new HttpGet(uri1));
try {
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertNotNull(response.getFirstHeader(SecureServlet.SESSION_ID_HEADER));
sessionId = response.getFirstHeader(SecureServlet.SESSION_ID_HEADER).getValue();
} finally {
HttpClientUtils.closeQuietly(response);
}
undeploy(DEPLOYMENT_1);
response = client.execute(new HttpGet(uri2));
try {
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertEquals(sessionId, response.getFirstHeader(SecureServlet.SESSION_ID_HEADER).getValue());
} finally {
HttpClientUtils.closeQuietly(response);
}
deploy(DEPLOYMENT_1);
response = client.execute(new HttpGet(uri1));
try {
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertEquals(sessionId, response.getFirstHeader(SecureServlet.SESSION_ID_HEADER).getValue());
} finally {
HttpClientUtils.closeQuietly(response);
}
} finally {
HttpClientUtils.closeQuietly(client);
}
}
use of org.apache.http.client.CredentialsProvider in project wildfly by wildfly.
the class TransportGuaranteeTestCase method checkGetURL.
/**
* Check response on given url
*
* @param url
* @param responseSubstring - if null we are checking response code only
* @return
* @throws Exception
*/
private boolean checkGetURL(String url, String responseSubstring, String user, String pass) throws Exception {
log.trace("Checking URL=" + url);
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY), new UsernamePasswordCredentials(user, pass));
CloseableHttpClient httpClient;
if (url.startsWith("https")) {
httpClient = TestHttpClientUtils.getHttpsClient(credentialsProvider);
} else {
httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
}
HttpGet get = new HttpGet(url);
HttpResponse hr;
try {
try {
hr = httpClient.execute(get);
} catch (Exception e) {
if (// in case substring is defined, rethrow exception so, we can easier analyze the cause
responseSubstring == null) // in case substring is defined, rethrow exception so, we can easier analyze the cause
{
return false;
} else {
throw new Exception(e);
}
}
int statusCode = hr.getStatusLine().getStatusCode();
if (statusCode != 200) {
log.trace("statusCode not expected. statusCode=" + statusCode + ", URL=" + url);
return false;
}
if (responseSubstring == null) {
// this indicates that negative test had problems
log.trace("statusCode==200 on URL=" + url);
return true;
}
String response = EntityUtils.toString(hr.getEntity());
if (response.indexOf(responseSubstring) != -1) {
return true;
} else {
log.trace("Response doesn't contain expected substring (" + responseSubstring + ")");
return false;
}
} finally {
if (httpClient != null) {
httpClient.close();
}
}
}
Aggregations