1 19 package org.openharmonise.him.authentication; 20 21 import java.io.ByteArrayInputStream ; 22 import java.io.IOException ; 23 import java.net.URI ; 24 import java.util.Iterator ; 25 26 import javax.swing.ImageIcon ; 27 import javax.swing.JFrame ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.parsers.FactoryConfigurationError ; 30 import javax.xml.parsers.ParserConfigurationException ; 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 ; 40 import org.w3c.dom.Element ; 41 import org.w3c.dom.NodeList ; 42 import org.w3c.dom.Text ; 43 import org.xml.sax.SAXException ; 44 45 import sun.misc.BASE64Encoder; 46 47 48 49 61 public class LocalAuthenticationStore extends AbstractAuthenticationStore { 62 63 66 private String m_sUsername = null; 67 68 71 private String m_sPassword = null; 72 73 76 private String m_sAuthDir = "/ContentManager/auths/las/"; 77 78 81 private String m_sAuthFilePath = null; 82 83 86 public LocalAuthenticationStore() { 87 super(); 88 this.readFromFile(); 89 } 90 91 96 public LocalAuthenticationStore(String sUsername) { 97 super(); 98 this.m_sUsername = sUsername; 99 this.readFromFile(); 100 } 101 102 107 public String getUsername() { 108 return this.m_sUsername; 109 } 110 111 115 public void save() { 116 this.writeToFile(); 117 } 118 119 124 public boolean isLoggedIn() { 125 return !(m_sUsername==null || this.m_sPassword==null); 126 } 127 128 131 private void login() { 132 JFrame tempFrame = new JFrame (); 133 tempFrame.setIconImage( ((ImageIcon )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 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 + "/" + 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 xmlDoc = null; 155 ByteArrayInputStream bis = null; 156 try { 157 bis = new ByteArrayInputStream (vfAuthFile.getContent()); 158 xmlDoc = 159 DocumentBuilderFactory 160 .newInstance() 161 .newDocumentBuilder().parse( bis ); 162 } catch (ParserConfigurationException e) { 163 e.printStackTrace(); 164 } catch (FactoryConfigurationError e) { 165 e.printStackTrace(); 166 } catch (SAXException e) { 167 e.printStackTrace(); 168 } catch (IOException e) { 169 e.printStackTrace(); 170 } finally { 171 try { 172 bis.close(); 173 } catch (IOException e1) { 174 e1.printStackTrace(); 175 } 176 } 177 178 Element elRoot = xmlDoc.getDocumentElement(); 179 NodeList nlAuths = elRoot.getElementsByTagName("auth"); 180 for(int i=0; i<nlAuths.getLength(); i++) { 181 Element elAuth = (Element )nlAuths.item(i); 182 Element elURI = (Element ) elAuth.getElementsByTagName("uri").item(0); 183 Element elUsername = (Element ) elAuth.getElementsByTagName("username").item(0); 184 Element elPassword = (Element ) elAuth.getElementsByTagName("password").item(0); 185 186 AuthInfo auth = new AuthInfo(); 187 String sURI = ((Text )elURI.getFirstChild()).getNodeValue(); 188 String sUser = ((Text )elUsername.getFirstChild()).getNodeValue(); 189 String sPass = ((Text )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 e1) { 196 e1.printStackTrace(); 197 } 198 199 this.m_auths.put( sURI, auth ); 200 } 201 } 202 203 } 204 205 209 private void writeToFile() { 210 if( this.isLoggedIn() ) { 211 Document xmlDoc = null; 212 try { 213 xmlDoc = 214 DocumentBuilderFactory 215 .newInstance() 216 .newDocumentBuilder() 217 .newDocument(); 218 } catch (ParserConfigurationException e) { 219 e.printStackTrace(); 220 } catch (FactoryConfigurationError e) { 221 e.printStackTrace(); 222 } 223 224 Element elRoot = xmlDoc.createElement("authstore"); 225 xmlDoc.appendChild(elRoot); 226 227 Iterator itor = this.m_auths.keySet().iterator(); 228 while(itor.hasNext()) { 229 String sURI = (String )itor.next(); 230 AuthInfo authInfo = (AuthInfo) this.m_auths.get(sURI); 231 Element elAuth = xmlDoc.createElement("auth"); 232 elRoot.appendChild(elAuth); 233 234 Element elURI = xmlDoc.createElement("uri"); 235 Text txt = null; 236 try { 237 txt = 238 xmlDoc.createTextNode( 239 PBE.encrypt( 240 this.m_sPassword.toCharArray(), 241 sURI)); 242 } catch (Exception e1) { 243 e1.printStackTrace(); 244 } 245 elURI.appendChild(txt); 246 elAuth.appendChild(elURI); 247 248 Element 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 e1) { 257 e1.printStackTrace(); 258 } 259 elUsername.appendChild(txt); 260 elAuth.appendChild(elUsername); 261 262 Element 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 e1) { 271 e1.printStackTrace(); 272 } 273 elPassword.appendChild(txt); 274 elAuth.appendChild(elPassword); 275 276 XMLPrettyPrint printer = new XMLPrettyPrint(); 277 try { 278 String 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 292 public AuthInfo getAuthentication(URI uri) { 293 AuthInfo authInfo = null; 294 295 authInfo = super.getAuthentication(uri.toString()); 296 297 if( authInfo==null ) { 298 299 JFrame tempFrame = new JFrame (); 300 tempFrame.setIconImage( ((ImageIcon )IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() ); 301 LoginDialog loginDialog = new LoginDialog(tempFrame); 302 loginDialog.show(); 303 String sUsername = loginDialog.getUsername(); 304 String 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 [] args) { 318 AbstractAuthenticationStore authStore = new LocalAuthenticationStore(); 319 System.exit(0); 320 } 321 322 325 public String getUserDisplayName(String sPath) { 326 return null; 327 } 328 329 } 330 | Popular Tags |