-
Notifications
You must be signed in to change notification settings - Fork 3
/
keepassjs.js
400 lines (363 loc) · 12.6 KB
/
keepassjs.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
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
INPUT_NO_INPUT = -1;
INPUT_REMOTE_URL = 0;
INPUT_LOCAL_FILE = 1;
INPUT_DROPBOX_FILE = 2;
var last_timeout = 0;
var current_timeout = 0;
var dropbox_url = "url";
var default_url = "Enter URL to your KDBX file here...";
var inputs = new Array(3);
var current_input = INPUT_NO_INPUT;
var keyfile = null;
function clear_password() {
$("#password").val("");
$("#keyfile").css("background-color", "transparent");
keyfile = null;
}
function select_input(input_type) {
current_input = INPUT_NO_INPUT;
var blocks = {
url_option: "transparent",
file_option: "transparent",
dropbox_option: "transparent"
};
switch (input_type) {
case INPUT_REMOTE_URL:
var v = $("#url").val();
if (v.length > 0 && v != default_url) {
blocks["url_option"] = "green";
current_input = input_type;
create_dbx_chooser();
}
break;
case INPUT_LOCAL_FILE:
if (inputs[input_type] != null) {
blocks["file_option"] = "green";
current_input = input_type;
create_dbx_chooser();
}
break;
case INPUT_DROPBOX_FILE:
blocks["dropbox_option"] = "green";
current_input = input_type;
break;
}
for (var block in blocks) {
$("#" + block).css("background-color", blocks[block]);
}
}
function show_groups(data) {
var groups = data[0];
var entries = data[1];
var groups_html = document.createElement("ul");
groups_html.id = "groups";
$("#tabs").append(groups_html);
for (var i in groups) {
var group = groups[i];
//Create tab
var tab = document.createElement("li");
var tabLink = document.createElement("a");
tabLink.setAttribute("href", "#" + group);
tabLink.innerHTML = group;
tab.appendChild(tabLink);
groups_html.appendChild(tab);
//Create tab content
var div = document.createElement("div");
div.setAttribute("id", group);
$("#tabs").append(div);
$("#" + group).addClass("group_content");
append_entries(entries, group);
}
$("#tabs").tabs().addClass("ui-tabs-vertical ui-helper-clearfix");
$("#tabs li").removeClass("ui-corner-top").addClass("ui-corner-left");
entries = null;
$(document).tooltip();
}
function append_entries(entries, group) {
var counter = 0;
for (var i in entries) {
counter++;
var entry = entries[i];
if (entry["Group"] !== group) {
continue;
}
var captionText = entry["Title"] + " -- " + entry["URL"];
captionText = document.createTextNode(captionText);
var caption = document.createElement("div");
caption.appendChild(captionText);
$("#" + group).append(caption);
var table = document.createElement("table");
$(table).css("width", "100%");
$("#" + group).append(table);
var tbody = document.createElement("tbody");
table.appendChild(tbody);
var display_keys = ["Title", "URL", "UserName", "Password", "Notes"];
for (var key in display_keys) {
var row = document.createElement("tr");
tbody.appendChild(row);
var value = entry[display_keys[key]];
var keyCell = document.createElement("td");
keyCell.appendChild(document.createTextNode(display_keys[key]));
var valueCell = document.createElement("td");
if (display_keys[key] == "Password") {
var passDisplay = document.createElement("input");
passDisplay.setAttribute("type", "password");
passDisplay.readOnly = "true";
$(passDisplay).click(function () {
$(this).blur();
$("#clipper").select();
});
passDisplay.size = 50;
passDisplay.value = value;
passDisplay.title = "Hold mouse over password and press Ctrl+C to copy (or OS equivalent)";
var passID = "pass-" + group + "-" + counter;
passDisplay.id = passID;
valueCell.appendChild(passDisplay);
passDisplay.onmouseover = function (e) {
$("#clipper").val(this.value);
$("#clipper").select();
};
passDisplay.onmouseleave = function (e) {
$("#clipper").val("");
};
var toggleDisplay = document.createElement("button");
toggleDisplay.innerHTML = "ABC";
toggleDisplay.setAttribute("pass_id", passID);
toggleDisplay.onclick = function (e) {
toggle_pass_display(this);
};
valueCell.appendChild(toggleDisplay);
} else if (display_keys[key] == "URL") {
var link = document.createElement("a");
link.setAttribute("href", value);
link.appendChild(document.createTextNode(value));
valueCell.appendChild(link);
} else {
valueCell.appendChild(document.createTextNode(value));
}
row.appendChild(keyCell);
row.appendChild(valueCell);
}
entry = null;
}
$("#" + group).accordion({
collapsible: true,
animate: false,
active: false,
heightStyle: "content"
});
}
function toggle_pass_display (button) {
var pass_id = button.getAttribute("pass_id");
if (button.innerHTML == "ABC") {
$("#" + pass_id).attr("type", "text");
button.innerHTML = "***";
} else {
$("#" + pass_id).attr("type", "password");
button.innerHTML = "ABC";
}
}
function load_keepass() {
var data = inputs[current_input];
data = new jDataView(data, 0, data.length, true)
var pass = $("#password").val();
var passes = new Array();
if (pass.length > 0) {
pass = readPassword(pass);
passes.push(pass);
}
if (keyfile != null) {
passes.push(keyfile);
}
try {
var entries = readKeePassFile(data, passes);
clear_password();
show_groups(entries);
var options = {
label: "Unload",
icons: {
primary: "ui-icon-locked"
}
};
} catch (e) {
Apprise("Cannot open KeePass Database: " + e);
$("#lock_file").click();
}
}
function load_url(url) {
/* jQuery does not support arraybuffer yet. so have to do XHR */
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
var arrayBuffer = oReq.response; // Note: not oReq.responseText
if (arrayBuffer) {
inputs[INPUT_REMOTE_URL] = arrayBuffer;
load_keepass();
}
};
oReq.onerror = function (e) {
Apprise("Cannot load URL " + url);
};
oReq.send(null);
}
function load_dropbox_url(url) {
/* jQuery does not support arraybuffer yet. so have to do XHR */
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
var arrayBuffer = oReq.response; // Note: not oReq.responseText
if (arrayBuffer) {
inputs[INPUT_DROPBOX_FILE] = arrayBuffer;
load_keepass();
}
};
oReq.onerror = function (e) {
Apprise("Cannot load URL " + url);
};
oReq.send(null);
}
function create_dbx_chooser() {
$("#dropbox_option").empty();
var dbx_btn = Dropbox.createChooseButton({
success: function (file) {
select_input(INPUT_DROPBOX_FILE);
dropbox_url = file[0].link;
},
linkType: "direct",
multiselect: false,
extensions: ['.kdbx'],
});
$("#dropbox_option").append(dbx_btn);
}
window.onload = function () {
create_dbx_chooser();
var dropzone = document.getElementById("file_option");
dropzone.ondragover = dropzone.ondragenter = function (event) {
event.stopPropagation();
event.preventDefault();
};
dropzone.ondrop = function (event) {
event.stopPropagation();
event.preventDefault();
var filesArray = event.dataTransfer.files;
if (filesArray.length > 0) {
var file = filesArray[0];
var reader = new FileReader();
reader.onload = function (e) {
inputs[INPUT_LOCAL_FILE] = e.target.result;
select_input(INPUT_LOCAL_FILE);
};
reader.onerror = function (e) {
Apprise("Cannot load local file " + file.name);
};
reader.readAsArrayBuffer(file);
}
select_input(INPUT_LOCAL_FILE);
};
dropzone = document.getElementById("keyfile");
dropzone.ondragover = dropzone.ondragenter = function (event) {
event.stopPropagation();
event.preventDefault();
};
dropzone.ondrop = function (event) {
event.stopPropagation();
event.preventDefault();
var filesArray = event.dataTransfer.files;
if (filesArray.length > 0) {
var file = filesArray[0];
var reader = new FileReader();
reader.onload = function (e) {
var dataview = new jDataView(e.target.result, 0,
e.target.result.length, true);
keyfile = readKeyFile(dataview);
$("#keyfile").css("background-color", "green");
};
reader.onerror = function (e) {
Apprise("Cannot load key file " + file.name);
};
reader.readAsArrayBuffer(file);
}
};
$("#github_source").button({
label: "GitHub",
icons: {
primary: "ui-icon-wrench"
}
}).click(function () {
var win = window.open("https://github.com/stevenmg/KeePassJS");
win.focus();
});
$("#load_go").button({
label: "Load",
icons: {
primary: "ui-icon-unlocked"
}
}).click(function () {
if (current_input == INPUT_REMOTE_URL) {
var url = $("#url").val();
load_url(url);
} else if (current_input == INPUT_LOCAL_FILE) {
load_keepass();
} else if (current_input == INPUT_DROPBOX_FILE) {
load_dropbox_url(dropbox_url);
} else if (current_input == INPUT_NO_INPUT) {
Apprise("No database selected");
return;
}
$("#load_file").css("display", "none");
$("#about").css("display", "none");
$("#lock_file").css("display", "block");
$("#tabs").css("display", "block");
});
$("#url").val(default_url);
$("#url").keyup(function (e) {
if (e.keyCode == 13) {
select_input(INPUT_REMOTE_URL);
}
}).click(function () {
if ($(this).val() == default_url) {
$(this).val("");
}
select_input(INPUT_REMOTE_URL);
}).blur(function () {
select_input(INPUT_REMOTE_URL);
if ($(this).val().length == 0) {
$(this).val(default_url);
}
});
$("#url_option").click(function () {
select_input(INPUT_REMOTE_URL);
});
$("#file_option").click(function () {
select_input(INPUT_LOCAL_FILE);
});
$("#password").keyup(function (e) {
if (e.keyCode == 13) {
$("#load_go").click();
}
});
$("#lock_file").button({
label: "Lock File",
icons: {
primary: "ui-icon-locked"
}
}).click(function () {
$(this).css("display", "none");
$("#about").css("display", "block");
$("#load_file").css("display", "block");
$("#tabs").empty();
$("#tabs").tabs("destroy");
$("#tabs").css("display", "none");
clear_password();
});
$(document).click(function () {
last_timeout = current_timeout;
clearTimeout(last_timeout);
current_timeout = setTimeout("$('#lock_file').click();", 180000);
});
$(window).on("beforeunload", function () {
$("#lock_file").click();
});
}