KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > authentication > LoginSequence


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.authentication;
20
21 import java.awt.*;
22 import java.net.*;
23 import java.rmi.*;
24
25 import javax.swing.*;
26 import javax.xml.rpc.*;
27
28 import org.openharmonise.him.*;
29 import org.openharmonise.him.authentication.gui.*;
30 import org.openharmonise.him.configuration.*;
31 import org.openharmonise.him.harmonise.*;
32 import org.openharmonise.him.harmonise.authentication.HarmoniseUser;
33 import org.openharmonise.swing.*;
34 import org.openharmonise.vfs.authentication.*;
35 import org.openharmonise.vfs.gui.*;
36 import org.openharmonise.vfs.servers.*;
37 import org.openharmonise.webdav.client.*;
38
39
40 /**
41  * Wraps all the logic for the login sequence. Will display the login
42  * dialog and handle multiple attempts, validation with Harmonise server
43  * and allowing the user to change their password, either due to an expiry
44  * of the password or simply if they want to.
45  *
46  * @author Matthew Large
47  * @version $Revision: 1.2 $
48  *
49  */

50 public class LoginSequence {
51
52     /**
53      * Authenication information built up by this sequence.
54      */

55     private AuthInfo m_authInfo = null;
56     
57     /**
58      * URL for the Harmonise User config web service.
59      */

60     private URL m_url = null;
61     
62     /**
63      * Harmonise WebDAV virtual file system.
64      */

65     private WebDAVFileSystem m_wdvfs = null;
66     
67     /**
68      * Login attempts in this sequence.
69      */

70     private int m_nLoginAttempts = 0;
71
72     /**
73      *
74      */

75     public LoginSequence() {
76         super();
77     }
78     
79     /**
80      * Runs the full login sequence.
81      *
82      * @param authStore Authentication store to use
83      * @return true if the login was successful
84      */

85     public boolean runSequence(AbstractAuthenticationStore authStore) {
86         String JavaDoc sValue = ConfigStore.getInstance().getPropertyValue("HARMONISE_SERVER");
87         if(sValue==null || sValue.equals("")) {
88             JFrame frame = new JFrame();
89             frame.setIconImage( ((ImageIcon)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() );
90         
91             SingleTextEntryDialog dialog = new SingleTextEntryDialog(frame, "Harmonise Server");
92             dialog.setLabelText("Enter the address of the Harmonise server (http(s)://...)");
93             dialog.show();
94             
95             if(dialog.getTextValue()!=null) {
96                 sValue = dialog.getTextValue();
97                 ConfigStore.getInstance().setProperty("HARMONISE_SERVER", dialog.getTextValue().trim());
98             } else {
99                 System.exit(1);
100             }
101         }
102
103         Server server = null;
104         try {
105             server =
106                 new Server(new URI(sValue),"org.openharmonise.webdav.client.WebDAVFileSystem",authStore);
107             ServerList.getInstance().addHarmoniseServer(server);
108         } catch (URISyntaxException e) {
109             e.printStackTrace();
110             System.exit(1);
111         }
112         
113         URI uri = server.getURI();
114         
115         String JavaDoc sURI = uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort() + "/webdav/services/HarmoniseService";
116         try {
117             m_url = new URL(sURI);
118         } catch (MalformedURLException e2) {
119             e2.printStackTrace();
120             System.exit(1);
121         }
122         
123         m_wdvfs = (WebDAVFileSystem) server.getVFS();
124         
125         boolean bLoginWorked = this.login();
126         
127         if(bLoginWorked) {
128             VFSUser user = new HarmoniseUser(ServerList.getInstance().getHarmoniseServer().getVFS(), this.m_wdvfs.currentUserResourcePath(this.m_authInfo));;
129             this.m_authInfo.setUser( user );
130         }
131         
132         return bLoginWorked;
133     }
134     
135     /**
136      * Returns the authentication information for the logged in user.
137      *
138      * @return Authentication information or null if the login failed
139      */

140     public AuthInfo getAuthInfo() {
141         return this.m_authInfo;
142     }
143     
144     /**
145      * Displays the login dialog and attempts to use the user
146      * supplied information to log into the server.
147      *
148      * @return true if the login attempt was successful
149      */

150     private boolean login() {
151         boolean bWorked = false;
152         
153         if(this.m_nLoginAttempts<3) {
154             JFrame tempFrame = new JFrame();
155             tempFrame.setTitle("Harmonise - Login");
156             tempFrame.setIconImage( ((ImageIcon)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() );
157             Dimension dims = tempFrame.getGraphicsConfiguration().getBounds().getSize();
158             tempFrame.setLocation(dims.width,dims.height);
159             tempFrame.setVisible(true);
160             LoginDialog loginDialog = new LoginDialog(tempFrame);
161             if(this.m_nLoginAttempts>0) {
162                 loginDialog.setMessage("Warning incorrect username/password, reenter details");
163             }
164             loginDialog.show();
165             
166             this.m_nLoginAttempts++;
167             
168             String JavaDoc sUsername = loginDialog.getUsername();
169             String JavaDoc sPassword = loginDialog.getPassword();
170         
171             if(loginDialog.changePassword()) {
172                bWorked = this.changePassword("Change your password.");
173                if(!bWorked) {
174                    this.login();
175                }
176            }else if(!sPassword.equals("")) {
177             bWorked = m_wdvfs.checkLoginDetails("/webdav", sUsername, sPassword);
178             if(bWorked) {
179                 this.m_authInfo = new AuthInfo();
180                 this.m_authInfo.setUsername(sUsername);
181                 this.m_authInfo.setPassword(sPassword);
182             } else {
183                 boolean bExpired = false;
184                 try {
185                     bExpired = this.hasExpired(sUsername, sPassword);
186                 } catch(Exception JavaDoc e) {
187                     e.printStackTrace();
188                 }
189                 if(bExpired) {
190                     bWorked = this.changePassword("Your password has expired, please change it.");
191                 } else {
192                     bWorked = this.login();
193                 }
194                 if(!bWorked) {
195                     this.m_authInfo = null;
196                 }
197             }
198         }
199         } else if(!bWorked) {
200             JFrame tempFrame = new JFrame();
201             tempFrame.setIconImage( ((ImageIcon)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() );
202             LoginMessageDialog messageDialog = new LoginMessageDialog(tempFrame);
203             messageDialog.setMessage("\n\n\tYou are locked out of the Harmonise system, please contact your system\n\tadministrator for further details.");
204             messageDialog.show();
205         }
206         
207         return bWorked;
208     }
209     
210     /**
211      * Checks if a username/password pair has expired.
212      *
213      * @param sUsername Username to check
214      * @param sPassword Password to check
215      * @return true if the username/password pair has expired
216      */

217     private boolean hasExpired(String JavaDoc sUsername, String JavaDoc sPassword) {
218         boolean bRetn = false;
219         try {
220             bRetn = UserConfigClient.hasPasswordExpired(m_url, sUsername, sPassword);
221         } catch (RemoteException e1) {
222             e1.printStackTrace();
223         } catch (ServiceException e1) {
224             e1.printStackTrace();
225         }
226         
227         return bRetn;
228     }
229     
230     /**
231      * Displays the change password dialog and attempts to change the
232      * password.
233      *
234      * @param sMessage Message to display in the change password dialog
235      * @return true if the password was changed successfully
236      */

237     private boolean changePassword(String JavaDoc sMessage) {
238         boolean bWorked = false;
239         
240         JFrame tempFrame = new JFrame();
241         tempFrame.setTitle("Harmonise - Change Password");
242         tempFrame.setIconImage( ((ImageIcon)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() );
243         Dimension dims = tempFrame.getGraphicsConfiguration().getBounds().getSize();
244         tempFrame.setLocation(dims.width,dims.height);
245         tempFrame.setVisible(true);
246         ChangePasswordDialog dialog = new ChangePasswordDialog(tempFrame);
247         if(sMessage!=null && !sMessage.equals("")) {
248             dialog.setInformationText(sMessage);
249         }
250         dialog.show();
251                     
252         String JavaDoc sNewPassword = dialog.getNewPassword();
253         if(!sNewPassword.equals("")) {
254             try {
255                 int nRetn = UserConfigClient.setPassword(m_url, dialog.getUsername(), dialog.getPassword(), dialog.getUsername(), dialog.getNewPassword());
256                 String JavaDoc sChangePasswordMessage = null;
257                 if(nRetn==UserConfigClient.CODE_AUTHENTICATION_FAIL) {
258                     sChangePasswordMessage = "Your current username/password information was incorrect";
259                 } else if(nRetn==UserConfigClient.CODE_INVALID_LENGTH) {
260                     sChangePasswordMessage = "Your new password is not long enough";
261                 } else if(nRetn==UserConfigClient.CODE_NO_ALPHA_CHAR) {
262                     sChangePasswordMessage = "Your new password must contain at least one letter";
263                 } else if(nRetn==UserConfigClient.CODE_NO_CASE_MIX) {
264                     sChangePasswordMessage = "Your new password must contain mixed case letters";
265                 } else if(nRetn==UserConfigClient.CODE_NO_NUM_CHAR) {
266                     sChangePasswordMessage = "Your new password must contain at least one number";
267                 } else if(nRetn==UserConfigClient.CODE_INVALID_USER_STATE) {
268                     //this.changePassword("Your new password must contain mixed case letters");
269
// TODO Can't do anything from this point...... what to do....
270
} else if(nRetn==UserConfigClient.CODE_PWD_REPEAT) {
271                     sChangePasswordMessage = "That password has been used too recently";
272                 } else if(nRetn==UserConfigClient.CODE_SUCCESS) {
273                     if(this.m_wdvfs.checkLoginDetails("/webdav", dialog.getUsername(), dialog.getNewPassword())) {
274                         this.m_authInfo = new AuthInfo();
275                         this.m_authInfo.setUsername(dialog.getUsername());
276                         this.m_authInfo.setPassword(dialog.getNewPassword());
277                         bWorked = true;
278                     }
279                 }
280                 if(sChangePasswordMessage != null){
281                     this.m_authInfo = null;
282                     bWorked = changePassword(sChangePasswordMessage);
283                 }
284             } catch (RemoteException e) {
285                 e.printStackTrace();
286             } catch (ServiceException e) {
287                 e.printStackTrace();
288             }
289         } else {
290             this.m_nLoginAttempts--;
291         }
292         
293         return bWorked;
294     }
295
296 }
297
Popular Tags