/services/sync/modules/identity.js

http://github.com/zpao/v8monkey · JavaScript · 143 lines · 81 code · 14 blank · 48 comment · 14 complexity · cfba029907cc171955fd67e439c84bd3 MD5 · raw file

  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is Bookmarks Sync.
  15. *
  16. * The Initial Developer of the Original Code is Mozilla.
  17. * Portions created by the Initial Developer are Copyright (C) 2007
  18. * the Initial Developer. All Rights Reserved.
  19. *
  20. * Contributor(s):
  21. * Dan Mills <thunder@mozilla.com>
  22. *
  23. * Alternatively, the contents of this file may be used under the terms of
  24. * either the GNU General Public License Version 2 or later (the "GPL"), or
  25. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26. * in which case the provisions of the GPL or the LGPL are applicable instead
  27. * of those above. If you wish to allow use of your version of this file only
  28. * under the terms of either the GPL or the LGPL, and not to allow others to
  29. * use your version of this file under the terms of the MPL, indicate your
  30. * decision by deleting the provisions above and replace them with the notice
  31. * and other provisions required by the GPL or the LGPL. If you do not delete
  32. * the provisions above, a recipient may use your version of this file under
  33. * the terms of any one of the MPL, the GPL or the LGPL.
  34. *
  35. * ***** END LICENSE BLOCK ***** */
  36. const EXPORTED_SYMBOLS = ['Identity', 'ID'];
  37. const Cc = Components.classes;
  38. const Ci = Components.interfaces;
  39. const Cr = Components.results;
  40. const Cu = Components.utils;
  41. Cu.import("resource://services-sync/constants.js");
  42. Cu.import("resource://services-sync/log4moz.js");
  43. Cu.import("resource://services-sync/util.js");
  44. // Avoid circular import.
  45. __defineGetter__("Service", function() {
  46. delete this.Service;
  47. Cu.import("resource://services-sync/service.js", this);
  48. return this.Service;
  49. });
  50. XPCOMUtils.defineLazyGetter(this, "ID", function () {
  51. return new IDManager();
  52. });
  53. // For storing identities we'll use throughout Weave
  54. function IDManager() {
  55. this._ids = {};
  56. }
  57. IDManager.prototype = {
  58. get: function IDMgr_get(name) {
  59. if (name in this._ids)
  60. return this._ids[name];
  61. return null;
  62. },
  63. set: function IDMgr_set(name, id) {
  64. this._ids[name] = id;
  65. return id;
  66. },
  67. del: function IDMgr_del(name) {
  68. delete this._ids[name];
  69. }
  70. };
  71. /*
  72. * Identity
  73. * These objects hold a realm, username, and password
  74. * They can hold a password in memory, but will try to fetch it from
  75. * the password manager if it's not set.
  76. * FIXME: need to rethink this stuff as part of a bigger identity mgmt framework
  77. */
  78. function Identity(realm, username, password) {
  79. this.realm = realm;
  80. this.username = username;
  81. this._password = password;
  82. if (password)
  83. this._passwordUTF8 = Utils.encodeUTF8(password);
  84. }
  85. Identity.prototype = {
  86. get password() {
  87. // Look up the password then cache it
  88. if (this._password == null)
  89. for each (let login in this._logins)
  90. if (login.username.toLowerCase() == this.username) {
  91. this._password = login.password;
  92. this._passwordUTF8 = Utils.encodeUTF8(login.password);
  93. }
  94. return this._password;
  95. },
  96. set password(value) {
  97. this._password = value;
  98. this._passwordUTF8 = Utils.encodeUTF8(value);
  99. },
  100. get passwordUTF8() {
  101. if (!this._passwordUTF8)
  102. this.password; // invoke password getter
  103. return this._passwordUTF8;
  104. },
  105. persist: function persist() {
  106. // Clean up any existing passwords unless it's what we're persisting
  107. let exists = false;
  108. for each (let login in this._logins) {
  109. if (login.username == this.username && login.password == this._password)
  110. exists = true;
  111. else
  112. Services.logins.removeLogin(login);
  113. }
  114. // No need to create the login after clearing out the other ones
  115. let log = Log4Moz.repository.getLogger("Sync.Identity");
  116. if (exists) {
  117. log.trace("Skipping persist: " + this.realm + " for " + this.username);
  118. return;
  119. }
  120. // Add the new username/password
  121. log.trace("Persisting " + this.realm + " for " + this.username);
  122. let nsLoginInfo = new Components.Constructor(
  123. "@mozilla.org/login-manager/loginInfo;1", Ci.nsILoginInfo, "init");
  124. let newLogin = new nsLoginInfo(PWDMGR_HOST, null, this.realm,
  125. this.username, this.password, "", "");
  126. Services.logins.addLogin(newLogin);
  127. },
  128. get _logins() Services.logins.findLogins({}, PWDMGR_HOST, null, this.realm)
  129. };