1 19 20 package com.sslexplorer.security.forms; 21 22 import java.util.regex.Pattern ; 23 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpSession ; 26 27 import org.apache.struts.Globals; 28 import org.apache.struts.action.ActionErrors; 29 import org.apache.struts.action.ActionMapping; 30 import org.apache.struts.action.ActionMessage; 31 32 import com.sslexplorer.core.forms.CoreForm; 33 import com.sslexplorer.properties.Property; 34 import com.sslexplorer.properties.impl.realms.RealmKey; 35 import com.sslexplorer.security.User; 36 import com.sslexplorer.security.UserDatabaseException; 37 38 44 public class SetPasswordForm extends CoreForm { 45 46 private static final long serialVersionUID = -4783370317292273239L; 47 54 public static final String SAVED_PASSWORD = "setPassword.saved.password"; 55 58 public static final String SAVED_FORCE_PASSWORD_CHANGE = "setPassword.saved.forceChange"; 59 60 private String newPassword; 61 private String confirmPassword; 62 private User user; 63 private boolean forceChangePasswordAtLogon; 64 65 68 public void initialize(User user) { 69 this.user = user; 70 } 71 72 75 public boolean getForceChangePasswordAtLogon() { 76 return forceChangePasswordAtLogon; 77 } 78 79 82 public void setForceChangePasswordAtLogon(boolean forceChangePasswordAtLogon) { 83 this.forceChangePasswordAtLogon = forceChangePasswordAtLogon; 84 } 85 86 89 public String getNewPassword() { 90 return newPassword; 91 } 92 93 96 public String getConfirmPassword() { 97 return confirmPassword; 98 } 99 100 103 public void setNewPassword(String newPassword) { 104 this.newPassword = newPassword; 105 } 106 107 110 public void setConfirmPassword(String confirmPassword) { 111 this.confirmPassword = confirmPassword; 112 } 113 114 120 public void reset(ActionMapping mapping, HttpServletRequest request) { 121 newPassword = ""; 122 confirmPassword = ""; 123 forceChangePasswordAtLogon = false; 124 } 125 126 132 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { 133 if ("commit".equals(request.getParameter("action"))) { 134 HttpSession session = request.getSession(); 135 String passwordToSet = (String ) session.getAttribute(SAVED_PASSWORD); 136 if(passwordToSet == null) { 137 return validate(); 138 } else { 139 newPassword = passwordToSet; 140 confirmPassword = passwordToSet; 141 forceChangePasswordAtLogon = (Boolean ) session.getAttribute(SAVED_FORCE_PASSWORD_CHANGE); 142 session.removeAttribute(SAVED_PASSWORD); 143 session.removeAttribute(SAVED_FORCE_PASSWORD_CHANGE); 144 } 145 } 146 return null; 147 } 148 149 private ActionErrors validate() { 150 ActionErrors errors = new ActionErrors(); 151 try { 152 if (getNewPassword().length() == 0) { 153 errors.add(Globals.ERROR_KEY, new ActionMessage("setPassword.error.noNewPassword")); 154 } else if (!getNewPassword().equals(getConfirmPassword())) { 155 errors.add(Globals.ERROR_KEY, new ActionMessage("setPassword.error.newAndConfirmPasswordsDontMatch")); 156 } else { 157 try { 160 String pattern = Property.getProperty(new RealmKey("security.password.pattern", getUser().getRealm().getResourceId())); 161 Pattern p = Pattern.compile(pattern); 162 if (!p.matcher(newPassword).matches()) { 163 errors.add(Globals.ERROR_KEY, new ActionMessage("setPassword.error.doesNotMatchPolicy")); 164 } 165 } catch (Exception e) { 166 throw new UserDatabaseException("Could not check password against current policy.", e); 167 } 168 } 169 } catch (Exception e) { 170 errors.add(Globals.ERROR_KEY, new ActionMessage("setPassword.error.validateFailed", e.getMessage())); 171 } 172 return errors; 173 } 174 175 178 public User getUser() { 179 return user; 180 } 181 } | Popular Tags |