AccountSavingAuthenticationSuccessHandler implements AuthenticationSuccessHandler { @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { setSavedAccountOptionCookie(request, response, authentication); redirectingHandler.onAuthenticationSuccess(request, response, authentication); } @Autowired  AccountSavingAuthenticationSuccessHandler(SavedRequestAwareAuthenticationSuccessHandler redirectingHandler, CurrentUserCookieFactory currentUserCookieFactory); @Override void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication); void setSavedAccountOptionCookie(HttpServletRequest request, HttpServletResponse response, Authentication authentication); static String encodeCookieValue(String inValue);  }
@SuppressWarnings("deprecation") @Test public void whenSuccessfullyAuthenticated_accountGetsSavedViaCookie() throws IOException, ServletException, CurrentUserCookieFactory.CurrentUserCookieEncodingException { IdentityZoneHolder.get().getConfig().setAccountChooserEnabled(true); Date yesterday = new Date(System.currentTimeMillis()-(1000*60*60*24)); UaaUser user = new UaaUser( "user-id", "username", "password", "email", Collections.EMPTY_LIST, "given name", "family name", yesterday, yesterday, "user-origin", null, true, IdentityZone.getUaaZoneId(), "salt", yesterday ); UaaPrincipal principal = new UaaPrincipal(user); UaaAuthentication authentication = new UaaAuthentication(principal, null, Collections.EMPTY_LIST, null, true, System.currentTimeMillis()); MockHttpServletRequest request = new MockHttpServletRequest(); request.setSecure(secure); MockHttpServletResponse response = new MockHttpServletResponse(); successHandler.onAuthenticationSuccess(request, response, authentication); Cookie accountOptionCookie = response.getCookie("Saved-Account-user-id"); assertThat(accountOptionCookie, notNullValue()); String cookieValue = accountOptionCookie.getValue(); SavedAccountOption expectedCookieValue = new SavedAccountOption(); expectedCookieValue.setUserId(user.getId()); expectedCookieValue.setUsername(user.getUsername()); expectedCookieValue.setEmail(user.getEmail()); expectedCookieValue.setOrigin(user.getOrigin()); assertEquals(URLEncoder.encode(JsonUtils.writeValueAsString(expectedCookieValue)), cookieValue); assertTrue(accountOptionCookie.isHttpOnly()); assertEquals(365*24*60*60, accountOptionCookie.getMaxAge()); assertEquals("/login", accountOptionCookie.getPath()); Assert.assertEquals(secure, accountOptionCookie.getSecure()); verify(redirectingHandler, times(1)).onAuthenticationSuccess(request, response, authentication); ArgumentCaptor<UaaPrincipal> uaaPrincipal = ArgumentCaptor.forClass(UaaPrincipal.class); verify(currentUserCookieFactory).getCookie(uaaPrincipal.capture()); assertEquals("user-id", uaaPrincipal.getValue().getId()); Cookie currentUserCookie = response.getCookie("Current-User"); assertThat(currentUserCookie, notNullValue()); assertThat(currentUserCookie.getValue(), containsString("user-id")); }
@Test public void empty_Account_Cookie() throws IOException, ServletException { IdentityZoneHolder.get().getConfig().setAccountChooserEnabled(false); Date yesterday = new Date(System.currentTimeMillis()-(1000*60*60*24)); UaaUser user = new UaaUser( "user-id", "username", "password", "email", Collections.EMPTY_LIST, "given name", "family name", yesterday, yesterday, "user-origin", null, true, IdentityZone.getUaaZoneId(), "salt", yesterday ); UaaPrincipal principal = new UaaPrincipal(user); UaaAuthentication authentication = new UaaAuthentication(principal, null, Collections.EMPTY_LIST, null, true, System.currentTimeMillis()); MockHttpServletRequest request = new MockHttpServletRequest(); request.setSecure(secure); MockHttpServletResponse response = new MockHttpServletResponse(); successHandler.onAuthenticationSuccess(request, response, authentication); Cookie accountOptionCookie = response.getCookie("Saved-Account-user-id"); assertThat(accountOptionCookie, nullValue()); }