001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.authentication;
020:
021: import java.awt.*;
022: import java.net.*;
023: import java.rmi.*;
024:
025: import javax.swing.*;
026: import javax.xml.rpc.*;
027:
028: import org.openharmonise.him.*;
029: import org.openharmonise.him.authentication.gui.*;
030: import org.openharmonise.him.configuration.*;
031: import org.openharmonise.him.harmonise.*;
032: import org.openharmonise.him.harmonise.authentication.HarmoniseUser;
033: import org.openharmonise.swing.*;
034: import org.openharmonise.vfs.authentication.*;
035: import org.openharmonise.vfs.gui.*;
036: import org.openharmonise.vfs.servers.*;
037: import org.openharmonise.webdav.client.*;
038:
039: /**
040: * Wraps all the logic for the login sequence. Will display the login
041: * dialog and handle multiple attempts, validation with Harmonise server
042: * and allowing the user to change their password, either due to an expiry
043: * of the password or simply if they want to.
044: *
045: * @author Matthew Large
046: * @version $Revision: 1.2 $
047: *
048: */
049: public class LoginSequence {
050:
051: /**
052: * Authenication information built up by this sequence.
053: */
054: private AuthInfo m_authInfo = null;
055:
056: /**
057: * URL for the Harmonise User config web service.
058: */
059: private URL m_url = null;
060:
061: /**
062: * Harmonise WebDAV virtual file system.
063: */
064: private WebDAVFileSystem m_wdvfs = null;
065:
066: /**
067: * Login attempts in this sequence.
068: */
069: private int m_nLoginAttempts = 0;
070:
071: /**
072: *
073: */
074: public LoginSequence() {
075: super ();
076: }
077:
078: /**
079: * Runs the full login sequence.
080: *
081: * @param authStore Authentication store to use
082: * @return true if the login was successful
083: */
084: public boolean runSequence(AbstractAuthenticationStore authStore) {
085: String sValue = ConfigStore.getInstance().getPropertyValue(
086: "HARMONISE_SERVER");
087: if (sValue == null || sValue.equals("")) {
088: JFrame frame = new JFrame();
089: frame.setIconImage(((ImageIcon) IconManager.getInstance()
090: .getIcon("32-sim-logo.gif")).getImage());
091:
092: SingleTextEntryDialog dialog = new SingleTextEntryDialog(
093: frame, "Harmonise Server");
094: dialog
095: .setLabelText("Enter the address of the Harmonise server (http(s)://...)");
096: dialog.show();
097:
098: if (dialog.getTextValue() != null) {
099: sValue = dialog.getTextValue();
100: ConfigStore.getInstance().setProperty(
101: "HARMONISE_SERVER",
102: dialog.getTextValue().trim());
103: } else {
104: System.exit(1);
105: }
106: }
107:
108: Server server = null;
109: try {
110: server = new Server(new URI(sValue),
111: "org.openharmonise.webdav.client.WebDAVFileSystem",
112: authStore);
113: ServerList.getInstance().addHarmoniseServer(server);
114: } catch (URISyntaxException e) {
115: e.printStackTrace();
116: System.exit(1);
117: }
118:
119: URI uri = server.getURI();
120:
121: String sURI = uri.getScheme() + "://" + uri.getHost() + ":"
122: + uri.getPort() + "/webdav/services/HarmoniseService";
123: try {
124: m_url = new URL(sURI);
125: } catch (MalformedURLException e2) {
126: e2.printStackTrace();
127: System.exit(1);
128: }
129:
130: m_wdvfs = (WebDAVFileSystem) server.getVFS();
131:
132: boolean bLoginWorked = this .login();
133:
134: if (bLoginWorked) {
135: VFSUser user = new HarmoniseUser(ServerList.getInstance()
136: .getHarmoniseServer().getVFS(), this .m_wdvfs
137: .currentUserResourcePath(this .m_authInfo));
138: ;
139: this .m_authInfo.setUser(user);
140: }
141:
142: return bLoginWorked;
143: }
144:
145: /**
146: * Returns the authentication information for the logged in user.
147: *
148: * @return Authentication information or null if the login failed
149: */
150: public AuthInfo getAuthInfo() {
151: return this .m_authInfo;
152: }
153:
154: /**
155: * Displays the login dialog and attempts to use the user
156: * supplied information to log into the server.
157: *
158: * @return true if the login attempt was successful
159: */
160: private boolean login() {
161: boolean bWorked = false;
162:
163: if (this .m_nLoginAttempts < 3) {
164: JFrame tempFrame = new JFrame();
165: tempFrame.setTitle("Harmonise - Login");
166: tempFrame.setIconImage(((ImageIcon) IconManager
167: .getInstance().getIcon("32-sim-logo.gif"))
168: .getImage());
169: Dimension dims = tempFrame.getGraphicsConfiguration()
170: .getBounds().getSize();
171: tempFrame.setLocation(dims.width, dims.height);
172: tempFrame.setVisible(true);
173: LoginDialog loginDialog = new LoginDialog(tempFrame);
174: if (this .m_nLoginAttempts > 0) {
175: loginDialog
176: .setMessage("Warning incorrect username/password, reenter details");
177: }
178: loginDialog.show();
179:
180: this .m_nLoginAttempts++;
181:
182: String sUsername = loginDialog.getUsername();
183: String sPassword = loginDialog.getPassword();
184:
185: if (loginDialog.changePassword()) {
186: bWorked = this .changePassword("Change your password.");
187: if (!bWorked) {
188: this .login();
189: }
190: } else if (!sPassword.equals("")) {
191: bWorked = m_wdvfs.checkLoginDetails("/webdav",
192: sUsername, sPassword);
193: if (bWorked) {
194: this .m_authInfo = new AuthInfo();
195: this .m_authInfo.setUsername(sUsername);
196: this .m_authInfo.setPassword(sPassword);
197: } else {
198: boolean bExpired = false;
199: try {
200: bExpired = this
201: .hasExpired(sUsername, sPassword);
202: } catch (Exception e) {
203: e.printStackTrace();
204: }
205: if (bExpired) {
206: bWorked = this
207: .changePassword("Your password has expired, please change it.");
208: } else {
209: bWorked = this .login();
210: }
211: if (!bWorked) {
212: this .m_authInfo = null;
213: }
214: }
215: }
216: } else if (!bWorked) {
217: JFrame tempFrame = new JFrame();
218: tempFrame.setIconImage(((ImageIcon) IconManager
219: .getInstance().getIcon("32-sim-logo.gif"))
220: .getImage());
221: LoginMessageDialog messageDialog = new LoginMessageDialog(
222: tempFrame);
223: messageDialog
224: .setMessage("\n\n\tYou are locked out of the Harmonise system, please contact your system\n\tadministrator for further details.");
225: messageDialog.show();
226: }
227:
228: return bWorked;
229: }
230:
231: /**
232: * Checks if a username/password pair has expired.
233: *
234: * @param sUsername Username to check
235: * @param sPassword Password to check
236: * @return true if the username/password pair has expired
237: */
238: private boolean hasExpired(String sUsername, String sPassword) {
239: boolean bRetn = false;
240: try {
241: bRetn = UserConfigClient.hasPasswordExpired(m_url,
242: sUsername, sPassword);
243: } catch (RemoteException e1) {
244: e1.printStackTrace();
245: } catch (ServiceException e1) {
246: e1.printStackTrace();
247: }
248:
249: return bRetn;
250: }
251:
252: /**
253: * Displays the change password dialog and attempts to change the
254: * password.
255: *
256: * @param sMessage Message to display in the change password dialog
257: * @return true if the password was changed successfully
258: */
259: private boolean changePassword(String sMessage) {
260: boolean bWorked = false;
261:
262: JFrame tempFrame = new JFrame();
263: tempFrame.setTitle("Harmonise - Change Password");
264: tempFrame.setIconImage(((ImageIcon) IconManager.getInstance()
265: .getIcon("32-sim-logo.gif")).getImage());
266: Dimension dims = tempFrame.getGraphicsConfiguration()
267: .getBounds().getSize();
268: tempFrame.setLocation(dims.width, dims.height);
269: tempFrame.setVisible(true);
270: ChangePasswordDialog dialog = new ChangePasswordDialog(
271: tempFrame);
272: if (sMessage != null && !sMessage.equals("")) {
273: dialog.setInformationText(sMessage);
274: }
275: dialog.show();
276:
277: String sNewPassword = dialog.getNewPassword();
278: if (!sNewPassword.equals("")) {
279: try {
280: int nRetn = UserConfigClient.setPassword(m_url, dialog
281: .getUsername(), dialog.getPassword(), dialog
282: .getUsername(), dialog.getNewPassword());
283: String sChangePasswordMessage = null;
284: if (nRetn == UserConfigClient.CODE_AUTHENTICATION_FAIL) {
285: sChangePasswordMessage = "Your current username/password information was incorrect";
286: } else if (nRetn == UserConfigClient.CODE_INVALID_LENGTH) {
287: sChangePasswordMessage = "Your new password is not long enough";
288: } else if (nRetn == UserConfigClient.CODE_NO_ALPHA_CHAR) {
289: sChangePasswordMessage = "Your new password must contain at least one letter";
290: } else if (nRetn == UserConfigClient.CODE_NO_CASE_MIX) {
291: sChangePasswordMessage = "Your new password must contain mixed case letters";
292: } else if (nRetn == UserConfigClient.CODE_NO_NUM_CHAR) {
293: sChangePasswordMessage = "Your new password must contain at least one number";
294: } else if (nRetn == UserConfigClient.CODE_INVALID_USER_STATE) {
295: //this.changePassword("Your new password must contain mixed case letters");
296: // TODO Can't do anything from this point...... what to do....
297: } else if (nRetn == UserConfigClient.CODE_PWD_REPEAT) {
298: sChangePasswordMessage = "That password has been used too recently";
299: } else if (nRetn == UserConfigClient.CODE_SUCCESS) {
300: if (this .m_wdvfs.checkLoginDetails("/webdav",
301: dialog.getUsername(), dialog
302: .getNewPassword())) {
303: this .m_authInfo = new AuthInfo();
304: this .m_authInfo.setUsername(dialog
305: .getUsername());
306: this .m_authInfo.setPassword(dialog
307: .getNewPassword());
308: bWorked = true;
309: }
310: }
311: if (sChangePasswordMessage != null) {
312: this .m_authInfo = null;
313: bWorked = changePassword(sChangePasswordMessage);
314: }
315: } catch (RemoteException e) {
316: e.printStackTrace();
317: } catch (ServiceException e) {
318: e.printStackTrace();
319: }
320: } else {
321: this.m_nLoginAttempts--;
322: }
323:
324: return bWorked;
325: }
326:
327: }
|