KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.struts.Globals;
28 import org.apache.struts.action.ActionErrors;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32 import org.apache.struts.action.ActionMessage;
33
34 import com.sslexplorer.core.CoreAttributeConstants;
35 import com.sslexplorer.core.CoreEvent;
36 import com.sslexplorer.core.CoreEventConstants;
37 import com.sslexplorer.core.CoreServlet;
38 import com.sslexplorer.core.CoreUtil;
39 import com.sslexplorer.core.actions.AuthenticatedDispatchAction;
40 import com.sslexplorer.security.LogonControllerFactory;
41 import com.sslexplorer.security.PublicKeyStore;
42 import com.sslexplorer.security.SessionInfo;
43 import com.sslexplorer.security.UpdatePrivateKeyPassphraseException;
44 import com.sslexplorer.security.forms.UpdatePrivateKeyPassphraseForm;
45
46 /**
47  * Implementation of
48  * {@link com.sslexplorer.core.actions.AuthenticatedDispatchAction} that is used
49  * when the passphrase of the users private must be changed.
50  * <p>
51  * This may happen for example if the key was created using their account
52  * password which has since changed.
53  *
54  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
55  * @see com.sslexplorer.security.forms.UpdatePrivateKeyPassphraseForm
56  */

57 public class UpdatePrivateKeyPassphraseDispatchAction extends AuthenticatedDispatchAction {
58     final static Log log = LogFactory.getLog(SetPasswordAction.class);
59
60     /**
61      * Constructor.
62      */

63     public UpdatePrivateKeyPassphraseDispatchAction() {
64         super();
65     }
66
67     /*
68      * (non-Javadoc)
69      *
70      * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping,
71      * org.apache.struts.action.ActionForm,
72      * javax.servlet.http.HttpServletRequest,
73      * javax.servlet.http.HttpServletResponse)
74      */

75     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
76                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
77         return mapping.findForward("display");
78     }
79
80     /**
81      * Commit the passphrase change.
82      *
83      * @param mapping mappng
84      * @param form form
85      * @param request request
86      * @param response response
87      * @return forward
88      * @throws Exception
89      */

90     public ActionForward commit(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
91                     throws Exception JavaDoc {
92         UpdatePrivateKeyPassphraseForm f = (UpdatePrivateKeyPassphraseForm) form;
93         
94         if (f.isResetPrivateKey()){
95             // user has opted to reset his key, this will mean all personal info will be lost.
96
PublicKeyStore.getInstance().removeKeys(getSessionInfo(request).getUser().getPrincipalName());
97             return cleanUpAndReturn(mapping, request, mapping.findForward("confirmReset"));
98         }
99         else{
100             /*
101              * Need to verify using the old password so the confidential
102              * user attributes can be decrypted
103              */

104             try {
105                 PublicKeyStore.getInstance().verifyPrivateKey(getSessionInfo(request).getUser().getPrincipalName(), f.getOldPassphrase().toCharArray());
106             } catch (UpdatePrivateKeyPassphraseException upkpe) {
107                 // incorrect passphrase
108
ActionErrors errs = new ActionErrors();
109                 errs.add(Globals.ERROR_KEY, new ActionMessage("updatePrivateKeyPassphrase.error.incorrectPassphrase"));
110                 saveErrors(request.getSession(), errs);
111                 return mapping.getInputForward();
112             }
113
114             /*
115              * Now change the passphrase
116              */

117             PublicKeyStore.getInstance().changePrivateKeyPassphrase(
118                             getSessionInfo(request).getUser().getPrincipalName(),
119                             f.getOldPassphrase(),
120                             new String JavaDoc(LogonControllerFactory.getInstance().getPasswordFromCredentials(
121                                             getSessionInfo(request).getCredentials())));
122             return cleanUpAndReturn(mapping, request, mapping.findForward("success"));
123         }
124     }
125
126     private ActionForward cleanUpAndReturn(ActionMapping mapping, HttpServletRequest JavaDoc request, ActionForward af) {
127         CoreUtil.removePageInterceptListener(request.getSession(), "updatePrivateKeyPassphrase");
128         /*
129          * And update the user attributes and fire the logon event
130          */

131         CoreServlet.getServlet().fireCoreEvent(
132                         new CoreEvent(this, CoreEventConstants.LOGON, getSessionInfo(request).getCredentials(),
133                                         getSessionInfo(request)).addAttribute(CoreAttributeConstants.EVENT_ATTR_IP_ADDRESS,
134                                         request.getRemoteAddr()).addAttribute(CoreAttributeConstants.EVENT_ATTR_HOST,
135                                         request.getRemoteHost()));
136
137         return af;
138     }
139
140     /**
141      * Cancel and logout.
142      *
143      * @param mapping mappng
144      * @param form form
145      * @param request request
146      * @param response response
147      * @return forward
148      * @throws Exception
149      */

150     public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
151                     throws Exception JavaDoc {
152         return mapping.findForward("cancel");
153     }
154
155     /*
156      * (non-Javadoc)
157      *
158      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping,
159      * org.apache.struts.action.ActionForm,
160      * javax.servlet.http.HttpServletRequest,
161      * javax.servlet.http.HttpServletResponse)
162      */

163     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
164         return SessionInfo.USER_CONSOLE_CONTEXT | SessionInfo.MANAGEMENT_CONSOLE_CONTEXT;
165     }
166
167 }
Popular Tags