-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.js
More file actions
124 lines (109 loc) · 3.66 KB
/
demo.js
File metadata and controls
124 lines (109 loc) · 3.66 KB
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
// @ts-check
// Preview with: http://htmlpreview.github.io/?https://github.com/garvankeeley/kinto-lockbox/blob/master/index.html
function createOriginHash(itemUuid, origin) {
let hashedOrigin = '';
generateHash(origin, hashSalt)
.then(hashed => {
hashedOrigin = hashed;
return kinto.collection(DB.originHashes.name).list({ filters: { [DB.originHashes.C.origin]: hashedOrigin } });
})
.then(foundRecords => {
if (foundRecords.data.length > 1) {
throw new Error(`generateHash wrong record count: ${foundRecords.data.length}`);
}
if (foundRecords.data.length < 1) {
return {
id: generateUUID(),
[DB.originHashes.C.origin]: hashedOrigin,
[DB.originHashes.C.itemIds]: [itemUuid]
};
}
const record = foundRecords.data[0];
record[DB.originHashes.C.itemIds].push(itemUuid);
return record;
})
.then(record => kinto.collection(DB.originHashes.name).upsert(record));
}
function listAll() {
kinto.collection(DB.items.name).list().then(result => {
let promises = result.data.map(row => {
return decryptItem(row).then(item => {
return `id: "${row.id}", data: ${item.site} ${item.username} ${item.password}`;
});
})
Promise.all(promises).then(results => {
document.getElementById('items-table').innerHTML = results.join('\n');
});
})
kinto.collection(DB.perItemKeys.name).list().then(result => {
let rows = result.data.map(row => {
return `itemId: "${row.itemId}", key: ${row.key}`;
})
document.getElementById('keys-table').innerHTML = rows.join('\n');
})
kinto.collection(DB.originHashes.name).list().then(results => {
results.data.forEach(r => console.log(r));
})
}
function main() {
const username = 'foo';
const password = 'doo';
const options = {
remote: 'https://kinto.dev.mozaws.net/v1/',
//requestMode: 'no-cors',
headers: {
Authorization: "Basic " + btoa(`${username}:${password}`)
}
};
// @ts-ignore
kinto = new Kinto(options);
document.getElementById('button-delete-all').onclick = () => {
const promises = [];
for (const table in DB) {
const collection = kinto.collection(table);
promises.push(collection.clear());
promises.push(collection.api.bucket(collection.bucket).deleteCollection(collection.name));
}
Promise.all(promises)
.then(res => listAll())
.catch(err => LL(err));
}
document.getElementById('button-clear-local-data').onclick = () => {
const promises = [];
for (const table in DB) {
const collection = kinto.collection(table);
promises.push(collection.clear());
}
Promise.all(promises)
.then(res => listAll());
}
document.getElementById('button-list-all').onclick = () => {
listAll();
}
document.getElementById('button-sync').onclick = () => {
const promises = [];
for (const table in DB) {
promises.push(kinto.collection(table).sync());
}
Promise.all(promises)
.then(res => listAll())
.catch(err => LL(err));
}
const form = document.getElementById('form-add');
form.addEventListener('submit', function (event) {
event.preventDefault();
const data = event.target['title'].value;
if (!data) {
addItem(sampleLoginsData[0]).then(res => listAll());
return;
}
const parts = data.split(':')
addItem({ site: parts[0], username: parts[1], password: parts[2] }).then(res => listAll());
});
addItem(sampleLoginsData[0]).then(res => listAll());
deriveKeyPBKDF(masterPassword, masterEncryptionSalt).then(key => {
masterEncryptionKey = key;
LL(key);
});
}
window.addEventListener('DOMContentLoaded', main);