-
Notifications
You must be signed in to change notification settings - Fork 4
/
syncLogic.js
196 lines (169 loc) · 6.29 KB
/
syncLogic.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
// Copyright 2024 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {eq} from "drizzle-orm";
import i18next from "i18next";
import * as schema from "./db/schema";
import * as api from "./api";
import {generateToken} from "./totpUtil";
import useStore from "./useStorage";
function handleTokenExpiration() {
const {clearAll} = useStore.getState();
clearAll();
}
function getLocalAccounts(db) {
return db.select().from(schema.accounts).all();
}
function getAccountKey(account) {
return `${account.accountName}:${account.issuer ?? ""}`;
}
async function updateLocalDatabase(db, accounts) {
return db.transaction(async(tx) => {
// remove all accounts
// await tx.delete(schema.accounts).run();
for (const account of accounts) {
if (account.id) {
if (account.deletedAt === null || account.deletedAt === undefined) {
// compare all fields
const acc = await tx.select().from(schema.accounts).where(eq(schema.accounts.id, account.id)).get();
if (acc.issuer === account.issuer &&
acc.accountName === account.accountName &&
acc.secretKey === account.secretKey &&
acc.deletedAt === account.deletedAt
) {
continue;
}
await tx.update(schema.accounts).set({
issuer: account.issuer,
accountName: account.accountName,
secretKey: account.secretKey,
deletedAt: null,
token: generateToken(account.secretKey),
changedAt: new Date(),
}).where(eq(schema.accounts.id, account.id));
} else {
await tx.delete(schema.accounts).where(eq(schema.accounts.id, account.id));
}
} else {
await tx.insert(schema.accounts).values({
issuer: account.issuer || null,
accountName: account.accountName,
secretKey: account.secretKey,
token: generateToken(account.secretKey),
});
}
}
});
}
function mergeAccounts(localAccounts, serverAccounts, serverTimestamp) {
const isNewer = (a, b) => new Date(a) > new Date(b);
const mergedAccounts = new Map();
const localAccountKeys = new Map();
// Process local accounts
for (const local of localAccounts) {
const key = getAccountKey(local);
mergedAccounts.set(key, {
...local,
synced: false,
});
// Store both current and old account keys for local accounts
localAccountKeys.set(key, local);
if (local.oldAccountName) {
const oldKey = getAccountKey({...local, accountName: local.oldAccountName});
localAccountKeys.set(oldKey, local);
}
}
const processedLocalKeys = new Set();
// Merge with server accounts
for (const server of serverAccounts) {
const serverKey = getAccountKey(server);
const localAccount = localAccountKeys.get(serverKey);
if (!localAccount) {
// New account from server
mergedAccounts.set(serverKey, {...server, synced: true});
} else {
const localKey = getAccountKey(localAccount);
const local = mergedAccounts.get(localKey);
if (isNewer(serverTimestamp, local.changedAt)) {
// Server has newer changes
mergedAccounts.set(localKey, {
...server,
id: local.id,
oldAccountName: local.accountName !== server.accountName ? local.accountName : local.oldAccountName,
synced: true,
});
} else if (local.accountName !== server.accountName) {
mergedAccounts.set(localKey, {
...local,
oldAccountName: server.accountName,
synced: false,
});
}
// If local is newer or deleted, keep the local version (already in mergedAccounts)
processedLocalKeys.add(localKey);
}
}
// Handle server-side deletions
for (const [key, local] of mergedAccounts) {
if (!processedLocalKeys.has(key) && local.syncAt && isNewer(serverTimestamp, local.syncAt)) {
// This account was not found on the server and was previously synced
// Mark it as deleted
mergedAccounts.set(key, {...local, deletedAt: new Date(), synced: true});
}
}
return Array.from(mergedAccounts.values());
}
export async function syncWithCloud(db, userInfo, serverUrl, token) {
try {
const localAccounts = await getLocalAccounts(db);
const {updatedTime, mfaAccounts: serverAccounts} = await api.getMfaAccounts(
serverUrl,
userInfo.owner,
userInfo.name,
token
);
const mergedAccounts = mergeAccounts(localAccounts, serverAccounts, updatedTime);
await updateLocalDatabase(db, mergedAccounts);
const accountsToSync = mergedAccounts.filter(account => account.deletedAt === null || account.deletedAt === undefined)
.map(account => ({
issuer: account.issuer,
accountName: account.accountName,
secretKey: account.secretKey,
}));
const serverAccountsStringified = serverAccounts.map(account => JSON.stringify({
issuer: account.issuer,
accountName: account.accountName,
secretKey: account.secretKey,
}));
const accountsToSyncStringified = accountsToSync.map(account => JSON.stringify(account));
if (JSON.stringify(accountsToSyncStringified.sort()) !== JSON.stringify(serverAccountsStringified.sort())) {
const {status} = await api.updateMfaAccounts(
serverUrl,
userInfo.owner,
userInfo.name,
accountsToSync,
token
);
if (status !== "ok") {
throw new Error(i18next.t("syncLogic.Sync failed"));
}
}
await db.update(schema.accounts).set({syncAt: new Date()}).run();
} catch (error) {
if (error.message.includes("Access token has expired")) {
handleTokenExpiration();
throw new Error(i18next.t("syncLogic.Access token has expired, please login again"));
}
throw error;
}
}