Search in sources :

Example 46 with AuthScope

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);
}
Also used : AuthScope(org.apache.commons.httpclient.auth.AuthScope)

Example 47 with AuthScope

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));
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) Properties(java.util.Properties) EasySSLProtocolSocketFactory(org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory) EasySSLProtocolSocketFactory(org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory) ProtocolSocketFactory(org.apache.commons.httpclient.protocol.ProtocolSocketFactory) AuthScope(org.apache.commons.httpclient.auth.AuthScope) Protocol(org.apache.commons.httpclient.protocol.Protocol) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) BeforeClass(org.junit.BeforeClass)

Example 48 with AuthScope

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));
    }
}
Also used : AuthScope(org.apache.commons.httpclient.auth.AuthScope) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 49 with AuthScope

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");
                }
            }
        }
    }
}
Also used : AuthState(org.apache.commons.httpclient.auth.AuthState) AuthScope(org.apache.commons.httpclient.auth.AuthScope) AuthScheme(org.apache.commons.httpclient.auth.AuthScheme)

Example 50 with AuthScope

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;
        }
    }
}
Also used : AuthChallengeException(org.apache.commons.httpclient.auth.AuthChallengeException) AuthState(org.apache.commons.httpclient.auth.AuthState) AuthScope(org.apache.commons.httpclient.auth.AuthScope) AuthScheme(org.apache.commons.httpclient.auth.AuthScheme)

Aggregations

AuthScope (org.apache.commons.httpclient.auth.AuthScope)52 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)34 Credentials (org.apache.commons.httpclient.Credentials)19 GetMethod (org.apache.commons.httpclient.methods.GetMethod)12 HttpClient (org.apache.commons.httpclient.HttpClient)11 URL (java.net.URL)10 IOException (java.io.IOException)5 NTCredentials (org.apache.commons.httpclient.NTCredentials)5 Protocol (org.apache.commons.httpclient.protocol.Protocol)5 ProtocolSocketFactory (org.apache.commons.httpclient.protocol.ProtocolSocketFactory)5 Header (org.apache.commons.httpclient.Header)4 AuthScheme (org.apache.commons.httpclient.auth.AuthScheme)4 AuthState (org.apache.commons.httpclient.auth.AuthState)4 EasySSLProtocolSocketFactory (org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory)4 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)3 DefaultClientConfig (com.sun.jersey.api.client.config.DefaultClientConfig)3 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 URISyntaxException (java.net.URISyntaxException)3 ArrayList (java.util.ArrayList)3