addon-sdk/source/lib/sdk/passwords/utils.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 module.metadata = {
michael@0 8 "stability": "unstable"
michael@0 9 };
michael@0 10
michael@0 11 const { Cc, Ci, CC } = require("chrome");
michael@0 12 const { uri: ADDON_URI } = require("../self");
michael@0 13 const loginManager = Cc["@mozilla.org/login-manager;1"].
michael@0 14 getService(Ci.nsILoginManager);
michael@0 15 const { URL: parseURL } = require("../url");
michael@0 16 const LoginInfo = CC("@mozilla.org/login-manager/loginInfo;1",
michael@0 17 "nsILoginInfo", "init");
michael@0 18
michael@0 19 function filterMatchingLogins(loginInfo)
michael@0 20 Object.keys(this).every(function(key) loginInfo[key] === this[key], this);
michael@0 21
michael@0 22 /**
michael@0 23 * Removes `user`, `password` and `path` fields from the given `url` if it's
michael@0 24 * 'http', 'https' or 'ftp'. All other URLs are returned unchanged.
michael@0 25 * @example
michael@0 26 * http://user:pass@www.site.com/foo/?bar=baz#bang -> http://www.site.com
michael@0 27 */
michael@0 28 function normalizeURL(url) {
michael@0 29 let { scheme, host, port } = parseURL(url);
michael@0 30 // We normalize URL only if it's `http`, `https` or `ftp`. All other types of
michael@0 31 // URLs (`resource`, `chrome`, etc..) should not be normalized as they are
michael@0 32 // used with add-on associated credentials path.
michael@0 33 return scheme === "http" || scheme === "https" || scheme === "ftp" ?
michael@0 34 scheme + "://" + (host || "") + (port ? ":" + port : "") :
michael@0 35 url
michael@0 36 }
michael@0 37
michael@0 38 function Login(options) {
michael@0 39 let login = Object.create(Login.prototype);
michael@0 40 Object.keys(options || {}).forEach(function(key) {
michael@0 41 if (key === 'url')
michael@0 42 login.hostname = normalizeURL(options.url);
michael@0 43 else if (key === 'formSubmitURL')
michael@0 44 login.formSubmitURL = options.formSubmitURL ?
michael@0 45 normalizeURL(options.formSubmitURL) : null;
michael@0 46 else if (key === 'realm')
michael@0 47 login.httpRealm = options.realm;
michael@0 48 else
michael@0 49 login[key] = options[key];
michael@0 50 });
michael@0 51
michael@0 52 return login;
michael@0 53 }
michael@0 54 Login.prototype.toJSON = function toJSON() {
michael@0 55 return {
michael@0 56 url: this.hostname || ADDON_URI,
michael@0 57 realm: this.httpRealm || null,
michael@0 58 formSubmitURL: this.formSubmitURL || null,
michael@0 59 username: this.username || null,
michael@0 60 password: this.password || null,
michael@0 61 usernameField: this.usernameField || '',
michael@0 62 passwordField: this.passwordField || '',
michael@0 63 }
michael@0 64 };
michael@0 65 Login.prototype.toLoginInfo = function toLoginInfo() {
michael@0 66 let { url, realm, formSubmitURL, username, password, usernameField,
michael@0 67 passwordField } = this.toJSON();
michael@0 68
michael@0 69 return new LoginInfo(url, formSubmitURL, realm, username, password,
michael@0 70 usernameField, passwordField);
michael@0 71 };
michael@0 72
michael@0 73 function loginToJSON(value) Login(value).toJSON()
michael@0 74
michael@0 75 /**
michael@0 76 * Returns array of `nsILoginInfo` objects that are stored in the login manager
michael@0 77 * and have all the properties with matching values as a given `options` object.
michael@0 78 * @param {Object} options
michael@0 79 * @returns {nsILoginInfo[]}
michael@0 80 */
michael@0 81 exports.search = function search(options) {
michael@0 82 return loginManager.getAllLogins()
michael@0 83 .filter(filterMatchingLogins, Login(options))
michael@0 84 .map(loginToJSON);
michael@0 85 };
michael@0 86
michael@0 87 /**
michael@0 88 * Stores login info created from the given `options` to the applications
michael@0 89 * built-in login management system.
michael@0 90 * @param {Object} options.
michael@0 91 */
michael@0 92 exports.store = function store(options) {
michael@0 93 loginManager.addLogin(Login(options).toLoginInfo());
michael@0 94 };
michael@0 95
michael@0 96 /**
michael@0 97 * Removes login info from the applications built-in login management system.
michael@0 98 * _Please note: When removing a login info the specified properties must
michael@0 99 * exactly match to the one that is already stored or exception will be thrown._
michael@0 100 * @param {Object} options.
michael@0 101 */
michael@0 102 exports.remove = function remove(options) {
michael@0 103 loginManager.removeLogin(Login(options).toLoginInfo());
michael@0 104 };

mercurial