1 11 package org.eclipse.core.internal.runtime.auth; 12 13 import java.io.*; 14 import java.net.URL ; 15 import java.util.*; 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.osgi.util.NLS; 18 19 28 public class AuthorizationDatabase { 29 public static final String PI_RUNTIME_AUTH = "org.eclipse.core.runtime.auth.compatibility"; 31 35 public static final int FAILED_READ_METADATA = 4; 36 37 41 public static final int FAILED_WRITE_METADATA = 5; 42 43 46 private static final int KEYRING_FILE_VERSION = 1; 47 48 53 private Hashtable authorizationInfo = new Hashtable(5); 54 55 58 private Hashtable protectionSpace = new Hashtable(5); 59 60 private File file = null; 61 private String password = null; 62 private boolean needsSaving = true; 63 64 68 public AuthorizationDatabase() { 69 super(); 70 } 71 72 90 public AuthorizationDatabase(String filename, String password) throws CoreException { 91 Assert.isNotNull(filename); 92 Assert.isNotNull(password); 93 this.password = password; 94 file = new File(filename); 95 load(); 96 } 97 98 118 public void addAuthorizationInfo(URL serverUrl, String realm, String authScheme, Map info) { 119 Assert.isNotNull(serverUrl); 120 Assert.isNotNull(realm); 121 Assert.isNotNull(authScheme); 122 Assert.isNotNull(info); 123 124 String url = serverUrl.toString(); 125 Hashtable realmToAuthScheme = (Hashtable) authorizationInfo.get(url); 126 if (realmToAuthScheme == null) { 127 realmToAuthScheme = new Hashtable(5); 128 authorizationInfo.put(url, realmToAuthScheme); 129 } 130 131 Hashtable authSchemeToInfo = (Hashtable) realmToAuthScheme.get(realm); 132 if (authSchemeToInfo == null) { 133 authSchemeToInfo = new Hashtable(5); 134 realmToAuthScheme.put(realm, authSchemeToInfo); 135 } 136 137 authSchemeToInfo.put(authScheme.toLowerCase(), info); 138 needsSaving = true; 139 } 140 141 153 public void addProtectionSpace(URL resourceUrl, String realm) { 154 Assert.isNotNull(resourceUrl); 155 Assert.isNotNull(realm); 156 157 if (!resourceUrl.getFile().endsWith("/")) { resourceUrl = URLTool.getParent(resourceUrl); 159 } 160 161 String oldRealm = getProtectionSpace(resourceUrl); 162 if (oldRealm != null && oldRealm.equals(realm)) { 163 return; 164 } 165 166 String url1 = resourceUrl.toString(); 167 Enumeration urls = protectionSpace.keys(); 168 while (urls.hasMoreElements()) { 169 String url2 = (String ) urls.nextElement(); 170 if (url1.startsWith(url2) || url2.startsWith(url1)) { 171 protectionSpace.remove(url2); 172 break; 173 } 174 } 175 176 protectionSpace.put(url1, realm); 177 needsSaving = true; 178 } 179 180 195 public void flushAuthorizationInfo(URL serverUrl, String realm, String authScheme) { 196 Hashtable realmToAuthScheme = (Hashtable) authorizationInfo.get(serverUrl.toString()); 197 198 if (realmToAuthScheme == null) { 199 return; 200 } 201 202 Hashtable authSchemeToInfo = (Hashtable) realmToAuthScheme.get(realm); 203 204 if (authSchemeToInfo == null) { 205 return; 206 } 207 208 authSchemeToInfo.remove(authScheme.toLowerCase()); 209 210 needsSaving = true; 211 } 212 213 230 public Map getAuthorizationInfo(URL serverUrl, String realm, String authScheme) { 231 Hashtable realmToAuthScheme = (Hashtable) authorizationInfo.get(serverUrl.toString()); 232 if (realmToAuthScheme == null) { 233 return null; 234 } 235 236 Hashtable authSchemeToInfo = (Hashtable) realmToAuthScheme.get(realm); 237 if (authSchemeToInfo == null) { 238 return null; 239 } 240 241 return (Map) authSchemeToInfo.get(authScheme.toLowerCase()); 242 } 243 244 253 public String getProtectionSpace(URL resourceUrl) { 254 while (resourceUrl != null) { 255 String realm = (String ) protectionSpace.get(resourceUrl.toString()); 256 if (realm != null) { 257 return realm; 258 } 259 resourceUrl = URLTool.getParent(resourceUrl); 260 } 261 262 return null; 263 } 264 265 private void load() throws CoreException { 266 if (file == null) 267 return; 268 if (!file.exists()) { 269 save(); 270 return; 271 } 272 try { 273 InputStream input = new FileInputStream(file); 274 try { 275 load(input); 276 } finally { 277 input.close(); 278 } 279 } catch (IOException e) { 280 throw new CoreException(new Status(IStatus.ERROR, PI_RUNTIME_AUTH, FAILED_READ_METADATA, NLS.bind(Messages.meta_unableToReadAuthorization, file), e)); 281 } catch (ClassNotFoundException e) { 282 throw new CoreException(new Status(IStatus.ERROR, PI_RUNTIME_AUTH, FAILED_READ_METADATA, NLS.bind(Messages.meta_unableToReadAuthorization, file), e)); 283 } 284 } 285 286 private void load(InputStream is) throws IOException, ClassNotFoundException , CoreException { 287 int version = is.read(); 289 if (version == KEYRING_FILE_VERSION) { 290 CipherInputStream cis = new CipherInputStream(is, password); 292 ObjectInputStream ois = new ObjectInputStream(cis); 293 try { 294 authorizationInfo = (Hashtable) ois.readObject(); 295 protectionSpace = (Hashtable) ois.readObject(); 296 } finally { 297 ois.close(); 298 } 299 } else { 300 Activator.log(new Status(IStatus.WARNING, PI_RUNTIME_AUTH, FAILED_READ_METADATA, Messages.meta_authFormatChanged, null)); 302 try { 304 is.close(); 305 } catch (IOException e) { 306 } 308 needsSaving = true; 309 save(); 310 } 311 } 312 313 316 public void save() throws CoreException { 317 if (!needsSaving || file == null) 318 return; 319 try { 320 file.delete(); 321 if ((!file.getParentFile().exists() && !file.getParentFile().mkdirs()) || !canWrite(file.getParentFile())) 322 throw new CoreException(new Status(IStatus.ERROR, PI_RUNTIME_AUTH, FAILED_WRITE_METADATA, NLS.bind(Messages.meta_unableToWriteAuthorization, file), null)); 323 file.createNewFile(); 324 FileOutputStream out = new FileOutputStream(file); 325 try { 326 save(out); 327 } finally { 328 out.close(); 329 } 330 } catch (IOException e) { 331 throw new CoreException(new Status(IStatus.ERROR, PI_RUNTIME_AUTH, FAILED_WRITE_METADATA, NLS.bind(Messages.meta_unableToWriteAuthorization, file), e)); 332 } 333 needsSaving = false; 334 } 335 336 private static boolean canWrite(File installDir) { 337 if (!installDir.canWrite()) 338 return false; 339 340 if (!installDir.isDirectory()) 341 return false; 342 343 File fileTest = null; 344 try { 345 fileTest = File.createTempFile("writtableArea", null, installDir); } catch (IOException e) { 347 return false; 349 } finally { 350 if (fileTest != null) 351 fileTest.delete(); 352 } 353 return true; 354 } 355 356 private void save(FileOutputStream os) throws IOException { 357 os.write(KEYRING_FILE_VERSION); 359 360 CipherOutputStream cos = new CipherOutputStream(os, password); 361 ObjectOutputStream oos = new ObjectOutputStream(cos); 362 try { 364 oos.writeObject(authorizationInfo); 365 oos.writeObject(protectionSpace); 366 os.flush(); 367 os.getFD().sync(); 368 } finally { 369 oos.close(); 370 } 371 } 372 373 377 public boolean setPassword(String oldValue, String newValue) { 378 if (!oldValue.equals(password)) 379 return false; 380 password = newValue; 381 needsSaving = true; 382 return true; 383 } 384 } 385 | Popular Tags |