KnoxSSOAuthenticationFilter implements Filter { protected String getJWTFromCookie(HttpServletRequest req) { String serializedJWT = null; Cookie[] cookies = req.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { LOG.debug(String.format("Found cookie: %s [%s]", cookie.getName(), cookie.getValue())); if (knoxCookie.equals(cookie.getName())) { if (LOG.isDebugEnabled()) { LOG.debug(knoxCookie + " cookie has been found and is being processed"); } serializedJWT = cookie.getValue(); break; } } } else { if (LOG.isDebugEnabled()) { LOG.debug(knoxCookie + " not found"); } } return serializedJWT; } KnoxSSOAuthenticationFilter(String userSearchBase,
                                     Path knoxKeyFile,
                                     String knoxKeyString,
                                     String knoxCookie,
                                     LdapTemplate ldapTemplate); @Override void init(FilterConfig filterConfig); @Override void destroy(); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain);  }
@Test public void getJWTFromCookieShouldProperlyReturnToken() { KnoxSSOAuthenticationFilter knoxSSOAuthenticationFilter = spy(new KnoxSSOAuthenticationFilter("userSearchBase", mock(Path.class), "knoxKeyString", "knoxCookie", mock(LdapTemplate.class) )); HttpServletRequest request = mock(HttpServletRequest.class); { assertNull(knoxSSOAuthenticationFilter.getJWTFromCookie(request)); } { Cookie cookie = new Cookie("someCookie", "someValue"); when(request.getCookies()).thenReturn(new Cookie[]{cookie}); assertNull(knoxSSOAuthenticationFilter.getJWTFromCookie(request)); } { Cookie cookie = new Cookie("knoxCookie", "token"); when(request.getCookies()).thenReturn(new Cookie[]{cookie}); assertEquals("token", knoxSSOAuthenticationFilter.getJWTFromCookie(request)); } }