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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/passwords/utils.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,104 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +module.metadata = {
    1.11 +  "stability": "unstable"
    1.12 +};
    1.13 +
    1.14 +const { Cc, Ci, CC } = require("chrome");
    1.15 +const { uri: ADDON_URI } = require("../self");
    1.16 +const loginManager = Cc["@mozilla.org/login-manager;1"].
    1.17 +                     getService(Ci.nsILoginManager);
    1.18 +const { URL: parseURL } = require("../url");
    1.19 +const LoginInfo = CC("@mozilla.org/login-manager/loginInfo;1",
    1.20 +                     "nsILoginInfo", "init");
    1.21 +
    1.22 +function filterMatchingLogins(loginInfo)
    1.23 +  Object.keys(this).every(function(key) loginInfo[key] === this[key], this);
    1.24 +
    1.25 +/**
    1.26 + * Removes `user`, `password` and `path` fields from the given `url` if it's
    1.27 + * 'http', 'https' or 'ftp'. All other URLs are returned unchanged.
    1.28 + * @example
    1.29 + * http://user:pass@www.site.com/foo/?bar=baz#bang -> http://www.site.com
    1.30 + */
    1.31 +function normalizeURL(url) {
    1.32 +  let { scheme, host, port } = parseURL(url);
    1.33 +  // We normalize URL only if it's `http`, `https` or `ftp`. All other types of
    1.34 +  // URLs (`resource`, `chrome`, etc..) should not be normalized as they are
    1.35 +  // used with add-on associated credentials path.
    1.36 +  return scheme === "http" || scheme === "https" || scheme === "ftp" ?
    1.37 +         scheme + "://" + (host || "") + (port ? ":" + port : "") :
    1.38 +         url
    1.39 +}
    1.40 +
    1.41 +function Login(options) {
    1.42 +  let login = Object.create(Login.prototype);
    1.43 +  Object.keys(options || {}).forEach(function(key) {
    1.44 +    if (key === 'url')
    1.45 +      login.hostname = normalizeURL(options.url);
    1.46 +    else if (key === 'formSubmitURL')
    1.47 +      login.formSubmitURL = options.formSubmitURL ?
    1.48 +                            normalizeURL(options.formSubmitURL) : null;
    1.49 +    else if (key === 'realm')
    1.50 +      login.httpRealm = options.realm;
    1.51 +    else 
    1.52 +      login[key] = options[key];
    1.53 +  });
    1.54 +
    1.55 +  return login;
    1.56 +}
    1.57 +Login.prototype.toJSON = function toJSON() {
    1.58 +  return {
    1.59 +    url: this.hostname || ADDON_URI,
    1.60 +    realm: this.httpRealm || null,
    1.61 +    formSubmitURL: this.formSubmitURL || null,
    1.62 +    username: this.username || null,
    1.63 +    password: this.password || null,
    1.64 +    usernameField: this.usernameField || '',
    1.65 +    passwordField: this.passwordField || '',
    1.66 +  }
    1.67 +};
    1.68 +Login.prototype.toLoginInfo = function toLoginInfo() {
    1.69 +  let { url, realm, formSubmitURL, username, password, usernameField,
    1.70 +        passwordField } = this.toJSON();
    1.71 +
    1.72 +  return new LoginInfo(url, formSubmitURL, realm, username, password,
    1.73 +                       usernameField, passwordField);
    1.74 +};
    1.75 +
    1.76 +function loginToJSON(value) Login(value).toJSON()
    1.77 +
    1.78 +/**
    1.79 + * Returns array of `nsILoginInfo` objects that are stored in the login manager
    1.80 + * and have all the properties with matching values as a given `options` object.
    1.81 + * @param {Object} options
    1.82 + * @returns {nsILoginInfo[]}
    1.83 + */
    1.84 +exports.search = function search(options) {
    1.85 +  return loginManager.getAllLogins()
    1.86 +                     .filter(filterMatchingLogins, Login(options))
    1.87 +                     .map(loginToJSON);
    1.88 +};
    1.89 +
    1.90 +/**
    1.91 + * Stores login info created from the given `options` to the applications
    1.92 + * built-in login management system.
    1.93 + * @param {Object} options.
    1.94 + */
    1.95 +exports.store = function store(options) {
    1.96 +  loginManager.addLogin(Login(options).toLoginInfo());
    1.97 +};
    1.98 +
    1.99 +/**
   1.100 + * Removes login info from the applications built-in login management system.
   1.101 + * _Please note: When removing a login info the specified properties must
   1.102 + * exactly match to the one that is already stored or exception will be thrown._
   1.103 + * @param {Object} options.
   1.104 + */
   1.105 +exports.remove = function remove(options) {
   1.106 +  loginManager.removeLogin(Login(options).toLoginInfo());
   1.107 +};

mercurial