-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
187 lines (169 loc) · 4.93 KB
/
Copy pathstorage.js
File metadata and controls
187 lines (169 loc) · 4.93 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
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
/**
* storage.js – CRUD for accounts using chrome.storage.local / sync.
*/
import { encryptSecret, decryptSecret } from './crypto.js';
const ACCOUNTS_KEY = "accounts";
/**
* Load all accounts from sync storage. If empty, checks local storage for migration.
*/
export async function loadAccounts() {
return new Promise((resolve) => {
chrome.storage.local.get(ACCOUNTS_KEY, async (localResult) => {
const localAccounts = localResult[ACCOUNTS_KEY];
if (localAccounts) {
resolve(localAccounts);
} else {
// Fallback: check sync storage for migration if local storage is empty
chrome.storage.sync.get(ACCOUNTS_KEY, async (syncResult) => {
const syncAccounts = syncResult[ACCOUNTS_KEY] || [];
if (syncAccounts.length > 0) {
// Save to local storage
await saveAccounts(syncAccounts);
// Clear sync storage to avoid duplication/confusion
chrome.storage.sync.remove(ACCOUNTS_KEY);
console.log("Accounts migrated from sync storage to local storage.");
}
resolve(syncAccounts);
});
}
});
});
}
/**
* Save full accounts array to local storage.
*/
export async function saveAccounts(accounts) {
return new Promise((resolve) =>
chrome.storage.local.set({ [ACCOUNTS_KEY]: accounts }, resolve)
);
}
/**
* Add a new account. The secret is encrypted before saving.
*/
export async function addAccount(service, login, secretPlain, period = 30, digits = 6, algorithm = "SHA-1", type = "totp", counter = 0, category = "none") {
const encrypted = await encryptSecret(secretPlain);
const accounts = await loadAccounts();
accounts.push({
id: Date.now().toString(36) + Math.random().toString(36).slice(2, 6),
service,
login,
secret: encrypted,
period,
digits,
algorithm,
type,
counter,
category
});
await saveAccounts(accounts);
}
/**
* Update an existing account details.
*/
export async function updateAccount(id, service, login, secretPlain, period = 30, digits = 6, algorithm = "SHA-1", type = "totp", counter = 0, category = "none") {
const accounts = await loadAccounts();
const index = accounts.findIndex(a => a.id === id);
if (index !== -1) {
let encrypted = accounts[index].secret;
if (secretPlain) {
encrypted = await encryptSecret(secretPlain);
}
accounts[index] = {
id,
service,
login,
secret: encrypted,
period,
digits,
algorithm,
type,
counter,
category
};
await saveAccounts(accounts);
}
}
/**
* Increment HOTP counter for an account.
*/
export async function incrementCounter(id) {
const accounts = await loadAccounts();
const index = accounts.findIndex(a => a.id === id);
if (index !== -1) {
accounts[index].counter = (accounts[index].counter || 0) + 1;
await saveAccounts(accounts);
return accounts[index].counter;
}
return null;
}
/**
* Remove an account by its id.
*/
export async function removeAccount(id) {
let accounts = await loadAccounts();
accounts = accounts.filter((a) => a.id !== id);
await saveAccounts(accounts);
}
const CATEGORIES_KEY = "custom_categories";
/**
* Load all custom categories.
*/
export async function loadCustomCategories() {
return new Promise((resolve) => {
chrome.storage.local.get(CATEGORIES_KEY, (localResult) => {
const localCats = localResult[CATEGORIES_KEY];
if (localCats) {
resolve(localCats);
} else {
// Migrate from sync
chrome.storage.sync.get(CATEGORIES_KEY, (syncResult) => {
const syncCats = syncResult[CATEGORIES_KEY] || [];
if (syncCats.length > 0) {
chrome.storage.local.set({ [CATEGORIES_KEY]: syncCats });
chrome.storage.sync.remove(CATEGORIES_KEY);
}
resolve(syncCats);
});
}
});
});
}
/**
* Save custom categories.
*/
export async function saveCustomCategories(categories) {
return new Promise((resolve) =>
chrome.storage.local.set({ [CATEGORIES_KEY]: categories }, resolve)
);
}
/**
* Add a new custom category name.
*/
export async function addCustomCategory(name) {
const cleanName = name.trim();
if (!cleanName) return false;
const categories = await loadCustomCategories();
if (categories.includes(cleanName)) return false;
categories.push(cleanName);
await saveCustomCategories(categories);
return true;
}
/**
* Remove a custom category by its name, resetting any linked account categories.
*/
export async function removeCustomCategory(name) {
let categories = await loadCustomCategories();
categories = categories.filter(c => c !== name);
await saveCustomCategories(categories);
const accounts = await loadAccounts();
let updated = false;
accounts.forEach(acc => {
if (acc.category === name) {
acc.category = "none";
updated = true;
}
});
if (updated) {
await saveAccounts(accounts);
}
}