ChangePasswordForm f = (ChangePasswordForm) form;
UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(getSessionInfo(request).getUser().getRealm());
if (!udb.supportsPasswordChange()) {
throw new Exception("Changing of passwords is not supported by the underlying user database.");
}
User user = LogonControllerFactory.getInstance().getUser(request);
SessionInfo info = this.getSessionInfo(request);
// Read in all of the confidential user attribute values
Properties confidentialAttributes = new Properties();
UserAttributes userAttributes = (UserAttributes) PropertyClassManager.getInstance().getPropertyClass(UserAttributes.NAME);
for (PropertyDefinition def : userAttributes.getDefinitions()) {
AttributeDefinition attrDef = (AttributeDefinition) def;
if (attrDef.getVisibility() == AttributeDefinition.USER_CONFIDENTIAL_ATTRIBUTE) {
confidentialAttributes.setProperty(def.getName(), attrDef.getPropertyClass()
.retrieveProperty(new UserAttributeKey(info.getUser(), def.getName())));
}
}
try {
// Change the password
udb.changePassword(user.getPrincipalName(), f.getOldPassword(), f.getNewPassword(), false);
PublicKeyStore publicKeyStore = PublicKeyStore.getInstance();
if ("automatic".equals(Property.getProperty(new SystemConfigKey("security.privateKeyMode")))) {
if (publicKeyStore.isPassphraseValid(user.getPrincipalName(), f.getOldPassword())) {
publicKeyStore.changePrivateKeyPassphrase(user.getPrincipalName(), f.getOldPassword(), f.getNewPassword());
publicKeyStore.removeCachedKeys(user.getPrincipalName());
publicKeyStore.verifyPrivateKey(user.getPrincipalName(), f.getNewPassword().toCharArray());
// Write back all of the confidential user attribute values
for (PropertyDefinition def : userAttributes.getDefinitions()) {
AttributeDefinition attrDef = (AttributeDefinition) def;
if (attrDef.getVisibility() == AttributeDefinition.USER_CONFIDENTIAL_ATTRIBUTE) {
Property.setProperty(new UserAttributeKey(info.getUser(), def.getName()), confidentialAttributes
.getProperty(def.getName()), info);
}
}
} else {
// if the keystore is out of sync, the only option is to remove the key
// this can happen if the password is changed natively
publicKeyStore.removeKeys(user.getPrincipalName());
CoreUtil.removePageInterceptListener(request.getSession(), "updatePrivateKeyPassphrase");
BundleActionMessage message = new BundleActionMessage("security", "resetPrivateKey.required.message");
GlobalWarningManager.getInstance().addToSession(new GlobalWarning(request.getSession(), message));
}
}
else {
publicKeyStore.removeCachedKeys(user.getPrincipalName());
}
CoreServlet.getServlet().fireCoreEvent(new CoreEvent(this,
CoreEventConstants.CHANGE_PASSWORD,
null,
info,