diff --git a/rse/plugins/org.eclipse.rse.core/src/org/eclipse/rse/core/PasswordPersistenceManager.java b/rse/plugins/org.eclipse.rse.core/src/org/eclipse/rse/core/PasswordPersistenceManager.java index db6a6314da6..d0c57b48298 100644 --- a/rse/plugins/org.eclipse.rse.core/src/org/eclipse/rse/core/PasswordPersistenceManager.java +++ b/rse/plugins/org.eclipse.rse.core/src/org/eclipse/rse/core/PasswordPersistenceManager.java @@ -49,6 +49,34 @@ import org.eclipse.rse.internal.core.RSECoreMessages; */ public class PasswordPersistenceManager { + /** + * Default System Type + */ + private static class DefaultSystemType extends AbstractRSESystemType implements IRSESystemType { + private static final String DEFAULT_ID = "DEFAULT"; //$NON-NLS-1$ + + private DefaultSystemType() { + super(DEFAULT_ID, DEFAULT_ID, RSECoreMessages.DefaultSystemType_Label, null, null); + } + + public String getId() { + //TODO consider a space character at the beginning to ensure uniqueness + return DEFAULT_ID; + } + + public String[] getSubsystemConfigurationIds() { + return null; + } + + public Object getAdapter(Class adapter) { + return null; + } + + public boolean isEnabled() { + return true; + } + } + // Keys used for using the Platform authorization methods // The server url is generic so we can lookup all registered user IDs / passwords // to display to the user in the password information preference page @@ -70,45 +98,20 @@ public class PasswordPersistenceManager { // Default user name public static final String DEFAULT_USER_NAME = "DEFAULT_USER"; //$NON-NLS-1$ - // New URL to store password map - private String newURL = null; - /* * Singleton instance */ private static PasswordPersistenceManager _instance; - /* - * Instance variables - */ - private RegisteredSystemType[] systemTypes; - /** - * Default System Type + * Retrieve the singleton instance of the PasswordPersistenceManger */ - private static class DefaultSystemType extends AbstractRSESystemType implements IRSESystemType { - private static final String DEFAULT_ID = "DEFAULT"; //$NON-NLS-1$ - - private DefaultSystemType() { - super(DEFAULT_ID, DEFAULT_ID, RSECoreMessages.DefaultSystemType_Label, null, null); - } - - public String getId() { - //TODO consider a space character at the beginning to ensure uniqueness - return DEFAULT_ID; - } - - public String[] getSubsystemConfigurationIds() { - return null; - } - - public Object getAdapter(Class adapter) { - return null; - } - - public boolean isEnabled() { - return true; + public static final synchronized PasswordPersistenceManager getInstance() { + if (_instance == null) { + _instance = new PasswordPersistenceManager(); + _instance.initExtensions(); } + return _instance; } /** @@ -117,12 +120,12 @@ public class PasswordPersistenceManager { private class RegisteredSystemType { private IRSESystemType _systemType; private boolean _userIDCaseSensitive; - + protected RegisteredSystemType(IRSESystemType systemType, boolean caseSensitive) { _systemType = systemType; _userIDCaseSensitive = caseSensitive; } - + /** * Returns the system type. * @return the system type. @@ -130,7 +133,7 @@ public class PasswordPersistenceManager { public IRSESystemType getSystemType() { return _systemType; } - + /** * Returns whether the user ID is case sensitive. * @return true if the user ID is case sensitive, false otherwise. @@ -140,6 +143,14 @@ public class PasswordPersistenceManager { } } + // New URL to store password map + private String newURL = null; + + /* + * Instance variables + */ + private RegisteredSystemType[] systemTypes; + /** * Singleton so private constructor */ @@ -153,17 +164,6 @@ public class PasswordPersistenceManager { newURL = SERVER_URL + userName; } - /** - * Retrieve the singleton instance of the PasswordPersistenceManger - */ - public static final synchronized PasswordPersistenceManager getInstance() { - if (_instance == null) { - _instance = new PasswordPersistenceManager(); - _instance.initExtensions(); - } - return _instance; - } - /* * initialization - register system types */ @@ -177,84 +177,157 @@ public class PasswordPersistenceManager { } /** - * Remove the entry from the keyring that matches the systemtype, hostname and - * user ID from the SystemSignonInfo parameter. + * Helper class for building the key to lookup the password for a specific + * userid and hostname in the Map */ - public void remove(SystemSignonInformation info) { - remove(info.getSystemType(), info.getHostname(), info.getUserId()); + private String getPasswordKey(String hname, String userid) { + String hostname = hname;//RSEUIPlugin.getQualifiedHostName(hname); + StringBuffer buffer = new StringBuffer(hostname); + buffer.append("//"); //$NON-NLS-1$ + buffer.append(userid); + return buffer.toString(); } - /** - * Removes all passwords for a host name for a given system type. Use the - * default system type explicitly to remove those entries. - * - * @param systemType The system type of the host - * @param hostName The IP address of the host in canonical format - * @return the number of passwords removed from the keyring - * @since org.eclipse.rse.core 3.0 + private String getHostnameFromPasswordKey(String passwordKey) { + int sepIndex = passwordKey.indexOf("//"); //$NON-NLS-1$ + return passwordKey.substring(0, sepIndex); + } + + private String getUserIdFromPasswordKey(String passwordKey) { + int sepIndex = passwordKey.indexOf("//"); //$NON-NLS-1$ + return passwordKey.substring(sepIndex + 2, passwordKey.length()); + } + + /* + * Retrieve the password map from the keyring for the specified system type */ - public int remove(IRSESystemType systemType, String hostName) { - Map passwords = getPasswordMap(systemType); - int numberRemoved = 0; - if (passwords != null) { - String hostPrefix = hostName + "//"; //$NON-NLS-1$ - Set keys = passwords.keySet(); - for (Iterator z = keys.iterator(); z.hasNext();) { - String key = (String) z.next(); - if (key.startsWith(hostPrefix)) { - z.remove(); // safely removes the key and the entry from the map - numberRemoved++; + private Map getPasswordMap(IRSESystemType systemType) { + Map passwords = null; + String systemTypeId = systemType.getId(); + + try { + URL serverURL = new URL(newURL); + passwords = Platform.getAuthorizationInfo(serverURL, systemTypeId, AUTH_SCHEME); + + // if no passwords found with new URL, check old URL + if (passwords == null) { + + URL oldServerURL1 = new URL(SERVER_URL + ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString()); + passwords = Platform.getAuthorizationInfo(oldServerURL1, systemTypeId, AUTH_SCHEME); + + // passwords found, so migrate to using new URL + if (passwords != null) { + savePasswordMap(systemTypeId, passwords); + } + // if still no passwords found, check with even older URL + else { + URL oldServerURL2 = new URL(SERVER_URL); + passwords = Platform.getAuthorizationInfo(oldServerURL2, systemTypeId, AUTH_SCHEME); + + // passwords found, so migrate to using new URL + if (passwords != null) { + savePasswordMap(systemTypeId, passwords); + } } } - if (numberRemoved > 0) { - savePasswordMap(systemType.getId(), passwords); + } catch (MalformedURLException e) { + RSECorePlugin.getDefault().getLogger().logError("PasswordPersistenceManager.getPasswordMap", e); //$NON-NLS-1$ + } + + return passwords; + } + + /* + * Retrieve the password map from the keyring for the specified system type + */ + private void savePasswordMap(String systemTypeId, Map passwords) { + try { + URL serverURL = new URL(newURL); + Platform.flushAuthorizationInfo(serverURL, systemTypeId, AUTH_SCHEME); + Platform.addAuthorizationInfo(serverURL, systemTypeId, AUTH_SCHEME, passwords); + } catch (MalformedURLException e) { + RSECorePlugin.getDefault().getLogger().logError("PasswordPersistenceManager.savePasswordMap", e); //$NON-NLS-1$ + } catch (CoreException e) { + RSECorePlugin.getDefault().getLogger().logError("PasswordPersistenceManager.savePasswordMap", e); //$NON-NLS-1$ + } + } + + private boolean removePassword(Map passwords, String hostname, String userid) { + boolean removed = false; + String password = null; + + String passwordKey = getPasswordKey(hostname, userid); + password = (String) passwords.get(passwordKey); + if (password != null) { + passwords.remove(passwordKey); + removed = true; + } else { + String phostname = hostname.toUpperCase(); + + // DKM - fallback for different case uids, hostnames or qualified/unqualified hostnames + Iterator keys = passwords.keySet().iterator(); + while (keys.hasNext() && password == null) { + String key = (String) keys.next(); + if (key.equalsIgnoreCase(passwordKey)) { + password = (String) passwords.get(key); + } else { + String khostname = getHostnameFromPasswordKey(key).toUpperCase(); + String kuid = getUserIdFromPasswordKey(key); + if (kuid.equalsIgnoreCase(userid)) { + // uid matches, check if hosts are the same + if (khostname.startsWith(phostname) || phostname.startsWith(khostname)) { + String qkhost = RSECorePlugin.getQualifiedHostName(khostname); + String qphost = RSECorePlugin.getQualifiedHostName(phostname); + if (qkhost.equals(qphost)) { + password = (String) passwords.get(key); + } + } + } + } + if (password != null) { + passwords.remove(key); + removed = true; + + } } } - return numberRemoved; + return removed; + } - /** - * Removes all entries from the keyring that match the hostname, userid, and system type. - * Use the default system type explicitly to remove those entries. - * @param systemType the systemType - * @param hostName the connection name - * @param userid the user id - */ - public void remove(IRSESystemType systemType, String hostName, String userid) { - String hostname = hostName;//RSEUIPlugin.getQualifiedHostName(hname); - // Convert userid to upper case if required - if (!isUserIDCaseSensitive(systemType)) { - userid = userid.toUpperCase(); - } - Map passwords = getPasswordMap(systemType); - if (passwords != null) { - if (removePassword(passwords, hostname, userid)) { - savePasswordMap(systemType.getId(), passwords); + private String getPassword(Map passwords, String hostname, String userid) { + String password = null; + + String passwordKey = getPasswordKey(hostname, userid); + password = (String) passwords.get(passwordKey); + if (password != null) return password; + + String phostname = hostname.toUpperCase(); + + // DKM - fallback for different case uids, hostnames or qualified/unqualified hostnames + Iterator keys = passwords.keySet().iterator(); + while (keys.hasNext() && password == null) { + String key = (String) keys.next(); + if (key.equalsIgnoreCase(passwordKey)) { + password = (String) passwords.get(key); + } else { + String khostname = getHostnameFromPasswordKey(key).toUpperCase(); + String kuid = getUserIdFromPasswordKey(key); + if (kuid.equalsIgnoreCase(userid)) { + // uid matches, check if hosts are the same + if (khostname.startsWith(phostname) || phostname.startsWith(khostname)) { + String qkhost = RSECorePlugin.getQualifiedHostName(khostname); + String qphost = RSECorePlugin.getQualifiedHostName(phostname); + if (qkhost.equals(qphost)) { + password = (String) passwords.get(key); + } + } + } } } - } - - /** - * Check if a password entry exists for the specified system type, hostname - * and userid. - */ - public boolean passwordExists(IRSESystemType systemtype, String hostname, String userid) { - - return passwordExists(systemtype, hostname, userid, true); - } - - /** - * Check if a password entry exists for the specified system type, hostname - * and userid. - * - * @param systemtype The system type to check for. - * @param hname The hostname to check for. - * @param userid The user ID to check for. - * @param checkDefault Whether or not to check for a default system type if the specified system type is not found. - */ - public boolean passwordExists(IRSESystemType systemtype, String hname, String userid, boolean checkDefault) { - String hostname = hname;//RSEUIPlugin.getQualifiedHostName(hname); - return (find(systemtype, hostname, userid) != null); + + return password; + } /** @@ -321,58 +394,27 @@ public class PasswordPersistenceManager { return result; } - /* - * Retrieve the password map from the keyring for the specified system type + /** + * Check if a password entry exists for the specified system type, hostname + * and userid. */ - private Map getPasswordMap(IRSESystemType systemType) { - Map passwords = null; - String systemTypeId = systemType.getId(); - - try { - URL serverURL = new URL(newURL); - passwords = Platform.getAuthorizationInfo(serverURL, systemTypeId, AUTH_SCHEME); - - // if no passwords found with new URL, check old URL - if (passwords == null) { - - URL oldServerURL1 = new URL(SERVER_URL + ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString()); - passwords = Platform.getAuthorizationInfo(oldServerURL1, systemTypeId, AUTH_SCHEME); - - // passwords found, so migrate to using new URL - if (passwords != null) { - savePasswordMap(systemTypeId, passwords); - } - // if still no passwords found, check with even older URL - else { - URL oldServerURL2 = new URL(SERVER_URL); - passwords = Platform.getAuthorizationInfo(oldServerURL2, systemTypeId, AUTH_SCHEME); - - // passwords found, so migrate to using new URL - if (passwords != null) { - savePasswordMap(systemTypeId, passwords); - } - } - } - } catch (MalformedURLException e) { - RSECorePlugin.getDefault().getLogger().logError("PasswordPersistenceManager.getPasswordMap", e); //$NON-NLS-1$ - } - - return passwords; + public boolean passwordExists(IRSESystemType systemtype, String hostname, String userid) { + + return passwordExists(systemtype, hostname, userid, true); } - /* - * Retrieve the password map from the keyring for the specified system type + /** + * Check if a password entry exists for the specified system type, hostname + * and userid. + * + * @param systemtype The system type to check for. + * @param hname The hostname to check for. + * @param userid The user ID to check for. + * @param checkDefault Whether or not to check for a default system type if the specified system type is not found. */ - private void savePasswordMap(String systemTypeId, Map passwords) { - try { - URL serverURL = new URL(newURL); - Platform.flushAuthorizationInfo(serverURL, systemTypeId, AUTH_SCHEME); - Platform.addAuthorizationInfo(serverURL, systemTypeId, AUTH_SCHEME, passwords); - } catch (MalformedURLException e) { - RSECorePlugin.getDefault().getLogger().logError("PasswordPersistenceManager.savePasswordMap", e); //$NON-NLS-1$ - } catch (CoreException e) { - RSECorePlugin.getDefault().getLogger().logError("PasswordPersistenceManager.savePasswordMap", e); //$NON-NLS-1$ - } + public boolean passwordExists(IRSESystemType systemtype, String hname, String userid, boolean checkDefault) { + String hostname = hname;//RSEUIPlugin.getQualifiedHostName(hname); + return (find(systemtype, hostname, userid) != null); } /** @@ -385,84 +427,6 @@ public class PasswordPersistenceManager { return find(systemtype, hostname, userid, true); } - private boolean removePassword(Map passwords, String hostname, String userid) { - boolean removed = false; - String password = null; - - String passwordKey = getPasswordKey(hostname, userid); - password = (String) passwords.get(passwordKey); - if (password != null) { - passwords.remove(passwordKey); - removed = true; - } else { - String phostname = hostname.toUpperCase(); - - // DKM - fallback for different case uids, hostnames or qualified/unqualified hostnames - Iterator keys = passwords.keySet().iterator(); - while (keys.hasNext() && password == null) { - String key = (String) keys.next(); - if (key.equalsIgnoreCase(passwordKey)) { - password = (String) passwords.get(key); - } else { - String khostname = getHostnameFromPasswordKey(key).toUpperCase(); - String kuid = getUserIdFromPasswordKey(key); - if (kuid.equalsIgnoreCase(userid)) { - // uid matches, check if hosts are the same - if (khostname.startsWith(phostname) || phostname.startsWith(khostname)) { - String qkhost = RSECorePlugin.getQualifiedHostName(khostname); - String qphost = RSECorePlugin.getQualifiedHostName(phostname); - if (qkhost.equals(qphost)) { - password = (String) passwords.get(key); - } - } - } - } - if (password != null) { - passwords.remove(key); - removed = true; - - } - } - } - return removed; - - } - - private String getPassword(Map passwords, String hostname, String userid) { - String password = null; - - String passwordKey = getPasswordKey(hostname, userid); - password = (String) passwords.get(passwordKey); - if (password != null) return password; - - String phostname = hostname.toUpperCase(); - - // DKM - fallback for different case uids, hostnames or qualified/unqualified hostnames - Iterator keys = passwords.keySet().iterator(); - while (keys.hasNext() && password == null) { - String key = (String) keys.next(); - if (key.equalsIgnoreCase(passwordKey)) { - password = (String) passwords.get(key); - } else { - String khostname = getHostnameFromPasswordKey(key).toUpperCase(); - String kuid = getUserIdFromPasswordKey(key); - if (kuid.equalsIgnoreCase(userid)) { - // uid matches, check if hosts are the same - if (khostname.startsWith(phostname) || phostname.startsWith(khostname)) { - String qkhost = RSECorePlugin.getQualifiedHostName(khostname); - String qphost = RSECorePlugin.getQualifiedHostName(phostname); - if (qkhost.equals(qphost)) { - password = (String) passwords.get(key); - } - } - } - } - } - - return password; - - } - /** * Find the persisted password for the specified systemtype, hostname and userid. * @@ -477,61 +441,81 @@ public class PasswordPersistenceManager { if (!isUserIDCaseSensitive(systemtype) && userid != null) { userid = userid.toUpperCase(); } - + Map passwords = getPasswordMap(systemtype); - + if (passwords != null) { String password = getPassword(passwords, hostname, userid); - + if (password != null) { return new SystemSignonInformation(hostname, userid, password, systemtype); } } - + // yantzi: RSE6.2 check for default system type entry with this hostname and user ID if (checkDefault && !DEFAULT_SYSTEM_TYPE.equals(systemtype)) { return find(DEFAULT_SYSTEM_TYPE, hostname, userid, false); } - + return null; } /** - * Helper class for building the key to lookup the password for a specific - * userid and hostname in the Map + * Remove the entry from the keyring that matches the systemtype, hostname and + * user ID from the SystemSignonInfo parameter. */ - private String getPasswordKey(String hname, String userid) { - String hostname = hname;//RSEUIPlugin.getQualifiedHostName(hname); - StringBuffer buffer = new StringBuffer(hostname); - buffer.append("//"); //$NON-NLS-1$ - buffer.append(userid); - return buffer.toString(); - } - - private String getHostnameFromPasswordKey(String passwordKey) { - int sepIndex = passwordKey.indexOf("//"); //$NON-NLS-1$ - return passwordKey.substring(0, sepIndex); - } - - private String getUserIdFromPasswordKey(String passwordKey) { - int sepIndex = passwordKey.indexOf("//"); //$NON-NLS-1$ - return passwordKey.substring(sepIndex + 2, passwordKey.length()); + public void remove(SystemSignonInformation info) { + remove(info.getSystemType(), info.getHostname(), info.getUserId()); } /** - * Helper method for determining if system type uses case sensitive user IDs + * Removes all passwords for a host name for a given system type. Use the + * default system type explicitly to remove those entries. + * + * @param systemType The system type of the host + * @param hostName The IP address of the host in canonical format + * @return the number of passwords removed from the keyring + * @since org.eclipse.rse.core 3.0 */ - public boolean isUserIDCaseSensitive(IRSESystemType systemType) { - // First find the correct provider - for (int i = 0; i < systemTypes.length; i++) { - - if (systemTypes[i].getSystemType().equals(systemType)) { - return systemTypes[i].isUserIDCaseSensitive(); + public int remove(IRSESystemType systemType, String hostName) { + Map passwords = getPasswordMap(systemType); + int numberRemoved = 0; + if (passwords != null) { + String hostPrefix = hostName + "//"; //$NON-NLS-1$ + Set keys = passwords.keySet(); + for (Iterator z = keys.iterator(); z.hasNext();) { + String key = (String) z.next(); + if (key.startsWith(hostPrefix)) { + z.remove(); // safely removes the key and the entry from the map + numberRemoved++; + } + } + if (numberRemoved > 0) { + savePasswordMap(systemType.getId(), passwords); } } + return numberRemoved; + } - //Not found: Default system type is case sensitive - return true; + /** + * Removes all entries from the keyring that match the hostname, userid, and system type. + * Use the default system type explicitly to remove those entries. + * @param systemType the systemType + * @param hostName the connection name + * @param userid the user id + */ + public void remove(IRSESystemType systemType, String hostName, String userid) { + String hostname = hostName;//RSEUIPlugin.getQualifiedHostName(hname); + // Convert userid to upper case if required + if (!isUserIDCaseSensitive(systemType)) { + userid = userid.toUpperCase(); + } + Map passwords = getPasswordMap(systemType); + if (passwords != null) { + if (removePassword(passwords, hostname, userid)) { + savePasswordMap(systemType.getId(), passwords); + } + } } /** @@ -592,4 +576,20 @@ public class PasswordPersistenceManager { return savedUserIDs; } + /** + * Helper method for determining if system type uses case sensitive user IDs + */ + public boolean isUserIDCaseSensitive(IRSESystemType systemType) { + // First find the correct provider + for (int i = 0; i < systemTypes.length; i++) { + + if (systemTypes[i].getSystemType().equals(systemType)) { + return systemTypes[i].isUserIDCaseSensitive(); + } + } + + //Not found: Default system type is case sensitive + return true; + } + } \ No newline at end of file