This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
/
datastore.js
250 lines (210 loc) · 5.67 KB
/
datastore.js
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
/* 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 UUID from "uuid";
import { broadcast } from "./message-ports";
export function convertInfo2Item(info) {
let title;
try {
title = (new URL(info.hostname)).host;
} catch (ex) {
title = info.hostname;
}
title = title.replace(/^http(s)?:\/\//, "").
replace(/^www\d*\./, "");
const id = info.guid;
const origins = [ info.hostname, info.formSubmitURL ].
filter((u) => !!u);
const timeLastUsed = info.timeLastUsed;
const timePasswordChanged = info.timePasswordChanged;
const timeCreated = info.timeCreated;
let item = {
id,
title,
origins,
timeLastUsed,
timePasswordChanged,
timeCreated,
realm: info.httpRealm,
tags: [],
entry: {
kind: "login",
username: info.username,
password: info.password,
usernameField: info.usernameField,
passwordField: info.passwordField,
},
};
return item;
}
export function convertItem2Info(item) {
// dropped on the floor (for now ...)
// * title
// * tags
// * entry.notes
// * history
// item.id ==> info.guid
// item.title ==> undefined
// item.tags ==> undefined
// item.origins[0] ==> info.hostname
// item.origins[1] || null ==> info.formSubmitURL
// item.entry.kind ==> undefined
// item.entry.username ==> info.username
// item.entry.password ==> info.password
// item.entry.usernameField || "" ==> info.usernameField
// item.entry.passwordField || "" ==> info.passwordField
// item.entry.notes ==> info.undefined
const guid = item.id;
const hostname = item.origins[0];
const formSubmitURL = item.origins[1] || null;
const httpRealm = item.realm || null;
const username = item.entry.username;
const password = item.entry.password;
const usernameField = item.entry.usernameField || "";
const passwordField = item.entry.passwordField || "";
const timeCreated = item.timeCreated;
const timeLastUsed = item.timeLastUsed;
const timePasswordChanged = item.timePasswordChanged;
let info = {
guid,
hostname,
formSubmitURL,
httpRealm,
username,
password,
usernameField,
passwordField,
timeCreated,
timeLastUsed,
timePasswordChanged,
};
return info;
}
class DataStore {
constructor() {
this._all = {};
}
async _allLogins() {
return Object.values(this._all);
}
async list() {
const logins = await this._allLogins();
const items = logins.map(convertInfo2Item);
return items;
}
async get(id) {
let one = this._all[id];
if (one) {
one = convertInfo2Item(one);
}
return one || null;
}
async add(item) {
if (!item) {
throw new TypeError("invalid item");
}
let info = convertItem2Info(item);
if (!info.hostname) {
throw new TypeError("missing hostname");
}
if (!info.password) {
throw new TypeError("missing password");
}
if (!info.username) {
info.username = "";
}
if (!info.formSubmitURL && !info.httpRealm) {
// assume formSubmitURL === hostname
info.formSubmitURL = info.hostname;
}
let added = {
...info,
guid: UUID(),
timesUsed: 0,
};
if (!added.timeLastUsed) {
added.timeLastUsed = Date.now();
}
if (!added.timeCreated) {
added.timeCreated = Date.now();
}
if (!added.timePasswordChanged) {
added.timePasswordChanged = Date.now();
}
await browser.experiments.logins.add(added);
added = convertInfo2Item(added);
return added;
}
async update(item) {
const id = item.id;
if (!id) {
throw new TypeError("id missing");
}
const info = convertItem2Info(item);
const orig = await this.get(id);
if (!orig) {
throw new Error(`no existing item for ${id}`);
}
let updated = {
...info,
timePasswordChanged: Date.now(),
};
await browser.experiments.logins.update(updated);
updated = convertInfo2Item(updated);
return updated;
}
async remove(id) {
const item = await this.get(id);
if (item) {
await browser.experiments.logins.remove(item.id);
}
return item || null;
}
// TODO: Until issue #21 is resolved, this stuff handles raw info from the
// API rather than UI items.
loadInfo(logins) {
this._all = {};
for (let login of logins) {
this._all[login.guid] = login;
}
}
addInfo(info) {
this._all[info.guid] = info;
const item = convertInfo2Item(info);
broadcast({ type: "added_item", item });
}
updateInfo(info) {
this._all[info.guid] = info;
const item = convertInfo2Item(info);
broadcast({ type: "updated_item", item });
}
removeInfo({ guid }) {
delete this._all[guid];
broadcast({ type: "removed_item", id: guid });
}
removeAll() {
this._all = {};
broadcast({ type: "removed_all" });
}
}
let bootstrap;
export async function openDataStore() {
if (!bootstrap) {
bootstrap = new DataStore();
}
return bootstrap;
}
export async function closeDataStore() {
bootstrap = null;
}
export async function initializeDataStore() {
const { logins } = browser.experiments;
const entries = await logins.getAll();
const dataStore = await openDataStore();
dataStore.loadInfo(entries);
logins.onAdded.addListener(({ login }) => dataStore.addInfo(login));
logins.onUpdated.addListener(({ login }) => dataStore.updateInfo(login));
logins.onRemoved.addListener(({ login }) => dataStore.removeInfo(login));
logins.onAllRemoved.addListener(() => dataStore.removeAll());
}
export default openDataStore;