SpringDigestPasswordValidationCallbackHandler extends AbstractCallbackHandler implements InitializingBean { @Override protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException { if (callback instanceof PasswordValidationCallback) { PasswordValidationCallback passwordCallback = (PasswordValidationCallback) callback; if (passwordCallback.getRequest() instanceof PasswordValidationCallback.DigestPasswordRequest) { PasswordValidationCallback.DigestPasswordRequest request = (PasswordValidationCallback.DigestPasswordRequest) passwordCallback .getRequest(); String username = request.getUsername(); UserDetails user = loadUserDetails(username); if (user != null) { SpringSecurityUtils.checkUserValidity(user); request.setPassword(user.getPassword()); } SpringSecurityDigestPasswordValidator validator = new SpringSecurityDigestPasswordValidator(user); passwordCallback.setValidator(validator); return; } } else if (callback instanceof TimestampValidationCallback) { TimestampValidationCallback timestampCallback = (TimestampValidationCallback) callback; timestampCallback.setValidator(new DefaultTimestampValidator()); } else if (callback instanceof CleanupCallback) { SecurityContextHolder.clearContext(); return; } throw new UnsupportedCallbackException(callback); }  void setUserCache(UserCache userCache); void setUserDetailsService(UserDetailsService userDetailsService); @Override void afterPropertiesSet();  }
@Test public void testAuthenticateUserDigestUserNotFound() throws Exception { expect(userDetailsService.loadUserByUsername(username)).andThrow(new UsernameNotFoundException(username)); replay(userDetailsService); callbackHandler.handleInternal(callback); boolean authenticated = callback.getResult(); Assert.assertFalse("Authenticated", authenticated); Assert.assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication()); verify(userDetailsService); }
@Test public void testAuthenticateUserDigestValid() throws Exception { User user = new User(username, password, true, true, true, true, Collections.<GrantedAuthority> emptyList()); expect(userDetailsService.loadUserByUsername(username)).andReturn(user); replay(userDetailsService); callbackHandler.handleInternal(callback); boolean authenticated = callback.getResult(); Assert.assertTrue("Not authenticated", authenticated); Assert.assertNotNull("No Authentication created", SecurityContextHolder.getContext().getAuthentication()); verify(userDetailsService); }
@Test public void testAuthenticateUserDigestValidInvalid() throws Exception { User user = new User(username, "Big bird", true, true, true, true, Collections.<GrantedAuthority> emptyList()); expect(userDetailsService.loadUserByUsername(username)).andReturn(user); replay(userDetailsService); callbackHandler.handleInternal(callback); boolean authenticated = callback.getResult(); Assert.assertFalse("Authenticated", authenticated); Assert.assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication()); verify(userDetailsService); }
@Test public void testAuthenticateUserDigestDisabled() throws Exception { User user = new User(username, "Ernie", false, true, true, true, Collections.<GrantedAuthority> emptyList()); expect(userDetailsService.loadUserByUsername(username)).andReturn(user); replay(userDetailsService); try { callbackHandler.handleInternal(callback); Assert.fail("disabled user authenticated"); } catch (DisabledException expected) { } verify(userDetailsService); }
@Test public void testCleanUp() throws Exception { TestingAuthenticationToken authentication = new TestingAuthenticationToken(new Object(), new Object(), Collections.<GrantedAuthority> emptyList()); SecurityContextHolder.getContext().setAuthentication(authentication); CleanupCallback cleanupCallback = new CleanupCallback(); callbackHandler.handleInternal(cleanupCallback); Assert.assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication()); }