KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > actions > ChangePasswordAction


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security.actions;
21
22 import java.text.DateFormat JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
63  */

64 public class ChangePasswordAction extends AuthenticatedAction {
65     /**
66      */

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 JavaDoc request, HttpServletResponse JavaDoc response)
72                     throws Exception JavaDoc {
73
74         ChangePasswordForm f = (ChangePasswordForm) form;
75         UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(getSessionInfo(request).getUser().getRealm());
76         if (!udb.supportsPasswordChange()) {
77             throw new Exception JavaDoc("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         // Read in all of the confidential user attribute values
84
Properties JavaDoc confidentialAttributes = new Properties JavaDoc();
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             // Change the password
97

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                 // Write back all of the confidential user attribute values
106
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 JavaDoc 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 JavaDoc 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 JavaDoc formatDate(Date JavaDoc toFormat) {
150         DateFormat JavaDoc format = new SimpleDateFormat JavaDoc("dd/MM/yyyy HH:mm");
151         return format.format(toFormat);
152     }
153     
154     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
155         return SessionInfo.USER_CONSOLE_CONTEXT;
156     }
157
158 }
Popular Tags