@prunellejs/auth 0.1.1 → 0.6.0
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/llms.txt +46 -22
- package/package.json +5 -4
- package/src/crypto.ts +57 -0
- package/src/http.ts +58 -1
- package/src/index.ts +206 -192
- package/src/links.ts +150 -0
- package/src/oauth-routes.ts +103 -41
- package/src/org-routes.ts +216 -0
- package/src/orgs.ts +386 -0
- package/src/passkey-client.ts +207 -0
- package/src/passkey-routes.ts +210 -0
- package/src/passkeys.ts +425 -0
- package/src/routes.ts +93 -68
- package/src/session.ts +113 -63
- package/src/sqlite-ext.ts +516 -0
- package/src/sqlite.ts +53 -39
- package/src/store-ext.ts +425 -0
- package/src/store.ts +73 -30
- package/src/totp-routes.ts +138 -0
- package/src/totp.ts +338 -0
- package/src/webauthn-crypto.ts +284 -0
package/src/store-ext.ts
ADDED
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
// Extended AuthStore types (TOTP 2FA, Passkeys/WebAuthn, Organizations) and in-memory implementation.
|
|
2
|
+
// All extension methods are optional on `AuthStore` so existing custom stores remain 100% compatible.
|
|
3
|
+
|
|
4
|
+
export interface TotpRecord {
|
|
5
|
+
userId: string
|
|
6
|
+
/** AES-256-GCM encrypted base32 secret (`v1.<iv>.<ciphertext>`). */
|
|
7
|
+
encryptedSecret: string
|
|
8
|
+
enabled: boolean
|
|
9
|
+
/** Last accepted 30-second TOTP time step (prevents replay within the same window). */
|
|
10
|
+
lastUsedStep?: number | undefined
|
|
11
|
+
createdAt: number
|
|
12
|
+
updatedAt: number
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface RecoveryCodeRecord {
|
|
16
|
+
id: string
|
|
17
|
+
userId: string
|
|
18
|
+
codeHash: string
|
|
19
|
+
salt: string
|
|
20
|
+
createdAt: number
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface PasskeyRecord {
|
|
24
|
+
/** Base64url-encoded WebAuthn credential ID. */
|
|
25
|
+
id: string
|
|
26
|
+
userId: string
|
|
27
|
+
/** JSON-serialized JWK public key derived from the COSE key. */
|
|
28
|
+
publicKeyJwk: string
|
|
29
|
+
/** COSE algorithm identifier: -7 (ES256), -257 (RS256), -8 (EdDSA). */
|
|
30
|
+
algorithm: number
|
|
31
|
+
signCount: number
|
|
32
|
+
transports?: string[] | undefined
|
|
33
|
+
name?: string | undefined
|
|
34
|
+
aaguid?: string | undefined
|
|
35
|
+
createdAt: number
|
|
36
|
+
lastUsedAt?: number | undefined
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface PasskeySummary {
|
|
40
|
+
id: string
|
|
41
|
+
name?: string | undefined
|
|
42
|
+
createdAt: number
|
|
43
|
+
lastUsedAt?: number | undefined
|
|
44
|
+
transports?: string[] | undefined
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface Organization {
|
|
48
|
+
id: string
|
|
49
|
+
slug: string
|
|
50
|
+
name: string
|
|
51
|
+
createdAt: number
|
|
52
|
+
updatedAt: number
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface OrgMembership {
|
|
56
|
+
orgId: string
|
|
57
|
+
userId: string
|
|
58
|
+
role: string
|
|
59
|
+
createdAt: number
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface OrgInvitation {
|
|
63
|
+
id: string
|
|
64
|
+
orgId: string
|
|
65
|
+
email: string
|
|
66
|
+
role: string
|
|
67
|
+
invitedBy: string
|
|
68
|
+
tokenHash: string
|
|
69
|
+
expiresAt: number
|
|
70
|
+
createdAt: number
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface UserOrgSummary {
|
|
74
|
+
org: Organization
|
|
75
|
+
role: string
|
|
76
|
+
joinedAt: number
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface UserExportExt {
|
|
80
|
+
totp?: { enabled: boolean, createdAt: number, recoveryCodesRemaining: number } | undefined
|
|
81
|
+
passkeys?: PasskeySummary[] | undefined
|
|
82
|
+
organizations?: Array<{ orgId: string, slug: string, name: string, role: string, joinedAt: number }> | undefined
|
|
83
|
+
invitations?: Array<{ id: string, orgId: string, email: string, role: string, expiresAt: number, createdAt: number }> | undefined
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface AuthStoreExt {
|
|
87
|
+
getTotp?(userId: string): TotpRecord | undefined | Promise<TotpRecord | undefined>
|
|
88
|
+
setTotp?(record: TotpRecord): void | Promise<void>
|
|
89
|
+
deleteTotp?(userId: string): void | Promise<void>
|
|
90
|
+
listRecoveryCodes?(userId: string): RecoveryCodeRecord[] | Promise<RecoveryCodeRecord[]>
|
|
91
|
+
setRecoveryCodes?(userId: string, codes: RecoveryCodeRecord[]): void | Promise<void>
|
|
92
|
+
consumeRecoveryCode?(id: string): boolean | Promise<boolean>
|
|
93
|
+
deleteRecoveryCodes?(userId: string): void | Promise<void>
|
|
94
|
+
|
|
95
|
+
getPasskey?(credentialId: string): PasskeyRecord | undefined | Promise<PasskeyRecord | undefined>
|
|
96
|
+
listPasskeys?(userId: string): PasskeyRecord[] | Promise<PasskeyRecord[]>
|
|
97
|
+
createPasskey?(record: PasskeyRecord): void | Promise<void>
|
|
98
|
+
updatePasskeyUsage?(credentialId: string, signCount: number, lastUsedAt: number): void | Promise<void>
|
|
99
|
+
deletePasskey?(userId: string, credentialId: string): boolean | Promise<boolean>
|
|
100
|
+
deleteUserPasskeys?(userId: string): void | Promise<void>
|
|
101
|
+
|
|
102
|
+
createOrg?(org: Organization): Organization | Promise<Organization>
|
|
103
|
+
getOrgById?(id: string): Organization | undefined | Promise<Organization | undefined>
|
|
104
|
+
getOrgBySlug?(slug: string): Organization | undefined | Promise<Organization | undefined>
|
|
105
|
+
deleteOrg?(orgId: string): void | Promise<void>
|
|
106
|
+
getOrgMember?(orgId: string, userId: string): OrgMembership | undefined | Promise<OrgMembership | undefined>
|
|
107
|
+
listOrgMembers?(orgId: string): OrgMembership[] | Promise<OrgMembership[]>
|
|
108
|
+
listUserOrgMemberships?(userId: string): UserOrgSummary[] | Promise<UserOrgSummary[]>
|
|
109
|
+
addOrgMember?(membership: OrgMembership): OrgMembership | Promise<OrgMembership>
|
|
110
|
+
updateOrgMemberRole?(orgId: string, userId: string, role: string): OrgMembership | undefined | Promise<OrgMembership | undefined>
|
|
111
|
+
removeOrgMember?(orgId: string, userId: string): boolean | Promise<boolean>
|
|
112
|
+
createOrgInvite?(invite: OrgInvitation): OrgInvitation | Promise<OrgInvitation>
|
|
113
|
+
getOrgInviteByTokenHash?(tokenHash: string): OrgInvitation | undefined | Promise<OrgInvitation | undefined>
|
|
114
|
+
listOrgInvites?(orgId: string): OrgInvitation[] | Promise<OrgInvitation[]>
|
|
115
|
+
deleteOrgInvite?(orgId: string, inviteId: string): boolean | Promise<boolean>
|
|
116
|
+
consumeOrgInvite?(tokenHash: string): OrgInvitation | undefined | Promise<OrgInvitation | undefined>
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function toPasskeySummary(record: PasskeyRecord): PasskeySummary {
|
|
120
|
+
return {
|
|
121
|
+
id: record.id,
|
|
122
|
+
createdAt: record.createdAt,
|
|
123
|
+
...(record.name !== undefined ? { name: record.name } : {}),
|
|
124
|
+
...(record.lastUsedAt !== undefined ? { lastUsedAt: record.lastUsedAt } : {}),
|
|
125
|
+
...(record.transports !== undefined ? { transports: [...record.transports] } : {}),
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export interface MemoryStoreExtHooks {
|
|
130
|
+
methods: Required<AuthStoreExt>
|
|
131
|
+
exportUserExt: (userId: string, email: string, now: number) => UserExportExt
|
|
132
|
+
deleteUserExt: (userId: string, email: string | undefined) => void
|
|
133
|
+
pruneExpiredExt: (now: number) => void
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
interface MemoryExtState {
|
|
137
|
+
totp: Map<string, TotpRecord>
|
|
138
|
+
recoveryCodes: Map<string, RecoveryCodeRecord>
|
|
139
|
+
passkeys: Map<string, PasskeyRecord>
|
|
140
|
+
orgs: Map<string, Organization>
|
|
141
|
+
orgsBySlug: Map<string, string>
|
|
142
|
+
orgMembers: Map<string, OrgMembership>
|
|
143
|
+
orgInvites: Map<string, OrgInvitation>
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const memberKey = (orgId: string, userId: string): string => `${orgId}:${userId}`
|
|
147
|
+
|
|
148
|
+
function buildMemoryTotpPasskeyMethods(state: MemoryExtState) {
|
|
149
|
+
const clonePasskey = (rec: PasskeyRecord): PasskeyRecord => ({
|
|
150
|
+
...rec,
|
|
151
|
+
...(rec.transports ? { transports: [...rec.transports] } : {}),
|
|
152
|
+
})
|
|
153
|
+
return {
|
|
154
|
+
getTotp(userId: string): TotpRecord | undefined {
|
|
155
|
+
const rec = state.totp.get(userId)
|
|
156
|
+
return rec ? { ...rec } : undefined
|
|
157
|
+
},
|
|
158
|
+
setTotp(record: TotpRecord): void {
|
|
159
|
+
state.totp.set(record.userId, { ...record })
|
|
160
|
+
},
|
|
161
|
+
deleteTotp(userId: string): void {
|
|
162
|
+
state.totp.delete(userId)
|
|
163
|
+
},
|
|
164
|
+
listRecoveryCodes(userId: string): RecoveryCodeRecord[] {
|
|
165
|
+
const out: RecoveryCodeRecord[] = []
|
|
166
|
+
for (const rec of state.recoveryCodes.values()) {
|
|
167
|
+
if (rec.userId === userId)
|
|
168
|
+
out.push({ ...rec })
|
|
169
|
+
}
|
|
170
|
+
return out
|
|
171
|
+
},
|
|
172
|
+
setRecoveryCodes(userId: string, codes: RecoveryCodeRecord[]): void {
|
|
173
|
+
for (const [id, rec] of state.recoveryCodes) {
|
|
174
|
+
if (rec.userId === userId)
|
|
175
|
+
state.recoveryCodes.delete(id)
|
|
176
|
+
}
|
|
177
|
+
for (const rec of codes)
|
|
178
|
+
state.recoveryCodes.set(rec.id, { ...rec })
|
|
179
|
+
},
|
|
180
|
+
consumeRecoveryCode(id: string): boolean {
|
|
181
|
+
return state.recoveryCodes.delete(id)
|
|
182
|
+
},
|
|
183
|
+
deleteRecoveryCodes(userId: string): void {
|
|
184
|
+
for (const [id, rec] of state.recoveryCodes) {
|
|
185
|
+
if (rec.userId === userId)
|
|
186
|
+
state.recoveryCodes.delete(id)
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
getPasskey(credentialId: string): PasskeyRecord | undefined {
|
|
190
|
+
const rec = state.passkeys.get(credentialId)
|
|
191
|
+
return rec ? clonePasskey(rec) : undefined
|
|
192
|
+
},
|
|
193
|
+
listPasskeys(userId: string): PasskeyRecord[] {
|
|
194
|
+
const out: PasskeyRecord[] = []
|
|
195
|
+
for (const rec of state.passkeys.values()) {
|
|
196
|
+
if (rec.userId === userId)
|
|
197
|
+
out.push(clonePasskey(rec))
|
|
198
|
+
}
|
|
199
|
+
out.sort((a, b) => a.createdAt - b.createdAt)
|
|
200
|
+
return out
|
|
201
|
+
},
|
|
202
|
+
createPasskey(record: PasskeyRecord): void {
|
|
203
|
+
state.passkeys.set(record.id, clonePasskey(record))
|
|
204
|
+
},
|
|
205
|
+
updatePasskeyUsage(credentialId: string, signCount: number, lastUsedAt: number): void {
|
|
206
|
+
const rec = state.passkeys.get(credentialId)
|
|
207
|
+
if (rec) {
|
|
208
|
+
rec.signCount = signCount
|
|
209
|
+
rec.lastUsedAt = lastUsedAt
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
deletePasskey(userId: string, credentialId: string): boolean {
|
|
213
|
+
const rec = state.passkeys.get(credentialId)
|
|
214
|
+
if (!rec || rec.userId !== userId)
|
|
215
|
+
return false
|
|
216
|
+
return state.passkeys.delete(credentialId)
|
|
217
|
+
},
|
|
218
|
+
deleteUserPasskeys(userId: string): void {
|
|
219
|
+
for (const [id, rec] of state.passkeys) {
|
|
220
|
+
if (rec.userId === userId)
|
|
221
|
+
state.passkeys.delete(id)
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function buildMemoryOrgMethods(state: MemoryExtState) {
|
|
228
|
+
return {
|
|
229
|
+
createOrg(org: Organization): Organization {
|
|
230
|
+
const copy: Organization = { ...org, slug: org.slug.toLowerCase() }
|
|
231
|
+
if (state.orgsBySlug.has(copy.slug))
|
|
232
|
+
throw new Error(`prunelle: organization slug "${copy.slug}" already exists`)
|
|
233
|
+
state.orgs.set(copy.id, copy)
|
|
234
|
+
state.orgsBySlug.set(copy.slug, copy.id)
|
|
235
|
+
return { ...copy }
|
|
236
|
+
},
|
|
237
|
+
getOrgById(id: string): Organization | undefined {
|
|
238
|
+
const rec = state.orgs.get(id)
|
|
239
|
+
return rec ? { ...rec } : undefined
|
|
240
|
+
},
|
|
241
|
+
getOrgBySlug(slug: string): Organization | undefined {
|
|
242
|
+
const id = state.orgsBySlug.get(slug.toLowerCase())
|
|
243
|
+
if (id === undefined)
|
|
244
|
+
return undefined
|
|
245
|
+
const rec = state.orgs.get(id)
|
|
246
|
+
return rec ? { ...rec } : undefined
|
|
247
|
+
},
|
|
248
|
+
deleteOrg(orgId: string): void {
|
|
249
|
+
const rec = state.orgs.get(orgId)
|
|
250
|
+
if (rec)
|
|
251
|
+
state.orgsBySlug.delete(rec.slug)
|
|
252
|
+
state.orgs.delete(orgId)
|
|
253
|
+
for (const [k, m] of state.orgMembers) {
|
|
254
|
+
if (m.orgId === orgId)
|
|
255
|
+
state.orgMembers.delete(k)
|
|
256
|
+
}
|
|
257
|
+
for (const [k, inv] of state.orgInvites) {
|
|
258
|
+
if (inv.orgId === orgId)
|
|
259
|
+
state.orgInvites.delete(k)
|
|
260
|
+
}
|
|
261
|
+
},
|
|
262
|
+
getOrgMember(orgId: string, userId: string): OrgMembership | undefined {
|
|
263
|
+
const m = state.orgMembers.get(memberKey(orgId, userId))
|
|
264
|
+
return m ? { ...m } : undefined
|
|
265
|
+
},
|
|
266
|
+
listOrgMembers(orgId: string): OrgMembership[] {
|
|
267
|
+
const out: OrgMembership[] = []
|
|
268
|
+
for (const m of state.orgMembers.values()) {
|
|
269
|
+
if (m.orgId === orgId)
|
|
270
|
+
out.push({ ...m })
|
|
271
|
+
}
|
|
272
|
+
out.sort((a, b) => a.createdAt - b.createdAt)
|
|
273
|
+
return out
|
|
274
|
+
},
|
|
275
|
+
listUserOrgMemberships(userId: string): UserOrgSummary[] {
|
|
276
|
+
const out: UserOrgSummary[] = []
|
|
277
|
+
for (const m of state.orgMembers.values()) {
|
|
278
|
+
if (m.userId !== userId)
|
|
279
|
+
continue
|
|
280
|
+
const org = state.orgs.get(m.orgId)
|
|
281
|
+
if (org)
|
|
282
|
+
out.push({ org: { ...org }, role: m.role, joinedAt: m.createdAt })
|
|
283
|
+
}
|
|
284
|
+
out.sort((a, b) => a.joinedAt - b.joinedAt)
|
|
285
|
+
return out
|
|
286
|
+
},
|
|
287
|
+
addOrgMember(membership: OrgMembership): OrgMembership {
|
|
288
|
+
const copy: OrgMembership = { ...membership }
|
|
289
|
+
state.orgMembers.set(memberKey(copy.orgId, copy.userId), copy)
|
|
290
|
+
return { ...copy }
|
|
291
|
+
},
|
|
292
|
+
updateOrgMemberRole(orgId: string, userId: string, role: string): OrgMembership | undefined {
|
|
293
|
+
const cur = state.orgMembers.get(memberKey(orgId, userId))
|
|
294
|
+
if (!cur)
|
|
295
|
+
return undefined
|
|
296
|
+
cur.role = role
|
|
297
|
+
return { ...cur }
|
|
298
|
+
},
|
|
299
|
+
removeOrgMember(orgId: string, userId: string): boolean {
|
|
300
|
+
return state.orgMembers.delete(memberKey(orgId, userId))
|
|
301
|
+
},
|
|
302
|
+
createOrgInvite(invite: OrgInvitation): OrgInvitation {
|
|
303
|
+
const copy: OrgInvitation = { ...invite, email: invite.email.trim().toLowerCase() }
|
|
304
|
+
state.orgInvites.set(copy.tokenHash, copy)
|
|
305
|
+
return { ...copy }
|
|
306
|
+
},
|
|
307
|
+
getOrgInviteByTokenHash(tokenHash: string): OrgInvitation | undefined {
|
|
308
|
+
const inv = state.orgInvites.get(tokenHash)
|
|
309
|
+
if (!inv)
|
|
310
|
+
return undefined
|
|
311
|
+
if (inv.expiresAt <= Date.now()) {
|
|
312
|
+
state.orgInvites.delete(tokenHash)
|
|
313
|
+
return undefined
|
|
314
|
+
}
|
|
315
|
+
return { ...inv }
|
|
316
|
+
},
|
|
317
|
+
listOrgInvites(orgId: string): OrgInvitation[] {
|
|
318
|
+
const now = Date.now()
|
|
319
|
+
const out: OrgInvitation[] = []
|
|
320
|
+
for (const [k, inv] of state.orgInvites) {
|
|
321
|
+
if (inv.expiresAt <= now) {
|
|
322
|
+
state.orgInvites.delete(k)
|
|
323
|
+
continue
|
|
324
|
+
}
|
|
325
|
+
if (inv.orgId === orgId)
|
|
326
|
+
out.push({ ...inv })
|
|
327
|
+
}
|
|
328
|
+
out.sort((a, b) => a.createdAt - b.createdAt)
|
|
329
|
+
return out
|
|
330
|
+
},
|
|
331
|
+
deleteOrgInvite(orgId: string, inviteId: string): boolean {
|
|
332
|
+
for (const [k, inv] of state.orgInvites) {
|
|
333
|
+
if (inv.orgId === orgId && inv.id === inviteId) {
|
|
334
|
+
state.orgInvites.delete(k)
|
|
335
|
+
return true
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return false
|
|
339
|
+
},
|
|
340
|
+
consumeOrgInvite(tokenHash: string): OrgInvitation | undefined {
|
|
341
|
+
const inv = state.orgInvites.get(tokenHash)
|
|
342
|
+
if (!inv)
|
|
343
|
+
return undefined
|
|
344
|
+
state.orgInvites.delete(tokenHash)
|
|
345
|
+
if (inv.expiresAt <= Date.now())
|
|
346
|
+
return undefined
|
|
347
|
+
return { ...inv }
|
|
348
|
+
},
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export function createMemoryStoreExt(): MemoryStoreExtHooks {
|
|
353
|
+
const state: MemoryExtState = {
|
|
354
|
+
totp: new Map(),
|
|
355
|
+
recoveryCodes: new Map(),
|
|
356
|
+
passkeys: new Map(),
|
|
357
|
+
orgs: new Map(),
|
|
358
|
+
orgsBySlug: new Map(),
|
|
359
|
+
orgMembers: new Map(),
|
|
360
|
+
orgInvites: new Map(),
|
|
361
|
+
}
|
|
362
|
+
const tp = buildMemoryTotpPasskeyMethods(state)
|
|
363
|
+
const org = buildMemoryOrgMethods(state)
|
|
364
|
+
|
|
365
|
+
return {
|
|
366
|
+
methods: { ...tp, ...org },
|
|
367
|
+
exportUserExt(userId, email, now) {
|
|
368
|
+
const totpRec = state.totp.get(userId)
|
|
369
|
+
const rcCount = tp.listRecoveryCodes(userId).length
|
|
370
|
+
const userPasskeys = tp.listPasskeys(userId).map(toPasskeySummary)
|
|
371
|
+
const organizations = org.listUserOrgMemberships(userId).map(item => ({
|
|
372
|
+
orgId: item.org.id,
|
|
373
|
+
slug: item.org.slug,
|
|
374
|
+
name: item.org.name,
|
|
375
|
+
role: item.role,
|
|
376
|
+
joinedAt: item.joinedAt,
|
|
377
|
+
}))
|
|
378
|
+
const invitations: NonNullable<UserExportExt['invitations']> = []
|
|
379
|
+
const normEmail = email.trim().toLowerCase()
|
|
380
|
+
for (const [k, inv] of state.orgInvites) {
|
|
381
|
+
if (inv.expiresAt <= now) {
|
|
382
|
+
state.orgInvites.delete(k)
|
|
383
|
+
continue
|
|
384
|
+
}
|
|
385
|
+
if (inv.email === normEmail || inv.invitedBy === userId) {
|
|
386
|
+
invitations.push({
|
|
387
|
+
id: inv.id,
|
|
388
|
+
orgId: inv.orgId,
|
|
389
|
+
email: inv.email,
|
|
390
|
+
role: inv.role,
|
|
391
|
+
expiresAt: inv.expiresAt,
|
|
392
|
+
createdAt: inv.createdAt,
|
|
393
|
+
})
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
invitations.sort((a, b) => a.createdAt - b.createdAt)
|
|
397
|
+
return {
|
|
398
|
+
...(totpRec ? { totp: { enabled: totpRec.enabled, createdAt: totpRec.createdAt, recoveryCodesRemaining: rcCount } } : {}),
|
|
399
|
+
passkeys: userPasskeys,
|
|
400
|
+
organizations,
|
|
401
|
+
invitations,
|
|
402
|
+
}
|
|
403
|
+
},
|
|
404
|
+
deleteUserExt(userId, email) {
|
|
405
|
+
state.totp.delete(userId)
|
|
406
|
+
tp.deleteRecoveryCodes(userId)
|
|
407
|
+
tp.deleteUserPasskeys(userId)
|
|
408
|
+
for (const [k, m] of state.orgMembers) {
|
|
409
|
+
if (m.userId === userId)
|
|
410
|
+
state.orgMembers.delete(k)
|
|
411
|
+
}
|
|
412
|
+
const normEmail = email?.trim().toLowerCase()
|
|
413
|
+
for (const [k, inv] of state.orgInvites) {
|
|
414
|
+
if (inv.invitedBy === userId || (normEmail !== undefined && inv.email === normEmail))
|
|
415
|
+
state.orgInvites.delete(k)
|
|
416
|
+
}
|
|
417
|
+
},
|
|
418
|
+
pruneExpiredExt(now) {
|
|
419
|
+
for (const [k, inv] of state.orgInvites) {
|
|
420
|
+
if (inv.expiresAt <= now)
|
|
421
|
+
state.orgInvites.delete(k)
|
|
422
|
+
}
|
|
423
|
+
},
|
|
424
|
+
}
|
|
425
|
+
}
|
package/src/store.ts
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
// Clean pluggable AuthStore contract + zero-setup in-memory store for dev & tests.
|
|
2
2
|
|
|
3
|
+
import type { AuthStoreExt, UserExportExt } from './store-ext'
|
|
4
|
+
import { createMemoryStoreExt } from './store-ext'
|
|
5
|
+
|
|
6
|
+
export type {
|
|
7
|
+
AuthStoreExt,
|
|
8
|
+
Organization,
|
|
9
|
+
OrgInvitation,
|
|
10
|
+
OrgMembership,
|
|
11
|
+
PasskeyRecord,
|
|
12
|
+
PasskeySummary,
|
|
13
|
+
RecoveryCodeRecord,
|
|
14
|
+
TotpRecord,
|
|
15
|
+
UserExportExt,
|
|
16
|
+
UserOrgSummary,
|
|
17
|
+
} from './store-ext'
|
|
18
|
+
export { toPasskeySummary } from './store-ext'
|
|
19
|
+
|
|
3
20
|
export interface AuthUser {
|
|
4
21
|
id: string
|
|
5
22
|
email: string
|
|
@@ -31,6 +48,7 @@ export interface OAuthAccount {
|
|
|
31
48
|
createdAt: number
|
|
32
49
|
}
|
|
33
50
|
|
|
51
|
+
/** Verification token purposes. `'reset-password'` is reserved for app-defined password-reset flows via `store.createToken`/`store.consumeToken`. */
|
|
34
52
|
export type TokenKind = 'magic-link' | 'verify-email' | 'reset-password'
|
|
35
53
|
|
|
36
54
|
export interface VerificationToken {
|
|
@@ -41,14 +59,14 @@ export interface VerificationToken {
|
|
|
41
59
|
expiresAt: number
|
|
42
60
|
}
|
|
43
61
|
|
|
44
|
-
export interface UserExport<TUser extends AuthUser = AuthUser> {
|
|
62
|
+
export interface UserExport<TUser extends AuthUser = AuthUser> extends UserExportExt {
|
|
45
63
|
user: PublicUser<TUser>
|
|
46
64
|
sessions: AuthSession[]
|
|
47
65
|
oauthAccounts: Array<{ provider: string, providerAccountId: string, createdAt: number }>
|
|
48
66
|
exportedAt: number
|
|
49
67
|
}
|
|
50
68
|
|
|
51
|
-
export interface AuthStore<TUser extends AuthUser = AuthUser> {
|
|
69
|
+
export interface AuthStore<TUser extends AuthUser = AuthUser> extends AuthStoreExt {
|
|
52
70
|
getUserById(id: string): TUser | undefined | Promise<TUser | undefined>
|
|
53
71
|
getUserByEmail(email: string): TUser | undefined | Promise<TUser | undefined>
|
|
54
72
|
createUser(user: TUser): TUser | Promise<TUser>
|
|
@@ -92,6 +110,7 @@ interface MemoryState<TUser extends AuthUser = AuthUser> {
|
|
|
92
110
|
}
|
|
93
111
|
|
|
94
112
|
const inMemoryStores = new WeakSet<object>()
|
|
113
|
+
const syncStores = new WeakSet<object>()
|
|
95
114
|
|
|
96
115
|
/** Marks `store` as one whose data lives in the process alone when `inMemory`, for `requires` to refuse it in production. */
|
|
97
116
|
export function keptInMemory<T extends object>(inMemory: boolean, store: T): T {
|
|
@@ -105,6 +124,51 @@ export function isKeptInMemory(store: object): boolean {
|
|
|
105
124
|
return inMemoryStores.has(store)
|
|
106
125
|
}
|
|
107
126
|
|
|
127
|
+
/** Marks a built-in store whose methods are guaranteed synchronous, enabling lazy session resolution on the very first request. */
|
|
128
|
+
export function markSyncStore<T extends object>(store: T): T {
|
|
129
|
+
syncStores.add(store)
|
|
130
|
+
return store
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Whether `store` is a known built-in synchronous `AuthStore`. */
|
|
134
|
+
export function isKnownSyncStore(store: object): boolean {
|
|
135
|
+
return syncStores.has(store)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function exportMemoryUser<TUser extends AuthUser>(
|
|
139
|
+
state: MemoryState<TUser>,
|
|
140
|
+
ext: ReturnType<typeof createMemoryStoreExt>,
|
|
141
|
+
userId: string,
|
|
142
|
+
): UserExport<TUser> | null {
|
|
143
|
+
const cur = state.users.get(userId)
|
|
144
|
+
if (!cur)
|
|
145
|
+
return null
|
|
146
|
+
const now = Date.now()
|
|
147
|
+
const sessions: AuthSession[] = []
|
|
148
|
+
for (const [k, s] of state.sessions) {
|
|
149
|
+
if (s.expiresAt <= now) {
|
|
150
|
+
state.sessions.delete(k)
|
|
151
|
+
continue
|
|
152
|
+
}
|
|
153
|
+
if (s.userId === userId)
|
|
154
|
+
sessions.push({ ...s })
|
|
155
|
+
}
|
|
156
|
+
sessions.sort((a, b) => b.createdAt - a.createdAt)
|
|
157
|
+
const oauthAccounts: UserExport['oauthAccounts'] = []
|
|
158
|
+
for (const a of state.oauth.values()) {
|
|
159
|
+
if (a.userId === userId)
|
|
160
|
+
oauthAccounts.push({ provider: a.provider, providerAccountId: a.providerAccountId, createdAt: a.createdAt })
|
|
161
|
+
}
|
|
162
|
+
oauthAccounts.sort((a, b) => a.createdAt - b.createdAt)
|
|
163
|
+
return {
|
|
164
|
+
user: toPublicUser(cur),
|
|
165
|
+
sessions,
|
|
166
|
+
oauthAccounts,
|
|
167
|
+
...ext.exportUserExt(userId, cur.email, now),
|
|
168
|
+
exportedAt: now,
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
108
172
|
export function memoryAuthStore<TUser extends AuthUser = AuthUser>(): AuthStore<TUser> {
|
|
109
173
|
const state: MemoryState<TUser> = {
|
|
110
174
|
users: new Map(),
|
|
@@ -113,8 +177,10 @@ export function memoryAuthStore<TUser extends AuthUser = AuthUser>(): AuthStore<
|
|
|
113
177
|
oauth: new Map(),
|
|
114
178
|
tokens: new Map(),
|
|
115
179
|
}
|
|
180
|
+
const ext = createMemoryStoreExt()
|
|
116
181
|
|
|
117
|
-
return keptInMemory(true, {
|
|
182
|
+
return markSyncStore(keptInMemory(true, {
|
|
183
|
+
...ext.methods,
|
|
118
184
|
getUserById(userId) {
|
|
119
185
|
return state.users.get(userId)
|
|
120
186
|
},
|
|
@@ -158,34 +224,10 @@ export function memoryAuthStore<TUser extends AuthUser = AuthUser>(): AuthStore<
|
|
|
158
224
|
if (t.userId === userId || (cur && t.email === cur.email))
|
|
159
225
|
state.tokens.delete(k)
|
|
160
226
|
}
|
|
227
|
+
ext.deleteUserExt(userId, cur?.email)
|
|
161
228
|
},
|
|
162
229
|
exportUser(userId) {
|
|
163
|
-
|
|
164
|
-
if (!cur)
|
|
165
|
-
return null
|
|
166
|
-
const now = Date.now()
|
|
167
|
-
const sessions: AuthSession[] = []
|
|
168
|
-
for (const [k, s] of state.sessions) {
|
|
169
|
-
if (s.expiresAt <= now) {
|
|
170
|
-
state.sessions.delete(k)
|
|
171
|
-
continue
|
|
172
|
-
}
|
|
173
|
-
if (s.userId === userId)
|
|
174
|
-
sessions.push({ ...s })
|
|
175
|
-
}
|
|
176
|
-
sessions.sort((a, b) => b.createdAt - a.createdAt)
|
|
177
|
-
const oauthAccounts: UserExport['oauthAccounts'] = []
|
|
178
|
-
for (const a of state.oauth.values()) {
|
|
179
|
-
if (a.userId === userId)
|
|
180
|
-
oauthAccounts.push({ provider: a.provider, providerAccountId: a.providerAccountId, createdAt: a.createdAt })
|
|
181
|
-
}
|
|
182
|
-
oauthAccounts.sort((a, b) => a.createdAt - b.createdAt)
|
|
183
|
-
return {
|
|
184
|
-
user: toPublicUser(cur),
|
|
185
|
-
sessions,
|
|
186
|
-
oauthAccounts,
|
|
187
|
-
exportedAt: now,
|
|
188
|
-
}
|
|
230
|
+
return exportMemoryUser(state, ext, userId)
|
|
189
231
|
},
|
|
190
232
|
getSession(tokenHash) {
|
|
191
233
|
const s = state.sessions.get(tokenHash)
|
|
@@ -262,6 +304,7 @@ export function memoryAuthStore<TUser extends AuthUser = AuthUser>(): AuthStore<
|
|
|
262
304
|
if (t.expiresAt <= now)
|
|
263
305
|
state.tokens.delete(k)
|
|
264
306
|
}
|
|
307
|
+
ext.pruneExpiredExt(now)
|
|
265
308
|
},
|
|
266
|
-
})
|
|
309
|
+
}))
|
|
267
310
|
}
|