use of org.apache.commons.httpclient.auth.AuthScope in project ecf by eclipse.
the class TestHttpState method testHostCredentials.
public void testHostCredentials() throws Exception {
HttpState state = new HttpState();
Credentials expected = new UsernamePasswordCredentials("name", "pass");
state.setCredentials(new AuthScope("host", AuthScope.ANY_PORT, AuthScope.ANY_REALM), expected);
Credentials got = state.getCredentials(DEFSCOPE);
assertEquals(expected, got);
}
use of org.apache.commons.httpclient.auth.AuthScope in project lobcder by skoulouzis.
the class WebDAVTest method setUpClass.
@BeforeClass
public static void setUpClass() throws Exception {
// String propBasePath = System.getProperty("user.home") + File.separator
// + "workspace" + File.separator + "lobcder-tests"
// + File.separator + "etc" + File.separator + "test.properties";
String propBasePath = "etc" + File.separator + "test.properties";
Properties prop = TestSettings.getTestProperties(propBasePath);
String testURL = prop.getProperty("webdav.test.url", "http://localhost:8080/lobcder/dav");
assertTrue(testURL != null);
if (!testURL.endsWith("/")) {
testURL = testURL + "/";
}
uri = URI.create(testURL);
root = uri.toASCIIString();
if (!root.endsWith("/")) {
root += "/";
}
username = prop.getProperty(("webdav.test.username1"), "user");
assertTrue(username != null);
password = prop.getProperty(("webdav.test.password1"), "token0");
assertTrue(password != null);
quckTest = Boolean.valueOf(prop.getProperty(("test.quick"), "true"));
int port = uri.getPort();
if (port == -1) {
port = 443;
}
ProtocolSocketFactory socketFactory = new EasySSLProtocolSocketFactory();
Protocol https = new Protocol("https", socketFactory, port);
Protocol.registerProtocol("https", https);
// List authPrefs = new ArrayList();
// authPrefs.add(AuthPolicy.BASIC);
// client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
client = new HttpClient();
client.getState().setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username, password));
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
restClient = Client.create(clientConfig);
restClient.addFilter(new com.sun.jersey.api.client.filter.HTTPBasicAuthFilter(username, password));
restURL = prop.getProperty(("rest.test.url"), "http://localhost:8080/lobcder/dav/rest/");
utils = new Utils(client);
client2 = new HttpClient();
assertNotNull(uri.getHost());
assertNotNull(uri.getPort());
assertNotNull(client2);
username2 = prop.getProperty(("webdav.test.username2"), "user2");
assertTrue(username2 != null);
password2 = prop.getProperty(("webdav.test.password2"), "passwd2");
assertTrue(password2 != null);
client2.getState().setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username2, password2));
}
use of org.apache.commons.httpclient.auth.AuthScope in project cloudstack by apache.
the class HTTPUtils method setCredentials.
/**
* @param username
* @param password
* @param httpClient
*/
public static void setCredentials(String username, String password, HttpClient httpClient) {
if (username != null && password != null && httpClient != null) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Setting credentials with username " + username + " for host " + httpClient.getHostConfiguration().getHost() + ":" + httpClient.getHostConfiguration().getPort());
}
httpClient.getParams().setAuthenticationPreemptive(true);
httpClient.getState().setCredentials(new AuthScope(httpClient.getHostConfiguration().getHost(), httpClient.getHostConfiguration().getPort(), AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, password));
}
}
use of org.apache.commons.httpclient.auth.AuthScope in project zaproxy by zaproxy.
the class HttpMethodDirector method authenticateHost.
private void authenticateHost(final HttpMethod method) throws AuthenticationException {
// Clean up existing authentication headers
boolean userDefinedAuthenticationHeaders = !cleanAuthHeaders(method, WWW_AUTH_RESP);
if (userDefinedAuthenticationHeaders) {
if (LOG.isDebugEnabled()) {
LOG.debug("User defined '" + WWW_AUTH_RESP + "' headers present in the request.");
}
}
AuthState authstate = method.getHostAuthState();
AuthScheme authscheme = authstate.getAuthScheme();
if (authscheme == null) {
return;
}
if (authstate.isAuthRequested() || !authscheme.isConnectionBased()) {
String host = method.getParams().getVirtualHost();
if (host == null) {
host = conn.getHost();
}
int port = conn.getPort();
AuthScope authscope = new AuthScope(host, port, authscheme.getRealm(), authscheme.getSchemeName());
if (LOG.isDebugEnabled()) {
LOG.debug("Authenticating with " + authscope);
}
Credentials credentials = this.state.getCredentials(authscope);
if (credentials != null) {
if (userDefinedAuthenticationHeaders) {
if (!method.getParams().getBooleanParameter(PARAM_REMOVE_USER_DEFINED_AUTH_HEADERS, false)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Ignoring authentication, user defined '" + WWW_AUTH_RESP + "' headers present in the request.");
}
return;
}
method.removeRequestHeader(WWW_AUTH_RESP);
if (LOG.isDebugEnabled()) {
LOG.debug("Removed user defined '" + WWW_AUTH_RESP + "' headers.");
}
}
String authstring = authscheme.authenticate(credentials, method);
if (authstring != null) {
method.addRequestHeader(new Header(WWW_AUTH_RESP, authstring, true));
}
} else {
if (LOG.isWarnEnabled()) {
LOG.warn("Required credentials not available for " + authscope);
if (method.getHostAuthState().isPreemptive()) {
LOG.warn("Preemptive authentication requested but no default " + "credentials available");
}
}
}
}
}
use of org.apache.commons.httpclient.auth.AuthScope in project zaproxy by zaproxy.
the class HttpMethodDirector method processWWWAuthChallenge.
private boolean processWWWAuthChallenge(final HttpMethod method) throws MalformedChallengeException, AuthenticationException {
AuthState authstate = method.getHostAuthState();
Map<?, ?> challenges = AuthChallengeParser.parseChallenges(method.getResponseHeaders(WWW_AUTH_CHALLENGE));
if (challenges.isEmpty()) {
LOG.debug("Authentication challenge(s) not found");
return false;
}
AuthScheme authscheme = null;
try {
authscheme = this.authProcessor.processChallenge(authstate, challenges);
} catch (AuthChallengeException e) {
if (LOG.isWarnEnabled()) {
LOG.warn(e.getMessage());
}
}
if (authscheme == null) {
return false;
}
String host = method.getParams().getVirtualHost();
if (host == null) {
host = conn.getHost();
}
int port = conn.getPort();
AuthScope authscope = new AuthScope(host, port, authscheme.getRealm(), authscheme.getSchemeName());
if (LOG.isDebugEnabled()) {
LOG.debug("Authentication scope: " + authscope);
}
if (authstate.isAuthAttempted() && authscheme.isComplete()) {
// Already tried and failed
Credentials credentials = promptForCredentials(authscheme, method.getParams(), authscope);
if (credentials == null) {
if (LOG.isInfoEnabled()) {
LOG.info("Failure authenticating with " + authscope);
}
return false;
} else {
return true;
}
} else {
authstate.setAuthAttempted(true);
Credentials credentials = this.state.getCredentials(authscope);
if (credentials == null) {
credentials = promptForCredentials(authscheme, method.getParams(), authscope);
}
if (credentials == null) {
if (LOG.isInfoEnabled()) {
LOG.info("No credentials available for " + authscope);
}
return false;
} else {
return true;
}
}
}
Aggregations