Example usage for org.springframework.security.web.authentication.www DigestAuthUtils splitEachArrayElementAndCreateMap

List of usage examples for org.springframework.security.web.authentication.www DigestAuthUtils splitEachArrayElementAndCreateMap

Introduction

In this page you can find the example usage for org.springframework.security.web.authentication.www DigestAuthUtils splitEachArrayElementAndCreateMap.

Prototype

static Map<String, String> splitEachArrayElementAndCreateMap(String[] array, String delimiter,
        String removeCharacters) 

Source Link

Document

Takes an array of Strings, and for each element removes any instances of removeCharacter, and splits the element based on the delimiter.

Usage

From source file:org.springframework.security.web.authentication.www.DigestAuthenticationEntryPointTests.java

@Test
public void testNormalOperation() throws Exception {
    DigestAuthenticationEntryPoint ep = new DigestAuthenticationEntryPoint();
    ep.setRealmName("hello");
    ep.setKey("key");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI("/some_path");

    MockHttpServletResponse response = new MockHttpServletResponse();

    ep.afterPropertiesSet();//from   www.j  av  a 2  s  . co  m

    ep.commence(request, response, new DisabledException("foobar"));

    // Check response is properly formed
    assertThat(response.getStatus()).isEqualTo(401);
    assertThat(response.getHeader("WWW-Authenticate").toString()).startsWith("Digest ");

    // Break up response header
    String header = response.getHeader("WWW-Authenticate").toString().substring(7);
    String[] headerEntries = StringUtils.commaDelimitedListToStringArray(header);
    Map<String, String> headerMap = DigestAuthUtils.splitEachArrayElementAndCreateMap(headerEntries, "=", "\"");

    assertThat(headerMap.get("realm")).isEqualTo("hello");
    assertThat(headerMap.get("qop")).isEqualTo("auth");
    assertThat(headerMap.get("stale")).isNull();

    checkNonceValid(headerMap.get("nonce"));
}

From source file:org.springframework.security.web.authentication.www.DigestAuthenticationEntryPointTests.java

@Test
public void testOperationIfDueToStaleNonce() throws Exception {
    DigestAuthenticationEntryPoint ep = new DigestAuthenticationEntryPoint();
    ep.setRealmName("hello");
    ep.setKey("key");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI("/some_path");

    MockHttpServletResponse response = new MockHttpServletResponse();

    ep.afterPropertiesSet();/*from   ww w .  j  ava  2 s  .  co m*/

    ep.commence(request, response, new NonceExpiredException("expired nonce"));

    // Check response is properly formed
    assertThat(response.getStatus()).isEqualTo(401);
    assertThat(response.getHeader("WWW-Authenticate").toString()).startsWith("Digest ");

    // Break up response header
    String header = response.getHeader("WWW-Authenticate").toString().substring(7);
    String[] headerEntries = StringUtils.commaDelimitedListToStringArray(header);
    Map<String, String> headerMap = DigestAuthUtils.splitEachArrayElementAndCreateMap(headerEntries, "=", "\"");

    assertThat(headerMap.get("realm")).isEqualTo("hello");
    assertThat(headerMap.get("qop")).isEqualTo("auth");
    assertThat(headerMap.get("stale")).isEqualTo("true");

    checkNonceValid(headerMap.get("nonce"));
}

From source file:org.springframework.security.web.authentication.www.DigestAuthenticationFilterTests.java

@Test
public void testExpiredNonceReturnsForbiddenWithStaleHeader() throws Exception {
    String nonce = generateNonce(0);
    String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM, PASSWORD, "GET", REQUEST_URI,
            QOP, nonce, NC, CNONCE);/*from  w  w  w .java  2 s .  c  om*/

    request.addHeader("Authorization",
            createAuthorizationHeader(USERNAME, REALM, nonce, REQUEST_URI, responseDigest, QOP, NC, CNONCE));

    Thread.sleep(1000); // ensures token expired

    MockHttpServletResponse response = executeFilterInContainerSimulator(filter, request, false);

    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
    assertThat(response.getStatus()).isEqualTo(401);

    String header = response.getHeader("WWW-Authenticate").toString().substring(7);
    String[] headerEntries = StringUtils.commaDelimitedListToStringArray(header);
    Map<String, String> headerMap = DigestAuthUtils.splitEachArrayElementAndCreateMap(headerEntries, "=", "\"");
    assertThat(headerMap.get("stale")).isEqualTo("true");
}