utils.js 3.6 KB

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