Example usage for org.apache.http.auth AUTH WWW_AUTH

List of usage examples for org.apache.http.auth AUTH WWW_AUTH

Introduction

In this page you can find the example usage for org.apache.http.auth AUTH WWW_AUTH.

Prototype

String WWW_AUTH

To view the source code for org.apache.http.auth AUTH WWW_AUTH.

Click Source Link

Document

The www authenticate challange header.

Usage

From source file:securitydigest.TestDigestScheme.java

public void testDigestAuthenticationWithNoRealm() throws Exception {
    Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, "Digest");
    try {/*from ww  w.  ja va 2  s. c  o m*/
        AuthScheme authscheme = new DigestScheme();
        authscheme.processChallenge(authChallenge);
        fail("Should have thrown MalformedChallengeException");
    } catch (MalformedChallengeException e) {
        // expected
    }
}

From source file:securitydigest.TestDigestScheme.java

public void testDigestAuthenticationWithNoRealm2() throws Exception {
    Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, "Digest ");
    try {//from w  ww. j  a v a  2  s  .co  m
        AuthScheme authscheme = new DigestScheme();
        authscheme.processChallenge(authChallenge);
        fail("Should have thrown MalformedChallengeException");
    } catch (MalformedChallengeException e) {
        // expected
    }
}

From source file:securitydigest.TestDigestScheme.java

public void testDigestAuthenticationWithDefaultCreds() throws Exception {
    String challenge = "Digest realm=\"realm1\", nonce=\"f2a3f18799759d4f1a1c068b92b573cb\"";
    Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge);
    HttpRequest request = new BasicHttpRequest("Simple", "/");
    Credentials cred = new UsernamePasswordCredentials("username", "password");
    AuthScheme authscheme = new DigestScheme();
    authscheme.processChallenge(authChallenge);
    Header authResponse = authscheme.authenticate(cred, request);

    Map<String, String> table = parseAuthResponse(authResponse);
    assertEquals("username", table.get("username"));
    assertEquals("realm1", table.get("realm"));
    assertEquals("/", table.get("uri"));
    assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce"));
    assertEquals("e95a7ddf37c2eab009568b1ed134f89a", table.get("response"));
}

From source file:com.soundcloud.playerapi.OAuth2Scheme.java

@Override
public void processChallenge(Header header) throws MalformedChallengeException {
    if (header == null) {
        throw new IllegalArgumentException("Header may not be null");
    }//  ww  w .  ja  v a2  s  .c  o m
    String authHeader = header.getName();
    if (!authHeader.equalsIgnoreCase(AUTH.WWW_AUTH)) {
        throw new MalformedChallengeException("Unexpected header name: " + authHeader);
    }

    CharArrayBuffer buffer;
    int pos;
    if (header instanceof FormattedHeader) {
        buffer = ((FormattedHeader) header).getBuffer();
        pos = ((FormattedHeader) header).getValuePos();
    } else {
        String s = header.getValue();
        if (s == null) {
            throw new MalformedChallengeException("Header value is null");
        }
        buffer = new CharArrayBuffer(s.length());
        buffer.append(s);
        pos = 0;
    }
    while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
        pos++;
    }
    int beginIndex = pos;
    while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
        pos++;
    }
    int endIndex = pos;
    String s = buffer.substring(beginIndex, endIndex);
    if (!s.equalsIgnoreCase(getSchemeName())) {
        throw new MalformedChallengeException("Invalid scheme identifier: " + s);
    }
    HeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
    ParserCursor cursor = new ParserCursor(pos, buffer.length());
    HeaderElement[] elements = parser.parseElements(buffer, cursor);
    if (elements.length == 0) {
        throw new MalformedChallengeException("Authentication challenge is empty");
    }
    for (HeaderElement element : elements) {
        this.mParams.put(element.getName(), element.getValue());
    }
}

From source file:org.ops4j.pax.url.mvn.internal.wagon.ConfigurableHttpWagon.java

@Override
protected CloseableHttpResponse execute(HttpUriRequest httpMethod) throws HttpException, IOException {
    setHeaders(httpMethod);/*from   www .j  a  v a  2s.  c o  m*/
    String userAgent = getUserAgent(httpMethod);
    if (userAgent != null) {
        httpMethod.setHeader(HTTP.USER_AGENT, userAgent);
    }

    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    // WAGON-273: default the cookie-policy to browser compatible
    requestConfigBuilder.setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY);

    Repository repo = getRepository();
    ProxyInfo proxyInfo = getProxyInfo(repo.getProtocol(), repo.getHost());
    if (proxyInfo != null) {
        HttpHost proxy = new HttpHost(proxyInfo.getHost(), proxyInfo.getPort());
        requestConfigBuilder.setProxy(proxy);
    }

    HttpMethodConfiguration config = getHttpConfiguration() == null ? null
            : getHttpConfiguration().getMethodConfiguration(httpMethod);

    if (config != null) {
        copyConfig(config, requestConfigBuilder);
    } else {
        requestConfigBuilder.setSocketTimeout(getReadTimeout());
        requestConfigBuilder.setConnectTimeout(getTimeout());
    }

    getLocalContext().setRequestConfig(requestConfigBuilder.build());

    if (config != null && config.isUsePreemptive()) {
        HttpHost targetHost = new HttpHost(repo.getHost(), repo.getPort(), repo.getProtocol());
        AuthScope targetScope = getBasicAuthScope().getScope(targetHost);

        if (getCredentialsProvider().getCredentials(targetScope) != null) {
            BasicScheme targetAuth = new BasicScheme();
            targetAuth.processChallenge(new BasicHeader(AUTH.WWW_AUTH, "BASIC preemptive"));
            getAuthCache().put(targetHost, targetAuth);
        }
    }

    if (proxyInfo != null) {
        if (proxyInfo.getHost() != null) {
            HttpHost proxyHost = new HttpHost(proxyInfo.getHost(), proxyInfo.getPort());
            AuthScope proxyScope = getProxyBasicAuthScope().getScope(proxyHost);

            String proxyUsername = proxyInfo.getUserName();
            String proxyPassword = proxyInfo.getPassword();
            String proxyNtlmHost = proxyInfo.getNtlmHost();
            String proxyNtlmDomain = proxyInfo.getNtlmDomain();

            if (proxyUsername != null && proxyPassword != null) {
                Credentials creds;
                if (proxyNtlmHost != null || proxyNtlmDomain != null) {
                    creds = new NTCredentials(proxyUsername, proxyPassword, proxyNtlmHost, proxyNtlmDomain);
                } else {
                    creds = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
                }

                getCredentialsProvider().setCredentials(proxyScope, creds);
                BasicScheme proxyAuth = new BasicScheme();
                proxyAuth.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC preemptive"));
                getAuthCache().put(proxyHost, proxyAuth);
            }
        }
    }

    return client.execute(httpMethod, getLocalContext());
}

