@vllnt/convex-invitations 0.1.0-canary.d3aaf07 → 0.1.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/dist/client/index.d.ts +19 -10
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +14 -7
- package/dist/client/index.js.map +1 -1
- package/dist/client/types.d.ts +2 -0
- package/dist/client/types.d.ts.map +1 -1
- package/dist/component/_generated/api.d.ts +4 -0
- package/dist/component/_generated/api.d.ts.map +1 -1
- package/dist/component/_generated/api.js.map +1 -1
- package/dist/component/_generated/component.d.ts +17 -16
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/_generated/server.d.ts +12 -0
- package/dist/component/_generated/server.d.ts.map +1 -1
- package/dist/component/_generated/server.js +2 -0
- package/dist/component/_generated/server.js.map +1 -1
- package/dist/component/convex.config.d.ts.map +1 -1
- package/dist/component/crons.d.ts.map +1 -1
- package/dist/component/crons.js +1 -0
- package/dist/component/crons.js.map +1 -1
- package/dist/component/legacyToken.d.ts +5 -0
- package/dist/component/legacyToken.d.ts.map +1 -0
- package/dist/component/legacyToken.js +15 -0
- package/dist/component/legacyToken.js.map +1 -0
- package/dist/component/mutations.d.ts +11 -7
- package/dist/component/mutations.d.ts.map +1 -1
- package/dist/component/mutations.js +89 -22
- package/dist/component/mutations.js.map +1 -1
- package/dist/component/queries.d.ts +14 -16
- package/dist/component/queries.d.ts.map +1 -1
- package/dist/component/queries.js +30 -10
- package/dist/component/queries.js.map +1 -1
- package/dist/component/schema.d.ts +23 -22
- package/dist/component/schema.d.ts.map +1 -1
- package/dist/component/schema.js +9 -9
- package/dist/component/schema.js.map +1 -1
- package/dist/component/token.d.ts +4 -0
- package/dist/component/token.d.ts.map +1 -0
- package/dist/component/token.js +17 -0
- package/dist/component/token.js.map +1 -0
- package/dist/component/validators.d.ts +36 -11
- package/dist/component/validators.d.ts.map +1 -1
- package/dist/component/validators.js +13 -0
- package/dist/component/validators.js.map +1 -1
- package/dist/shared.d.ts.map +1 -1
- package/package.json +18 -15
- package/src/client/index.ts +31 -13
- package/src/client/types.ts +6 -0
- package/src/component/_generated/api.ts +4 -0
- package/src/component/_generated/component.ts +20 -15
- package/src/component/_generated/server.ts +13 -0
- package/src/component/crons.ts +7 -0
- package/src/component/legacyToken.ts +24 -0
- package/src/component/migration.test.ts +110 -0
- package/src/component/mutations.ts +95 -23
- package/src/component/queries.ts +31 -10
- package/src/component/schema.ts +9 -9
- package/src/component/token.ts +19 -0
- package/src/component/validators.ts +14 -0
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import { ConvexError, v } from "convex/values";
|
|
2
2
|
import { api } from "./_generated/api";
|
|
3
3
|
import { mutation } from "./_generated/server";
|
|
4
|
+
import { DEFAULT_RETENTION_MS } from "../shared";
|
|
5
|
+
import { migrateLegacyToken, withLegacyTokenFallback } from "./legacyToken";
|
|
6
|
+
import { tokenDigest, validateToken } from "./token";
|
|
4
7
|
import { invitationView, jsonValue } from "./validators";
|
|
5
8
|
|
|
9
|
+
const DEFAULT_MIGRATION_BATCH = 50;
|
|
10
|
+
const MAX_MIGRATION_BATCH = 100;
|
|
11
|
+
|
|
6
12
|
/**
|
|
7
13
|
* Issue an invitation to a `resourceRef` and return the single-use `token` the
|
|
8
14
|
* host hands to the invitee (out of band — an email, a link). The invite is
|
|
@@ -29,11 +35,17 @@ export const issue = mutation({
|
|
|
29
35
|
},
|
|
30
36
|
returns: v.object({ token: v.string(), expiresAt: v.number() }),
|
|
31
37
|
handler: async (ctx, args) => {
|
|
32
|
-
|
|
38
|
+
validateToken(args.token);
|
|
39
|
+
const tokenHash = await tokenDigest(args.token);
|
|
40
|
+
const existingHash = await ctx.db
|
|
41
|
+
.query("invitations")
|
|
42
|
+
.withIndex("by_token_hash", (q) => q.eq("tokenHash", tokenHash))
|
|
43
|
+
.unique();
|
|
44
|
+
const existingLegacy = await ctx.db
|
|
33
45
|
.query("invitations")
|
|
34
46
|
.withIndex("by_token", (q) => q.eq("token", args.token))
|
|
35
47
|
.unique();
|
|
36
|
-
if (
|
|
48
|
+
if (existingHash !== null || existingLegacy !== null) {
|
|
37
49
|
throw new ConvexError({
|
|
38
50
|
code: "DUPLICATE_TOKEN",
|
|
39
51
|
message: `invitation token already exists`,
|
|
@@ -41,7 +53,7 @@ export const issue = mutation({
|
|
|
41
53
|
}
|
|
42
54
|
|
|
43
55
|
await ctx.db.insert("invitations", {
|
|
44
|
-
|
|
56
|
+
tokenHash,
|
|
45
57
|
resourceRef: args.resourceRef,
|
|
46
58
|
role: args.role,
|
|
47
59
|
inviterRef: args.inviterRef,
|
|
@@ -55,6 +67,49 @@ export const issue = mutation({
|
|
|
55
67
|
},
|
|
56
68
|
});
|
|
57
69
|
|
|
70
|
+
/** Migrate raw pre-upgrade bearer tokens to SHA-256 digests in bounded batches. */
|
|
71
|
+
export const migrateLegacyTokens = mutation({
|
|
72
|
+
args: { batch: v.optional(v.number()) },
|
|
73
|
+
returns: v.number(),
|
|
74
|
+
handler: async (ctx, args) => {
|
|
75
|
+
const batch = args.batch ?? DEFAULT_MIGRATION_BATCH;
|
|
76
|
+
if (
|
|
77
|
+
!Number.isFinite(batch) ||
|
|
78
|
+
!Number.isInteger(batch) ||
|
|
79
|
+
batch < 1 ||
|
|
80
|
+
batch > MAX_MIGRATION_BATCH
|
|
81
|
+
) {
|
|
82
|
+
throw new ConvexError({
|
|
83
|
+
code: "INVALID_BATCH",
|
|
84
|
+
message: `batch must be an integer between 1 and ${MAX_MIGRATION_BATCH}`,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const emptyToken = await ctx.db
|
|
89
|
+
.query("invitations")
|
|
90
|
+
.withIndex("by_token", (q) => q.eq("token", ""))
|
|
91
|
+
.unique();
|
|
92
|
+
const remaining = batch - (emptyToken === null ? 0 : 1);
|
|
93
|
+
const nonempty =
|
|
94
|
+
remaining === 0
|
|
95
|
+
? []
|
|
96
|
+
: await ctx.db
|
|
97
|
+
.query("invitations")
|
|
98
|
+
.withIndex("by_token", (q) => q.gt("token", ""))
|
|
99
|
+
.take(remaining);
|
|
100
|
+
const legacy = emptyToken === null ? nonempty : [emptyToken, ...nonempty];
|
|
101
|
+
for (const invite of legacy) {
|
|
102
|
+
// Both index ranges above select rows whose optional raw token is defined.
|
|
103
|
+
const token = invite.token as string;
|
|
104
|
+
await migrateLegacyToken(ctx.db, invite, await tokenDigest(token));
|
|
105
|
+
}
|
|
106
|
+
if (legacy.length === batch) {
|
|
107
|
+
await ctx.scheduler.runAfter(0, api.mutations.migrateLegacyTokens, { batch });
|
|
108
|
+
}
|
|
109
|
+
return legacy.length;
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
|
|
58
113
|
/**
|
|
59
114
|
* Redeem an invitation by its `token` — the single-use accept transaction.
|
|
60
115
|
* Atomically: the invite must exist, be `pending`, and not be past its
|
|
@@ -84,16 +139,20 @@ export const accept = mutation({
|
|
|
84
139
|
payload: v.optional(jsonValue),
|
|
85
140
|
}),
|
|
86
141
|
handler: async (ctx, args) => {
|
|
87
|
-
|
|
142
|
+
validateToken(args.token);
|
|
143
|
+
const tokenHash = await tokenDigest(args.token);
|
|
144
|
+
const hashed = await ctx.db
|
|
88
145
|
.query("invitations")
|
|
89
|
-
.withIndex("
|
|
146
|
+
.withIndex("by_token_hash", (q) => q.eq("tokenHash", tokenHash))
|
|
90
147
|
.unique();
|
|
91
|
-
|
|
148
|
+
const found = await withLegacyTokenFallback(ctx.db, hashed, args.token);
|
|
149
|
+
if (found === null) {
|
|
92
150
|
throw new ConvexError({
|
|
93
151
|
code: "NOT_FOUND",
|
|
94
152
|
message: `invitation not found`,
|
|
95
153
|
});
|
|
96
154
|
}
|
|
155
|
+
const invite = await migrateLegacyToken(ctx.db, found, tokenHash);
|
|
97
156
|
if (invite.state !== "pending") {
|
|
98
157
|
throw new ConvexError({
|
|
99
158
|
code: `ALREADY_${invite.state.toUpperCase()}`,
|
|
@@ -109,7 +168,7 @@ export const accept = mutation({
|
|
|
109
168
|
});
|
|
110
169
|
}
|
|
111
170
|
|
|
112
|
-
await ctx.db.patch(invite._id, {
|
|
171
|
+
await ctx.db.patch("invitations", invite._id, {
|
|
113
172
|
state: "accepted",
|
|
114
173
|
acceptedAt: now,
|
|
115
174
|
acceptedBy: args.acceptedBy,
|
|
@@ -137,23 +196,27 @@ export const revoke = mutation({
|
|
|
137
196
|
args: { token: v.string() },
|
|
138
197
|
returns: v.null(),
|
|
139
198
|
handler: async (ctx, args) => {
|
|
140
|
-
|
|
199
|
+
validateToken(args.token);
|
|
200
|
+
const tokenHash = await tokenDigest(args.token);
|
|
201
|
+
const hashed = await ctx.db
|
|
141
202
|
.query("invitations")
|
|
142
|
-
.withIndex("
|
|
203
|
+
.withIndex("by_token_hash", (q) => q.eq("tokenHash", tokenHash))
|
|
143
204
|
.unique();
|
|
144
|
-
|
|
205
|
+
const found = await withLegacyTokenFallback(ctx.db, hashed, args.token);
|
|
206
|
+
if (found === null) {
|
|
145
207
|
throw new ConvexError({
|
|
146
208
|
code: "NOT_FOUND",
|
|
147
209
|
message: `invitation not found`,
|
|
148
210
|
});
|
|
149
211
|
}
|
|
212
|
+
const invite = await migrateLegacyToken(ctx.db, found, tokenHash);
|
|
150
213
|
if (invite.state !== "pending") {
|
|
151
214
|
throw new ConvexError({
|
|
152
215
|
code: `ALREADY_${invite.state.toUpperCase()}`,
|
|
153
216
|
message: `invitation is already ${invite.state} and cannot be revoked`,
|
|
154
217
|
});
|
|
155
218
|
}
|
|
156
|
-
await ctx.db.patch(invite._id, { state: "revoked", revokedAt: Date.now() });
|
|
219
|
+
await ctx.db.patch("invitations", invite._id, { state: "revoked", revokedAt: Date.now() });
|
|
157
220
|
return null;
|
|
158
221
|
},
|
|
159
222
|
});
|
|
@@ -169,18 +232,22 @@ export const peek = mutation({
|
|
|
169
232
|
args: { token: v.string() },
|
|
170
233
|
returns: v.union(v.null(), invitationView),
|
|
171
234
|
handler: async (ctx, args) => {
|
|
172
|
-
|
|
235
|
+
validateToken(args.token);
|
|
236
|
+
const tokenHash = await tokenDigest(args.token);
|
|
237
|
+
const hashed = await ctx.db
|
|
173
238
|
.query("invitations")
|
|
174
|
-
.withIndex("
|
|
239
|
+
.withIndex("by_token_hash", (q) => q.eq("tokenHash", tokenHash))
|
|
175
240
|
.unique();
|
|
176
|
-
|
|
241
|
+
const found = await withLegacyTokenFallback(ctx.db, hashed, args.token);
|
|
242
|
+
if (found === null) {
|
|
177
243
|
return null;
|
|
178
244
|
}
|
|
245
|
+
const invite = await migrateLegacyToken(ctx.db, found, tokenHash);
|
|
179
246
|
if (invite.state === "pending" && invite.expiresAt <= Date.now()) {
|
|
180
|
-
await ctx.db.patch(invite._id, { state: "expired" });
|
|
181
|
-
return { ...view(invite), state: "expired" as const };
|
|
247
|
+
await ctx.db.patch("invitations", invite._id, { state: "expired" });
|
|
248
|
+
return { ...view(invite, args.token), state: "expired" as const };
|
|
182
249
|
}
|
|
183
|
-
return view(invite);
|
|
250
|
+
return view(invite, args.token);
|
|
184
251
|
},
|
|
185
252
|
});
|
|
186
253
|
|
|
@@ -212,9 +279,15 @@ export const prune = mutation({
|
|
|
212
279
|
},
|
|
213
280
|
returns: v.number(),
|
|
214
281
|
handler: async (ctx, args) => {
|
|
282
|
+
if (!Number.isFinite(args.batch) || !Number.isInteger(args.batch) || args.batch < 1 || args.batch > 500) {
|
|
283
|
+
throw new ConvexError({
|
|
284
|
+
code: "INVALID_BATCH",
|
|
285
|
+
message: "batch must be an integer between 1 and 500",
|
|
286
|
+
});
|
|
287
|
+
}
|
|
215
288
|
const now = Date.now();
|
|
216
289
|
const before = args.before ?? now;
|
|
217
|
-
const retentionBefore = args.retentionBefore ?? now;
|
|
290
|
+
const retentionBefore = args.retentionBefore ?? now - DEFAULT_RETENTION_MS;
|
|
218
291
|
|
|
219
292
|
// Retention sweep: the three terminal states, queried explicitly (no
|
|
220
293
|
// query-in-loop), each consuming whatever batch budget the earlier passes
|
|
@@ -244,7 +317,7 @@ export const prune = mutation({
|
|
|
244
317
|
.take(args.batch - accepted.length - revoked.length)
|
|
245
318
|
: [];
|
|
246
319
|
for (const row of [...accepted, ...revoked, ...expired]) {
|
|
247
|
-
await ctx.db.delete(row._id);
|
|
320
|
+
await ctx.db.delete("invitations", row._id);
|
|
248
321
|
}
|
|
249
322
|
const removed = accepted.length + revoked.length + expired.length;
|
|
250
323
|
|
|
@@ -260,7 +333,7 @@ export const prune = mutation({
|
|
|
260
333
|
.take(remaining)
|
|
261
334
|
: [];
|
|
262
335
|
for (const row of stale) {
|
|
263
|
-
await ctx.db.patch(row._id, { state: "expired" });
|
|
336
|
+
await ctx.db.patch("invitations", row._id, { state: "expired" });
|
|
264
337
|
}
|
|
265
338
|
|
|
266
339
|
const touched = removed + stale.length;
|
|
@@ -277,7 +350,6 @@ export const prune = mutation({
|
|
|
277
350
|
|
|
278
351
|
/** Project a stored invitation row to its public view (drops internal fields). */
|
|
279
352
|
function view(invite: {
|
|
280
|
-
token: string;
|
|
281
353
|
resourceRef: string;
|
|
282
354
|
role?: unknown;
|
|
283
355
|
inviterRef?: string;
|
|
@@ -289,9 +361,9 @@ function view(invite: {
|
|
|
289
361
|
acceptedAt?: number;
|
|
290
362
|
acceptedBy?: string;
|
|
291
363
|
revokedAt?: number;
|
|
292
|
-
}) {
|
|
364
|
+
}, token: string) {
|
|
293
365
|
return {
|
|
294
|
-
token
|
|
366
|
+
token,
|
|
295
367
|
resourceRef: invite.resourceRef,
|
|
296
368
|
role: invite.role,
|
|
297
369
|
inviterRef: invite.inviterRef,
|
package/src/component/queries.ts
CHANGED
|
@@ -1,13 +1,31 @@
|
|
|
1
1
|
import { v } from "convex/values";
|
|
2
2
|
import { paginationOptsValidator } from "convex/server";
|
|
3
3
|
import { query } from "./_generated/server";
|
|
4
|
-
import {
|
|
4
|
+
import { withLegacyTokenFallback } from "./legacyToken";
|
|
5
|
+
import { tokenDigest, validateToken } from "./token";
|
|
6
|
+
import { invitationMetadataView, invitationState, invitationView } from "./validators";
|
|
5
7
|
import type { Doc } from "./_generated/dataModel";
|
|
6
8
|
|
|
7
9
|
/** Project a stored invitation row to its public view (drops internal fields). */
|
|
8
|
-
function
|
|
10
|
+
function metadataView(invite: Doc<"invitations">) {
|
|
9
11
|
return {
|
|
10
|
-
|
|
12
|
+
resourceRef: invite.resourceRef,
|
|
13
|
+
role: invite.role,
|
|
14
|
+
inviterRef: invite.inviterRef,
|
|
15
|
+
inviteeRef: invite.inviteeRef,
|
|
16
|
+
payload: invite.payload,
|
|
17
|
+
state: invite.state,
|
|
18
|
+
createdAt: invite.createdAt,
|
|
19
|
+
expiresAt: invite.expiresAt,
|
|
20
|
+
acceptedAt: invite.acceptedAt,
|
|
21
|
+
acceptedBy: invite.acceptedBy,
|
|
22
|
+
revokedAt: invite.revokedAt,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function view(invite: Doc<"invitations">, token: string) {
|
|
27
|
+
return {
|
|
28
|
+
token,
|
|
11
29
|
resourceRef: invite.resourceRef,
|
|
12
30
|
role: invite.role,
|
|
13
31
|
inviterRef: invite.inviterRef,
|
|
@@ -33,11 +51,14 @@ export const getByToken = query({
|
|
|
33
51
|
args: { token: v.string() },
|
|
34
52
|
returns: v.union(v.null(), invitationView),
|
|
35
53
|
handler: async (ctx, args) => {
|
|
36
|
-
|
|
54
|
+
validateToken(args.token);
|
|
55
|
+
const tokenHash = await tokenDigest(args.token);
|
|
56
|
+
const hashed = await ctx.db
|
|
37
57
|
.query("invitations")
|
|
38
|
-
.withIndex("
|
|
58
|
+
.withIndex("by_token_hash", (q) => q.eq("tokenHash", tokenHash))
|
|
39
59
|
.unique();
|
|
40
|
-
|
|
60
|
+
const invite = await withLegacyTokenFallback(ctx.db, hashed, args.token);
|
|
61
|
+
return invite === null ? null : view(invite, args.token);
|
|
41
62
|
},
|
|
42
63
|
});
|
|
43
64
|
|
|
@@ -52,7 +73,7 @@ export const getByToken = query({
|
|
|
52
73
|
export const listPending = query({
|
|
53
74
|
args: { resourceRef: v.string(), paginationOpts: paginationOptsValidator },
|
|
54
75
|
returns: v.object({
|
|
55
|
-
page: v.array(
|
|
76
|
+
page: v.array(invitationMetadataView),
|
|
56
77
|
isDone: v.boolean(),
|
|
57
78
|
continueCursor: v.string(),
|
|
58
79
|
splitCursor: v.optional(v.union(v.string(), v.null())),
|
|
@@ -72,7 +93,7 @@ export const listPending = query({
|
|
|
72
93
|
)
|
|
73
94
|
.order("asc")
|
|
74
95
|
.paginate(args.paginationOpts);
|
|
75
|
-
return { ...result, page: result.page.map(
|
|
96
|
+
return { ...result, page: result.page.map(metadataView) };
|
|
76
97
|
},
|
|
77
98
|
});
|
|
78
99
|
|
|
@@ -88,7 +109,7 @@ export const listByResourceState = query({
|
|
|
88
109
|
paginationOpts: paginationOptsValidator,
|
|
89
110
|
},
|
|
90
111
|
returns: v.object({
|
|
91
|
-
page: v.array(
|
|
112
|
+
page: v.array(invitationMetadataView),
|
|
92
113
|
isDone: v.boolean(),
|
|
93
114
|
continueCursor: v.string(),
|
|
94
115
|
splitCursor: v.optional(v.union(v.string(), v.null())),
|
|
@@ -108,6 +129,6 @@ export const listByResourceState = query({
|
|
|
108
129
|
)
|
|
109
130
|
.order("asc")
|
|
110
131
|
.paginate(args.paginationOpts);
|
|
111
|
-
return { ...result, page: result.page.map(
|
|
132
|
+
return { ...result, page: result.page.map(metadataView) };
|
|
112
133
|
},
|
|
113
134
|
});
|
package/src/component/schema.ts
CHANGED
|
@@ -3,28 +3,27 @@ import { v } from "convex/values";
|
|
|
3
3
|
import { invitationState, jsonValue } from "./validators";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Sandboxed table — the invitation's own concern. A `
|
|
7
|
-
* component-minted single-use secret
|
|
6
|
+
* Sandboxed table — the invitation's own concern. A `tokenHash` is the
|
|
7
|
+
* SHA-256 digest of a component-minted single-use secret; the host hands the raw token
|
|
8
8
|
* it to the invitee (out of band — email, link) and the invitee redeems it via
|
|
9
9
|
* `accept`. `resourceRef` is the opaque host resource the invite grants access
|
|
10
10
|
* to (an org, a team, a workspace); `role`/`payload` are opaque host data
|
|
11
11
|
* carried through to the host on accept. `state` tracks the lifecycle and
|
|
12
12
|
* `expiresAt` the TTL.
|
|
13
13
|
*
|
|
14
|
-
* Indexes serve token redemption/lookup (`
|
|
14
|
+
* Indexes serve token redemption/lookup (`by_token_hash`), the pending-per-resource
|
|
15
15
|
* listing (`by_resource_state`), the TTL expiry sweep over open invites
|
|
16
16
|
* (`by_state_expires`), and the terminal retention sweep
|
|
17
17
|
* (`by_state_created`, oldest-first).
|
|
18
18
|
*
|
|
19
|
-
* The component holds no membership
|
|
20
|
-
*
|
|
21
|
-
* the hashed/revocable token to `@vllnt/convex-tokens` and write the granted
|
|
22
|
-
* membership via `@vllnt/convex-memberships`; today the `token` lives here and
|
|
23
|
-
* acceptance returns the grant to the host to apply.
|
|
19
|
+
* The component holds no membership. It stores only a deterministic token digest,
|
|
20
|
+
* returns the raw token once on issue, and returns the grant to the host on acceptance.
|
|
24
21
|
*/
|
|
25
22
|
export default defineSchema({
|
|
26
23
|
invitations: defineTable({
|
|
27
|
-
|
|
24
|
+
tokenHash: v.optional(v.string()),
|
|
25
|
+
/** @deprecated migration-only raw token for rows written before hashed storage. */
|
|
26
|
+
token: v.optional(v.string()),
|
|
28
27
|
resourceRef: v.string(),
|
|
29
28
|
role: v.optional(jsonValue),
|
|
30
29
|
inviterRef: v.optional(v.string()),
|
|
@@ -37,6 +36,7 @@ export default defineSchema({
|
|
|
37
36
|
acceptedBy: v.optional(v.string()),
|
|
38
37
|
revokedAt: v.optional(v.number()),
|
|
39
38
|
})
|
|
39
|
+
.index("by_token_hash", ["tokenHash"])
|
|
40
40
|
.index("by_token", ["token"])
|
|
41
41
|
.index("by_resource_state", ["resourceRef", "state", "createdAt"])
|
|
42
42
|
.index("by_state_expires", ["state", "expiresAt"])
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ConvexError } from "convex/values";
|
|
2
|
+
|
|
3
|
+
const encoder = new TextEncoder();
|
|
4
|
+
const MAX_TOKEN_LENGTH = 512;
|
|
5
|
+
|
|
6
|
+
export function validateToken(token: string): void {
|
|
7
|
+
if (token.length === 0 || token.length > MAX_TOKEN_LENGTH) {
|
|
8
|
+
throw new ConvexError({
|
|
9
|
+
code: "INVALID_TOKEN",
|
|
10
|
+
message: `token must be between 1 and ${MAX_TOKEN_LENGTH} characters`,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Deterministic SHA-256 digest used for bearer-token lookup without storing raw credentials. */
|
|
16
|
+
export async function tokenDigest(token: string): Promise<string> {
|
|
17
|
+
const bytes = new Uint8Array(await crypto.subtle.digest("SHA-256", encoder.encode(token)));
|
|
18
|
+
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
19
|
+
}
|
|
@@ -28,6 +28,20 @@ export const invitationState = v.union(
|
|
|
28
28
|
* interpreting them. `token` is the single-use secret the host hands to the
|
|
29
29
|
* invitee; the host gates who may read it.
|
|
30
30
|
*/
|
|
31
|
+
export const invitationMetadataView = v.object({
|
|
32
|
+
resourceRef: v.string(),
|
|
33
|
+
role: v.optional(jsonValue),
|
|
34
|
+
inviterRef: v.optional(v.string()),
|
|
35
|
+
inviteeRef: v.optional(v.string()),
|
|
36
|
+
payload: v.optional(jsonValue),
|
|
37
|
+
state: invitationState,
|
|
38
|
+
createdAt: v.number(),
|
|
39
|
+
expiresAt: v.number(),
|
|
40
|
+
acceptedAt: v.optional(v.number()),
|
|
41
|
+
acceptedBy: v.optional(v.string()),
|
|
42
|
+
revokedAt: v.optional(v.number()),
|
|
43
|
+
});
|
|
44
|
+
|
|
31
45
|
export const invitationView = v.object({
|
|
32
46
|
token: v.string(),
|
|
33
47
|
resourceRef: v.string(),
|