1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-25 18:05:33 +02:00

[225320] Rearranging elements prior to adding code for secure storage - no code changes.

This commit is contained in:
David Dykstal 2012-03-13 02:35:26 +00:00
parent 2712a2eff0
commit 93fde9f633

View file

@ -49,40 +49,6 @@ import org.eclipse.rse.internal.core.RSECoreMessages;
*/
public class PasswordPersistenceManager {
// 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
private static final String SERVER_URL = "file://rse"; //$NON-NLS-1$
private static final String AUTH_SCHEME = ""; // no authorization scheme specified for apis //$NON-NLS-1$
// Add return codes
public static final int RC_OK = 0;
public static final int RC_ALREADY_EXISTS = 1;
/** @since org.eclipse.rse.core 3.0 */
public static final int RC_DENIED = 2;
public static final int RC_ERROR = -1;
// Default System Type, on a lookup if the specified system type and hostname is not found
// then the call will automatically lookup the default system type and hostname
public static final IRSESystemType DEFAULT_SYSTEM_TYPE = new DefaultSystemType();
// 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
*/
@ -111,6 +77,43 @@ public class PasswordPersistenceManager {
}
}
// 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
private static final String SERVER_URL = "file://rse"; //$NON-NLS-1$
private static final String AUTH_SCHEME = ""; // no authorization scheme specified for apis //$NON-NLS-1$
// Add return codes
public static final int RC_OK = 0;
public static final int RC_ALREADY_EXISTS = 1;
/** @since org.eclipse.rse.core 3.0 */
public static final int RC_DENIED = 2;
public static final int RC_ERROR = -1;
// Default System Type, on a lookup if the specified system type and hostname is not found
// then the call will automatically lookup the default system type and hostname
public static final IRSESystemType DEFAULT_SYSTEM_TYPE = new DefaultSystemType();
// Default user name
public static final String DEFAULT_USER_NAME = "DEFAULT_USER"; //$NON-NLS-1$
/*
* Singleton instance
*/
private static PasswordPersistenceManager _instance;
/**
* Retrieve the singleton instance of the PasswordPersistenceManger
*/
public static final synchronized PasswordPersistenceManager getInstance() {
if (_instance == null) {
_instance = new PasswordPersistenceManager();
_instance.initExtensions();
}
return _instance;
}
/**
* Inner class used for storing registered system types
*/
@ -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,148 +177,25 @@ 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());
}
/**
* 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 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;
}
/**
* 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);
}
}
}
/**
* 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) {
private String getPasswordKey(String hname, String userid) {
String hostname = hname;//RSEUIPlugin.getQualifiedHostName(hname);
return (find(systemtype, hostname, userid) != null);
StringBuffer buffer = new StringBuffer(hostname);
buffer.append("//"); //$NON-NLS-1$
buffer.append(userid);
return buffer.toString();
}
/**
* Add a password to the password database.
* This will not update the entry for the default system type
* @param info The signon information to store
* @param overwrite Whether to overwrite any existing entry
* @return
* RC_OK if the password was successfully stored
* RC_ALREADY_EXISTS if the password already exists and overwrite was false
*/
public int add(SystemSignonInformation info, boolean overwrite) {
return add(info, overwrite, false);
private String getHostnameFromPasswordKey(String passwordKey) {
int sepIndex = passwordKey.indexOf("//"); //$NON-NLS-1$
return passwordKey.substring(0, sepIndex);
}
/**
* Add a password to the password database.
* @param info The signon information to store
* @param overwrite If true then overwrite the existing entry for this systemtype, hostname, and userid.
* @param updateDefault if true then set the entry for the default systemtype, hostname, and user ID, according to the overwrite setting.
* @return
* RC_OK if the password was successfully stored.
* RC_ALREADY_EXISTS if the password already exists and overwrite was false
* RC_DENIED if passwords may not be saved for this system type and host
*/
public int add(SystemSignonInformation info, boolean overwrite, boolean updateDefault) {
int result = RC_OK;
IRSESystemType systemType = info.getSystemType();
String hostName = info.getHostname();
String userId = info.getUserId();
String newPassword = info.getPassword();
boolean deny = RSEPreferencesManager.getDenyPasswordSave(systemType, hostName);
if (!deny) {
if (!isUserIDCaseSensitive(systemType)) {
userId = userId.toUpperCase();
info.setUserId(userId);
}
if (updateDefault) {
if (systemType != DEFAULT_SYSTEM_TYPE) {
SystemSignonInformation newInfo = new SystemSignonInformation(hostName, userId, newPassword, DEFAULT_SYSTEM_TYPE);
result = add(newInfo, overwrite, false);
}
}
Map passwords = getPasswordMap(systemType);
if (passwords == null) {
passwords = new HashMap(5);
}
String oldPassword = getPassword(passwords, hostName, userId);
if (oldPassword != null) {
if (overwrite) {
removePassword(passwords, hostName, userId);
} else {
result = RC_ALREADY_EXISTS;
}
}
if (result == RC_OK) {
String passwordKey = getPasswordKey(hostName, userId);
passwords.put(passwordKey, newPassword);
savePasswordMap(systemType.getId(), passwords);
}
} else {
result = RC_DENIED;
}
return result;
private String getUserIdFromPasswordKey(String passwordKey) {
int sepIndex = passwordKey.indexOf("//"); //$NON-NLS-1$
return passwordKey.substring(sepIndex + 2, passwordKey.length());
}
/*
@ -375,16 +252,6 @@ public class PasswordPersistenceManager {
}
}
/**
* Find the password for the specified systemtype, hostname and userid.
* If one is not found then the default system type is used.
* The system type in the signon information returned may not be the same as the system type
* specfied in the argument.
*/
public SystemSignonInformation find(IRSESystemType systemtype, String hostname, String userid) {
return find(systemtype, hostname, userid, true);
}
private boolean removePassword(Map passwords, String hostname, String userid) {
boolean removed = false;
String password = null;
@ -463,6 +330,103 @@ public class PasswordPersistenceManager {
}
/**
* Add a password to the password database.
* This will not update the entry for the default system type
* @param info The signon information to store
* @param overwrite Whether to overwrite any existing entry
* @return
* RC_OK if the password was successfully stored
* RC_ALREADY_EXISTS if the password already exists and overwrite was false
*/
public int add(SystemSignonInformation info, boolean overwrite) {
return add(info, overwrite, false);
}
/**
* Add a password to the password database.
* @param info The signon information to store
* @param overwrite If true then overwrite the existing entry for this systemtype, hostname, and userid.
* @param updateDefault if true then set the entry for the default systemtype, hostname, and user ID, according to the overwrite setting.
* @return
* RC_OK if the password was successfully stored.
* RC_ALREADY_EXISTS if the password already exists and overwrite was false
* RC_DENIED if passwords may not be saved for this system type and host
*/
public int add(SystemSignonInformation info, boolean overwrite, boolean updateDefault) {
int result = RC_OK;
IRSESystemType systemType = info.getSystemType();
String hostName = info.getHostname();
String userId = info.getUserId();
String newPassword = info.getPassword();
boolean deny = RSEPreferencesManager.getDenyPasswordSave(systemType, hostName);
if (!deny) {
if (!isUserIDCaseSensitive(systemType)) {
userId = userId.toUpperCase();
info.setUserId(userId);
}
if (updateDefault) {
if (systemType != DEFAULT_SYSTEM_TYPE) {
SystemSignonInformation newInfo = new SystemSignonInformation(hostName, userId, newPassword, DEFAULT_SYSTEM_TYPE);
result = add(newInfo, overwrite, false);
}
}
Map passwords = getPasswordMap(systemType);
if (passwords == null) {
passwords = new HashMap(5);
}
String oldPassword = getPassword(passwords, hostName, userId);
if (oldPassword != null) {
if (overwrite) {
removePassword(passwords, hostName, userId);
} else {
result = RC_ALREADY_EXISTS;
}
}
if (result == RC_OK) {
String passwordKey = getPasswordKey(hostName, userId);
passwords.put(passwordKey, newPassword);
savePasswordMap(systemType.getId(), passwords);
}
} else {
result = RC_DENIED;
}
return result;
}
/**
* 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);
}
/**
* Find the password for the specified systemtype, hostname and userid.
* If one is not found then the default system type is used.
* The system type in the signon information returned may not be the same as the system type
* specfied in the argument.
*/
public SystemSignonInformation find(IRSESystemType systemtype, String hostname, String userid) {
return find(systemtype, hostname, userid, true);
}
/**
* Find the persisted password for the specified systemtype, hostname and userid.
*
@ -497,41 +461,61 @@ public class PasswordPersistenceManager {
}
/**
* 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;
}
}