Example usage for org.apache.http.impl.auth DigestScheme authenticate

List of usage examples for org.apache.http.impl.auth DigestScheme authenticate

Introduction

In this page you can find the example usage for org.apache.http.impl.auth DigestScheme authenticate.

Prototype

@Deprecated
public Header authenticate(final Credentials credentials, final HttpRequest request)
        throws AuthenticationException 

Source Link

Usage

From source file:org.picketbox.test.config.ProtectedResourceManagerUnitTestCase.java

@Test
public void testNotAuthorizedResource() throws Exception {
    URL url = new URL(urlStr + "confidentialResource");

    DefaultHttpClient httpclient = null;
    try {/*from   w  w  w  . j  av  a 2s.c  o  m*/
        String user = "Aladdin";
        String pass = "Open Sesame";

        httpclient = new DefaultHttpClient();

        HttpGet httpget = new HttpGet(url.toExternalForm());
        HttpResponse response = httpclient.execute(httpget);
        assertEquals(401, response.getStatusLine().getStatusCode());
        Header[] headers = response.getHeaders(PicketBoxConstants.HTTP_WWW_AUTHENTICATE);

        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);

        Header header = headers[0];
        String value = header.getValue();
        value = value.substring(7).trim();

        String[] tokens = HTTPDigestUtil.quoteTokenize(value);
        DigestHolder digestHolder = HTTPDigestUtil.digest(tokens);

        DigestScheme digestAuth = new DigestScheme();
        digestAuth.overrideParamter("algorithm", "MD5");
        digestAuth.overrideParamter("realm", digestHolder.getRealm());
        digestAuth.overrideParamter("nonce", digestHolder.getNonce());
        digestAuth.overrideParamter("qop", "auth");
        digestAuth.overrideParamter("nc", "0001");
        digestAuth.overrideParamter("cnonce", DigestScheme.createCnonce());
        digestAuth.overrideParamter("opaque", digestHolder.getOpaque());

        httpget = new HttpGet(url.toExternalForm());
        Header auth = digestAuth.authenticate(new UsernamePasswordCredentials(user, pass), httpget);
        System.out.println(auth.getName());
        System.out.println(auth.getValue());

        httpget.setHeader(auth);

        System.out.println("executing request" + httpget.getRequestLine());
        response = httpclient.execute(httpget);
        entity = response.getEntity();

        System.out.println("----------------------------------------");
        StatusLine statusLine = response.getStatusLine();
        System.out.println(statusLine);
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
        }
        assertEquals(403, statusLine.getStatusCode());
        EntityUtils.consume(entity);
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.opencastproject.kernel.security.TrustedHttpClientImpl.java

/**
 * Handles the necessary handshake for digest authenticaion in the case where it isn't a GET operation.
 * /*from w  ww.  ja v a 2  s .  co  m*/
 * @param httpUriRequest
 *          The request location to get the digest authentication for.
 * @param httpClient
 *          The client to send the request through.
 * @throws TrustedHttpClientException
 *           Thrown if the client cannot be shutdown.
 */
private void manuallyHandleDigestAuthentication(HttpUriRequest httpUriRequest, HttpClient httpClient)
        throws TrustedHttpClientException {
    HttpRequestBase digestRequest;
    try {
        digestRequest = (HttpRequestBase) httpUriRequest.getClass().newInstance();
    } catch (Exception e) {
        throw new IllegalStateException("Can not create a new " + httpUriRequest.getClass().getName());
    }
    digestRequest.setURI(httpUriRequest.getURI());
    digestRequest.setHeader(REQUESTED_AUTH_HEADER, DIGEST_AUTH);
    String[] realmAndNonce = getRealmAndNonce(digestRequest);

    if (realmAndNonce != null) {
        // Set the user/pass
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, pass);

        // Set up the digest authentication with the required values
        DigestScheme digestAuth = new DigestScheme();
        digestAuth.overrideParamter("realm", realmAndNonce[0]);
        digestAuth.overrideParamter("nonce", realmAndNonce[1]);

        // Add the authentication header
        try {
            httpUriRequest.setHeader(digestAuth.authenticate(creds, httpUriRequest));
        } catch (Exception e) {
            // close the http connection(s)
            httpClient.getConnectionManager().shutdown();
            throw new TrustedHttpClientException(e);
        }
    }
}