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
  1. /**
  2.  * Prototype object for easy storing and retrieving login informations
  3.  * by an extension. It supports both of Mozilla’s components for this task –
  4.  * nsIPasswordManager and nsILoginManager so it should work on Firefox 3
  5.  * but also on older versions.
  6.  *
  7.  * For more details see these URLs:
  8.  *   http://developer.mozilla.org/en/docs/Using_nsILoginManager
  9.  *   http://developer.mozilla.org/en/docs/Using_nsIPasswordManager
  10.  */
  11. function LoginManagerPrototype(aHostName)
  12. {
  13.   // Holds host name
  14.   this.mHostName = aHostName;
  15.  
  16.   // Initialization part of password manager. It checks which version
  17.   // of Firefox we are using and set up the object according to it.
  18.   if(Components.classes["@mozilla.org/login-manager;1"] != null)
  19.   {
  20.     // We are using Firefox 3
  21.     this.mPasswordService = Components.classes["@mozilla.org/login-manager;1"].
  22.         getService(Components.interfaces.nsILoginManager);
  23.    
  24.     // Helper properties
  25.     this.mFormSubmitURL = "User login";
  26.     this.mHttpRealm = null;
  27.    
  28.     /**
  29.      * Save user login informations (username and password)
  30.      * @param aUserName {string} User’s name
  31.      * @param aUserPass {string} User’s password
  32.      */
  33.     this.savePassword = function(aUserName, aUserPass)
  34.     {
  35.       var nsLoginInfo = new Components.Constructor(
  36.           "@mozilla.org/login-manager/loginInfo;1",
  37.           Components.interfaces.nsILoginInfo, "init");
  38.      
  39.       var login_info = new nsLoginInfo(this.mHostName, "User login", null,
  40.           aUserName, aUserPass, null, null);
  41.      
  42.       this.mPasswordService.addLogin(login_info);
  43.     }; // end savePassword(aUserName, aUserPass)
  44.    
  45.     /**
  46.      * Retrieve password for given user name
  47.      * @param aUserName {string} User’s name
  48.      * @returns {object} Returns object with user name and password or FALSE
  49.      *                   if no corresponding login informations founded.
  50.      */
  51.     this.getPassword = function(aUserName)
  52.     {
  53.       try {
  54.         var logins = this.mPasswordService.findLogins({}, this.mHostName,
  55.             this.mFormSubmitURL, this.mHttpRealm);
  56.        
  57.         for(var i=0; i<logins.length; i++) {
  58.           if(logins[i].username == aUserName)
  59.             return logins[i].password;
  60.         }
  61.       } catch(e) { /* … */ }
  62.      
  63.       return false;
  64.     }; // end getPassword(aUserName)
  65.   }
  66.   else
  67.   {
  68.     // We are using older versions of Gecko
  69.     this.mPasswordService = Components.classes["@mozilla.org/passwordmanager;1"].
  70.         getService(Components.interfaces.nsIPasswordManager);
  71.    
  72.     /**
  73.      * Save user login informations (username and password)
  74.      * @param aUserName {string} User’s name
  75.      * @param aUserPass {string} User’s password
  76.      */
  77.     this.savePassword = function(aUserName, aUserPass)
  78.     {
  79.       this.mPasswordService.addUser(this.mHostName, aUserName, aUserPass);
  80.     }; // end savePassword(aUserName, aUserPass)
  81.    
  82.     /**
  83.      * Retrieve password for given user name
  84.      * @param aUserName {string} User’s name
  85.      * @returns {object} Returns object with user name and password or FALSE
  86.      *                   if no corresponding login informations founded.
  87.      */
  88.     LoginManagerPrototype.prototype.getPassword = function(aUserName)
  89.     {
  90.       var password = false;
  91.       var enumerator = this.mPasswordService.enumerator;
  92.      
  93.       while(enumerator.hasMoreElements()) {
  94.         try {
  95.           var pass = enumerator.getNext().
  96.               QueryInterface(Components.interfaces.nsIPassword);
  97.          
  98.           if(pass.host == this.mHostName && pass.user == aUserName) {
  99.             password = pass.password;
  100.             break;
  101.           }
  102.         } catch(e) { /* … */ }
  103.       }
  104.      
  105.       return password;
  106.     }; // end getPassword(aUserName)
  107.   }
  108. }; // End of LoginManagerPrototype()
  109.  

You can download this script here.