UsingPasswordManagerInSunbird

Scriptlets/info

found a post on the newsgroups on howto set amaster password:

DM wrote:

> When Phoenix 0.6 (Firebird) stores my password for the first time, it tells
> me that for safety, I'd better use a master password. However, nowhere on any
> of the menus can I find a tool to set a master password, like one can do with Mozilla. Am I missing something, or is this the artefact of a code cut and paste from the Mozilla project?

In Firebird, go to about:config and add a new boolean value, call it wallet.crypto and set it to true. Now, any passwords you save from this point on will be protected by a master password. Note that passwords saved before you turned on wallet crypto will not be protected by the master password.

creating a new instance of the pw manager:

var passwordManager = Components.classes ["@mozilla.org/passwordmanager;1"] .createInstance();

creating a reference to the pw manager:

passwordmanager = Components.classes["@mozilla.org/passwordmanager;1"].getService(); passwordmanager = passwordmanager.QueryInterface(Components.interfaces.nsIPasswordManager);

determine if database is encrypted

  try {
    var pref = Components.classes["@mozilla.org/preferences-service;1"]
                     .getService(Components.interfaces.nsIPrefBranch);
    try {
      encrypted = pref.getBoolPref("wallet.crypto");
    } catch(e) {
      dump("wallet.crypto pref is missing from all.js");
    }

  } catch (ex) {
    dump("failed to get prefs service!\n");
    pref = null;
  }

iterating over the saved passwords:

function LoadSignons() {
  // loads signons into table
  var enumerator = passwordmanager.enumerator;
  var count = 0;

  while (enumerator.hasMoreElements()) {
    var nextPassword;
    try {
      nextPassword = enumerator.getNext();

      nextPassword = nextPassword.QueryInterface(Components.interfaces.nsIPassword);
      var host = nextPassword.host;
      var user = nextPassword.user;
      var password = nextPassword.password;
      var rawuser = user;

      // if no username supplied, try to parse it out of the url
      if (user == "") {
        var ioService = Components.classes["@mozilla.org/network/io-service;1"]
                      .getService(Components.interfaces.nsIIOService);
        try {
          user = ioService.newURI(host, null, null).username;
          if (user == "") {
            user = "<>";
          }
        } catch(e) {
          user = "<>";
        }
      }

      if (encrypted) {
        user = kSignonBundle.getFormattedString("encrypted", [user], 1);
      }

      signons[count] = new Signon(count++, host, user, rawuser, password);
    } catch(e) {
      /* An entry is corrupt. Go to next element. */
    }
  }

Functions to save and retrieve passwords:

function getSavedPassword(url, username) {
  var passwordManagerInternal = getPasswordManagerInternal();
  var host = {value:""};
  var user =  {value:""};
  var password = {value:""}; 
  // Strip off terminal "/"
  var len = url.length;
  if (len && url.charAt(len-1) == "\/") {
    url = url.slice(0, len-1);
  }
  
  try {
    passwordManagerInternal.findPasswordEntry(url, username, "", host, user, password);
    return password.value;
  } catch (e) {}
  return "";
}

function savePassword(url, username, password) {
  var passwordManager = getPasswordManager();
  if (passwordManager) {
    try {
      // Remove existing entry
       passwordManager.removeUser(url, username);
   } catch (e) {}
      //add new entry
   try{
      passwordManager.addUser(url, username, password);
    } catch (e) {}
    return true;
  }
  return false;
}

var gPasswordManager;

function getPasswordManager() {
  if (!gPasswordManager) {
    var passwordManager = Components.classes["@mozilla.org/passwordmanager;1"].createInstance();
    if (passwordManager) {
      gPasswordManager = passwordManager.QueryInterface(Components.interfaces.nsIPasswordManager);
   }
  }
  return gPasswordManager;
}

var gPasswordManagerInternal;

function getPasswordManagerInternal() {
  if (!gPasswordManagerInternal) {
    try {
      gPasswordManagerInternal = Components.classes["@mozilla.org/passwordmanager;1"].createInstance(Components.interfaces.nsIPasswordManagerInternal);
    } catch (e) {}
  }
  return gPasswordManagerInternal;
}
-- IvoVanDongen - 17 Oct 2005
Topic revision: r3 - 20 Oct 2005 - 15:25:59 - IvoVanDongen

tip TWiki Tip of the Day
Creating a Table of Contents
The TWikiVariables % nop TOC% will automatically create a table of contents for a topic based on the ... Read on Read more

 
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback