2008-06-26 14:49:01 -07:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Bookmarks Sync.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Justin Dolske <dolske@mozilla.com>
|
2009-03-02 15:42:57 -08:00
|
|
|
* Anant Narayanan <anant@kix.in>
|
2008-06-26 14:49:01 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2008-06-03 14:08:53 -07:00
|
|
|
const EXPORTED_SYMBOLS = ['PasswordEngine'];
|
|
|
|
|
|
|
|
const Cu = Components.utils;
|
2009-03-02 15:42:57 -08:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
2008-06-03 14:08:53 -07:00
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
Cu.import("resource://weave/log4moz.js");
|
2008-06-03 14:08:53 -07:00
|
|
|
Cu.import("resource://weave/util.js");
|
|
|
|
Cu.import("resource://weave/engines.js");
|
|
|
|
Cu.import("resource://weave/stores.js");
|
2008-06-30 12:26:41 -07:00
|
|
|
Cu.import("resource://weave/trackers.js");
|
2009-03-02 15:42:57 -08:00
|
|
|
Cu.import("resource://weave/async.js");
|
|
|
|
Cu.import("resource://weave/ext/Observers.js");
|
|
|
|
Cu.import("resource://weave/type_records/passwords.js");
|
2008-06-03 14:08:53 -07:00
|
|
|
|
2008-06-29 17:37:11 -07:00
|
|
|
Function.prototype.async = Async.sugar;
|
|
|
|
|
2008-06-18 12:04:49 -07:00
|
|
|
function PasswordEngine() {
|
|
|
|
this._init();
|
2008-06-03 14:08:53 -07:00
|
|
|
}
|
|
|
|
PasswordEngine.prototype = {
|
2009-03-02 15:42:57 -08:00
|
|
|
__proto__: SyncEngine.prototype,
|
|
|
|
name: "passwords",
|
|
|
|
displayName: "Passwords",
|
|
|
|
logName: "Passwords",
|
|
|
|
_storeObj: PasswordStore,
|
|
|
|
_trackerObj: PasswordTracker,
|
|
|
|
_recordObj: LoginRec,
|
2009-03-02 18:55:26 -08:00
|
|
|
|
|
|
|
_syncStartup: function PasswordEngine__syncStartup() {
|
|
|
|
let self = yield;
|
|
|
|
this._store.cacheLogins();
|
|
|
|
yield SyncEngine.prototype._syncStartup.async(this, self.cb);
|
|
|
|
},
|
|
|
|
|
2009-03-02 16:15:48 -08:00
|
|
|
/* Wipe cache when sync finishes */
|
2009-03-02 15:57:37 -08:00
|
|
|
_syncFinish: function PasswordEngine__syncFinish() {
|
2009-03-02 15:42:57 -08:00
|
|
|
let self = yield;
|
2009-03-02 18:55:26 -08:00
|
|
|
this._store.clearLoginCache();
|
2009-03-02 15:42:57 -08:00
|
|
|
yield SyncEngine.prototype._syncFinish.async(this, self.cb);
|
2009-03-03 01:32:10 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
_recordLike: function SyncEngine__recordLike(a, b) {
|
|
|
|
if (a.cleartext == null || b.cleartext == null)
|
|
|
|
return false;
|
|
|
|
if (a.cleartext.hostname == b.cleartext.hostname) {
|
|
|
|
}
|
|
|
|
if (a.cleartext.hostname != b.cleartext.hostname ||
|
|
|
|
a.cleartext.httpRealm != b.cleartext.httpRealm ||
|
|
|
|
a.cleartext.username != b.cleartext.username)
|
|
|
|
return false;
|
|
|
|
if (!a.cleartext.formSubmitURL || !b.cleartext.formSubmitURL)
|
|
|
|
return true;
|
|
|
|
return a.cleartext.formSubmitURL == b.cleartext.formSubmitURL;
|
2008-06-03 14:08:53 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function PasswordStore() {
|
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
PasswordStore.prototype = {
|
2009-03-02 15:42:57 -08:00
|
|
|
__proto__: Store.prototype,
|
2008-06-03 14:08:53 -07:00
|
|
|
_logName: "PasswordStore",
|
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
_nsLoginInfo: null,
|
|
|
|
_init: function PasswordStore_init() {
|
|
|
|
Store.prototype._init.call(this);
|
|
|
|
this._nsLoginInfo = new Components.Constructor(
|
|
|
|
"@mozilla.org/login-manager/loginInfo;1",
|
|
|
|
Ci.nsILoginInfo,
|
|
|
|
"init"
|
2009-03-02 18:55:26 -08:00
|
|
|
);
|
2009-03-02 15:42:57 -08:00
|
|
|
},
|
2009-03-02 18:55:26 -08:00
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
_nsLoginInfoFromRecord: function PasswordStore__nsLoginInfoRec(record) {
|
2009-03-03 01:48:13 -08:00
|
|
|
let info = new this._nsLoginInfo(record.hostname,
|
|
|
|
record.formSubmitURL,
|
|
|
|
record.httpRealm,
|
|
|
|
record.username,
|
|
|
|
record.password,
|
|
|
|
record.usernameField,
|
|
|
|
record.passwordField);
|
|
|
|
info.QueryInterface(Ci.nsILoginMetaInfo);
|
|
|
|
info.guid = record.id;
|
|
|
|
return info;
|
2008-06-03 14:08:53 -07:00
|
|
|
},
|
2008-06-29 17:37:11 -07:00
|
|
|
|
2009-03-02 18:55:26 -08:00
|
|
|
cacheLogins: function PasswordStore_cacheLogins() {
|
2009-03-03 01:32:10 -08:00
|
|
|
this._log.debug("Caching all logins");
|
2009-03-02 18:55:26 -08:00
|
|
|
this._loginItems = this.getAllIDs();
|
|
|
|
},
|
|
|
|
|
|
|
|
clearLoginCache: function PasswordStore_clearLoginCache() {
|
2009-03-03 01:32:10 -08:00
|
|
|
this._log.debug("Clearing login cache");
|
2009-03-02 18:55:26 -08:00
|
|
|
this._loginItems = null;
|
|
|
|
},
|
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
getAllIDs: function PasswordStore__getAllIDs() {
|
|
|
|
let items = {};
|
2009-03-10 04:30:30 -07:00
|
|
|
let logins = Svc.Login.getAllLogins({});
|
2009-03-02 18:55:26 -08:00
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
for (let i = 0; i < logins.length; i++) {
|
|
|
|
let metaInfo = logins[i].QueryInterface(Ci.nsILoginMetaInfo);
|
2009-03-03 01:32:10 -08:00
|
|
|
items[metaInfo.guid] = metaInfo;
|
2009-03-02 15:42:57 -08:00
|
|
|
}
|
2009-03-02 18:55:26 -08:00
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
return items;
|
2008-06-27 20:16:43 -07:00
|
|
|
},
|
2008-06-03 14:08:53 -07:00
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
changeItemID: function PasswordStore__changeItemID(oldID, newID) {
|
2009-03-03 01:32:10 -08:00
|
|
|
this._log.debug("Changing item ID: " + oldID + " to " + newID);
|
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
if (!(oldID in this._loginItems)) {
|
2009-03-03 01:32:10 -08:00
|
|
|
this._log.warn("Can't change item ID: item doesn't exist");
|
2009-03-02 15:42:57 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (newID in this._loginItems) {
|
2009-03-03 01:32:10 -08:00
|
|
|
this._log.warn("Can't change item ID: new ID already in use");
|
2009-03-02 15:42:57 -08:00
|
|
|
return;
|
|
|
|
}
|
2009-03-02 18:55:26 -08:00
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
let prop = Cc["@mozilla.org/hash-property-bag;1"].
|
2009-03-03 01:32:10 -08:00
|
|
|
createInstance(Ci.nsIWritablePropertyBag2);
|
2009-03-02 15:42:57 -08:00
|
|
|
prop.setPropertyAsAUTF8String("guid", newID);
|
2008-06-03 14:08:53 -07:00
|
|
|
|
2009-03-10 04:30:30 -07:00
|
|
|
Svc.Login.modifyLogin(this._loginItems[oldID], prop);
|
2008-06-03 14:08:53 -07:00
|
|
|
},
|
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
itemExists: function PasswordStore__itemExists(id) {
|
2009-03-03 01:32:10 -08:00
|
|
|
return (id in this._loginItems);
|
2009-03-02 15:42:57 -08:00
|
|
|
},
|
2008-06-29 17:37:11 -07:00
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
createRecord: function PasswordStore__createRecord(guid, cryptoMetaURL) {
|
|
|
|
let record = new LoginRec();
|
|
|
|
record.id = guid;
|
|
|
|
if (guid in this._loginItems) {
|
|
|
|
let login = this._loginItems[guid];
|
|
|
|
record.encryption = cryptoMetaURL;
|
|
|
|
record.hostname = login.hostname;
|
|
|
|
record.formSubmitURL = login.formSubmitURL;
|
|
|
|
record.httpRealm = login.httpRealm;
|
|
|
|
record.username = login.username;
|
|
|
|
record.password = login.password;
|
|
|
|
record.usernameField = login.usernameField;
|
|
|
|
record.passwordField = login.passwordField;
|
2008-06-28 01:13:14 -07:00
|
|
|
} else {
|
2009-03-02 15:42:57 -08:00
|
|
|
/* Deleted item */
|
|
|
|
record.cleartext = null;
|
2008-06-28 01:13:14 -07:00
|
|
|
}
|
2009-03-02 15:42:57 -08:00
|
|
|
return record;
|
2008-06-03 14:08:53 -07:00
|
|
|
},
|
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
create: function PasswordStore__create(record) {
|
2009-03-03 01:32:10 -08:00
|
|
|
this._log.debug("Adding login for " + record.hostname);
|
2009-03-10 04:30:30 -07:00
|
|
|
Svc.Login.addLogin(this._nsLoginInfoFromRecord(record));
|
2008-06-03 14:08:53 -07:00
|
|
|
},
|
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
remove: function PasswordStore__remove(record) {
|
2009-03-03 01:32:10 -08:00
|
|
|
this._log.debug("Removing login " + record.id);
|
2009-03-02 15:42:57 -08:00
|
|
|
if (record.id in this._loginItems) {
|
2009-03-10 04:30:30 -07:00
|
|
|
Svc.Login.removeLogin(this._loginItems[record.id]);
|
2009-03-02 15:42:57 -08:00
|
|
|
return;
|
|
|
|
}
|
2009-03-02 18:55:26 -08:00
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
this._log.debug("Asked to remove record that doesn't exist, ignoring!");
|
|
|
|
},
|
2008-06-03 14:08:53 -07:00
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
update: function PasswordStore__update(record) {
|
2009-03-03 01:32:10 -08:00
|
|
|
this._log.debug("Updating login for " + record.hostname);
|
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
if (!(record.id in this._loginItems)) {
|
|
|
|
this._log.debug("Skipping update for unknown item: " + record.id);
|
|
|
|
return;
|
2008-06-03 14:08:53 -07:00
|
|
|
}
|
2009-03-02 15:42:57 -08:00
|
|
|
let login = this._loginItems[record.id];
|
|
|
|
this._log.trace("Updating " + record.id + " (" + itemId + ")");
|
2008-06-03 14:08:53 -07:00
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
let newinfo = this._nsLoginInfoFromRecord(record);
|
2009-03-10 04:30:30 -07:00
|
|
|
Svc.Login.modifyLogin(login, newinfo);
|
2008-06-03 14:08:53 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
wipe: function PasswordStore_wipe() {
|
2009-03-10 04:30:30 -07:00
|
|
|
Svc.Login.removeAllLogins();
|
2008-06-03 14:08:53 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-06-30 11:19:07 -07:00
|
|
|
function PasswordTracker() {
|
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
PasswordTracker.prototype = {
|
2009-03-02 15:42:57 -08:00
|
|
|
__proto__: Tracker.prototype,
|
2008-06-30 11:19:07 -07:00
|
|
|
_logName: "PasswordTracker",
|
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
_init: function PasswordTracker_init() {
|
|
|
|
Tracker.prototype._init.call(this);
|
|
|
|
Observers.add("passwordmgr-storage-changed", this);
|
2008-07-25 01:06:23 -07:00
|
|
|
},
|
2008-06-30 11:19:07 -07:00
|
|
|
|
2009-03-02 15:42:57 -08:00
|
|
|
/* A single add, remove or change is 15 points, all items removed is 50 */
|
|
|
|
observe: function PasswordTracker_observe(aSubject, aTopic, aData) {
|
|
|
|
if (this.ignoreAll)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (aData) {
|
|
|
|
case 'addLogin':
|
|
|
|
case 'modifyLogin':
|
|
|
|
case 'removeLogin':
|
|
|
|
let metaInfo = aSubject.QueryInterface(Ci.nsILoginMetaInfo);
|
|
|
|
this._score += 15;
|
2009-03-03 01:32:10 -08:00
|
|
|
this._log.debug(aData + ": " + metaInfo.guid);
|
2009-03-02 15:42:57 -08:00
|
|
|
this.addChangedID(metaInfo.guid);
|
|
|
|
break;
|
|
|
|
case 'removeAllLogins':
|
2009-03-03 01:32:10 -08:00
|
|
|
this._log.debug(aData);
|
2009-03-02 15:42:57 -08:00
|
|
|
this._score += 50;
|
|
|
|
break;
|
|
|
|
}
|
2008-06-30 11:19:07 -07:00
|
|
|
}
|
2008-06-30 12:26:41 -07:00
|
|
|
};
|