In many of my extensions I used nsIPasswordManager for storing and retrieving user login informations but now is comming Mozilla Firefox 3 (currently the first beta version is released) and brings new component for this purpose – nsILoginManager. So I’ve decided to make a class which can be used without any worries about Firefox’s version.
GeSHi © 2004, Nigel McNie
-
/**
-
* Prototype object for easy storing and retrieving login informations
-
* by an extension. It supports both of Mozilla’s components for this task –
-
* nsIPasswordManager and nsILoginManager so it should work on Firefox 3
-
* but also on older versions.
-
*
-
* For more details see these URLs:
-
* http://developer.mozilla.org/en/docs/Using_nsILoginManager
-
* http://developer.mozilla.org/en/docs/Using_nsIPasswordManager
-
*/
-
function LoginManagerPrototype(aHostName)
-
{
-
// Holds host name
-
this.mHostName = aHostName;
-
-
// Initialization part of password manager. It checks which version
-
// of Firefox we are using and set up the object according to it.
-
if(Components.classes["@mozilla.org/login-manager;1"] != null)
-
{
-
// We are using Firefox 3
-
this.mPasswordService = Components.classes["@mozilla.org/login-manager;1"].
-
getService(Components.interfaces.nsILoginManager);
-
-
// Helper properties
-
this.mFormSubmitURL = "User login";
-
this.mHttpRealm = null;
-
-
/**
-
* Save user login informations (username and password)
-
* @param aUserName {string} User’s name
-
* @param aUserPass {string} User’s password
-
*/
-
this.savePassword = function(aUserName, aUserPass)
-
{
-
var nsLoginInfo = new Components.Constructor(
-
"@mozilla.org/login-manager/loginInfo;1",
-
Components.interfaces.nsILoginInfo, "init");
-
-
var login_info = new nsLoginInfo(this.mHostName, "User login", null,
-
aUserName, aUserPass, null, null);
-
-
this.mPasswordService.addLogin(login_info);
-
}; // end savePassword(aUserName, aUserPass)
-
-
/**
-
* Retrieve password for given user name
-
* @param aUserName {string} User’s name
-
* @returns {object} Returns object with user name and password or FALSE
-
* if no corresponding login informations founded.
-
*/
-
this.getPassword = function(aUserName)
-
{
-
try {
-
var logins = this.mPasswordService.findLogins({}, this.mHostName,
-
this.mFormSubmitURL, this.mHttpRealm);
-
-
for(var i=0; i<logins.length; i++) {
-
if(logins[i].username == aUserName)
-
return logins[i].password;
-
}
-
} catch(e) { /* … */ }
-
-
return false;
-
}; // end getPassword(aUserName)
-
}
-
else
-
{
-
// We are using older versions of Gecko
-
this.mPasswordService = Components.classes["@mozilla.org/passwordmanager;1"].
-
getService(Components.interfaces.nsIPasswordManager);
-
-
/**
-
* Save user login informations (username and password)
-
* @param aUserName {string} User’s name
-
* @param aUserPass {string} User’s password
-
*/
-
this.savePassword = function(aUserName, aUserPass)
-
{
-
this.mPasswordService.addUser(this.mHostName, aUserName, aUserPass);
-
}; // end savePassword(aUserName, aUserPass)
-
-
/**
-
* Retrieve password for given user name
-
* @param aUserName {string} User’s name
-
* @returns {object} Returns object with user name and password or FALSE
-
* if no corresponding login informations founded.
-
*/
-
LoginManagerPrototype.prototype.getPassword = function(aUserName)
-
{
-
var password = false;
-
var enumerator = this.mPasswordService.enumerator;
-
-
while(enumerator.hasMoreElements()) {
-
try {
-
var pass = enumerator.getNext().
-
QueryInterface(Components.interfaces.nsIPassword);
-
-
if(pass.host == this.mHostName && pass.user == aUserName) {
-
password = pass.password;
-
break;
-
}
-
} catch(e) { /* … */ }
-
}
-
-
return password;
-
}; // end getPassword(aUserName)
-
}
-
}; // End of LoginManagerPrototype()
-
You can download this script here.
