@voltro/plugin-auth 0.85.0 → 0.87.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/CHANGELOG.md +5987 -0
- package/README.md +93 -0
- package/THIRD-PARTY-NOTICES.md +1122 -31
- package/dist/admission.js +9 -0
- package/dist/appUserStore.js +19 -0
- package/dist/authExtensions.js +9 -0
- package/dist/emailVerification.js +64 -0
- package/dist/guest.d.ts +99 -0
- package/dist/guest.js +3 -0
- package/dist/guestClient.js +237 -0
- package/dist/guestCredential.js +12 -0
- package/dist/guestPasswordUpgrade.js +85 -0
- package/dist/guestPlugin.js +166 -0
- package/dist/guestSchema.js +20 -0
- package/dist/guestSecrets.js +10 -0
- package/dist/guestService.js +224 -0
- package/dist/handlers.js +337 -0
- package/dist/impersonation.js +176 -0
- package/dist/index.d.ts +282 -20
- package/dist/index.js +30 -88
- package/dist/invitations.js +187 -0
- package/dist/mail.js +10 -0
- package/dist/memberships.js +8 -0
- package/dist/native/guestStorage.js +41 -0
- package/dist/native.d.ts +110 -0
- package/dist/native.js +45 -43
- package/dist/nativeRoutes.js +109 -0
- package/dist/overlayPlugin.js +167 -0
- package/dist/passkeyHandlers.js +162 -0
- package/dist/plugin.d.ts +123 -10
- package/dist/plugin.js +519 -5
- package/dist/postgresUserStore.js +475 -0
- package/dist/revocation.js +50 -0
- package/dist/schema.d.ts +45 -6
- package/dist/schema.js +99 -96
- package/dist/sessionFamily.js +15 -0
- package/dist/sessionRegistry.js +133 -0
- package/dist/signedClaims.js +24 -0
- package/dist/strategy.d.ts +17 -9
- package/dist/strategy.js +69 -3
- package/dist/subjectGuard.js +11 -0
- package/dist/totp.js +55 -0
- package/dist/userStore.js +292 -0
- package/dist/web/guestStorage.js +47 -0
- package/dist/web/passkeyClient.js +78 -0
- package/dist/web/requireAuth.js +9 -0
- package/dist/web/subjectContext.js +13 -0
- package/dist/web.d.ts +94 -0
- package/dist/web.js +6 -90
- package/package.json +8 -3
- package/dist/plugin-CbmcyEJs.js +0 -2018
- package/dist/strategy-XaGjSicX.js +0 -353
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { clearSessionCookie as e, resolveSessionSecret as t, sessionSecretsOf as n } from "./session.js";
|
|
2
|
+
import { CSRF_COOKIE_NAME as r, issueCsrfToken as i, verifyCsrf as a } from "./csrf.js";
|
|
3
|
+
import { validateGuestSecrets as o } from "./guestSecrets.js";
|
|
4
|
+
import { GuestAuthError as s, dataStoreGuestAuth as c } from "./guestService.js";
|
|
5
|
+
import { guestTables as l } from "./guestSchema.js";
|
|
6
|
+
import { sessionsTable as u } from "./schema.js";
|
|
7
|
+
import { Duration as d, Effect as f, Result as p } from "effect";
|
|
8
|
+
import { buildSetCookie as m, noteSigningLifetime as h, readCookie as g, readSessionValue as _, verifySessionKeyed as v } from "@voltro/protocol/session";
|
|
9
|
+
import { definePlugin as y } from "@voltro/protocol";
|
|
10
|
+
//#region src/guestPlugin.ts
|
|
11
|
+
var b = (e, t, n = {}) => ({
|
|
12
|
+
status: e,
|
|
13
|
+
body: JSON.stringify(t),
|
|
14
|
+
contentType: "application/json",
|
|
15
|
+
headers: {
|
|
16
|
+
"cache-control": "no-store",
|
|
17
|
+
...n
|
|
18
|
+
}
|
|
19
|
+
}), x = (x) => {
|
|
20
|
+
let S = x.prefix ?? "/auth/guest";
|
|
21
|
+
if (!/^\/[a-zA-Z0-9/_-]+$/.test(S) || S.endsWith("/")) throw Error("Guest route prefix must be an absolute path without a trailing slash");
|
|
22
|
+
if (!x.tenantId.trim()) throw Error("Guest authentication requires a server-owned tenant");
|
|
23
|
+
let C = d.toSeconds(x.ttl ?? "365 days"), w = d.toSeconds(x.sessionTtl ?? "7 days");
|
|
24
|
+
if (![C, w].every((e) => Number.isSafeInteger(e) && e > 0 && e <= 31536e4)) throw Error("Guest authentication lifetimes must be positive bounded whole seconds");
|
|
25
|
+
h("session", w * 1e3);
|
|
26
|
+
let T = () => o(x.secret ?? t()), E, D = /* @__PURE__ */ new WeakMap(), O = (e) => {
|
|
27
|
+
let t = D.get(e);
|
|
28
|
+
if (t) return t;
|
|
29
|
+
let n = x.service?.(e) ?? c(e, {
|
|
30
|
+
tenantId: x.tenantId,
|
|
31
|
+
secret: T,
|
|
32
|
+
ttlSeconds: C,
|
|
33
|
+
sessionTtlSeconds: w,
|
|
34
|
+
upgradeable: x.upgradeable ?? !0,
|
|
35
|
+
...x.secure === void 0 ? {} : { secure: x.secure }
|
|
36
|
+
});
|
|
37
|
+
return D.set(e, n), n;
|
|
38
|
+
}, k = (e) => f.gen(function* () {
|
|
39
|
+
return e.tenantId !== x.tenantId || e.kind !== "guest" ? yield* f.fail(new s({ code: "credential_rejected" })) : E ? O(E) : yield* f.fail(new s({ code: "store_unavailable" }));
|
|
40
|
+
}), A = async (t) => {
|
|
41
|
+
let n = T(), o = typeof n == "string" ? n : n.current.secret;
|
|
42
|
+
if (t.method === "GET" && t.path === S + "/csrf") {
|
|
43
|
+
let e = i(o);
|
|
44
|
+
return b(200, { token: e }, { "set-cookie": m(r, e, {
|
|
45
|
+
path: "/",
|
|
46
|
+
sameSite: "lax",
|
|
47
|
+
secure: x.secure ?? !0,
|
|
48
|
+
httpOnly: !1,
|
|
49
|
+
maxAgeSeconds: 3600
|
|
50
|
+
}) });
|
|
51
|
+
}
|
|
52
|
+
if (![
|
|
53
|
+
"/create",
|
|
54
|
+
"/resume",
|
|
55
|
+
"/prepare-rotation",
|
|
56
|
+
"/rotate",
|
|
57
|
+
"/revoke"
|
|
58
|
+
].some((e) => t.path === S + e)) return b(404, { error: "not_found" });
|
|
59
|
+
if (t.method !== "POST") return b(405, { error: "method_not_allowed" }, { allow: "POST" });
|
|
60
|
+
if ((t.headers["content-type"] ?? "").split(";")[0]?.trim() !== "application/json") return b(415, { error: "json_required" });
|
|
61
|
+
if (t.rawBody.byteLength > 2048) return b(413, { error: "body_too_large" });
|
|
62
|
+
if (t.headers.cookie && !a(t.headers["x-csrf-token"], g(t.headers.cookie, "voltro:csrf"), o)) return b(403, { error: "csrf_rejected" });
|
|
63
|
+
let s = t.store ?? E;
|
|
64
|
+
if (!s) return b(503, { error: "store_unavailable" });
|
|
65
|
+
let c;
|
|
66
|
+
try {
|
|
67
|
+
c = JSON.parse(new TextDecoder().decode(t.rawBody));
|
|
68
|
+
} catch {
|
|
69
|
+
return b(400, { error: "invalid_input" });
|
|
70
|
+
}
|
|
71
|
+
if (typeof c != "object" || !c || Array.isArray(c)) return b(400, { error: "invalid_input" });
|
|
72
|
+
let l = c, u = t.path === S + "/create", d = t.path === S + "/rotate";
|
|
73
|
+
if (Object.keys(l).some((e) => u || e !== "credential" && !(d && e === "nextCredential")) || !u && typeof l.credential != "string" || d && typeof l.nextCredential != "string") return b(400, { error: "invalid_input" });
|
|
74
|
+
let h = O(s);
|
|
75
|
+
if (u && _(t.headers)) return b(409, { error: "session_already_present" });
|
|
76
|
+
let v = typeof l.credential == "string" ? l.credential : "";
|
|
77
|
+
if (t.path === S + "/prepare-rotation") {
|
|
78
|
+
let e = await f.runPromise(f.result(h.prepareRotation(v)));
|
|
79
|
+
return p.isFailure(e) ? b(e.failure.code === "credential_rejected" ? 401 : 503, { error: e.failure.code }) : b(200, e.success);
|
|
80
|
+
}
|
|
81
|
+
let y = await f.runPromise(f.result(u ? h.create() : t.path === S + "/resume" ? h.resume(v) : d ? h.rotate(v, String(l.nextCredential)) : h.revoke(v).pipe(f.as(null))));
|
|
82
|
+
if (p.isFailure(y)) return b(y.failure.code === "credential_rejected" ? 401 : 503, { error: y.failure.code });
|
|
83
|
+
if (y.success === null) return b(200, { revoked: !0 }, { "set-cookie": e({ secure: x.secure ?? !0 }) });
|
|
84
|
+
let { subject: C, value: w, setCookie: D, deviceCredential: k } = y.success;
|
|
85
|
+
return b(200, {
|
|
86
|
+
subject: C,
|
|
87
|
+
session: w,
|
|
88
|
+
...k === void 0 ? {} : { deviceCredential: k }
|
|
89
|
+
}, { "set-cookie": D });
|
|
90
|
+
};
|
|
91
|
+
return {
|
|
92
|
+
...y({
|
|
93
|
+
name: "@voltro/plugin-auth#guest",
|
|
94
|
+
baseName: "@voltro/plugin-auth",
|
|
95
|
+
permissions: ["store:write"],
|
|
96
|
+
extendSchema: { tables: [...l, u] },
|
|
97
|
+
declaredEnv: x.secret === void 0 ? [{
|
|
98
|
+
name: "VOLTRO_SESSION_SECRET",
|
|
99
|
+
required: !0,
|
|
100
|
+
secret: !0,
|
|
101
|
+
generate: "base64url",
|
|
102
|
+
description: "Signs this project's ordinary and guest sessions"
|
|
103
|
+
}] : [],
|
|
104
|
+
privacy: { collects: [{
|
|
105
|
+
category: "identifiers",
|
|
106
|
+
linked: !0,
|
|
107
|
+
tracking: !1,
|
|
108
|
+
purposes: ["app-functionality"]
|
|
109
|
+
}] },
|
|
110
|
+
subjectPrivacy: {
|
|
111
|
+
tables: [...l, u].map((e) => e.tableName),
|
|
112
|
+
kinds: ["guest"],
|
|
113
|
+
erasePhase: "identity"
|
|
114
|
+
},
|
|
115
|
+
onErase: (e, t) => k(t).pipe(f.flatMap((t) => t.erase(e)), f.asVoid),
|
|
116
|
+
onExport: (e, t) => k(t).pipe(f.flatMap((t) => t.export(e))),
|
|
117
|
+
httpRoutes: [{
|
|
118
|
+
method: "*",
|
|
119
|
+
path: S,
|
|
120
|
+
handle: A
|
|
121
|
+
}],
|
|
122
|
+
bindDataStore: (e) => {
|
|
123
|
+
E = e;
|
|
124
|
+
},
|
|
125
|
+
onActivate: () => f.sync(() => {
|
|
126
|
+
T();
|
|
127
|
+
}),
|
|
128
|
+
onDeactivate: () => f.sync(() => {
|
|
129
|
+
E = void 0;
|
|
130
|
+
})
|
|
131
|
+
}),
|
|
132
|
+
guest: { service: () => {
|
|
133
|
+
if (!E) throw Error("Guest lifecycle requires the bound app DataStore");
|
|
134
|
+
return O(E);
|
|
135
|
+
} },
|
|
136
|
+
auth: { sessionStrategy: {
|
|
137
|
+
id: "voltro-guest",
|
|
138
|
+
resolve: async (e) => {
|
|
139
|
+
let t = _(e.headers);
|
|
140
|
+
if (!t) return { kind: "skip" };
|
|
141
|
+
let r = v(t, n(T()));
|
|
142
|
+
if (!r) return {
|
|
143
|
+
kind: "failed",
|
|
144
|
+
reason: "voltro session cookie failed verification"
|
|
145
|
+
};
|
|
146
|
+
if (r.subject.type !== "user" || r.subject.kind !== "guest") return { kind: "skip" };
|
|
147
|
+
let i = e.store ?? E;
|
|
148
|
+
if (!i) return {
|
|
149
|
+
kind: "failed",
|
|
150
|
+
reason: "guest session registry unavailable"
|
|
151
|
+
};
|
|
152
|
+
let a = await f.runPromise(f.result(O(i).isLive(r.subject)));
|
|
153
|
+
return p.isSuccess(a) && a.success ? {
|
|
154
|
+
kind: "matched",
|
|
155
|
+
subject: r.subject,
|
|
156
|
+
credentialExpiresAt: r.exp * 1e3
|
|
157
|
+
} : {
|
|
158
|
+
kind: "failed",
|
|
159
|
+
reason: "guest session revoked or expired"
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
} }
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
//#endregion
|
|
166
|
+
export { x as guestAuthPlugin };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { boolean as e, id as t, integer as n, table as r, text as i, timestamp as a } from "@voltro/database";
|
|
2
|
+
//#region src/guestSchema.ts
|
|
3
|
+
var o = r("authGuests", {
|
|
4
|
+
id: t(),
|
|
5
|
+
tenantId: i(),
|
|
6
|
+
upgradeable: e(),
|
|
7
|
+
expiresAt: a(),
|
|
8
|
+
createdAt: a(),
|
|
9
|
+
lastSeenAt: a()
|
|
10
|
+
}).index(["tenantId"]), s = r("authGuestDevices", {
|
|
11
|
+
id: t(),
|
|
12
|
+
guestId: i(),
|
|
13
|
+
tenantId: i(),
|
|
14
|
+
secretHash: i().serverOnly(),
|
|
15
|
+
generation: n(),
|
|
16
|
+
expiresAt: a(),
|
|
17
|
+
createdAt: a()
|
|
18
|
+
}).index(["guestId"]).index(["tenantId"]), c = [o, s];
|
|
19
|
+
//#endregion
|
|
20
|
+
export { s as authGuestDevicesTable, o as authGuestsTable, c as guestTables };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { sessionSecretsOf as e } from "./session.js";
|
|
2
|
+
import { assertProductionSessionSecret as t } from "@voltro/protocol/session";
|
|
3
|
+
//#region src/guestSecrets.ts
|
|
4
|
+
var n = (n) => {
|
|
5
|
+
let r = e(n);
|
|
6
|
+
for (let e of [r.current, ...r.previous ? [r.previous] : []]) t({ VOLTRO_SESSION_SECRET: e.secret });
|
|
7
|
+
return n;
|
|
8
|
+
};
|
|
9
|
+
//#endregion
|
|
10
|
+
export { n as validateGuestSecrets };
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { newAuthId as e } from "./userStore.js";
|
|
2
|
+
import { dataStoreSessionRegistry as t, issueRegisteredSession as n } from "./sessionRegistry.js";
|
|
3
|
+
import { validateGuestSecrets as r } from "./guestSecrets.js";
|
|
4
|
+
import { parseGuestCredential as i } from "./guestCredential.js";
|
|
5
|
+
import { createHash as a, randomBytes as o, timingSafeEqual as s } from "node:crypto";
|
|
6
|
+
import { Data as c, Effect as l } from "effect";
|
|
7
|
+
import { noteSigningLifetime as u } from "@voltro/protocol/session";
|
|
8
|
+
import { and as d, eq as f } from "@voltro/database";
|
|
9
|
+
//#region src/guestService.ts
|
|
10
|
+
var p = class extends c.TaggedError("GuestAuthError") {}, m = "authGuests", h = "authGuestDevices", g = 86400, _ = () => new p({ code: "credential_rejected" }), v = (e) => a("sha256").update(e).digest("hex"), y = (e) => e instanceof Date ? e.getTime() : typeof e == "string" ? Date.parse(e) : NaN, b = (e, t) => ({
|
|
11
|
+
table: e,
|
|
12
|
+
predicate: t,
|
|
13
|
+
order: [],
|
|
14
|
+
take: void 0,
|
|
15
|
+
skip: void 0,
|
|
16
|
+
projection: void 0
|
|
17
|
+
}), x = (e, t, n) => d(f(e, t), f("tenantId", n)), S = (e, t) => typeof e != "string" || !/^[a-f0-9]{64}$/.test(e) ? !1 : s(Buffer.from(e, "hex"), Buffer.from(v(t), "hex")), C = (e) => {
|
|
18
|
+
let t = i(e);
|
|
19
|
+
if (!t) throw _();
|
|
20
|
+
return t;
|
|
21
|
+
}, w = (e) => l.tryPromise({
|
|
22
|
+
try: e,
|
|
23
|
+
catch: (e) => e instanceof p ? e : new p({ code: "store_unavailable" })
|
|
24
|
+
}), T = (i, a) => {
|
|
25
|
+
if (!a.tenantId.trim()) throw Error("Guest authentication requires a server-owned tenant");
|
|
26
|
+
let s = a.ttlSeconds ?? 365 * g, c = a.sessionTtlSeconds ?? 7 * g;
|
|
27
|
+
if (![s, c].every((e) => Number.isSafeInteger(e) && e > 0 && e <= 3650 * g)) throw Error("Guest authentication lifetimes must be positive bounded whole seconds");
|
|
28
|
+
u("session", c * 1e3);
|
|
29
|
+
let l = a.now ?? Date.now, d = (e, t, n, r) => e.query(b(t, x(n, r, a.tenantId))), f = async (e, t, n) => {
|
|
30
|
+
let r = await e.update(m, t, { lastSeenAt: new Date(n) });
|
|
31
|
+
if (!r || r.tenantId !== a.tenantId) throw _();
|
|
32
|
+
return r;
|
|
33
|
+
}, T = async (e, t, n) => {
|
|
34
|
+
let r = C(t), i = await f(e, r.guestId, n), a = (await d(e, h, "id", r.deviceId))[0];
|
|
35
|
+
if (!a || a.guestId !== r.guestId || !S(a.secretHash, r.secret) || !(y(i.expiresAt) > n) || !(y(a.expiresAt) > n) || !Number.isSafeInteger(a.generation) || Number(a.generation) < 0) throw _();
|
|
36
|
+
return {
|
|
37
|
+
guest: i,
|
|
38
|
+
device: a,
|
|
39
|
+
parsed: r
|
|
40
|
+
};
|
|
41
|
+
}, E = async (e, i, o, s) => {
|
|
42
|
+
let l = Math.floor((Math.min(y(i.expiresAt), y(o.expiresAt)) - s) / 1e3);
|
|
43
|
+
if (l < 1) throw _();
|
|
44
|
+
let u = {
|
|
45
|
+
type: "user",
|
|
46
|
+
kind: "guest",
|
|
47
|
+
id: String(i.id),
|
|
48
|
+
tenantId: a.tenantId,
|
|
49
|
+
metadata: {
|
|
50
|
+
guestDeviceId: o.id,
|
|
51
|
+
guestDeviceGeneration: o.generation
|
|
52
|
+
}
|
|
53
|
+
}, d = await n(t(e), u, r(typeof a.secret == "function" ? a.secret() : a.secret), {
|
|
54
|
+
ttlSeconds: Math.min(c, l),
|
|
55
|
+
now: () => s,
|
|
56
|
+
...a.secure === void 0 ? {} : { secure: a.secure }
|
|
57
|
+
});
|
|
58
|
+
return {
|
|
59
|
+
...d,
|
|
60
|
+
subject: {
|
|
61
|
+
...u,
|
|
62
|
+
metadata: {
|
|
63
|
+
...u.metadata,
|
|
64
|
+
sessionId: d.sessionId
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}, D = async (e, t) => {
|
|
69
|
+
for (let n of await d(e, "sessions", "userId", t)) await e.delete("sessions", String(n.id));
|
|
70
|
+
};
|
|
71
|
+
return {
|
|
72
|
+
create: () => w(async () => {
|
|
73
|
+
let t = l(), n = e("guest"), r = e("gdev"), c = o(32).toString("base64url"), u = {
|
|
74
|
+
id: n,
|
|
75
|
+
tenantId: a.tenantId,
|
|
76
|
+
upgradeable: a.upgradeable ?? !0,
|
|
77
|
+
expiresAt: new Date(t + s * 1e3),
|
|
78
|
+
createdAt: new Date(t),
|
|
79
|
+
lastSeenAt: new Date(t)
|
|
80
|
+
}, d = {
|
|
81
|
+
id: r,
|
|
82
|
+
guestId: n,
|
|
83
|
+
tenantId: a.tenantId,
|
|
84
|
+
secretHash: v(c),
|
|
85
|
+
generation: 0,
|
|
86
|
+
expiresAt: u.expiresAt,
|
|
87
|
+
createdAt: new Date(t)
|
|
88
|
+
};
|
|
89
|
+
return i.transactional(async (e) => (await e.insert(m, u), await e.insert(h, d), {
|
|
90
|
+
...await E(e, u, d, t),
|
|
91
|
+
deviceCredential: `${n}.${r}.${c}`
|
|
92
|
+
}));
|
|
93
|
+
}),
|
|
94
|
+
resume: (e) => w(async () => {
|
|
95
|
+
let t = l();
|
|
96
|
+
return i.transactional(async (n) => {
|
|
97
|
+
let { guest: r, device: i } = await T(n, e, t);
|
|
98
|
+
return E(n, r, i, t);
|
|
99
|
+
});
|
|
100
|
+
}),
|
|
101
|
+
prepareRotation: (e) => w(async () => {
|
|
102
|
+
r(typeof a.secret == "function" ? a.secret() : a.secret);
|
|
103
|
+
let t = l(), n = o(32).toString("base64url");
|
|
104
|
+
return i.transactional(async (r) => {
|
|
105
|
+
let { parsed: i } = await T(r, e, t);
|
|
106
|
+
return { deviceCredential: `${i.guestId}.${i.deviceId}.${n}` };
|
|
107
|
+
});
|
|
108
|
+
}),
|
|
109
|
+
rotate: (e, t) => w(async () => {
|
|
110
|
+
let n = l(), r = C(t);
|
|
111
|
+
return i.transactional(async (i) => {
|
|
112
|
+
let { guest: a, device: o, parsed: s } = await T(i, e, n);
|
|
113
|
+
if (r.guestId !== s.guestId || r.deviceId !== s.deviceId || r.secret === s.secret) throw _();
|
|
114
|
+
let c = Number(o.generation) + 1;
|
|
115
|
+
if (!Number.isSafeInteger(c)) throw _();
|
|
116
|
+
let l = await i.update(h, s.deviceId, {
|
|
117
|
+
secretHash: v(r.secret),
|
|
118
|
+
generation: c
|
|
119
|
+
});
|
|
120
|
+
if (!l) throw _();
|
|
121
|
+
return await D(i, s.guestId), {
|
|
122
|
+
...await E(i, a, l, n),
|
|
123
|
+
deviceCredential: t
|
|
124
|
+
};
|
|
125
|
+
});
|
|
126
|
+
}),
|
|
127
|
+
revoke: (e) => w(async () => {
|
|
128
|
+
let t = l();
|
|
129
|
+
await i.transactional(async (n) => {
|
|
130
|
+
let { parsed: r } = await T(n, e, t);
|
|
131
|
+
await n.delete(h, r.deviceId), await D(n, r.guestId);
|
|
132
|
+
});
|
|
133
|
+
}),
|
|
134
|
+
upgrade: (e, t) => w(async () => {
|
|
135
|
+
r(typeof a.secret == "function" ? a.secret() : a.secret);
|
|
136
|
+
let n = l();
|
|
137
|
+
return i.transactional(async (r) => {
|
|
138
|
+
let { guest: i, parsed: o } = await T(r, e, n);
|
|
139
|
+
if (i.upgradeable !== !0 && i.upgradeable !== 1) throw _();
|
|
140
|
+
if ((await d(r, "users", "id", o.guestId)).length !== 0) throw new p({ code: "upgrade_conflict" });
|
|
141
|
+
await D(r, o.guestId);
|
|
142
|
+
let s = await t({
|
|
143
|
+
store: r,
|
|
144
|
+
subjectId: o.guestId,
|
|
145
|
+
tenantId: a.tenantId,
|
|
146
|
+
createdAt: new Date(y(i.createdAt))
|
|
147
|
+
}), c = (await d(r, "users", "id", o.guestId))[0];
|
|
148
|
+
if (!c || c.id !== o.guestId || c.tenantId !== a.tenantId) throw new p({ code: "upgrade_conflict" });
|
|
149
|
+
for (let e of await d(r, h, "guestId", o.guestId)) await r.delete(h, String(e.id));
|
|
150
|
+
if (!await r.delete(m, o.guestId)) throw _();
|
|
151
|
+
return s;
|
|
152
|
+
});
|
|
153
|
+
}),
|
|
154
|
+
erase: (e) => w(async () => i.transactional(async (t) => {
|
|
155
|
+
let n = await t.update(m, e, { lastSeenAt: new Date(l()) });
|
|
156
|
+
if (!n) return !1;
|
|
157
|
+
if (n.tenantId !== a.tenantId) throw _();
|
|
158
|
+
for (let n of await d(t, h, "guestId", e)) await t.delete(h, String(n.id));
|
|
159
|
+
return await D(t, e), t.delete(m, e);
|
|
160
|
+
})),
|
|
161
|
+
mergeInto: (e, t, n) => w(async () => {
|
|
162
|
+
r(typeof a.secret == "function" ? a.secret() : a.secret);
|
|
163
|
+
let o = l();
|
|
164
|
+
if (typeof t != "string" || !t.trim()) throw _();
|
|
165
|
+
return i.transactional(async (r) => {
|
|
166
|
+
let { guest: i, parsed: s } = await T(r, e, o);
|
|
167
|
+
if (i.upgradeable !== !0 && i.upgradeable !== 1) throw _();
|
|
168
|
+
if (t === s.guestId) throw new p({ code: "upgrade_conflict" });
|
|
169
|
+
let c = await r.update("users", t, { updatedAt: new Date(o) });
|
|
170
|
+
if (!c || c.id !== t || c.tenantId !== a.tenantId || c.deactivatedAt !== null && c.deactivatedAt !== void 0 || (await d(r, m, "id", t)).length !== 0 || (await d(r, "users", "id", s.guestId)).length !== 0) throw _();
|
|
171
|
+
await D(r, s.guestId);
|
|
172
|
+
let l = await n({
|
|
173
|
+
store: r,
|
|
174
|
+
subjectId: s.guestId,
|
|
175
|
+
targetId: t,
|
|
176
|
+
tenantId: a.tenantId,
|
|
177
|
+
createdAt: new Date(y(i.createdAt))
|
|
178
|
+
}), u = (await d(r, "users", "id", t))[0];
|
|
179
|
+
if (!u || u.id !== t || u.tenantId !== a.tenantId || (await d(r, "users", "id", s.guestId)).length !== 0) throw new p({ code: "upgrade_conflict" });
|
|
180
|
+
for (let e of await d(r, h, "guestId", s.guestId)) await r.delete(h, String(e.id));
|
|
181
|
+
if (!await r.delete(m, s.guestId)) throw _();
|
|
182
|
+
return l;
|
|
183
|
+
});
|
|
184
|
+
}),
|
|
185
|
+
export: (e) => w(async () => {
|
|
186
|
+
let t = (await d(i, m, "id", e))[0];
|
|
187
|
+
if (!t) return null;
|
|
188
|
+
let n = await d(i, h, "guestId", e), r = await d(i, "sessions", "userId", e);
|
|
189
|
+
return {
|
|
190
|
+
subject: {
|
|
191
|
+
id: e,
|
|
192
|
+
tenantId: a.tenantId,
|
|
193
|
+
kind: "guest",
|
|
194
|
+
upgradeable: t.upgradeable === !0 || t.upgradeable === 1,
|
|
195
|
+
createdAt: y(t.createdAt),
|
|
196
|
+
expiresAt: y(t.expiresAt)
|
|
197
|
+
},
|
|
198
|
+
devices: n.map((e) => ({
|
|
199
|
+
id: String(e.id),
|
|
200
|
+
generation: Number(e.generation),
|
|
201
|
+
createdAt: y(e.createdAt),
|
|
202
|
+
expiresAt: y(e.expiresAt)
|
|
203
|
+
})),
|
|
204
|
+
sessions: r.map((e) => ({
|
|
205
|
+
id: String(e.id),
|
|
206
|
+
expiresAt: y(e.expiresAt)
|
|
207
|
+
}))
|
|
208
|
+
};
|
|
209
|
+
}),
|
|
210
|
+
isLive: (e) => w(async () => {
|
|
211
|
+
if (e.type !== "user" || e.kind !== "guest" || e.tenantId !== a.tenantId) return !1;
|
|
212
|
+
let t = e.metadata?.sessionId, n = e.metadata?.guestDeviceId, r = e.metadata?.guestDeviceGeneration;
|
|
213
|
+
if (typeof t != "string" || typeof n != "string" || !Number.isSafeInteger(r)) return !1;
|
|
214
|
+
let o = l(), s = (await d(i, m, "id", e.id))[0];
|
|
215
|
+
if (!s || !(y(s.expiresAt) > o)) return !1;
|
|
216
|
+
let c = (await d(i, h, "id", n))[0];
|
|
217
|
+
if (!c || c.guestId !== e.id || c.generation !== r || !(y(c.expiresAt) > o)) return !1;
|
|
218
|
+
let u = (await d(i, "sessions", "id", t))[0];
|
|
219
|
+
return u?.userId === e.id && y(u.expiresAt) > o;
|
|
220
|
+
})
|
|
221
|
+
};
|
|
222
|
+
};
|
|
223
|
+
//#endregion
|
|
224
|
+
export { p as GuestAuthError, T as dataStoreGuestAuth };
|