SimplePasswordValidationCallbackHandler 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.PlainTextPasswordRequest) { passwordCallback.setValidator(new SimplePlainTextPasswordValidator()); } else if (passwordCallback.getRequest() instanceof PasswordValidationCallback.DigestPasswordRequest) { PasswordValidationCallback.DigestPasswordRequest digestPasswordRequest = (PasswordValidationCallback.DigestPasswordRequest) passwordCallback .getRequest(); String password = users.get(digestPasswordRequest.getUsername()); digestPasswordRequest.setPassword(password); passwordCallback.setValidator(new PasswordValidationCallback.DigestPasswordValidator()); } } else if (callback instanceof TimestampValidationCallback) { TimestampValidationCallback timestampCallback = (TimestampValidationCallback) callback; timestampCallback.setValidator(new DefaultTimestampValidator()); } else { throw new UnsupportedCallbackException(callback); } }  void setUsers(Properties users); void setUsersMap(Map<String, String> users); @Override void afterPropertiesSet();  }
@Test public void testPlainTextPasswordValid() throws Exception { PasswordValidationCallback.PlainTextPasswordRequest request = new PasswordValidationCallback.PlainTextPasswordRequest( "Bert", "Ernie"); PasswordValidationCallback callback = new PasswordValidationCallback(request); handler.handleInternal(callback); boolean authenticated = callback.getResult(); Assert.assertTrue("Not authenticated", authenticated); }
@Test public void testPlainTextPasswordInvalid() throws Exception { PasswordValidationCallback.PlainTextPasswordRequest request = new PasswordValidationCallback.PlainTextPasswordRequest( "Bert", "Big bird"); PasswordValidationCallback callback = new PasswordValidationCallback(request); handler.handleInternal(callback); boolean authenticated = callback.getResult(); Assert.assertFalse("Authenticated", authenticated); }
@Test public void testPlainTextPasswordNoSuchUser() throws Exception { PasswordValidationCallback.PlainTextPasswordRequest request = new PasswordValidationCallback.PlainTextPasswordRequest( "Big bird", "Bert"); PasswordValidationCallback callback = new PasswordValidationCallback(request); handler.handleInternal(callback); boolean authenticated = callback.getResult(); Assert.assertFalse("Authenticated", authenticated); }
@Test public void testDigestPasswordValid() throws Exception { String username = "Bert"; String nonce = "9mdsYDCrjjYRur0rxzYt2oD7"; String passwordDigest = "kwNstEaiFOrI7B31j7GuETYvdgk="; String creationTime = "2006-06-01T23:48:42Z"; PasswordValidationCallback.DigestPasswordRequest request = new PasswordValidationCallback.DigestPasswordRequest( username, passwordDigest, nonce, creationTime); PasswordValidationCallback callback = new PasswordValidationCallback(request); handler.handleInternal(callback); boolean authenticated = callback.getResult(); Assert.assertTrue("Authenticated", authenticated); }
@Test public void testDigestPasswordInvalid() throws Exception { String username = "Bert"; String nonce = "9mdsYDCrjjYRur0rxzYt2oD7"; String passwordDigest = "kwNstEaiFOrI7B31j7GuETYvdgk"; String creationTime = "2006-06-01T23:48:42Z"; PasswordValidationCallback.DigestPasswordRequest request = new PasswordValidationCallback.DigestPasswordRequest( username, passwordDigest, nonce, creationTime); PasswordValidationCallback callback = new PasswordValidationCallback(request); handler.handleInternal(callback); boolean authenticated = callback.getResult(); Assert.assertFalse("Authenticated", authenticated); }