@jskit-ai/auth-provider-local-core 0.2.10 → 0.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/locales/en.json +10 -1
- package/package.json +7 -6
- package/src/server/lib/accounts.js +94 -34
- package/src/server/lib/config.js +83 -7
- package/src/server/lib/emailConfirmation.js +188 -0
- package/src/server/lib/fileBackend.js +104 -222
- package/src/server/lib/fileRecords.js +323 -0
- package/src/server/lib/firstAccountSetup.js +186 -0
- package/src/server/lib/index.js +2 -0
- package/src/server/lib/oauthFlows.js +16 -1
- package/src/server/lib/passwordFlows.js +7 -3
- package/src/server/lib/registerHookDecorator.js +15 -2
- package/src/server/lib/service.js +42 -4
- package/src/server/lib/signInThrottle.js +123 -0
- package/src/server/providers/AuthLocalProvider.js +18 -3
- package/src/server/types.js +47 -2
- package/test/codedTexts.test.js +1 -1
- package/test/emailConfirmation.test.js +77 -0
- package/test/firstAccountSetup.test.js +202 -0
- package/test/oauthSignIn.test.js +34 -1
- package/test/packageMetadata.test.js +2 -1
- package/test/providerRuntime.test.js +140 -4
- package/test/signInThrottle.test.js +88 -0
- package/test-support/emailConfirmationScenario.js +86 -0
- package/test-support/localService.js +11 -3
- package/test-support/oauthHarness.js +4 -2
- package/test-support/signInThrottleScenario.js +71 -0
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
import fs from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
|
+
import {
|
|
4
|
+
parseIdentities,
|
|
5
|
+
parseOAuthAttempts,
|
|
6
|
+
parseRecovery,
|
|
7
|
+
parseRegistrations,
|
|
8
|
+
parseSessions,
|
|
9
|
+
parseSignInFailures,
|
|
10
|
+
parseUsers,
|
|
11
|
+
serializeIdentities,
|
|
12
|
+
serializeOAuthAttempts,
|
|
13
|
+
serializeRecovery,
|
|
14
|
+
serializeRegistrations,
|
|
15
|
+
serializeSessions,
|
|
16
|
+
serializeSignInFailures,
|
|
17
|
+
serializeUsers
|
|
18
|
+
} from "./fileRecords.js";
|
|
3
19
|
|
|
4
20
|
const USER_FILE = "users.passwd";
|
|
5
21
|
const SESSION_FILE = "sessions.passwd";
|
|
6
22
|
const RECOVERY_FILE = "recovery.passwd";
|
|
7
23
|
const IDENTITY_FILE = "identities.passwd";
|
|
8
24
|
const OAUTH_ATTEMPT_FILE = "oauth-attempts.passwd";
|
|
25
|
+
const SIGN_IN_FAILURE_FILE = "sign-in-failures.passwd";
|
|
26
|
+
const REGISTRATION_FILE = "registrations.passwd";
|
|
9
27
|
const LOCK_FILE = "store.lock";
|
|
10
28
|
const JOURNAL_FILE = "transaction.journal";
|
|
11
29
|
const LOCK_STALE_MS = 30_000;
|
|
@@ -14,226 +32,6 @@ function nowIso() {
|
|
|
14
32
|
return new Date().toISOString();
|
|
15
33
|
}
|
|
16
34
|
|
|
17
|
-
function encodeField(value) {
|
|
18
|
-
return Buffer.from(String(value ?? ""), "utf8").toString("base64url");
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function decodeField(value) {
|
|
22
|
-
return Buffer.from(String(value || ""), "base64url").toString("utf8");
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function splitLine(line, expectedType) {
|
|
26
|
-
const parts = String(line || "").split(":");
|
|
27
|
-
if (parts[0] !== expectedType || parts[1] !== "v1") {
|
|
28
|
-
throw new Error(`Invalid ${expectedType} record.`);
|
|
29
|
-
}
|
|
30
|
-
return parts;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function parseUsers(content) {
|
|
34
|
-
const users = [];
|
|
35
|
-
for (const line of String(content || "").split("\n").filter(Boolean)) {
|
|
36
|
-
const parts = splitLine(line, "user");
|
|
37
|
-
if (parts.length !== 12) {
|
|
38
|
-
throw new Error("Invalid user record field count.");
|
|
39
|
-
}
|
|
40
|
-
users.push({
|
|
41
|
-
id: parts[2],
|
|
42
|
-
email: decodeField(parts[3]),
|
|
43
|
-
displayName: decodeField(parts[4]),
|
|
44
|
-
password: {
|
|
45
|
-
algorithm: parts[5],
|
|
46
|
-
version: parts[6],
|
|
47
|
-
salt: parts[7],
|
|
48
|
-
hash: parts[8]
|
|
49
|
-
},
|
|
50
|
-
createdAt: decodeField(parts[9]),
|
|
51
|
-
updatedAt: decodeField(parts[10]),
|
|
52
|
-
disabled: parts[11] === "1"
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
return users;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function serializeUsers(users) {
|
|
59
|
-
return [...users]
|
|
60
|
-
.sort((left, right) => String(left.id).localeCompare(String(right.id)))
|
|
61
|
-
.map((user) =>
|
|
62
|
-
[
|
|
63
|
-
"user",
|
|
64
|
-
"v1",
|
|
65
|
-
user.id,
|
|
66
|
-
encodeField(user.email),
|
|
67
|
-
encodeField(user.displayName),
|
|
68
|
-
user.password.algorithm,
|
|
69
|
-
user.password.version,
|
|
70
|
-
user.password.salt,
|
|
71
|
-
user.password.hash,
|
|
72
|
-
encodeField(user.createdAt),
|
|
73
|
-
encodeField(user.updatedAt),
|
|
74
|
-
user.disabled ? "1" : "0"
|
|
75
|
-
].join(":")
|
|
76
|
-
)
|
|
77
|
-
.join("\n") + (users.length > 0 ? "\n" : "");
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function parseSessions(content) {
|
|
81
|
-
const sessions = [];
|
|
82
|
-
for (const line of String(content || "").split("\n").filter(Boolean)) {
|
|
83
|
-
const parts = splitLine(line, "session");
|
|
84
|
-
if (parts.length !== 9) {
|
|
85
|
-
throw new Error("Invalid session record field count.");
|
|
86
|
-
}
|
|
87
|
-
sessions.push({
|
|
88
|
-
id: parts[2],
|
|
89
|
-
userId: parts[3],
|
|
90
|
-
tokenHash: parts[4],
|
|
91
|
-
purpose: parts[5] || "normal",
|
|
92
|
-
createdAt: decodeField(parts[6]),
|
|
93
|
-
expiresAt: decodeField(parts[7]),
|
|
94
|
-
revokedAt: decodeField(parts[8] || "")
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
return sessions;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function serializeSessions(sessions) {
|
|
101
|
-
return [...sessions]
|
|
102
|
-
.sort((left, right) => String(left.id).localeCompare(String(right.id)))
|
|
103
|
-
.map((session) =>
|
|
104
|
-
[
|
|
105
|
-
"session",
|
|
106
|
-
"v1",
|
|
107
|
-
session.id,
|
|
108
|
-
session.userId,
|
|
109
|
-
session.tokenHash,
|
|
110
|
-
session.purpose || "normal",
|
|
111
|
-
encodeField(session.createdAt),
|
|
112
|
-
encodeField(session.expiresAt),
|
|
113
|
-
encodeField(session.revokedAt || "")
|
|
114
|
-
].join(":")
|
|
115
|
-
)
|
|
116
|
-
.join("\n") + (sessions.length > 0 ? "\n" : "");
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function parseRecovery(content) {
|
|
120
|
-
const records = [];
|
|
121
|
-
for (const line of String(content || "").split("\n").filter(Boolean)) {
|
|
122
|
-
const parts = splitLine(line, "recovery");
|
|
123
|
-
if (parts.length !== 8) {
|
|
124
|
-
throw new Error("Invalid recovery record field count.");
|
|
125
|
-
}
|
|
126
|
-
records.push({
|
|
127
|
-
id: parts[2],
|
|
128
|
-
userId: parts[3],
|
|
129
|
-
tokenHash: parts[4],
|
|
130
|
-
createdAt: decodeField(parts[5]),
|
|
131
|
-
expiresAt: decodeField(parts[6]),
|
|
132
|
-
usedAt: decodeField(parts[7] || "")
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
return records;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function serializeRecovery(records) {
|
|
139
|
-
return [...records]
|
|
140
|
-
.sort((left, right) => String(left.id).localeCompare(String(right.id)))
|
|
141
|
-
.map((record) =>
|
|
142
|
-
[
|
|
143
|
-
"recovery",
|
|
144
|
-
"v1",
|
|
145
|
-
record.id,
|
|
146
|
-
record.userId,
|
|
147
|
-
record.tokenHash,
|
|
148
|
-
encodeField(record.createdAt),
|
|
149
|
-
encodeField(record.expiresAt),
|
|
150
|
-
encodeField(record.usedAt || "")
|
|
151
|
-
].join(":")
|
|
152
|
-
)
|
|
153
|
-
.join("\n") + (records.length > 0 ? "\n" : "");
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function parseIdentities(content) {
|
|
157
|
-
const identities = [];
|
|
158
|
-
for (const line of String(content || "").split("\n").filter(Boolean)) {
|
|
159
|
-
const parts = splitLine(line, "identity");
|
|
160
|
-
if (parts.length !== 7) {
|
|
161
|
-
throw new Error("Invalid identity record field count.");
|
|
162
|
-
}
|
|
163
|
-
identities.push({
|
|
164
|
-
userId: parts[2],
|
|
165
|
-
provider: parts[3],
|
|
166
|
-
providerSubject: decodeField(parts[4]),
|
|
167
|
-
email: decodeField(parts[5]),
|
|
168
|
-
createdAt: decodeField(parts[6])
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
return identities;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function serializeIdentities(identities) {
|
|
175
|
-
return [...identities]
|
|
176
|
-
.sort((left, right) =>
|
|
177
|
-
`${left.provider}:${left.providerSubject}`.localeCompare(`${right.provider}:${right.providerSubject}`))
|
|
178
|
-
.map((identity) =>
|
|
179
|
-
[
|
|
180
|
-
"identity",
|
|
181
|
-
"v1",
|
|
182
|
-
identity.userId,
|
|
183
|
-
identity.provider,
|
|
184
|
-
encodeField(identity.providerSubject),
|
|
185
|
-
encodeField(identity.email),
|
|
186
|
-
encodeField(identity.createdAt)
|
|
187
|
-
].join(":")
|
|
188
|
-
)
|
|
189
|
-
.join("\n") + (identities.length > 0 ? "\n" : "");
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function parseOAuthAttempts(content) {
|
|
193
|
-
const attempts = [];
|
|
194
|
-
for (const line of String(content || "").split("\n").filter(Boolean)) {
|
|
195
|
-
const parts = splitLine(line, "oauth_attempt");
|
|
196
|
-
if (parts.length !== 12) {
|
|
197
|
-
throw new Error("Invalid OAuth attempt record field count.");
|
|
198
|
-
}
|
|
199
|
-
attempts.push({
|
|
200
|
-
browserKeyHash: parts[2],
|
|
201
|
-
provider: parts[3],
|
|
202
|
-
intent: parts[4],
|
|
203
|
-
userId: parts[5],
|
|
204
|
-
state: parts[6],
|
|
205
|
-
nonce: parts[7],
|
|
206
|
-
codeVerifier: parts[8],
|
|
207
|
-
returnTo: decodeField(parts[9]),
|
|
208
|
-
createdAt: decodeField(parts[10]),
|
|
209
|
-
expiresAt: decodeField(parts[11])
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
return attempts;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function serializeOAuthAttempts(attempts) {
|
|
216
|
-
return [...attempts]
|
|
217
|
-
.sort((left, right) => String(left.browserKeyHash).localeCompare(String(right.browserKeyHash)))
|
|
218
|
-
.map((attempt) =>
|
|
219
|
-
[
|
|
220
|
-
"oauth_attempt",
|
|
221
|
-
"v1",
|
|
222
|
-
attempt.browserKeyHash,
|
|
223
|
-
attempt.provider,
|
|
224
|
-
attempt.intent,
|
|
225
|
-
attempt.userId,
|
|
226
|
-
attempt.state,
|
|
227
|
-
attempt.nonce,
|
|
228
|
-
attempt.codeVerifier,
|
|
229
|
-
encodeField(attempt.returnTo),
|
|
230
|
-
encodeField(attempt.createdAt),
|
|
231
|
-
encodeField(attempt.expiresAt)
|
|
232
|
-
].join(":")
|
|
233
|
-
)
|
|
234
|
-
.join("\n") + (attempts.length > 0 ? "\n" : "");
|
|
235
|
-
}
|
|
236
|
-
|
|
237
35
|
async function readText(filePath) {
|
|
238
36
|
try {
|
|
239
37
|
return await fs.readFile(filePath, "utf8");
|
|
@@ -282,7 +80,8 @@ function parseJournal(content) {
|
|
|
282
80
|
}
|
|
283
81
|
return parsed.files.map((entry) => {
|
|
284
82
|
const name = String(entry?.name || "").trim();
|
|
285
|
-
|
|
83
|
+
const files = [USER_FILE, SESSION_FILE, RECOVERY_FILE, IDENTITY_FILE, OAUTH_ATTEMPT_FILE, SIGN_IN_FAILURE_FILE, REGISTRATION_FILE];
|
|
84
|
+
if (!files.includes(name)) {
|
|
286
85
|
throw new Error("Invalid local auth transaction journal file.");
|
|
287
86
|
}
|
|
288
87
|
return {
|
|
@@ -418,6 +217,9 @@ function createTx(state, touched) {
|
|
|
418
217
|
async findByEmail(email) {
|
|
419
218
|
return clone(state.users.find((user) => user.email === email) || null);
|
|
420
219
|
},
|
|
220
|
+
async hasAny() {
|
|
221
|
+
return state.users.length > 0;
|
|
222
|
+
},
|
|
421
223
|
async updatePassword(userId, passwordRecord) {
|
|
422
224
|
const user = state.users.find((entry) => entry.id === userId);
|
|
423
225
|
if (!user) {
|
|
@@ -580,6 +382,72 @@ function createTx(state, touched) {
|
|
|
580
382
|
}
|
|
581
383
|
return count;
|
|
582
384
|
}
|
|
385
|
+
}),
|
|
386
|
+
signInFailures: Object.freeze({
|
|
387
|
+
async find(email) {
|
|
388
|
+
return clone(state.signInFailures.find((record) => record.email === email) || null);
|
|
389
|
+
},
|
|
390
|
+
async save(record) {
|
|
391
|
+
state.signInFailures = [
|
|
392
|
+
...state.signInFailures.filter((entry) => entry.email !== record.email),
|
|
393
|
+
clone(record)
|
|
394
|
+
];
|
|
395
|
+
mark("signInFailures");
|
|
396
|
+
return clone(record);
|
|
397
|
+
},
|
|
398
|
+
async clear(email) {
|
|
399
|
+
const before = state.signInFailures.length;
|
|
400
|
+
state.signInFailures = state.signInFailures.filter((entry) => entry.email !== email);
|
|
401
|
+
const count = before - state.signInFailures.length;
|
|
402
|
+
if (count > 0) {
|
|
403
|
+
mark("signInFailures");
|
|
404
|
+
}
|
|
405
|
+
return count;
|
|
406
|
+
},
|
|
407
|
+
async deleteBefore(lastFailedAt) {
|
|
408
|
+
const before = state.signInFailures.length;
|
|
409
|
+
const cutoff = Date.parse(lastFailedAt);
|
|
410
|
+
state.signInFailures = state.signInFailures.filter((entry) => Date.parse(entry.lastFailedAt) >= cutoff);
|
|
411
|
+
const count = before - state.signInFailures.length;
|
|
412
|
+
if (count > 0) {
|
|
413
|
+
mark("signInFailures");
|
|
414
|
+
}
|
|
415
|
+
return count;
|
|
416
|
+
}
|
|
417
|
+
}),
|
|
418
|
+
registrations: Object.freeze({
|
|
419
|
+
async save(record) {
|
|
420
|
+
state.registrations = [
|
|
421
|
+
...state.registrations.filter((entry) => entry.email !== record.email),
|
|
422
|
+
clone(record)
|
|
423
|
+
];
|
|
424
|
+
mark("registrations");
|
|
425
|
+
return clone(record);
|
|
426
|
+
},
|
|
427
|
+
async findByEmail(email) {
|
|
428
|
+
return clone(state.registrations.find((record) => record.email === email) || null);
|
|
429
|
+
},
|
|
430
|
+
async findByTokenHash(tokenHash) {
|
|
431
|
+
return clone(state.registrations.find((record) => record.tokenHash === tokenHash) || null);
|
|
432
|
+
},
|
|
433
|
+
async delete(email) {
|
|
434
|
+
const before = state.registrations.length;
|
|
435
|
+
state.registrations = state.registrations.filter((entry) => entry.email !== email);
|
|
436
|
+
const count = before - state.registrations.length;
|
|
437
|
+
if (count > 0) {
|
|
438
|
+
mark("registrations");
|
|
439
|
+
}
|
|
440
|
+
return count;
|
|
441
|
+
},
|
|
442
|
+
async deleteExpired(now) {
|
|
443
|
+
const before = state.registrations.length;
|
|
444
|
+
state.registrations = state.registrations.filter((entry) => Date.parse(entry.expiresAt) > Date.parse(now));
|
|
445
|
+
const count = before - state.registrations.length;
|
|
446
|
+
if (count > 0) {
|
|
447
|
+
mark("registrations");
|
|
448
|
+
}
|
|
449
|
+
return count;
|
|
450
|
+
}
|
|
583
451
|
})
|
|
584
452
|
});
|
|
585
453
|
}
|
|
@@ -597,7 +465,9 @@ function createLocalFileBackend({ storeDir }) {
|
|
|
597
465
|
sessions: parseSessions(await readText(path.join(resolvedStoreDir, SESSION_FILE))),
|
|
598
466
|
recovery: parseRecovery(await readText(path.join(resolvedStoreDir, RECOVERY_FILE))),
|
|
599
467
|
identities: parseIdentities(await readText(path.join(resolvedStoreDir, IDENTITY_FILE))),
|
|
600
|
-
oauthAttempts: parseOAuthAttempts(await readText(path.join(resolvedStoreDir, OAUTH_ATTEMPT_FILE)))
|
|
468
|
+
oauthAttempts: parseOAuthAttempts(await readText(path.join(resolvedStoreDir, OAUTH_ATTEMPT_FILE))),
|
|
469
|
+
signInFailures: parseSignInFailures(await readText(path.join(resolvedStoreDir, SIGN_IN_FAILURE_FILE))),
|
|
470
|
+
registrations: parseRegistrations(await readText(path.join(resolvedStoreDir, REGISTRATION_FILE)))
|
|
601
471
|
};
|
|
602
472
|
const touched = new Set();
|
|
603
473
|
const result = await callback(createTx(state, touched));
|
|
@@ -632,6 +502,18 @@ function createLocalFileBackend({ storeDir }) {
|
|
|
632
502
|
content: serializeOAuthAttempts(state.oauthAttempts)
|
|
633
503
|
});
|
|
634
504
|
}
|
|
505
|
+
if (touched.has("signInFailures")) {
|
|
506
|
+
commitEntries.push({
|
|
507
|
+
name: SIGN_IN_FAILURE_FILE,
|
|
508
|
+
content: serializeSignInFailures(state.signInFailures)
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
if (touched.has("registrations")) {
|
|
512
|
+
commitEntries.push({
|
|
513
|
+
name: REGISTRATION_FILE,
|
|
514
|
+
content: serializeRegistrations(state.registrations)
|
|
515
|
+
});
|
|
516
|
+
}
|
|
635
517
|
if (commitEntries.length > 0) {
|
|
636
518
|
await writeCommitJournal(resolvedStoreDir, commitEntries);
|
|
637
519
|
for (const entry of commitEntries) {
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
// The local file backend's records: one line per record, `type:v1:` then its
|
|
2
|
+
// fields, with free text base64url-encoded so it cannot contain the separator.
|
|
3
|
+
|
|
4
|
+
function encodeField(value) {
|
|
5
|
+
return Buffer.from(String(value ?? ""), "utf8").toString("base64url");
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function decodeField(value) {
|
|
9
|
+
return Buffer.from(String(value || ""), "base64url").toString("utf8");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function splitLine(line, expectedType) {
|
|
13
|
+
const parts = String(line || "").split(":");
|
|
14
|
+
if (parts[0] !== expectedType || parts[1] !== "v1") {
|
|
15
|
+
throw new Error(`Invalid ${expectedType} record.`);
|
|
16
|
+
}
|
|
17
|
+
return parts;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function parseUsers(content) {
|
|
21
|
+
const users = [];
|
|
22
|
+
for (const line of String(content || "").split("\n").filter(Boolean)) {
|
|
23
|
+
const parts = splitLine(line, "user");
|
|
24
|
+
if (parts.length !== 12) {
|
|
25
|
+
throw new Error("Invalid user record field count.");
|
|
26
|
+
}
|
|
27
|
+
users.push({
|
|
28
|
+
id: parts[2],
|
|
29
|
+
email: decodeField(parts[3]),
|
|
30
|
+
displayName: decodeField(parts[4]),
|
|
31
|
+
password: {
|
|
32
|
+
algorithm: parts[5],
|
|
33
|
+
version: parts[6],
|
|
34
|
+
salt: parts[7],
|
|
35
|
+
hash: parts[8]
|
|
36
|
+
},
|
|
37
|
+
createdAt: decodeField(parts[9]),
|
|
38
|
+
updatedAt: decodeField(parts[10]),
|
|
39
|
+
disabled: parts[11] === "1"
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return users;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function serializeUsers(users) {
|
|
46
|
+
return [...users]
|
|
47
|
+
.sort((left, right) => String(left.id).localeCompare(String(right.id)))
|
|
48
|
+
.map((user) =>
|
|
49
|
+
[
|
|
50
|
+
"user",
|
|
51
|
+
"v1",
|
|
52
|
+
user.id,
|
|
53
|
+
encodeField(user.email),
|
|
54
|
+
encodeField(user.displayName),
|
|
55
|
+
user.password.algorithm,
|
|
56
|
+
user.password.version,
|
|
57
|
+
user.password.salt,
|
|
58
|
+
user.password.hash,
|
|
59
|
+
encodeField(user.createdAt),
|
|
60
|
+
encodeField(user.updatedAt),
|
|
61
|
+
user.disabled ? "1" : "0"
|
|
62
|
+
].join(":")
|
|
63
|
+
)
|
|
64
|
+
.join("\n") + (users.length > 0 ? "\n" : "");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function parseSessions(content) {
|
|
68
|
+
const sessions = [];
|
|
69
|
+
for (const line of String(content || "").split("\n").filter(Boolean)) {
|
|
70
|
+
const parts = splitLine(line, "session");
|
|
71
|
+
if (parts.length !== 9) {
|
|
72
|
+
throw new Error("Invalid session record field count.");
|
|
73
|
+
}
|
|
74
|
+
sessions.push({
|
|
75
|
+
id: parts[2],
|
|
76
|
+
userId: parts[3],
|
|
77
|
+
tokenHash: parts[4],
|
|
78
|
+
purpose: parts[5] || "normal",
|
|
79
|
+
createdAt: decodeField(parts[6]),
|
|
80
|
+
expiresAt: decodeField(parts[7]),
|
|
81
|
+
revokedAt: decodeField(parts[8] || "")
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return sessions;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function serializeSessions(sessions) {
|
|
88
|
+
return [...sessions]
|
|
89
|
+
.sort((left, right) => String(left.id).localeCompare(String(right.id)))
|
|
90
|
+
.map((session) =>
|
|
91
|
+
[
|
|
92
|
+
"session",
|
|
93
|
+
"v1",
|
|
94
|
+
session.id,
|
|
95
|
+
session.userId,
|
|
96
|
+
session.tokenHash,
|
|
97
|
+
session.purpose || "normal",
|
|
98
|
+
encodeField(session.createdAt),
|
|
99
|
+
encodeField(session.expiresAt),
|
|
100
|
+
encodeField(session.revokedAt || "")
|
|
101
|
+
].join(":")
|
|
102
|
+
)
|
|
103
|
+
.join("\n") + (sessions.length > 0 ? "\n" : "");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function parseRecovery(content) {
|
|
107
|
+
const records = [];
|
|
108
|
+
for (const line of String(content || "").split("\n").filter(Boolean)) {
|
|
109
|
+
const parts = splitLine(line, "recovery");
|
|
110
|
+
if (parts.length !== 8) {
|
|
111
|
+
throw new Error("Invalid recovery record field count.");
|
|
112
|
+
}
|
|
113
|
+
records.push({
|
|
114
|
+
id: parts[2],
|
|
115
|
+
userId: parts[3],
|
|
116
|
+
tokenHash: parts[4],
|
|
117
|
+
createdAt: decodeField(parts[5]),
|
|
118
|
+
expiresAt: decodeField(parts[6]),
|
|
119
|
+
usedAt: decodeField(parts[7] || "")
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return records;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function serializeRecovery(records) {
|
|
126
|
+
return [...records]
|
|
127
|
+
.sort((left, right) => String(left.id).localeCompare(String(right.id)))
|
|
128
|
+
.map((record) =>
|
|
129
|
+
[
|
|
130
|
+
"recovery",
|
|
131
|
+
"v1",
|
|
132
|
+
record.id,
|
|
133
|
+
record.userId,
|
|
134
|
+
record.tokenHash,
|
|
135
|
+
encodeField(record.createdAt),
|
|
136
|
+
encodeField(record.expiresAt),
|
|
137
|
+
encodeField(record.usedAt || "")
|
|
138
|
+
].join(":")
|
|
139
|
+
)
|
|
140
|
+
.join("\n") + (records.length > 0 ? "\n" : "");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function parseIdentities(content) {
|
|
144
|
+
const identities = [];
|
|
145
|
+
for (const line of String(content || "").split("\n").filter(Boolean)) {
|
|
146
|
+
const parts = splitLine(line, "identity");
|
|
147
|
+
if (parts.length !== 7) {
|
|
148
|
+
throw new Error("Invalid identity record field count.");
|
|
149
|
+
}
|
|
150
|
+
identities.push({
|
|
151
|
+
userId: parts[2],
|
|
152
|
+
provider: parts[3],
|
|
153
|
+
providerSubject: decodeField(parts[4]),
|
|
154
|
+
email: decodeField(parts[5]),
|
|
155
|
+
createdAt: decodeField(parts[6])
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
return identities;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function serializeIdentities(identities) {
|
|
162
|
+
return [...identities]
|
|
163
|
+
.sort((left, right) =>
|
|
164
|
+
`${left.provider}:${left.providerSubject}`.localeCompare(`${right.provider}:${right.providerSubject}`))
|
|
165
|
+
.map((identity) =>
|
|
166
|
+
[
|
|
167
|
+
"identity",
|
|
168
|
+
"v1",
|
|
169
|
+
identity.userId,
|
|
170
|
+
identity.provider,
|
|
171
|
+
encodeField(identity.providerSubject),
|
|
172
|
+
encodeField(identity.email),
|
|
173
|
+
encodeField(identity.createdAt)
|
|
174
|
+
].join(":")
|
|
175
|
+
)
|
|
176
|
+
.join("\n") + (identities.length > 0 ? "\n" : "");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function parseOAuthAttempts(content) {
|
|
180
|
+
const attempts = [];
|
|
181
|
+
for (const line of String(content || "").split("\n").filter(Boolean)) {
|
|
182
|
+
const parts = splitLine(line, "oauth_attempt");
|
|
183
|
+
if (parts.length !== 12) {
|
|
184
|
+
throw new Error("Invalid OAuth attempt record field count.");
|
|
185
|
+
}
|
|
186
|
+
attempts.push({
|
|
187
|
+
browserKeyHash: parts[2],
|
|
188
|
+
provider: parts[3],
|
|
189
|
+
intent: parts[4],
|
|
190
|
+
userId: parts[5],
|
|
191
|
+
state: parts[6],
|
|
192
|
+
nonce: parts[7],
|
|
193
|
+
codeVerifier: parts[8],
|
|
194
|
+
returnTo: decodeField(parts[9]),
|
|
195
|
+
createdAt: decodeField(parts[10]),
|
|
196
|
+
expiresAt: decodeField(parts[11])
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
return attempts;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function serializeOAuthAttempts(attempts) {
|
|
203
|
+
return [...attempts]
|
|
204
|
+
.sort((left, right) => String(left.browserKeyHash).localeCompare(String(right.browserKeyHash)))
|
|
205
|
+
.map((attempt) =>
|
|
206
|
+
[
|
|
207
|
+
"oauth_attempt",
|
|
208
|
+
"v1",
|
|
209
|
+
attempt.browserKeyHash,
|
|
210
|
+
attempt.provider,
|
|
211
|
+
attempt.intent,
|
|
212
|
+
attempt.userId,
|
|
213
|
+
attempt.state,
|
|
214
|
+
attempt.nonce,
|
|
215
|
+
attempt.codeVerifier,
|
|
216
|
+
encodeField(attempt.returnTo),
|
|
217
|
+
encodeField(attempt.createdAt),
|
|
218
|
+
encodeField(attempt.expiresAt)
|
|
219
|
+
].join(":")
|
|
220
|
+
)
|
|
221
|
+
.join("\n") + (attempts.length > 0 ? "\n" : "");
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function parseSignInFailures(content) {
|
|
225
|
+
const records = [];
|
|
226
|
+
for (const line of String(content || "").split("\n").filter(Boolean)) {
|
|
227
|
+
const parts = splitLine(line, "sign_in_failure");
|
|
228
|
+
if (parts.length !== 6) {
|
|
229
|
+
throw new Error("Invalid sign-in failure record field count.");
|
|
230
|
+
}
|
|
231
|
+
records.push({
|
|
232
|
+
email: decodeField(parts[2]),
|
|
233
|
+
failures: Number(parts[3]),
|
|
234
|
+
lastFailedAt: decodeField(parts[4]),
|
|
235
|
+
lockedUntil: decodeField(parts[5] || "")
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
return records;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function serializeSignInFailures(records) {
|
|
242
|
+
return [...records]
|
|
243
|
+
.sort((left, right) => String(left.email).localeCompare(String(right.email)))
|
|
244
|
+
.map((record) =>
|
|
245
|
+
[
|
|
246
|
+
"sign_in_failure",
|
|
247
|
+
"v1",
|
|
248
|
+
encodeField(record.email),
|
|
249
|
+
String(record.failures),
|
|
250
|
+
encodeField(record.lastFailedAt),
|
|
251
|
+
encodeField(record.lockedUntil || "")
|
|
252
|
+
].join(":")
|
|
253
|
+
)
|
|
254
|
+
.join("\n") + (records.length > 0 ? "\n" : "");
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// A registration waiting for its email to be confirmed; the invitation fields
|
|
258
|
+
// are empty when it came without one.
|
|
259
|
+
function parseRegistrations(content) {
|
|
260
|
+
const records = [];
|
|
261
|
+
for (const line of String(content || "").split("\n").filter(Boolean)) {
|
|
262
|
+
const parts = splitLine(line, "registration");
|
|
263
|
+
if (parts.length !== 13) {
|
|
264
|
+
throw new Error("Invalid registration record field count.");
|
|
265
|
+
}
|
|
266
|
+
const invitationToken = decodeField(parts[8]);
|
|
267
|
+
records.push({
|
|
268
|
+
email: decodeField(parts[2]),
|
|
269
|
+
displayName: decodeField(parts[3]),
|
|
270
|
+
password: {
|
|
271
|
+
algorithm: parts[4],
|
|
272
|
+
version: parts[5],
|
|
273
|
+
salt: parts[6],
|
|
274
|
+
hash: parts[7]
|
|
275
|
+
},
|
|
276
|
+
invitation: invitationToken ? { token: invitationToken, source: decodeField(parts[9]) } : null,
|
|
277
|
+
tokenHash: parts[10],
|
|
278
|
+
createdAt: decodeField(parts[11]),
|
|
279
|
+
expiresAt: decodeField(parts[12])
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
return records;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function serializeRegistrations(records) {
|
|
286
|
+
return [...records]
|
|
287
|
+
.sort((left, right) => String(left.email).localeCompare(String(right.email)))
|
|
288
|
+
.map((record) =>
|
|
289
|
+
[
|
|
290
|
+
"registration",
|
|
291
|
+
"v1",
|
|
292
|
+
encodeField(record.email),
|
|
293
|
+
encodeField(record.displayName),
|
|
294
|
+
record.password.algorithm,
|
|
295
|
+
record.password.version,
|
|
296
|
+
record.password.salt,
|
|
297
|
+
record.password.hash,
|
|
298
|
+
encodeField(record.invitation?.token || ""),
|
|
299
|
+
encodeField(record.invitation?.source || ""),
|
|
300
|
+
record.tokenHash,
|
|
301
|
+
encodeField(record.createdAt),
|
|
302
|
+
encodeField(record.expiresAt)
|
|
303
|
+
].join(":")
|
|
304
|
+
)
|
|
305
|
+
.join("\n") + (records.length > 0 ? "\n" : "");
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export {
|
|
309
|
+
parseIdentities,
|
|
310
|
+
parseOAuthAttempts,
|
|
311
|
+
parseRecovery,
|
|
312
|
+
parseRegistrations,
|
|
313
|
+
parseSessions,
|
|
314
|
+
parseSignInFailures,
|
|
315
|
+
parseUsers,
|
|
316
|
+
serializeIdentities,
|
|
317
|
+
serializeOAuthAttempts,
|
|
318
|
+
serializeRecovery,
|
|
319
|
+
serializeRegistrations,
|
|
320
|
+
serializeSessions,
|
|
321
|
+
serializeSignInFailures,
|
|
322
|
+
serializeUsers
|
|
323
|
+
};
|