CurrentUserCookieRequestFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { if (isAuthenticated()) { UaaPrincipal principal = (UaaPrincipal) getContext().getAuthentication().getPrincipal(); try { Cookie currentUserCookie = currentUserCookieFactory.getCookie(principal); response.addCookie(currentUserCookie); } catch (CurrentUserCookieFactory.CurrentUserCookieEncodingException e) { logger.error(errorMessage(principal), e); handleError(response, principal); return; } } else { Cookie currentUserCookie = currentUserCookieFactory.getNullCookie(); response.addCookie(currentUserCookie); } filterChain.doFilter(request, response); } CurrentUserCookieRequestFilter(CurrentUserCookieFactory currentUserCookieFactory);  static final String CURRENT_USER_COOKIE_ERROR; }
@Test public void whenUserIsAuthenticated_addsCurrentUserCookie() throws ServletException, IOException, CurrentUserCookieFactory.CurrentUserCookieEncodingException { UaaAuthentication authentication = new UaaAuthentication(new UaaPrincipal("user-guid", "marissa", "marissa@test.org", "uaa", "", ""), Collections.emptyList(), null); SecurityContextHolder.getContext().setAuthentication(authentication); when(currentUserCookieFactory.getCookie(any(UaaPrincipal.class))).thenReturn(new Cookie("Current-User", "current-user-cookie-value")); filter.doFilterInternal(req, res, filterChain); assertThat(res.getCookie("Current-User").getValue(), equalTo("current-user-cookie-value")); verify(filterChain).doFilter(req, res); }
@Test public void whenUserIsNotAuthenticated_clearsCurrentUserCookie() throws IOException, ServletException { when(currentUserCookieFactory.getNullCookie()).thenReturn(new Cookie("Current-User", null)); filter.doFilterInternal(req, res, filterChain); assertThat(res.getCookie("Current-User").getValue(), nullValue()); verify(filterChain).doFilter(req, res); }
@Test public void whenCurrentUserExceptionOccurs_respondWithInternalServerError() throws CurrentUserCookieFactory.CurrentUserCookieEncodingException, ServletException, IOException { UaaAuthentication authentication = new UaaAuthentication(new UaaPrincipal("user-guid", "marissa", "marissa@test.org", "uaa", "", ""), Collections.emptyList(), null); SecurityContextHolder.getContext().setAuthentication(authentication); when(currentUserCookieFactory.getCookie(any(UaaPrincipal.class))).thenThrow(currentUserCookieFactory.new CurrentUserCookieEncodingException(null)); filter.doFilterInternal(req, res, filterChain); assertEquals(500, res.getStatus()); assertEquals("application/json", res.getContentType()); assertThat(JsonUtils.readTree(res.getContentAsString()).get("error").textValue(), equalTo("current_user_cookie_error")); assertThat(JsonUtils.readTree(res.getContentAsString()).get("error_description").textValue(), equalTo("There was a problem while creating the Current-User cookie for user id user-guid")); verifyZeroInteractions(filterChain); }