PageRenderTime 28ms CodeModel.GetById 17ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 37const EXPORTED_SYMBOLS = ['Identity', 'ID'];
 38
 39const Cc = Components.classes;
 40const Ci = Components.interfaces;
 41const Cr = Components.results;
 42const Cu = Components.utils;
 43
 44Cu.import("resource://services-sync/constants.js");
 45Cu.import("resource://services-sync/log4moz.js");
 46Cu.import("resource://services-sync/util.js");
 47
 48// Avoid circular import.
 49__defineGetter__("Service", function() {
 50  delete this.Service;
 51  Cu.import("resource://services-sync/service.js", this);
 52  return this.Service;
 53});
 54
 55XPCOMUtils.defineLazyGetter(this, "ID", function () {
 56  return new IDManager();
 57});
 58
 59// For storing identities we'll use throughout Weave
 60function IDManager() {
 61  this._ids = {};
 62}
 63IDManager.prototype = {
 64  get: function IDMgr_get(name) {
 65    if (name in this._ids)
 66      return this._ids[name];
 67    return null;
 68  },
 69  set: function IDMgr_set(name, id) {
 70    this._ids[name] = id;
 71    return id;
 72  },
 73  del: function IDMgr_del(name) {
 74    delete this._ids[name];
 75  }
 76};
 77
 78/*
 79 * Identity
 80 * These objects hold a realm, username, and password
 81 * They can hold a password in memory, but will try to fetch it from
 82 * the password manager if it's not set.
 83 * FIXME: need to rethink this stuff as part of a bigger identity mgmt framework
 84 */
 85
 86function Identity(realm, username, password) {
 87  this.realm = realm;
 88  this.username = username;
 89  this._password = password;
 90  if (password)
 91    this._passwordUTF8 = Utils.encodeUTF8(password);
 92}
 93Identity.prototype = {
 94  get password() {
 95    // Look up the password then cache it
 96    if (this._password == null)
 97      for each (let login in this._logins)
 98        if (login.username.toLowerCase() == this.username) {
 99          this._password = login.password;
100          this._passwordUTF8 = Utils.encodeUTF8(login.password);
101        }
102    return this._password;
103  },
104
105  set password(value) {
106    this._password = value;
107    this._passwordUTF8 = Utils.encodeUTF8(value);
108  },
109
110  get passwordUTF8() {
111    if (!this._passwordUTF8)
112      this.password; // invoke password getter
113    return this._passwordUTF8;
114  },
115
116  persist: function persist() {
117    // Clean up any existing passwords unless it's what we're persisting
118    let exists = false;
119    for each (let login in this._logins) {
120      if (login.username == this.username && login.password == this._password)
121        exists = true;
122      else
123        Services.logins.removeLogin(login);
124    }
125
126    // No need to create the login after clearing out the other ones
127    let log = Log4Moz.repository.getLogger("Sync.Identity");
128    if (exists) {
129      log.trace("Skipping persist: " + this.realm + " for " + this.username);
130      return;
131    }
132
133    // Add the new username/password
134    log.trace("Persisting " + this.realm + " for " + this.username);
135    let nsLoginInfo = new Components.Constructor(
136      "@mozilla.org/login-manager/loginInfo;1", Ci.nsILoginInfo, "init");
137    let newLogin = new nsLoginInfo(PWDMGR_HOST, null, this.realm,
138      this.username, this.password, "", "");
139    Services.logins.addLogin(newLogin);
140  },
141
142  get _logins() Services.logins.findLogins({}, PWDMGR_HOST, null, this.realm)
143};