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

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

Introduction

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

Prototype

@Override
public void processChallenge(final Header header) throws MalformedChallengeException 

Source Link

Document

Processes the Digest challenge.

Usage

From source file:com.jonbanjo.cups.operations.AuthHeader.java

static void makeAuthHeader(HttpRequest request, AuthInfo auth) {

    if (auth.reason == AuthInfo.AUTH_NOT_SUPPORTED) {
        return;/*  w  ww .j  a  v a 2 s  .  co  m*/
    }

    if (auth.username.equals("") || (auth.password.equals(""))) {
        auth.reason = AuthInfo.AUTH_REQUIRED;
        return;
    }

    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(auth.username, auth.password);
    if (auth.getType().equals("Basic")) {
        BasicScheme basicScheme = new BasicScheme();
        try {
            auth.setAuthHeader(basicScheme.authenticate(creds, request));
        } catch (Exception e) {
            System.err.println(e.toString());
            auth.reason = AuthInfo.AUTH_BAD;
        }
    } else if (auth.getType().equals("Digest")) {
        try {
            DigestScheme digestScheme = new DigestScheme();
            digestScheme.processChallenge(auth.getHttpHeader());
            auth.setAuthHeader(digestScheme.authenticate(creds, request));
            String test0 = auth.getHttpHeader().getValue();
            String test1 = auth.getAuthHeader().getValue();
            System.out.println();
        } catch (Exception e) {
            System.err.println(e.toString());
            auth.reason = AuthInfo.AUTH_BAD;
        }
    } else {
        auth.reason = AuthInfo.AUTH_NOT_SUPPORTED;
    }
}

From source file:com.liferay.mobile.android.auth.basic.DigestAuthentication.java

@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {

    Request request = response.request();
    Builder builder = request.newBuilder();

    try {//from   w  ww. j  a  v  a  2s. c  om
        BasicHeader authenticateHeader = new BasicHeader(Headers.WWW_AUTHENTICATE,
                response.header(Headers.WWW_AUTHENTICATE));

        DigestScheme scheme = new DigestScheme();
        scheme.processChallenge(authenticateHeader);

        BasicHttpRequest basicHttpRequest = new BasicHttpRequest(request.method(), request.uri().getPath());

        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);

        String authorizationHeader = scheme.authenticate(credentials, basicHttpRequest).getValue();

        builder.addHeader(Headers.AUTHORIZATION, authorizationHeader);
    } catch (Exception e) {
        throw new IOException(e);
    }

    return builder.build();
}

From source file:com.liferay.mobile.sdk.auth.DigestAuthentication.java

@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {

    Request request = response.request();

    Builder builder = request.newBuilder();

    try {/*from ww w  .j  ava  2s .c o m*/
        BasicHeader authenticateHeader = new BasicHeader(Headers.WWW_AUTHENTICATE,
                response.header(Headers.WWW_AUTHENTICATE));

        DigestScheme scheme = new DigestScheme();

        scheme.processChallenge(authenticateHeader);

        BasicHttpRequest basicHttpRequest = new BasicHttpRequest(request.method(), request.uri().getPath());

        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);

        String authorizationHeader = scheme.authenticate(credentials, basicHttpRequest).getValue();

        builder.addHeader(Headers.AUTHORIZATION, authorizationHeader);
    } catch (Exception e) {
        throw new IOException(e);
    }

    return builder.build();
}

From source file:android.net.http.DefaultHttpClientTest.java

private void authenticateDigestAlgorithm(String algorithm) throws Exception {
    String challenge = "Digest realm=\"protected area\", " + "nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", "
            + "algorithm=" + algorithm;
    DigestScheme digestScheme = new DigestScheme();
    digestScheme.processChallenge(new BasicHeader("WWW-Authenticate", challenge));
    HttpGet get = new HttpGet();
    digestScheme.authenticate(new UsernamePasswordCredentials("username", "password"), get);
}

From source file:io.fabric8.devops.connector.DevOpsConnector.java

protected void createGerritRepo(String repoName, String gerritUser, String gerritPwd,
        String gerritGitInitialCommit, String gerritGitRepoDescription) throws Exception {

    // lets add defaults if not env vars
    if (Strings.isNullOrBlank(gerritUser)) {
        gerritUser = "admin";
    }/*from w  ww  .ja v  a2  s  . c  o  m*/
    if (Strings.isNullOrBlank(gerritPwd)) {
        gerritPwd = "secret";
    }

    log.info("A Gerrit git repo will be created for this name : " + repoName);

    String gerritAddress = KubernetesHelper.getServiceURL(kubernetes, ServiceNames.GERRIT, namespace, "http",
            true);
    log.info("Found gerrit address: " + gerritAddress + " for namespace: " + namespace
            + " on Kubernetes address: " + kubernetes.getMasterUrl());

    if (Strings.isNullOrBlank(gerritAddress)) {
        throw new Exception("No address for service " + ServiceNames.GERRIT + " in namespace: " + namespace
                + " on Kubernetes address: " + kubernetes.getMasterUrl());
    }

    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpClient httpclientPost = HttpClients.createDefault();
    String GERRIT_URL = gerritAddress + "/a/projects/" + repoName;
    HttpGet httpget = new HttpGet(GERRIT_URL);
    System.out.println("Requesting : " + httpget.getURI());

    try {
        //Initial request without credentials returns "HTTP/1.1 401 Unauthorized"
        HttpResponse response = httpclient.execute(httpget);
        System.out.println(response.getStatusLine());

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
            // Get current current "WWW-Authenticate" header from response
            // WWW-Authenticate:Digest realm="My Test Realm", qop="auth",
            // nonce="cdcf6cbe6ee17ae0790ed399935997e8", opaque="ae40d7c8ca6a35af15460d352be5e71c"
            Header authHeader = response.getFirstHeader(AUTH.WWW_AUTH);
            System.out.println("authHeader = " + authHeader);

            DigestScheme digestScheme = new DigestScheme();

            //Parse realm, nonce sent by server.
            digestScheme.processChallenge(authHeader);

            UsernamePasswordCredentials creds = new UsernamePasswordCredentials(gerritUser, gerritPwd);
            httpget.addHeader(digestScheme.authenticate(creds, httpget, null));

            HttpPost httpPost = new HttpPost(GERRIT_URL);
            httpPost.addHeader(digestScheme.authenticate(creds, httpPost, null));
            httpPost.addHeader("Content-Type", "application/json");

            CreateRepositoryDTO createRepoDTO = new CreateRepositoryDTO();
            createRepoDTO.setDescription(gerritGitRepoDescription);
            createRepoDTO.setName(repoName);
            createRepoDTO.setCreate_empty_commit(Boolean.valueOf(gerritGitInitialCommit));

            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(createRepoDTO);

            HttpEntity entity = new StringEntity(json);
            httpPost.setEntity(entity);

            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclientPost.execute(httpPost, responseHandler);
            System.out.println("responseBody : " + responseBody);
        }

    } catch (MalformedChallengeException e) {
        e.printStackTrace();
    } catch (AuthenticationException e) {
        e.printStackTrace();
    } catch (ConnectException e) {
        System.out.println("Gerrit Server is not responding");
    } catch (HttpResponseException e) {
        System.out.println("Response from Gerrit Server : " + e.getMessage());
        throw new Exception("Repository " + repoName + " already exists !");
    } finally {
        httpclient.close();
        httpclientPost.close();
    }
}