AccountSavingAuthenticationSuccessHandler implements AuthenticationSuccessHandler { public void setSavedAccountOptionCookie(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IllegalArgumentException { Object principal = authentication.getPrincipal(); if(!(principal instanceof UaaPrincipal)) { throw new IllegalArgumentException("Unrecognized authentication principle."); } UaaPrincipal uaaPrincipal = (UaaPrincipal) principal; if(IdentityZoneHolder.get().getConfig().isAccountChooserEnabled()) { SavedAccountOption savedAccountOption = new SavedAccountOption(); savedAccountOption.setEmail(uaaPrincipal.getEmail()); savedAccountOption.setOrigin(uaaPrincipal.getOrigin()); savedAccountOption.setUserId(uaaPrincipal.getId()); savedAccountOption.setUsername(uaaPrincipal.getName()); Cookie savedAccountCookie = new Cookie("Saved-Account-" + uaaPrincipal.getId(), encodeCookieValue(JsonUtils.writeValueAsString(savedAccountOption))); savedAccountCookie.setPath(request.getContextPath() + "/login"); savedAccountCookie.setHttpOnly(true); savedAccountCookie.setSecure(request.isSecure()); savedAccountCookie.setMaxAge(365*24*60*60); response.addCookie(savedAccountCookie); } Cookie currentUserCookie = null; try { currentUserCookie = currentUserCookieFactory.getCookie(uaaPrincipal); } catch (CurrentUserCookieFactory.CurrentUserCookieEncodingException e) { logger.error(String.format("There was an error while creating the Current-Account cookie for user %s", uaaPrincipal.getId()), e); } response.addCookie(currentUserCookie); } @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);  }
@Test public void invalid_principal_throws() { Authentication a = mock(Authentication.class); when(a.getPrincipal()).thenReturn(new Object()); try { successHandler.setSavedAccountOptionCookie(new MockHttpServletRequest(), new MockHttpServletResponse(), a); }catch (IllegalArgumentException x) { assertEquals("Unrecognized authentication principle.", x.getMessage()); } }