diff options
-rw-r--r-- | lib/passwords.js | 57 | ||||
-rw-r--r-- | tests/test-password.js | 18 |
2 files changed, 75 insertions, 0 deletions
diff --git a/lib/passwords.js b/lib/passwords.js new file mode 100644 index 0000000..a5adee0 --- /dev/null +++ b/lib/passwords.js @@ -0,0 +1,57 @@ +/*jslint forin: true, rhino: true, onevar: false, browser: true, evil: true, laxbreak: true, undef: true, nomen: true, eqeqeq: true, bitwise: true, maxerr: 1000, immed: false, white: false, plusplus: false, regexp: false, undef: false */ +// Released under the MIT/X11 license +// http://www.opensource.org/licenses/mit-license.php +"use strict"; +var {Cc,Ci,components} = require("chrome"); + +function getLoginInfo(username, pass, + domain, realm) { + var nsLoginInfo = new components. + Constructor("@mozilla.org/login-manager/loginInfo;1", + Ci.nsILoginInfo, "init"); + return new nsLoginInfo(domain, + null, realm, username, pass, "", ""); +} + +function getPasswordManager() { + return Cc["@mozilla.org/login-manager;1"]. + getService(Ci.nsILoginManager); +} + +exports.setPassword = function setPassword (username, pass, + domain, realm) { + var lInfo = getLoginInfo(username, pass, domain, realm); + getPasswordManager().addLogin(lInfo); +}; + +var getPassword = exports.getPassword = function getPassword(username, + domain, realm) { + + var pwMgr = getPasswordManager(); + var logins = pwMgr.findLogins({}, domain, "", realm); + var ourLogins = Array.filter(logins, function (x) { + return (x.username = username); + }, this); + // What to do when we have more than one password? + if (ourLogins.length > 0) { + return ourLogins[0].password; + } else { + return null; + } +}; + +exports.removePassword = function removePassword(username, + domain, realm) { + var pass = getPassword(username, domain, realm); + var pwMgr = getPasswordManager(); + var logins = pwMgr.findLogins({}, domain, "", realm); + + // Don't do Array.forEach here ... emulating break there + // is an abomination + for (var i = 0, ii = logins.length; i < ii; i++) { + if (logins[i].username === username) { + pwMgr.removeLogin(logins[i]); + break; + } + } +}; diff --git a/tests/test-password.js b/tests/test-password.js new file mode 100644 index 0000000..e605dc4 --- /dev/null +++ b/tests/test-password.js @@ -0,0 +1,18 @@ +/*jslint forin: true, rhino: true, onevar: false, browser: true, evil: true, laxbreak: true, undef: true, nomen: true, eqeqeq: true, bitwise: true, maxerr: 1000, immed: false, white: false, plusplus: false, regexp: false, undef: false */ +// Released under the MIT/X11 license +// http://www.opensource.org/licenses/mit-license.php +"use strict"; +var passMod = require("passwords"); +var BTSRealm = "BTSTest"; +var testLogin = "testUser"; +var testPass = "verySecret"; +var testDomain = "http://www.example.com"; + +exports.ensureSetPassword = function (test) { + passMod.setPassword(testLogin, testPass, + testDomain, BTSRealm); + test.assertEqual(passMod.getPassword(testLogin, + testDomain, BTSRealm), testPass, ""); + passMod.removePassword(testLogin, testPass, + testDomain, BTSRealm); +}; |