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.io.ByteArrayInputStream;
022: import java.io.IOException;
023: import java.net.URI;
024: import java.util.Iterator;
025:
026: import javax.swing.ImageIcon;
027: import javax.swing.JFrame;
028: import javax.xml.parsers.DocumentBuilderFactory;
029: import javax.xml.parsers.FactoryConfigurationError;
030: import javax.xml.parsers.ParserConfigurationException;
031:
032: import org.openharmonise.commons.xml.*;
033: import org.openharmonise.commons.xml.namespace.*;
034: import org.openharmonise.him.authentication.gui.*;
035: import org.openharmonise.him.configuration.*;
036: import org.openharmonise.vfs.*;
037: import org.openharmonise.vfs.authentication.*;
038: import org.openharmonise.vfs.gui.*;
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Element;
041: import org.w3c.dom.NodeList;
042: import org.w3c.dom.Text;
043: import org.xml.sax.SAXException;
044:
045: import sun.misc.BASE64Encoder;
046:
047: /**
048: * This class handles all the authentication work for the local
049: * Content Manager application. It will store a set authentication information
050: * for users in files on the local filesystem, encrypting them. It will prompt users
051: * to log in to the authentication store and then either provide autemtication
052: * information to requesting VirtualFileSystems, or prompt the users to provide this
053: * information and then store it.
054: *
055: * @author Matthew Large
056: * @version $Revision: 1.1 $
057: *
058: */
059: public class LocalAuthenticationStore extends
060: AbstractAuthenticationStore {
061:
062: /**
063: * Username.
064: */
065: private String m_sUsername = null;
066:
067: /**
068: * Password.
069: */
070: private String m_sPassword = null;
071:
072: /**
073: * Full path to local authentication store collection in local virtual file system.
074: */
075: private String m_sAuthDir = "/ContentManager/auths/las/";
076:
077: /**
078: * Full path to authentication information virtuall file in local virtual file system.
079: */
080: private String m_sAuthFilePath = null;
081:
082: /**
083: * Default contructor.
084: */
085: public LocalAuthenticationStore() {
086: super ();
087: this .readFromFile();
088: }
089:
090: /**
091: * Construcs a LocalAuthenticationStore for a specific username.
092: *
093: * @param sUsername Username to find authentication store file for
094: */
095: public LocalAuthenticationStore(String sUsername) {
096: super ();
097: this .m_sUsername = sUsername;
098: this .readFromFile();
099: }
100:
101: /**
102: * Returns the username for this local authentication store.
103: *
104: * @return Username
105: */
106: public String getUsername() {
107: return this .m_sUsername;
108: }
109:
110: /**
111: * Saves all authentication information to an encrypted authentication store
112: * file.
113: */
114: public void save() {
115: this .writeToFile();
116: }
117:
118: /**
119: * To check if there is a user logged in to this Authentication Store.
120: *
121: * @return True if there is a user logged into this Authentication Store
122: */
123: public boolean isLoggedIn() {
124: return !(m_sUsername == null || this .m_sPassword == null);
125: }
126:
127: /**
128: * Shows a login window requesting a username and password from the user.
129: */
130: private void login() {
131: JFrame tempFrame = new JFrame();
132: tempFrame.setIconImage(((ImageIcon) IconManager.getInstance()
133: .getIcon("32-sim-logo.gif")).getImage());
134: LoginDialog loginDialog = new LoginDialog(tempFrame);
135: loginDialog.show();
136: this .m_sUsername = loginDialog.getUsername();
137: this .m_sPassword = loginDialog.getPassword();
138: }
139:
140: /**
141: * Loads a user's authentication information from an authentication store file.
142: */
143: private void readFromFile() {
144: if (!this .isLoggedIn()) {
145: this .login();
146: }
147: String sNamePassCombi = this .m_sUsername + this .m_sPassword;
148:
149: this .m_sAuthFilePath = this .m_sAuthDir
150: + "/"
151: + new BASE64Encoder().encode(sNamePassCombi.getBytes())
152: .replace('=', '_') + ".ath";
153:
154: VirtualFile vfAuthFile = ConfigStore.getInstance()
155: .getApplicationFileSystem().getVirtualFile(
156: this .m_sAuthFilePath).getResource();
157:
158: if (vfAuthFile.exists()) {
159: Document xmlDoc = null;
160: ByteArrayInputStream bis = null;
161: try {
162: bis = new ByteArrayInputStream(vfAuthFile.getContent());
163: xmlDoc = DocumentBuilderFactory.newInstance()
164: .newDocumentBuilder().parse(bis);
165: } catch (ParserConfigurationException e) {
166: e.printStackTrace();
167: } catch (FactoryConfigurationError e) {
168: e.printStackTrace();
169: } catch (SAXException e) {
170: e.printStackTrace();
171: } catch (IOException e) {
172: e.printStackTrace();
173: } finally {
174: try {
175: bis.close();
176: } catch (IOException e1) {
177: e1.printStackTrace();
178: }
179: }
180:
181: Element elRoot = xmlDoc.getDocumentElement();
182: NodeList nlAuths = elRoot.getElementsByTagName("auth");
183: for (int i = 0; i < nlAuths.getLength(); i++) {
184: Element elAuth = (Element) nlAuths.item(i);
185: Element elURI = (Element) elAuth.getElementsByTagName(
186: "uri").item(0);
187: Element elUsername = (Element) elAuth
188: .getElementsByTagName("username").item(0);
189: Element elPassword = (Element) elAuth
190: .getElementsByTagName("password").item(0);
191:
192: AuthInfo auth = new AuthInfo();
193: String sURI = ((Text) elURI.getFirstChild())
194: .getNodeValue();
195: String sUser = ((Text) elUsername.getFirstChild())
196: .getNodeValue();
197: String sPass = ((Text) elPassword.getFirstChild())
198: .getNodeValue();
199:
200: try {
201: sURI = PBE.decrypt(this .m_sPassword.toCharArray(),
202: sURI);
203: auth.setUsername(PBE.decrypt(this .m_sPassword
204: .toCharArray(), sUser));
205: auth.setPassword(PBE.decrypt(this .m_sPassword
206: .toCharArray(), sPass));
207: } catch (Exception e1) {
208: e1.printStackTrace();
209: }
210:
211: this .m_auths.put(sURI, auth);
212: }
213: }
214:
215: }
216:
217: /**
218: * Writes all of the current user's authentication information to an
219: * authentication store file.
220: */
221: private void writeToFile() {
222: if (this .isLoggedIn()) {
223: Document xmlDoc = null;
224: try {
225: xmlDoc = DocumentBuilderFactory.newInstance()
226: .newDocumentBuilder().newDocument();
227: } catch (ParserConfigurationException e) {
228: e.printStackTrace();
229: } catch (FactoryConfigurationError e) {
230: e.printStackTrace();
231: }
232:
233: Element elRoot = xmlDoc.createElement("authstore");
234: xmlDoc.appendChild(elRoot);
235:
236: Iterator itor = this .m_auths.keySet().iterator();
237: while (itor.hasNext()) {
238: String sURI = (String) itor.next();
239: AuthInfo authInfo = (AuthInfo) this .m_auths.get(sURI);
240: Element elAuth = xmlDoc.createElement("auth");
241: elRoot.appendChild(elAuth);
242:
243: Element elURI = xmlDoc.createElement("uri");
244: Text txt = null;
245: try {
246: txt = xmlDoc.createTextNode(PBE.encrypt(
247: this .m_sPassword.toCharArray(), sURI));
248: } catch (Exception e1) {
249: e1.printStackTrace();
250: }
251: elURI.appendChild(txt);
252: elAuth.appendChild(elURI);
253:
254: Element elUsername = xmlDoc.createElement("username");
255: txt = null;
256: try {
257: txt = xmlDoc.createTextNode(PBE.encrypt(
258: this .m_sPassword.toCharArray(), authInfo
259: .getUsername()));
260: } catch (Exception e1) {
261: e1.printStackTrace();
262: }
263: elUsername.appendChild(txt);
264: elAuth.appendChild(elUsername);
265:
266: Element elPassword = xmlDoc.createElement("password");
267: txt = null;
268: try {
269: txt = xmlDoc.createTextNode(PBE.encrypt(
270: this .m_sPassword.toCharArray(), authInfo
271: .getPassword()));
272: } catch (Exception e1) {
273: e1.printStackTrace();
274: }
275: elPassword.appendChild(txt);
276: elAuth.appendChild(elPassword);
277:
278: XMLPrettyPrint printer = new XMLPrettyPrint();
279: try {
280: String sContent = printer.printNode(elRoot);
281: VirtualFile vfAuthFile = ConfigStore.getInstance()
282: .getApplicationFileSystem().getVirtualFile(
283: this .m_sAuthFilePath).getResource();
284: vfAuthFile.setContent(sContent.getBytes());
285: ConfigStore.getInstance()
286: .getApplicationFileSystem()
287: .synchroniseFile(vfAuthFile);
288: } catch (NamespaceClashException e2) {
289: e2.printStackTrace();
290: }
291: }
292: }
293: }
294:
295: /* (non-Javadoc)
296: * @see com.simulacramedia.vfs.authentication.AbstractAuthenticationStore#getAuthentication(java.net.URI)
297: */
298: public AuthInfo getAuthentication(URI uri) {
299: AuthInfo authInfo = null;
300:
301: authInfo = super .getAuthentication(uri.toString());
302:
303: if (authInfo == null) {
304:
305: JFrame tempFrame = new JFrame();
306: tempFrame.setIconImage(((ImageIcon) IconManager
307: .getInstance().getIcon("32-sim-logo.gif"))
308: .getImage());
309: LoginDialog loginDialog = new LoginDialog(tempFrame);
310: loginDialog.show();
311: String sUsername = loginDialog.getUsername();
312: String sPassword = loginDialog.getPassword();
313:
314: if (sUsername != null && sPassword != null) {
315: authInfo = new AuthInfo();
316: authInfo.setUsername(sUsername);
317: authInfo.setPassword(sPassword);
318: this .m_auths.put(uri.toString(), authInfo);
319: }
320: }
321:
322: return authInfo;
323: }
324:
325: public static void main(String[] args) {
326: AbstractAuthenticationStore authStore = new LocalAuthenticationStore();
327: System.exit(0);
328: }
329:
330: /* (non-Javadoc)
331: * @see com.simulacramedia.vfs.authentication.AbstractAuthenticationStore#getUserDisplayName(java.lang.String)
332: */
333: public String getUserDisplayName(String sPath) {
334: return null;
335: }
336:
337: }
|