1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
import { LoginHelper } from "resource://gre/modules/LoginHelper.sys.mjs";
import { DataSourceBase } from "resource://gre/modules/megalist/aggregator/datasources/DataSourceBase.sys.mjs";
import { LoginCSVImport } from "resource://gre/modules/LoginCSVImport.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
LoginBreaches: "resource:///modules/LoginBreaches.sys.mjs",
});
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
"BREACH_ALERTS_ENABLED",
"signon.management.page.breach-alerts.enabled",
false
);
/**
* Data source for Logins.
*
* Each login is represented by 3 lines: origin, username and password.
*
* Protypes are used to reduce memory need because for different records
* similar lines will differ in values only.
*/
export class LoginDataSource extends DataSourceBase {
#originPrototype;
#usernamePrototype;
#passwordPrototype;
#loginsDisabledMessage;
#enabled;
#header;
constructor(...args) {
super(...args);
// Wait for Fluent to provide strings before loading data
this.localizeStrings({
headerLabel: "passwords-section-label",
originLabel: "passwords-origin-label",
usernameLabel: "passwords-username-label",
passwordLabel: "passwords-password-label",
passwordsDisabled: "passwords-disabled",
passwordsImportFilePickerTitle: "passwords-import-file-picker-title",
passwordsImportFilePickerImportButton:
"passwords-import-file-picker-import-button",
passwordsImportFilePickerCsvFilterTitle:
"passwords-import-file-picker-csv-filter-title",
passwordsImportFilePickerTsvFilterTitle:
"passwords-import-file-picker-tsv-filter-title",
dismissBreachCommandLabel: "passwords-dismiss-breach-alert-command",
}).then(strings => {
const copyCommand = { id: "Copy", label: "command-copy" };
const editCommand = { id: "Edit", label: "command-edit" };
const deleteCommand = { id: "Delete", label: "command-delete" };
const dismissBreachCommand = {
id: "DismissBreach",
label: strings.dismissBreachCommandLabel,
};
const noOriginSticker = { type: "error", label: "😾 Missing origin" };
const noPasswordSticker = { type: "error", label: "😾 Missing password" };
const breachedSticker = { type: "warning", label: "BREACH" };
const vulnerableSticker = { type: "risk", label: "🤮 Vulnerable" };
this.#loginsDisabledMessage = strings.passwordsDisabled;
this.#header = this.createHeaderLine(strings.headerLabel);
this.#header.commands.push(
{ id: "Create", label: "passwords-command-create" },
{ id: "Import", label: "passwords-command-import" },
{ id: "Export", label: "passwords-command-export" },
{ id: "RemoveAll", label: "passwords-command-remove-all" },
{ id: "Settings", label: "passwords-command-settings" },
{ id: "Help", label: "passwords-command-help" }
);
this.#header.executeImport = async () =>
this.#importFromFile(
strings.passwordsImportFilePickerTitle,
strings.passwordsImportFilePickerImportButton,
strings.passwordsImportFilePickerCsvFilterTitle,
strings.passwordsImportFilePickerTsvFilterTitle
);
this.#header.executeRemoveAll = () => this.#removeAllPasswords();
this.#header.executeSettings = () => this.#openPreferences();
this.#header.executeHelp = () => this.#getHelp();
this.#header.executeExport = () => this.#exportAllPasswords();
this.#originPrototype = this.prototypeDataLine({
label: { value: strings.originLabel },
start: { value: true },
value: {
get() {
return this.record.displayOrigin;
},
},
valueIcon: {
get() {
return `page-icon:${this.record.origin}`;
},
},
href: {
get() {
return this.record.origin;
},
},
commands: {
*value() {
yield { id: "Open", label: "command-open" };
yield copyCommand;
yield "-";
yield deleteCommand;
if (this.breached) {
yield dismissBreachCommand;
}
},
},
executeDismissBreach: {
value() {
lazy.LoginBreaches.recordBreachAlertDismissal(this.record.guid);
delete this.breached;
this.refreshOnScreen();
},
},
executeCopy: {
value() {
this.copyToClipboard(this.record.origin);
},
},
executeDelete: {
value() {
this.setLayout({ id: "remove-login" });
},
},
stickers: {
*value() {
if (this.isEditing() && !this.editingValue.length) {
yield noOriginSticker;
}
if (this.breached) {
yield breachedSticker;
}
},
},
});
this.#usernamePrototype = this.prototypeDataLine({
label: { value: strings.usernameLabel },
value: {
get() {
return this.editingValue ?? this.record.username;
},
},
commands: { value: [copyCommand, editCommand, "-", deleteCommand] },
executeEdit: {
value() {
this.editingValue = this.record.username ?? "";
this.refreshOnScreen();
},
},
executeSave: {
value(value) {
try {
const modifiedLogin = this.record.clone();
modifiedLogin.username = value;
Services.logins.modifyLogin(this.record, modifiedLogin);
} catch (error) {
//todo
console.error("failed to modify login", error);
}
this.executeCancel();
},
},
});
this.#passwordPrototype = this.prototypeDataLine({
label: { value: strings.passwordLabel },
concealed: { value: true, writable: true },
end: { value: true },
value: {
get() {
return (
this.editingValue ??
(this.concealed ? "••••••••" : this.record.password)
);
},
},
stickers: {
*value() {
if (this.isEditing() && !this.editingValue.length) {
yield noPasswordSticker;
}
if (this.vulnerable) {
yield vulnerableSticker;
}
},
},
commands: {
*value() {
if (this.concealed) {
yield { id: "Reveal", label: "command-reveal", verify: true };
} else {
yield { id: "Conceal", label: "command-conceal" };
}
yield { ...copyCommand, verify: true };
yield editCommand;
yield "-";
yield deleteCommand;
},
},
executeReveal: {
value() {
this.concealed = false;
this.refreshOnScreen();
},
},
executeConceal: {
value() {
this.concealed = true;
this.refreshOnScreen();
},
},
executeCopy: {
value() {
this.copyToClipboard(this.record.password);
},
},
executeEdit: {
value() {
this.editingValue = this.record.password ?? "";
this.refreshOnScreen();
},
},
executeSave: {
value(value) {
if (!value) {
return;
}
try {
const modifiedLogin = this.record.clone();
modifiedLogin.password = value;
Services.logins.modifyLogin(this.record, modifiedLogin);
} catch (error) {
//todo
console.error("failed to modify login", error);
}
this.executeCancel();
},
},
});
Services.obs.addObserver(this, "passwordmgr-storage-changed");
Services.prefs.addObserver("signon.rememberSignons", this);
Services.prefs.addObserver(
"signon.management.page.breach-alerts.enabled",
this
);
Services.prefs.addObserver(
"signon.management.page.vulnerable-passwords.enabled",
this
);
this.#reloadDataSource();
});
}
async #importFromFile(title, buttonLabel, csvTitle, tsvTitle) {
const { BrowserWindowTracker } = ChromeUtils.importESModule(
"resource:///modules/BrowserWindowTracker.sys.mjs"
);
const browsingContext = BrowserWindowTracker.getTopWindow().browsingContext;
let { result, path } = await this.openFilePickerDialog(
title,
buttonLabel,
[
{
title: csvTitle,
extensionPattern: "*.csv",
},
{
title: tsvTitle,
extensionPattern: "*.tsv",
},
],
browsingContext
);
if (result != Ci.nsIFilePicker.returnCancel) {
try {
const summary = await LoginCSVImport.importFromCSV(path);
const counts = { added: 0, modified: 0, no_change: 0, error: 0 };
for (const item of summary) {
counts[item.result] += 1;
}
const l10nArgs = Object.values(counts).map(count => ({ count }));
this.setLayout({
id: "import-logins",
l10nArgs,
});
} catch (e) {
this.setLayout({ id: "import-error" });
}
}
}
async openFilePickerDialog(
title,
okButtonLabel,
appendFilters,
browsingContext
) {
return new Promise(resolve => {
let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
fp.init(browsingContext, title, Ci.nsIFilePicker.modeOpen);
for (const appendFilter of appendFilters) {
fp.appendFilter(appendFilter.title, appendFilter.extensionPattern);
}
fp.appendFilters(Ci.nsIFilePicker.filterAll);
fp.okButtonLabel = okButtonLabel;
fp.open(async result => {
resolve({ result, path: fp.file.path });
});
});
}
#removeAllPasswords() {
let count = 0;
let currentRecord;
for (const line of this.lines) {
if (line.record != currentRecord) {
count += 1;
currentRecord = line.record;
}
}
this.setLayout({ id: "remove-logins", l10nArgs: [{ count }] });
}
#exportAllPasswords() {
this.setLayout({ id: "export-logins" });
}
confirmRemoveAll() {
Services.logins.removeAllLogins();
this.cancelDialog();
}
confirmExportLogins() {
// TODO: Implement this.
// We need to simplify this function first
// https://searchfox.org/mozilla-central/source/browser/components/aboutlogins/AboutLoginsParent.sys.mjs#377
// It's too messy right now.
this.cancelDialog();
}
confirmRemoveLogin() {
// TODO: Simplify getting record directly.
const login = this.lines?.[0]?.record;
Services.logins.removeLogin(login);
this.cancelDialog();
}
confirmRetryImport() {
// TODO: Implement this.
this.cancelDialog();
}
#openPreferences() {
const { BrowserWindowTracker } = ChromeUtils.importESModule(
"resource:///modules/BrowserWindowTracker.sys.mjs"
);
const browser = BrowserWindowTracker.getTopWindow().gBrowser;
browser.ownerGlobal.openPreferences("privacy-logins");
}
#getHelp() {
const { BrowserWindowTracker } = ChromeUtils.importESModule(
"resource:///modules/BrowserWindowTracker.sys.mjs"
);
const browser = BrowserWindowTracker.getTopWindow().gBrowser;
const SUPPORT_URL =
Services.urlFormatter.formatURLPref("app.support.baseURL") +
"password-manager-remember-delete-edit-logins";
browser.ownerGlobal.openWebLinkIn(SUPPORT_URL, "tab", {
relatedToCurrent: true,
});
}
/**
* Enumerate all the lines provided by this data source.
*
* @param {string} searchText used to filter data
*/
*enumerateLines(searchText) {
if (this.#enabled === undefined) {
// Async Fluent API makes it possible to have data source waiting
// for the localized strings, which can be detected by undefined in #enabled.
return;
}
yield this.#header;
if (this.#header.collapsed || !this.#enabled) {
return;
}
const stats = { count: 0, total: 0 };
searchText = searchText.toUpperCase();
yield* this.enumerateLinesForMatchingRecords(
searchText,
stats,
login =>
login.displayOrigin.toUpperCase().includes(searchText) ||
login.username.toUpperCase().includes(searchText) ||
login.password.toUpperCase().includes(searchText)
);
this.formatMessages({
id:
stats.count == stats.total
? "passwords-count"
: "passwords-filtered-count",
args: stats,
}).then(([headerLabel]) => {
this.#header.value = headerLabel;
});
}
/**
* Sync lines array with the actual data source.
* This function reads all logins from the storage, adds or updates lines and
* removes lines for the removed logins.
*/
async #reloadDataSource() {
this.#enabled = Services.prefs.getBoolPref("signon.rememberSignons");
if (!this.#enabled) {
this.#reloadEmptyDataSource();
return;
}
const logins = await LoginHelper.getAllUserFacingLogins();
this.beforeReloadingDataSource();
const breachesMap = lazy.BREACH_ALERTS_ENABLED
? await lazy.LoginBreaches.getPotentialBreachesByLoginGUID(logins)
: new Map();
logins.forEach(login => {
// Similar domains will be grouped together
// www. will have least effect on the sorting
const parts = login.displayOrigin.split(".");
// Exclude TLD domain
//todo support eTLD and use public suffix here https://publicsuffix.org
if (parts.length > 1) {
parts.length -= 1;
}
const domain = parts.reverse().join(".");
const lineId = `${domain}:${login.username}:${login.guid}`;
let originLine = this.addOrUpdateLine(
login,
lineId + "0",
this.#originPrototype
);
this.addOrUpdateLine(login, lineId + "1", this.#usernamePrototype);
let passwordLine = this.addOrUpdateLine(
login,
lineId + "2",
this.#passwordPrototype
);
originLine.breached = breachesMap.has(login.guid);
passwordLine.vulnerable = lazy.LoginBreaches.isVulnerablePassword(login);
});
this.afterReloadingDataSource();
}
#reloadEmptyDataSource() {
this.lines.length = 0;
//todo: user can enable passwords by activating Passwords header line
this.#header.value = this.#loginsDisabledMessage;
this.refreshAllLinesOnScreen();
}
observe(_subj, topic, message) {
if (
topic == "passwordmgr-storage-changed" ||
message == "signon.rememberSignons" ||
message == "signon.management.page.breach-alerts.enabled" ||
message == "signon.management.page.vulnerable-passwords.enabled"
) {
this.#reloadDataSource();
}
}
}
|