Using nsIPasswordManager

  • Revision slug: Using_nsIPasswordManager
  • Revision title: Using nsIPasswordManager
  • Revision id: 113620
  • Created:
  • Creator: ericpp
  • Is current revision? No
  • Comment 11 words added

Revision Content

I just had to use the Password Manager for a project I was working on, so I thought I'd braindump my notes to the wiki while I was at it. This code is tested, but barely, and I am by no means an expert on this area. Review and cleanup would be appreciated. Zachlipton 22:52, 18 July 2006 (PDT)

The code on this page will work with applications using Toolkit 1.8 and below such as Firefox 2.0.0.x and Thunderbird 2.0.0.x. For similar functionality in Toolkit 1.9, see Using nsILoginManager.

Working with Password Manager

Extensions often need to securely store passwords to external sites, web applications, and so on. To do so securely, they can use nsIPasswordManager, which provides for secure storage of sensitive password information.

Getting nsIPasswordManager

To get a component implementing nsIPasswordManager, use the following:

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

Storing a password

To store a password in the password manager, you need three things: a hostname/URL (you'll need this to retrieve the password again later), a username, and a password. Of this information, the password is the only data that will be stored securely. Adding a password to the password manager is easy:

passwordManager.addUser('host', 'username', 'password');

Since there's no provision to include names of HTML input fields, no password stored by this interface will be used to fill in passwords on web pages. nsILoginManager, available in Toolkit 1.9, does let you include input field names.

Retrieving a password

Retrieving a password from the password manager is more difficult. The example below should serve as a starting point:

// the host name of the password we are looking for
var queryString = 'http://www.example.com';
// ask the password manager for an enumerator:
var e = passwordManager.enumerator;
// step through each password in the password manager until we find the one we want:
while (e.hasMoreElements()) {
    try {
        // get an nsIPassword object out of the password manager.
        // This contains the actual password...
        var pass = e.getNext().QueryInterface(Components.interfaces.nsIPassword);
        if (pass.host == queryString) {
             // found it!
             alert(pass.user); // the username
             alert(pass.password); // the password
             break;
        }
    } catch (ex) {
        // do something if decrypting the password failed--probably a continue
    }
}

Note that the user will be prompted for their master password if they have chosen to set one to secure their passwords.

Removing a password

passwordManager.removeUser('host','username');

Revision Source

<div class="note">
<p>I just had to use the Password Manager for a project I was working on, so I thought I'd braindump my notes to the wiki while I was at it. This code is tested, but barely, and I am by no means an expert on this area. Review and cleanup would be appreciated. <a href="/User:Zachlipton" title="User:Zachlipton">Zachlipton</a> 22:52, 18 July 2006 (PDT)</p>
</div>
<p>The code on this page will work with applications using Toolkit 1.8 and below such as Firefox 2.0.0.x and Thunderbird 2.0.0.x. For similar functionality in Toolkit 1.9, see <a href="/en/XPCOM_Interface_Reference/Using_nsILoginManager" title="en/XPCOM_Interface_Reference/Using_nsILoginManager">Using nsILoginManager</a>.</p>
<h3 id="Working_with_Password_Manager" name="Working_with_Password_Manager">Working with Password Manager</h3>
<p>Extensions often need to securely store passwords to external sites, web applications, and so on. To do so securely, they can use <code><a href="/en/nsIPasswordManager" title="en/nsIPasswordManager">nsIPasswordManager</a></code>, which provides for secure storage of sensitive password information.</p>
<h3 id="Getting_nsIPasswordManager" name="Getting_nsIPasswordManager">Getting <code>nsIPasswordManager</code></h3>
<p>To get a component implementing <code>nsIPasswordManager</code>, use the following:</p>
<pre>var passwordManager = Components.classes["@mozilla.org/passwordmanager;1"]
                                .getService(Components.interfaces.nsIPasswordManager);
</pre>
<h3 id="Storing_a_password" name="Storing_a_password">Storing a password</h3>
<p>To store a password in the password manager, you need three things: a hostname/URL (you'll need this to retrieve the password again later), a username, and a password. Of this information, the password is the only data that will be stored securely. Adding a password to the password manager is easy:</p>
<pre class="eval">passwordManager.addUser('host', 'username', 'password');
</pre>
<p>Since there's no provision to include names of HTML input fields, no password stored by this interface will be used to fill in passwords on web pages. <a href="/en/XPCOM_Interface_Reference/nsILoginManager" title="en/XPCOM_Interface_Reference/nsILoginManager">nsILoginManager</a>, available in Toolkit 1.9, does let you include input field names.</p>
<h3 id="Retrieving_a_password" name="Retrieving_a_password">Retrieving a password</h3>
<p>Retrieving a password from the password manager is more difficult. The example below should serve as a starting point:</p>
<pre>// the host name of the password we are looking for
var queryString = 'http://www.example.com';
// ask the password manager for an enumerator:
var e = passwordManager.enumerator;
// step through each password in the password manager until we find the one we want:
while (e.hasMoreElements()) {
    try {
        // get an nsIPassword object out of the password manager.
        // This contains the actual password...
        var pass = e.getNext().QueryInterface(Components.interfaces.nsIPassword);
        if (pass.host == queryString) {
             // found it!
             alert(pass.user); // the username
             alert(pass.password); // the password
             break;
        }
    } catch (ex) {
        // do something if decrypting the password failed--probably a continue
    }
}
</pre>
<p>Note that the user will be prompted for their master password if they have chosen to set one to secure their passwords.</p>
<h3 id="Removing_a_password" name="Removing_a_password">Removing a password</h3>
<pre>passwordManager.removeUser('host','username');
</pre>
Revert to this revision