-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.js
More file actions
137 lines (120 loc) · 4.13 KB
/
Copy pathbackup.js
File metadata and controls
137 lines (120 loc) · 4.13 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
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* backup.js – Handles data backup export and import logic with duplicate detection.
*/
import { loadAccounts, addAccount } from './storage.js';
import { decryptSecret } from './crypto.js';
import { isValidBase32 } from './totp.js';
import { getTranslation } from './i18n.js';
export async function exportBackup(toastFn) {
try {
const accounts = await loadAccounts();
const decryptedAccounts = [];
for (const acc of accounts) {
try {
const plainSecret = await decryptSecret(acc.secret);
decryptedAccounts.push({
service: acc.service,
login: acc.login,
secret: plainSecret,
period: acc.period || 30,
digits: acc.digits || 6,
algorithm: acc.algorithm || "SHA-1",
type: acc.type || "totp",
counter: acc.counter || 0,
category: acc.category || "none"
});
} catch (err) {
console.error("Failed to decrypt account during backup export:", acc.service, err);
}
}
const backupData = {
source: "OnePass Auth Backup",
version: 1,
exportedAt: new Date().toISOString(),
accounts: decryptedAccounts
};
const jsonString = JSON.stringify(backupData, null, 2);
const blob = new Blob([jsonString], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `onepass_auth_backup_${new Date().toISOString().slice(0, 10)}.json`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
toastFn(getTranslation("toast_backup_downloaded"), "success");
} catch (err) {
console.error("Backup export error:", err);
toastFn(getTranslation("toast_backup_export_error"), "error");
}
}
export function importBackup(file, toastFn, renderAccountsFn) {
if (!file) return;
try {
const reader = new FileReader();
reader.onload = async (event) => {
try {
const data = JSON.parse(event.target.result);
if (data.source !== "OnePass Auth Backup" || !Array.isArray(data.accounts)) {
toastFn(getTranslation("toast_backup_invalid"), "error");
return;
}
const currentAccounts = await loadAccounts();
const currentPlainList = [];
for (const acc of currentAccounts) {
try {
const plain = await decryptSecret(acc.secret);
currentPlainList.push({ service: acc.service, login: acc.login, secret: plain });
} catch (err) {}
}
let importedCount = 0;
let skippedCount = 0;
for (const acc of data.accounts) {
if (!acc.service || !acc.login || !acc.secret) {
skippedCount++;
continue;
}
if (!isValidBase32(acc.secret)) {
skippedCount++;
continue;
}
const isDuplicate = currentPlainList.some(curr =>
curr.service.toLowerCase() === acc.service.toLowerCase() &&
curr.login.toLowerCase() === acc.login.toLowerCase() &&
curr.secret === acc.secret
);
if (isDuplicate) {
skippedCount++;
continue;
}
await addAccount(
acc.service,
acc.login,
acc.secret,
acc.period || 30,
acc.digits || 6,
acc.algorithm || "SHA-1",
acc.type || "totp",
acc.counter || 0,
acc.category || "none"
);
importedCount++;
}
if (importedCount > 0) {
toastFn(getTranslation("toast_imported_backup_count", importedCount), "success");
await renderAccountsFn();
} else {
toastFn(getTranslation("toast_backup_all_added"), "success");
}
} catch (err) {
console.error("Failed to parse JSON backup:", err);
toastFn(getTranslation("toast_file_read_error"), "error");
}
};
reader.readAsText(file);
} catch (err) {
console.error("Backup import file error:", err);
toastFn(getTranslation("toast_file_import_error"), "error");
}
}