1 19 20 package com.sslexplorer.security.actions; 21 22 import java.text.DateFormat ; 23 import java.text.SimpleDateFormat ; 24 import java.util.Date ; 25 import java.util.Properties ; 26 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.apache.struts.action.ActionForm; 31 import org.apache.struts.action.ActionForward; 32 import org.apache.struts.action.ActionMapping; 33 import org.apache.struts.action.ActionMessage; 34 import org.apache.struts.action.ActionMessages; 35 36 import com.sslexplorer.boot.PropertyClassManager; 37 import com.sslexplorer.boot.PropertyDefinition; 38 import com.sslexplorer.core.CoreEvent; 39 import com.sslexplorer.core.CoreEventConstants; 40 import com.sslexplorer.core.CoreServlet; 41 import com.sslexplorer.core.CoreUtil; 42 import com.sslexplorer.core.UserDatabaseManager; 43 import com.sslexplorer.core.actions.AuthenticatedAction; 44 import com.sslexplorer.policyframework.Permission; 45 import com.sslexplorer.policyframework.PolicyConstants; 46 import com.sslexplorer.properties.Property; 47 import com.sslexplorer.properties.attributes.AttributeDefinition; 48 import com.sslexplorer.properties.impl.systemconfig.SystemConfigKey; 49 import com.sslexplorer.properties.impl.userattributes.UserAttributeKey; 50 import com.sslexplorer.properties.impl.userattributes.UserAttributes; 51 import com.sslexplorer.security.Constants; 52 import com.sslexplorer.security.InvalidLoginCredentialsException; 53 import com.sslexplorer.security.LogonControllerFactory; 54 import com.sslexplorer.security.PasswordChangeTooSoonException; 55 import com.sslexplorer.security.PasswordPolicyViolationException; 56 import com.sslexplorer.security.PublicKeyStore; 57 import com.sslexplorer.security.SessionInfo; 58 import com.sslexplorer.security.User; 59 import com.sslexplorer.security.UserDatabase; 60 import com.sslexplorer.security.forms.ChangePasswordForm; 61 62 64 public class ChangePasswordAction extends AuthenticatedAction { 65 67 public ChangePasswordAction() { 68 super(PolicyConstants.PASSWORD_RESOURCE_TYPE, new Permission[] { PolicyConstants.PERM_CHANGE }); 69 } 70 71 public ActionForward onExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 72 throws Exception { 73 74 ChangePasswordForm f = (ChangePasswordForm) form; 75 UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(getSessionInfo(request).getUser().getRealm()); 76 if (!udb.supportsPasswordChange()) { 77 throw new Exception ("Changing of passwords is not supported by the underlying user database."); 78 } 79 User user = LogonControllerFactory.getInstance().getUser(request); 80 81 SessionInfo info = this.getSessionInfo(request); 82 83 Properties confidentialAttributes = new Properties (); 85 UserAttributes userAttributes = (UserAttributes) PropertyClassManager.getInstance().getPropertyClass(UserAttributes.NAME); 86 for (PropertyDefinition def : userAttributes.getDefinitions()) { 87 AttributeDefinition attrDef = (AttributeDefinition) def; 88 if (attrDef.getVisibility() == AttributeDefinition.USER_CONFIDENTIAL_ATTRIBUTE) { 89 confidentialAttributes.setProperty(def.getName(), attrDef.getPropertyClass() 90 .retrieveProperty(new UserAttributeKey(info.getUser(), def.getName()))); 91 } 92 } 93 94 try { 95 96 98 udb.changePassword(user.getPrincipalName(), f.getOldPassword(), f.getNewPassword(), false); 99 100 if ("automatic".equals(Property.getProperty(new SystemConfigKey("security.privateKeyMode")))) { 101 PublicKeyStore.getInstance().changePrivateKeyPassphrase(user.getPrincipalName(), f.getOldPassword(), f.getNewPassword()); 102 PublicKeyStore.getInstance().removeCachedKeys(user.getPrincipalName()); 103 PublicKeyStore.getInstance().verifyPrivateKey(user.getPrincipalName(), f.getNewPassword().toCharArray()); 104 105 for (PropertyDefinition def : userAttributes.getDefinitions()) { 107 AttributeDefinition attrDef = (AttributeDefinition) def; 108 if (attrDef.getVisibility() == AttributeDefinition.USER_CONFIDENTIAL_ATTRIBUTE) { 109 Property.setProperty(new UserAttributeKey(info.getUser(), def.getName()), confidentialAttributes.getProperty(def.getName()) , info); 110 } 111 } 112 } 113 else { 114 PublicKeyStore.getInstance().removeCachedKeys(user.getPrincipalName()); 115 } 116 CoreServlet.getServlet().fireCoreEvent(new CoreEvent(this, 117 CoreEventConstants.CHANGE_PASSWORD, 118 null, 119 info, 120 CoreEvent.STATE_SUCCESSFUL)); 121 request.getSession().removeAttribute(Constants.PASSWORD_CHANGE_REASON_MESSAGE); 122 CoreUtil.removeGlobalWarning(request.getSession(), "globalWarning.passwordNearExpiry"); 123 CoreUtil.removePageInterceptListener(request.getSession(), "changePassword"); 124 } catch (InvalidLoginCredentialsException e) { 125 CoreServlet.getServlet().fireCoreEvent(new CoreEvent(this, CoreEventConstants.CHANGE_PASSWORD, null, info, e)); 126 127 ActionMessages errors = new ActionMessages(); 128 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("security.cannotChangePassword", e.getMessage())); 129 saveErrors(request, errors); 130 return mapping.findForward("failure"); 131 132 } catch (PasswordChangeTooSoonException e) { 133 CoreServlet.getServlet().fireCoreEvent(new CoreEvent(this, CoreEventConstants.CHANGE_PASSWORD, null, info, e)); 134 Date requiredData = ((PasswordChangeTooSoonException) e).getRequiredDate(); 135 saveError(request, "security.cannotChangePassword.tooSoon", formatDate(requiredData)); 136 return mapping.findForward("failure"); 137 } catch (PasswordPolicyViolationException e) { 138 CoreServlet.getServlet().fireCoreEvent(new CoreEvent(this, CoreEventConstants.CHANGE_PASSWORD, null, info, e)); 139 saveError(request, "changePassword.error.doesNotMatchPolicy"); 140 return mapping.findForward("failure"); 141 } catch (Exception e) { 142 CoreServlet.getServlet().fireCoreEvent(new CoreEvent(this, CoreEventConstants.CHANGE_PASSWORD, null, info, e)); 143 throw e; 144 } 145 146 return mapping.findForward("success"); 147 } 148 149 private static String formatDate(Date toFormat) { 150 DateFormat format = new SimpleDateFormat ("dd/MM/yyyy HH:mm"); 151 return format.format(toFormat); 152 } 153 154 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 155 return SessionInfo.USER_CONSOLE_CONTEXT; 156 } 157 158 } | Popular Tags |