diff options
-rw-r--r-- | docs/passwords.md | 51 | ||||
-rw-r--r-- | lib/bzpage.js | 8 | ||||
-rw-r--r-- | lib/main.js | 4 | ||||
-rw-r--r-- | lib/passwords.js | 57 | ||||
-rw-r--r-- | lib/util.js | 9 | ||||
-rw-r--r-- | tests/test-password.js | 18 |
6 files changed, 138 insertions, 9 deletions
diff --git a/docs/passwords.md b/docs/passwords.md new file mode 100644 index 0000000..283c54b --- /dev/null +++ b/docs/passwords.md @@ -0,0 +1,51 @@ +The `passwords` module provides access to the Firefox Password Manager. + +<api name="setPassword"> +@method +The `setPassword` method sets new authentication pair to the encrypted +Firefox's nsILoginManager. + +It is most useful for storing sensitive data with private information +to the store. It is most useful in situation where script would need +some personal information, but it is our responsibility to store it +securely. + +@param username {string} login name +@param pass {string} password +@param domain {string} hostname of the server we are connecting to as URL. +As for example "http://bugzilla.mozilla.org" +@param realm {string} String distinguishing this login to other ones for +the same hostname. + +@returns {string} +</api> + +<api name="getPassword"> +@method +The `getPassword` method returns the password specified by the +parameters of this function. + +@param username {string} login name +@param domain {string} the same as in `setPassword` method. +@param realm {string} the same as in `setPassword` method. + +@returns {string} +</api> + +<api name="removePassword"> +@method +The `removePassword` method remove the login information from the storage. +Note that the serach parameters of this function must much **EXACTLY** +parameters of the corresponding call to `setPassword`. + +@param username {string} login name +@param domain {string} the same as in `setPassword` method. +@param realm {string} the same as in `setPassword` method. + +@returns {string} +</api> + +Examples +-------- + +For examples see `tests/test-password.js`
\ No newline at end of file diff --git a/lib/bzpage.js b/lib/bzpage.js index d9d679d..3ac61d6 100644 --- a/lib/bzpage.js +++ b/lib/bzpage.js @@ -11,8 +11,8 @@ var Color = require("color").Color; var TriagedDistro = 13; var NumberOfFrames = 7; var bugURL = "https://bugzilla.redhat.com/show_bug.cgi?id="; -var BSTPrefNS = "bugzilla-triage.setting."; -exports.BSTPrefNS = BSTPrefNS; +var BTSPrefNS = "bugzilla-triage.setting."; +exports.BTSPrefNS = BTSPrefNS; // ==================================================================================== // BZPage's methods @@ -66,7 +66,7 @@ var BZPage = function BZPage(win, config) { this.doc.getElementById("configurationButton").addEventListener( "click", function(evt) { - var prfNm = BSTPrefNS+"JSONURL"; + var prfNm = BTSPrefNS+"JSONURL"; var url = preferences.get(prfNm,""); var reply = that.win.prompt("New location of JSON configuration file",url); @@ -680,7 +680,7 @@ BZPage.prototype.getAttachments = function getAttachments () { * @return String with the password */ BZPage.prototype.getPassword = function getPassword () { - var prefName = BSTPrefNS+"BZpassword"; + var prefName = BTSPrefNS+"BZpassword"; if (preferences.isSet(prefName)) { return preferences.get(prefName,undefined); } else { diff --git a/lib/main.js b/lib/main.js index e14f947..937f237 100644 --- a/lib/main.js +++ b/lib/main.js @@ -19,7 +19,7 @@ var urlMod = require("url"); var selfMod = require("self"); var Request = require("request").Request; var preferences = require("preferences-service"); -var BSTPrefNS = require("bzpage").BSTPrefNS; +var BTSPrefNS = require("bzpage").BTSPrefNS; // Use my JSON for now before it is fixed for general public var JSONURLDefault = "https://fedorahosted.org/released"+ "/bugzilla-triage-scripts/Config_data.json"; @@ -86,7 +86,7 @@ function skipThisPage(doc) { } function initialize(callback) { - var prefName = BSTPrefNS+"JSONURL"; + var prefName = BTSPrefNS+"JSONURL"; var urlStr = ""; if (preferences.isSet(prefName)) { diff --git a/lib/passwords.js b/lib/passwords.js new file mode 100644 index 0000000..49b945f --- /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.setLogin = function setLogin (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.removeLogin = function removeLogin(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/lib/util.js b/lib/util.js index b72f7f0..f582d1d 100644 --- a/lib/util.js +++ b/lib/util.js @@ -197,7 +197,10 @@ var filterByRegexp = exports.filterByRegexp = * * @return String with the password */ -var getPassword = exports.getPassword = function getPassword() { +var getPassword = exports.getPassword = function getPassword(prompt) { + if (prompt === null) { + prompt = "Enter password:"; + } var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"] .getService(Ci.nsIPromptService); var password = { @@ -206,7 +209,7 @@ var getPassword = exports.getPassword = function getPassword() { var check = { value : true }; // default the checkbox to true - var result = prompts.promptPassword(null, "Title", "Enter password:", + var result = prompts.promptPassword(null, "Title", prompt, password, null, check); // result is true if OK was pressed, false if cancel was pressed. // password.value is set if OK was pressed. @@ -216,4 +219,4 @@ var getPassword = exports.getPassword = function getPassword() { } else { return undefined; } -};
\ No newline at end of file +}; diff --git a/tests/test-password.js b/tests/test-password.js new file mode 100644 index 0000000..e46ed9c --- /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.ensurePasswordMachinery = function (test) { + passMod.setLogin(testLogin, testPass, + testDomain, BTSRealm); + test.assertEqual(passMod.getPassword(testLogin, + testDomain, BTSRealm), testPass, ""); + passMod.removeLogin(testLogin, testPass, + testDomain, BTSRealm); +}; |