JwtAuthenticationFilter implements AmbariAuthenticationFilter { @Override public boolean shouldApply(HttpServletRequest httpServletRequest) { boolean shouldApply = false; if (jwtProperties != null) { String serializedJWT = getJWTFromCookie(httpServletRequest); shouldApply = (serializedJWT != null && isAuthenticationRequired(serializedJWT)); } return shouldApply; } JwtAuthenticationFilter(Configuration configuration, AuthenticationEntryPoint entryPoint, Users users);  JwtAuthenticationFilter(JwtAuthenticationProperties jwtProperties, AuthenticationEntryPoint entryPoint,
                                 Users users); @Override boolean shouldApply(HttpServletRequest httpServletRequest); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain); @Override void destroy();  }
@Test public void testShouldApplyTrue() throws JOSEException { JwtAuthenticationProperties properties = createTestProperties(); JwtAuthenticationFilter filter = new JwtAuthenticationFilter(properties, null, null); SignedJWT token = getInvalidToken(); Cookie cookie = createMock(Cookie.class); expect(cookie.getName()).andReturn("non-default").atLeastOnce(); expect(cookie.getValue()).andReturn(token.serialize()).atLeastOnce(); HttpServletRequest request = createMock(HttpServletRequest.class); expect(request.getCookies()).andReturn(new Cookie[]{cookie}); replay(request, cookie); Assert.assertTrue(filter.shouldApply(request)); verify(request, cookie); }
@Test public void testShouldApplyTrueBadToken() throws JOSEException { JwtAuthenticationProperties properties = createTestProperties(); JwtAuthenticationFilter filter = new JwtAuthenticationFilter(properties, null, null); Cookie cookie = createMock(Cookie.class); expect(cookie.getName()).andReturn("non-default").atLeastOnce(); expect(cookie.getValue()).andReturn("bad token").atLeastOnce(); HttpServletRequest request = createMock(HttpServletRequest.class); expect(request.getCookies()).andReturn(new Cookie[]{cookie}); replay(request, cookie); Assert.assertTrue(filter.shouldApply(request)); verify(request, cookie); }
@Test public void testShouldApplyFalseMissingCookie() throws JOSEException { JwtAuthenticationProperties properties = createTestProperties(); JwtAuthenticationFilter filter = new JwtAuthenticationFilter(properties, null, null); Cookie cookie = createMock(Cookie.class); expect(cookie.getName()).andReturn("some-other-cookie").atLeastOnce(); HttpServletRequest request = createMock(HttpServletRequest.class); expect(request.getCookies()).andReturn(new Cookie[]{cookie}); replay(request, cookie); Assert.assertFalse(filter.shouldApply(request)); verify(request, cookie); }
@Test public void testShouldApplyFalseNotEnabled() throws JOSEException { JwtAuthenticationFilter filter = new JwtAuthenticationFilter((JwtAuthenticationProperties) null, null, null); HttpServletRequest request = createMock(HttpServletRequest.class); replay(request); Assert.assertFalse(filter.shouldApply(request)); verify(request); }