KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.ByteArrayInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import javax.swing.ImageIcon JavaDoc;
27 import javax.swing.JFrame JavaDoc;
28 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31
32 import org.openharmonise.commons.xml.*;
33 import org.openharmonise.commons.xml.namespace.*;
34 import org.openharmonise.him.authentication.gui.*;
35 import org.openharmonise.him.configuration.*;
36 import org.openharmonise.vfs.*;
37 import org.openharmonise.vfs.authentication.*;
38 import org.openharmonise.vfs.gui.*;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41 import org.w3c.dom.NodeList JavaDoc;
42 import org.w3c.dom.Text JavaDoc;
43 import org.xml.sax.SAXException JavaDoc;
44
45 import sun.misc.BASE64Encoder;
46
47
48
49 /**
50  * This class handles all the authentication work for the local
51  * Content Manager application. It will store a set authentication information
52  * for users in files on the local filesystem, encrypting them. It will prompt users
53  * to log in to the authentication store and then either provide autemtication
54  * information to requesting VirtualFileSystems, or prompt the users to provide this
55  * information and then store it.
56  *
57  * @author Matthew Large
58  * @version $Revision: 1.1 $
59  *
60  */

61 public class LocalAuthenticationStore extends AbstractAuthenticationStore {
62
63     /**
64      * Username.
65      */

66     private String JavaDoc m_sUsername = null;
67     
68     /**
69      * Password.
70      */

71     private String JavaDoc m_sPassword = null;
72     
73     /**
74      * Full path to local authentication store collection in local virtual file system.
75      */

76     private String JavaDoc m_sAuthDir = "/ContentManager/auths/las/";
77     
78     /**
79      * Full path to authentication information virtuall file in local virtual file system.
80      */

81     private String JavaDoc m_sAuthFilePath = null;
82
83     /**
84      * Default contructor.
85      */

86     public LocalAuthenticationStore() {
87         super();
88         this.readFromFile();
89     }
90     
91     /**
92      * Construcs a LocalAuthenticationStore for a specific username.
93      *
94      * @param sUsername Username to find authentication store file for
95      */

96     public LocalAuthenticationStore(String JavaDoc sUsername) {
97         super();
98         this.m_sUsername = sUsername;
99         this.readFromFile();
100     }
101     
102     /**
103      * Returns the username for this local authentication store.
104      *
105      * @return Username
106      */

107     public String JavaDoc getUsername() {
108         return this.m_sUsername;
109     }
110     
111     /**
112      * Saves all authentication information to an encrypted authentication store
113      * file.
114      */

115     public void save() {
116         this.writeToFile();
117     }
118     
119     /**
120      * To check if there is a user logged in to this Authentication Store.
121      *
122      * @return True if there is a user logged into this Authentication Store
123      */

124     public boolean isLoggedIn() {
125         return !(m_sUsername==null || this.m_sPassword==null);
126     }
127     
128     /**
129      * Shows a login window requesting a username and password from the user.
130      */

131     private void login() {
132         JFrame JavaDoc tempFrame = new JFrame JavaDoc();
133         tempFrame.setIconImage( ((ImageIcon JavaDoc)IconManager.getInstance().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 JavaDoc sNamePassCombi = this.m_sUsername + this.m_sPassword;
148         
149         this.m_sAuthFilePath = this.m_sAuthDir + "/" + new BASE64Encoder().encode(sNamePassCombi.getBytes()).replace('=','_') + ".ath";
150         
151         VirtualFile vfAuthFile = ConfigStore.getInstance().getApplicationFileSystem().getVirtualFile(this.m_sAuthFilePath).getResource();
152         
153         if( vfAuthFile.exists() ) {
154             Document JavaDoc xmlDoc = null;
155             ByteArrayInputStream JavaDoc bis = null;
156             try {
157                 bis = new ByteArrayInputStream JavaDoc(vfAuthFile.getContent());
158                 xmlDoc =
159                     DocumentBuilderFactory
160                         .newInstance()
161                         .newDocumentBuilder().parse( bis );
162             } catch (ParserConfigurationException JavaDoc e) {
163                 e.printStackTrace();
164             } catch (FactoryConfigurationError JavaDoc e) {
165                 e.printStackTrace();
166             } catch (SAXException JavaDoc e) {
167                 e.printStackTrace();
168             } catch (IOException JavaDoc e) {
169                 e.printStackTrace();
170             } finally {
171                 try {
172                     bis.close();
173                 } catch (IOException JavaDoc e1) {
174                     e1.printStackTrace();
175                 }
176             }
177             
178             Element JavaDoc elRoot = xmlDoc.getDocumentElement();
179             NodeList JavaDoc nlAuths = elRoot.getElementsByTagName("auth");
180             for(int i=0; i<nlAuths.getLength(); i++) {
181                 Element JavaDoc elAuth = (Element JavaDoc)nlAuths.item(i);
182                 Element JavaDoc elURI = (Element JavaDoc) elAuth.getElementsByTagName("uri").item(0);
183                 Element JavaDoc elUsername = (Element JavaDoc) elAuth.getElementsByTagName("username").item(0);
184                 Element JavaDoc elPassword = (Element JavaDoc) elAuth.getElementsByTagName("password").item(0);
185                 
186                 AuthInfo auth = new AuthInfo();
187                 String JavaDoc sURI = ((Text JavaDoc)elURI.getFirstChild()).getNodeValue();
188                 String JavaDoc sUser = ((Text JavaDoc)elUsername.getFirstChild()).getNodeValue();
189                 String JavaDoc sPass = ((Text JavaDoc)elPassword.getFirstChild()).getNodeValue();
190
191                 try {
192                     sURI = PBE.decrypt(this.m_sPassword.toCharArray(), sURI);
193                     auth.setUsername( PBE.decrypt(this.m_sPassword.toCharArray(), sUser) );
194                     auth.setPassword( PBE.decrypt(this.m_sPassword.toCharArray(), sPass) );
195                 } catch (Exception JavaDoc e1) {
196                     e1.printStackTrace();
197                 }
198                 
199                 this.m_auths.put( sURI, auth );
200             }
201         }
202         
203     }
204     
205     /**
206      * Writes all of the current user's authentication information to an
207      * authentication store file.
208      */

209     private void writeToFile() {
210         if( this.isLoggedIn() ) {
211             Document JavaDoc xmlDoc = null;
212             try {
213                 xmlDoc =
214                     DocumentBuilderFactory
215                         .newInstance()
216                         .newDocumentBuilder()
217                         .newDocument();
218             } catch (ParserConfigurationException JavaDoc e) {
219                 e.printStackTrace();
220             } catch (FactoryConfigurationError JavaDoc e) {
221                 e.printStackTrace();
222             }
223             
224             Element JavaDoc elRoot = xmlDoc.createElement("authstore");
225             xmlDoc.appendChild(elRoot);
226             
227             Iterator JavaDoc itor = this.m_auths.keySet().iterator();
228             while(itor.hasNext()) {
229                 String JavaDoc sURI = (String JavaDoc)itor.next();
230                 AuthInfo authInfo = (AuthInfo) this.m_auths.get(sURI);
231                 Element JavaDoc elAuth = xmlDoc.createElement("auth");
232                 elRoot.appendChild(elAuth);
233                 
234                 Element JavaDoc elURI = xmlDoc.createElement("uri");
235                 Text JavaDoc txt = null;
236                 try {
237                     txt =
238                         xmlDoc.createTextNode(
239                             PBE.encrypt(
240                                 this.m_sPassword.toCharArray(),
241                                 sURI));
242                 } catch (Exception JavaDoc e1) {
243                     e1.printStackTrace();
244                 }
245                 elURI.appendChild(txt);
246                 elAuth.appendChild(elURI);
247                 
248                 Element JavaDoc elUsername = xmlDoc.createElement("username");
249                 txt = null;
250                 try {
251                     txt =
252                         xmlDoc.createTextNode(
253                             PBE.encrypt(
254                                 this.m_sPassword.toCharArray(),
255                                 authInfo.getUsername()));
256                 } catch (Exception JavaDoc e1) {
257                     e1.printStackTrace();
258                 }
259                 elUsername.appendChild(txt);
260                 elAuth.appendChild(elUsername);
261                 
262                 Element JavaDoc elPassword = xmlDoc.createElement("password");
263                 txt = null;
264                 try {
265                     txt =
266                         xmlDoc.createTextNode(
267                             PBE.encrypt(
268                                 this.m_sPassword.toCharArray(),
269                                 authInfo.getPassword()));
270                 } catch (Exception JavaDoc e1) {
271                     e1.printStackTrace();
272                 }
273                 elPassword.appendChild(txt);
274                 elAuth.appendChild(elPassword);
275                 
276                 XMLPrettyPrint printer = new XMLPrettyPrint();
277                 try {
278                     String JavaDoc sContent = printer.printNode(elRoot);
279                     VirtualFile vfAuthFile = ConfigStore.getInstance().getApplicationFileSystem().getVirtualFile(this.m_sAuthFilePath).getResource();
280                     vfAuthFile.setContent(sContent.getBytes());
281                     ConfigStore.getInstance().getApplicationFileSystem().synchroniseFile(vfAuthFile);
282                 } catch (NamespaceClashException e2) {
283                     e2.printStackTrace();
284                 }
285             }
286         }
287     }
288     
289     /* (non-Javadoc)
290      * @see com.simulacramedia.vfs.authentication.AbstractAuthenticationStore#getAuthentication(java.net.URI)
291      */

292     public AuthInfo getAuthentication(URI JavaDoc uri) {
293         AuthInfo authInfo = null;
294         
295         authInfo = super.getAuthentication(uri.toString());
296         
297         if( authInfo==null ) {
298
299             JFrame JavaDoc tempFrame = new JFrame JavaDoc();
300             tempFrame.setIconImage( ((ImageIcon JavaDoc)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() );
301             LoginDialog loginDialog = new LoginDialog(tempFrame);
302             loginDialog.show();
303             String JavaDoc sUsername = loginDialog.getUsername();
304             String JavaDoc sPassword = loginDialog.getPassword();
305             
306             if( sUsername!=null && sPassword!=null ) {
307                 authInfo = new AuthInfo();
308                 authInfo.setUsername(sUsername);
309                 authInfo.setPassword(sPassword);
310                 this.m_auths.put(uri.toString(), authInfo);
311             }
312         }
313         
314         return authInfo;
315     }
316     
317     public static void main(String JavaDoc[] args) {
318         AbstractAuthenticationStore authStore = new LocalAuthenticationStore();
319         System.exit(0);
320     }
321
322     /* (non-Javadoc)
323      * @see com.simulacramedia.vfs.authentication.AbstractAuthenticationStore#getUserDisplayName(java.lang.String)
324      */

325     public String JavaDoc getUserDisplayName(String JavaDoc sPath) {
326         return null;
327     }
328
329 }
330
Popular Tags