From source file:securitydigest.TestDigestScheme.java

public void testDigestAuthentication() throws Exception {
    String challenge = "Digest realm=\"realm1\", nonce=\"f2a3f18799759d4f1a1c068b92b573cb\"";
    Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge);
    HttpRequest request = new BasicHttpRequest("Simple", "/");
    Credentials cred = new UsernamePasswordCredentials("username", "password");
    AuthScheme authscheme = new DigestScheme();
    authscheme.processChallenge(authChallenge);
    Header authResponse = authscheme.authenticate(cred, request);

    Map<String, String> table = parseAuthResponse(authResponse);
    assertEquals("username", table.get("username"));
    assertEquals("realm1", table.get("realm"));
    assertEquals("/", table.get("uri"));
    assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce"));
    assertEquals("e95a7ddf37c2eab009568b1ed134f89a", table.get("response"));
}

From source file:securitydigest.TestDigestScheme.java

public void testDigestAuthenticationWithSHA() throws Exception {
    String challenge = "Digest realm=\"realm1\", " + "nonce=\"f2a3f18799759d4f1a1c068b92b573cb\", "
            + "algorithm=SHA";
    Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge);
    HttpRequest request = new BasicHttpRequest("Simple", "/");
    Credentials cred = new UsernamePasswordCredentials("username", "password");
    AuthScheme authscheme = new DigestScheme();
    authscheme.processChallenge(authChallenge);
    Header authResponse = authscheme.authenticate(cred, request);

    Map<String, String> table = parseAuthResponse(authResponse);
    assertEquals("username", table.get("username"));
    assertEquals("realm1", table.get("realm"));
    assertEquals("/", table.get("uri"));
    assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce"));
    assertEquals("8769e82e4e28ecc040b969562b9050580c6d186d", table.get("response"));
}

From source file:securitydigest.TestDigestScheme.java

public void testDigestAuthenticationWithQueryStringInDigestURI() throws Exception {
    String challenge = "Digest realm=\"realm1\", nonce=\"f2a3f18799759d4f1a1c068b92b573cb\"";
    Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge);
    HttpRequest request = new BasicHttpRequest("Simple", "/?param=value");
    Credentials cred = new UsernamePasswordCredentials("username", "password");
    AuthScheme authscheme = new DigestScheme();
    authscheme.processChallenge(authChallenge);
    Header authResponse = authscheme.authenticate(cred, request);

    Map<String, String> table = parseAuthResponse(authResponse);
    assertEquals("username", table.get("username"));
    assertEquals("realm1", table.get("realm"));
    assertEquals("/?param=value", table.get("uri"));
    assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce"));
    assertEquals("a847f58f5fef0bc087bcb9c3eb30e042", table.get("response"));
}

From source file:com.intel.cosbench.api.httpauth.HttpAuth.java

private void dumpResponse(HttpResponse response) {
    try {//from   w  w  w.j  a  va 2  s . c o  m
        System.out.println("\nStatus Line");
        System.out.println("-----------");
        System.out.println(response.getStatusLine());

        Header authHeader = response.getFirstHeader(AUTH.WWW_AUTH);
        System.out.println("Auth Header = " + authHeader);
        Header authRspHeader = response.getFirstHeader(AUTH.WWW_AUTH_RESP);
        System.out.println("Auth Rsp Header = " + authRspHeader);
        System.out.println("\nHeaders");
        System.out.println("-------");
        for (Header header : response.getAllHeaders()) {
            System.out.println(header.toString());
        }

        System.out.println("\nBody");
        System.out.println("----");
        System.out.println(EntityUtils.toString(response.getEntity()));
        EntityUtils.consume(response.getEntity());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.gooddata.http.client.GoodDataHttpClient.java

private GoodDataChallengeType identifyGoodDataChallenge(final HttpResponse response) {
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
        final Header[] headers = response.getHeaders(AUTH.WWW_AUTH);
        if (headers != null) {
            for (final Header header : headers) {
                final String challenge = header.getValue();
                if (challenge.contains(COOKIE_GDC_AUTH_SST)) {
                    // this is actually not used as in refreshTT() we rely on status code only
                    return GoodDataChallengeType.SST;
                } else if (challenge.contains(COOKIE_GDC_AUTH_TT)) {
                    return GoodDataChallengeType.TT;
                }//from  w  w  w .j a v  a2  s.c o m
            }
        }
    }
    return GoodDataChallengeType.UNKNOWN;
}