@objectstack/plugin-sharing 6.8.1 → 6.9.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.
@@ -0,0 +1,266 @@
1
+ // Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
2
+
3
+ /**
4
+ * REST surface for ShareLinkService.
5
+ *
6
+ * POST /api/v1/share-links → create a link
7
+ * GET /api/v1/share-links → list links (?object, ?recordId, ?includeRevoked)
8
+ * DELETE /api/v1/share-links/:idOrToken → revoke
9
+ * GET /api/v1/share-links/:token/resolve → resolve token, returns { record, link, redactFields }
10
+ *
11
+ * The resolve route is intentionally public — it's the only endpoint
12
+ * holders of a token need. It does:
13
+ *
14
+ * 1. Look up the row by token (via ShareLinkService.resolveToken,
15
+ * which gates audience / expiry / password and stamps usage).
16
+ * 2. Fetch the underlying record with a SYSTEM context (so the read
17
+ * bypasses normal RLS — the token IS the authorisation).
18
+ * 3. Strip `redactFields` from the record before returning.
19
+ *
20
+ * For browser-rendered share pages, the front-end calls this endpoint
21
+ * and renders the response read-only.
22
+ */
23
+
24
+ import type { IHttpServer, IHttpRequest, IHttpResponse, RouteHandler } from '@objectstack/spec/contracts';
25
+ import type { ShareLinkExecutionContext } from '@objectstack/spec/contracts';
26
+ import type { ShareLinkService } from './share-link-service.js';
27
+ import type { SharingEngine } from './sharing-service.js';
28
+
29
+ const SYSTEM_CTX = { isSystem: true, roles: [], permissions: [] } as const;
30
+
31
+ export interface ShareLinkRoutesOptions {
32
+ basePath?: string;
33
+ /** Read caller identity for authenticated routes. */
34
+ contextFromRequest?: (req: IHttpRequest) => ShareLinkExecutionContext;
35
+ }
36
+
37
+ const defaultContext = (req: IHttpRequest): ShareLinkExecutionContext => {
38
+ const header = (name: string): string | undefined => {
39
+ const v = req.headers?.[name];
40
+ return Array.isArray(v) ? v[0] : v;
41
+ };
42
+ return {
43
+ userId: header('x-user-id'),
44
+ tenantId: header('x-tenant-id'),
45
+ };
46
+ };
47
+
48
+ function sendError(res: IHttpResponse, status: number, code: string, message: string) {
49
+ res.status(status).json({ error: { code, message } });
50
+ }
51
+
52
+ /** Strip `redactFields` from a record (also removes from nested arrays of objects). */
53
+ function applyRedaction(record: any, redactFields: string[]): any {
54
+ if (!record || typeof record !== 'object' || redactFields.length === 0) return record;
55
+ if (Array.isArray(record)) return record.map((r) => applyRedaction(r, redactFields));
56
+ const out: any = {};
57
+ for (const [k, v] of Object.entries(record)) {
58
+ if (redactFields.includes(k)) continue;
59
+ out[k] = v;
60
+ }
61
+ return out;
62
+ }
63
+
64
+ export function registerShareLinkRoutes(
65
+ http: IHttpServer,
66
+ service: ShareLinkService,
67
+ engine: SharingEngine,
68
+ opts: ShareLinkRoutesOptions = {},
69
+ ): void {
70
+ const base = opts.basePath ?? '/api/v1/share-links';
71
+ const ctxOf = opts.contextFromRequest ?? defaultContext;
72
+
73
+ // ── CREATE ─────────────────────────────────────────────────────
74
+ http.post(base, (async (req, res) => {
75
+ try {
76
+ const ctx = ctxOf(req);
77
+ const body: any = req.body ?? {};
78
+ if (!body.object || !body.recordId) {
79
+ return sendError(res, 400, 'VALIDATION_FAILED', 'object and recordId are required');
80
+ }
81
+ const link = await service.createLink(
82
+ {
83
+ object: body.object,
84
+ recordId: body.recordId,
85
+ permission: body.permission,
86
+ audience: body.audience,
87
+ expiresAt: body.expiresAt ?? null,
88
+ emailAllowlist: body.emailAllowlist,
89
+ password: body.password,
90
+ redactFields: body.redactFields,
91
+ label: body.label,
92
+ },
93
+ ctx,
94
+ );
95
+ // Echo the token in the create response only — the listing
96
+ // endpoint also returns it (admins need to copy/recreate URLs),
97
+ // but downstream API consumers typically derive the public URL
98
+ // from `link.token` immediately.
99
+ await res.status(201).json({ link });
100
+ } catch (err: any) {
101
+ sendError(res, err?.status ?? 500, err?.code ?? 'INTERNAL', err?.message ?? 'Failed to create link');
102
+ }
103
+ }) satisfies RouteHandler);
104
+
105
+ // ── LIST ───────────────────────────────────────────────────────
106
+ http.get(base, (async (req, res) => {
107
+ try {
108
+ const ctx = ctxOf(req);
109
+ const q = req.query ?? {};
110
+ const link = await service.listLinks(
111
+ {
112
+ object: typeof q.object === 'string' ? q.object : undefined,
113
+ recordId: typeof q.recordId === 'string' ? q.recordId : undefined,
114
+ createdBy: typeof q.createdBy === 'string' ? q.createdBy : undefined,
115
+ includeRevoked: q.includeRevoked === 'true' || q.includeRevoked === '1',
116
+ },
117
+ ctx,
118
+ );
119
+ await res.json({ links: link });
120
+ } catch (err: any) {
121
+ sendError(res, err?.status ?? 500, err?.code ?? 'INTERNAL', err?.message ?? 'Failed to list links');
122
+ }
123
+ }) satisfies RouteHandler);
124
+
125
+ // ── REVOKE ─────────────────────────────────────────────────────
126
+ http.delete(`${base}/:idOrToken`, (async (req, res) => {
127
+ try {
128
+ const ctx = ctxOf(req);
129
+ await service.revokeLink(req.params.idOrToken, ctx);
130
+ await res.status(200).json({ ok: true });
131
+ } catch (err: any) {
132
+ sendError(res, err?.status ?? 500, err?.code ?? 'INTERNAL', err?.message ?? 'Failed to revoke link');
133
+ }
134
+ }) satisfies RouteHandler);
135
+
136
+ // ── PUBLIC RESOLVE ────────────────────────────────────────────
137
+ //
138
+ // No `ctxOf` here — the token IS the authorisation. We still allow
139
+ // probes from a signed-in user so audience=signed_in is satisfiable.
140
+ http.get(`${base}/:token/resolve`, (async (req, res) => {
141
+ try {
142
+ const q = req.query ?? {};
143
+ const signedInUserId = (() => {
144
+ const v = req.headers?.['x-user-id'];
145
+ return Array.isArray(v) ? v[0] : v;
146
+ })();
147
+ const recipientEmail = typeof q.email === 'string' ? q.email : undefined;
148
+ const providedPassword =
149
+ typeof q.password === 'string'
150
+ ? q.password
151
+ : (() => {
152
+ const v = req.headers?.['x-share-password'];
153
+ return Array.isArray(v) ? v[0] : v;
154
+ })();
155
+
156
+ const resolved = await service.resolveToken(req.params.token, {
157
+ signedInUserId,
158
+ recipientEmail,
159
+ providedPassword,
160
+ });
161
+ if (!resolved) {
162
+ // Probe row to give a more useful status code (401 vs 404 vs 410).
163
+ const probe = await engine.find('sys_share_link', {
164
+ where: { token: req.params.token },
165
+ limit: 1,
166
+ context: SYSTEM_CTX,
167
+ } as any);
168
+ const row = Array.isArray(probe) && probe[0] ? (probe[0] as any) : null;
169
+ if (row && !row.revoked_at && (!row.expires_at || Date.parse(row.expires_at) > Date.now())) {
170
+ if (row.password_hash) {
171
+ return sendError(
172
+ res,
173
+ 401,
174
+ providedPassword ? 'WRONG_PASSWORD' : 'NEEDS_PASSWORD',
175
+ providedPassword ? 'Incorrect password' : 'This link requires a password',
176
+ );
177
+ }
178
+ if (row.audience === 'signed_in' && !signedInUserId) {
179
+ return sendError(res, 401, 'SIGN_IN_REQUIRED', 'Please sign in to view this link');
180
+ }
181
+ }
182
+ if (row && (row.revoked_at || (row.expires_at && Date.parse(row.expires_at) <= Date.now()))) {
183
+ return sendError(res, 410, 'EXPIRED_OR_REVOKED', 'Share link has expired or been revoked');
184
+ }
185
+ return sendError(res, 404, 'INVALID_OR_EXPIRED', 'Share link is invalid, expired, or revoked');
186
+ }
187
+
188
+ // Fetch the underlying record with system context — the token
189
+ // gates access, RLS does not.
190
+ const rows = await engine.find(resolved.link.object_name, {
191
+ where: { id: resolved.link.record_id },
192
+ limit: 1,
193
+ context: SYSTEM_CTX,
194
+ } as any);
195
+ const record = Array.isArray(rows) && rows[0] ? rows[0] : null;
196
+ if (!record) {
197
+ return sendError(res, 410, 'RECORD_GONE', 'The shared record no longer exists');
198
+ }
199
+
200
+ await res.json({
201
+ record: applyRedaction(record, resolved.redactFields),
202
+ link: {
203
+ id: resolved.link.id,
204
+ token: resolved.link.token,
205
+ object_name: resolved.link.object_name,
206
+ record_id: resolved.link.record_id,
207
+ permission: resolved.link.permission,
208
+ audience: resolved.link.audience,
209
+ expires_at: resolved.link.expires_at,
210
+ label: resolved.link.label,
211
+ created_at: resolved.link.created_at,
212
+ },
213
+ redactFields: resolved.redactFields,
214
+ });
215
+ } catch (err: any) {
216
+ sendError(res, err?.status ?? 500, err?.code ?? 'INTERNAL', err?.message ?? 'Failed to resolve link');
217
+ }
218
+ }) satisfies RouteHandler);
219
+
220
+ // ──────────────────────────────────────────────────────────────
221
+ // Object-specific related-records lookup.
222
+ //
223
+ // Some objects only make sense alongside their children — most
224
+ // notably `ai_conversations` and the `ai_messages` they own. Rather
225
+ // than baking every relationship into the resolver, we expose a
226
+ // narrow, opt-in `GET /:token/messages` route that:
227
+ //
228
+ // 1. Re-validates the capability token (so revocation / expiry
229
+ // kicks in even after the original resolve).
230
+ // 2. Confirms the shared record really is an `ai_conversations`.
231
+ // 3. Returns the conversation's messages, ordered by creation.
232
+ //
233
+ // Other object kinds can register additional public endpoints
234
+ // following the same pattern.
235
+ // ──────────────────────────────────────────────────────────────
236
+ http.get(`${base}/:token/messages`, (async (req, res) => {
237
+ try {
238
+ const password =
239
+ typeof req.query?.password === 'string' ? (req.query.password as string) : undefined;
240
+ const resolved = await service.resolveToken(req.params.token, { providedPassword: password });
241
+ if (!resolved) {
242
+ sendError(res, 404, 'NOT_FOUND', 'Share link not found');
243
+ return;
244
+ }
245
+ if (resolved.link.object_name !== 'ai_conversations') {
246
+ sendError(res, 400, 'UNSUPPORTED', 'This share link does not expose messages');
247
+ return;
248
+ }
249
+ const SYSTEM_CTX = { isSystem: true, roles: [], permissions: [] } as const;
250
+ const rows = await engine.find('ai_messages', {
251
+ where: { conversation_id: resolved.link.record_id },
252
+ sort: [{ field: 'created_at', direction: 'asc' }],
253
+ limit: 500,
254
+ context: SYSTEM_CTX,
255
+ } as any);
256
+ res.status(200).json({ data: rows ?? [] });
257
+ } catch (err: any) {
258
+ sendError(
259
+ res,
260
+ err?.status ?? 500,
261
+ err?.code ?? 'INTERNAL',
262
+ err?.message ?? 'Failed to load messages',
263
+ );
264
+ }
265
+ }) satisfies RouteHandler);
266
+ }
@@ -0,0 +1,166 @@
1
+ // Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
2
+
3
+ import { describe, it, expect, beforeEach } from 'vitest';
4
+ import { ShareLinkService } from './share-link-service.js';
5
+
6
+ interface FakeRow { [k: string]: any }
7
+
8
+ function makeFakeEngine(schemas: Record<string, any>) {
9
+ const tables: Record<string, FakeRow[]> = {};
10
+ const ensure = (name: string) => (tables[name] ??= []);
11
+
12
+ function matches(row: FakeRow, filter: any): boolean {
13
+ if (!filter || typeof filter !== 'object') return true;
14
+ for (const [k, v] of Object.entries(filter)) {
15
+ if (row[k] !== v) return false;
16
+ }
17
+ return true;
18
+ }
19
+
20
+ return {
21
+ _tables: tables,
22
+ getSchema(name: string) { return schemas[name]; },
23
+ async find(object: string, options?: any) {
24
+ const filter = options?.filter ?? options?.where;
25
+ return ensure(object).filter(r => matches(r, filter));
26
+ },
27
+ async insert(object: string, data: any) {
28
+ const row = { ...data };
29
+ ensure(object).push(row);
30
+ return row;
31
+ },
32
+ async update(object: string, idOrData: any, dataOrOptions?: any) {
33
+ const data = typeof idOrData === 'object' ? idOrData : dataOrOptions;
34
+ const id = typeof idOrData === 'object' ? idOrData.id : idOrData;
35
+ const table = ensure(object);
36
+ const i = table.findIndex(r => r.id === id);
37
+ if (i >= 0) table[i] = { ...table[i], ...data };
38
+ return table[i];
39
+ },
40
+ async delete() { return { id: null }; },
41
+ };
42
+ }
43
+
44
+ const SCHEMAS = {
45
+ sys_share_link: { name: 'sys_share_link', fields: {} },
46
+ // The opt-in target.
47
+ ai_conversations: {
48
+ name: 'ai_conversations',
49
+ publicSharing: {
50
+ enabled: true,
51
+ allowedAudiences: ['link_only', 'signed_in'],
52
+ allowedPermissions: ['view'],
53
+ redactFields: ['metadata'],
54
+ maxExpiryDays: 30,
55
+ },
56
+ fields: { id: {}, title: {}, metadata: {} },
57
+ },
58
+ // Sharing not enabled.
59
+ sys_user: {
60
+ name: 'sys_user',
61
+ fields: { id: {}, email: {} },
62
+ },
63
+ };
64
+
65
+ describe('ShareLinkService', () => {
66
+ let engine: ReturnType<typeof makeFakeEngine>;
67
+ let service: ShareLinkService;
68
+
69
+ beforeEach(() => {
70
+ engine = makeFakeEngine(SCHEMAS);
71
+ // Seed a real conversation row so existence checks pass.
72
+ engine._tables.ai_conversations = [{ id: 'c1', title: 'Demo' }];
73
+ service = new ShareLinkService({ engine: engine as any });
74
+ });
75
+
76
+ it('mints a link for an opt-in object', async () => {
77
+ const link = await service.createLink(
78
+ {
79
+ object: 'ai_conversations',
80
+ recordId: 'c1',
81
+ audience: 'link_only',
82
+ permission: 'view',
83
+ },
84
+ { userId: 'u1' },
85
+ );
86
+ expect(link.token).toMatch(/^[A-Za-z0-9_-]{20,}$/);
87
+ expect(link.permission).toBe('view');
88
+ expect(link.audience).toBe('link_only');
89
+ expect(engine._tables.sys_share_link).toHaveLength(1);
90
+ });
91
+
92
+ it('rejects objects that did not opt in', async () => {
93
+ await expect(
94
+ service.createLink(
95
+ { object: 'sys_user', recordId: 'u1', audience: 'link_only', permission: 'view' },
96
+ { userId: 'u1' },
97
+ ),
98
+ ).rejects.toThrow(/sharing/i);
99
+ });
100
+
101
+ it('rejects a permission outside the allow-list', async () => {
102
+ await expect(
103
+ service.createLink(
104
+ {
105
+ object: 'ai_conversations',
106
+ recordId: 'c1',
107
+ audience: 'link_only',
108
+ permission: 'edit',
109
+ },
110
+ { userId: 'u1' },
111
+ ),
112
+ ).rejects.toThrow(/permission/i);
113
+ });
114
+
115
+ it('resolves a freshly minted token', async () => {
116
+ const link = await service.createLink(
117
+ {
118
+ object: 'ai_conversations',
119
+ recordId: 'c1',
120
+ audience: 'link_only',
121
+ permission: 'view',
122
+ },
123
+ { userId: 'u1' },
124
+ );
125
+ const resolved = await service.resolveToken(link.token);
126
+ expect(resolved).not.toBeNull();
127
+ expect(resolved!.link.record_id).toBe('c1');
128
+ expect(resolved!.redactFields).toContain('metadata');
129
+ });
130
+
131
+ it('returns null for an unknown token', async () => {
132
+ expect(await service.resolveToken('nope-not-a-real-token-xyz')).toBeNull();
133
+ });
134
+
135
+ it('refuses to resolve a revoked token', async () => {
136
+ const link = await service.createLink(
137
+ {
138
+ object: 'ai_conversations',
139
+ recordId: 'c1',
140
+ audience: 'link_only',
141
+ permission: 'view',
142
+ },
143
+ { userId: 'u1' },
144
+ );
145
+ await service.revokeLink(link.id, { userId: 'u1' });
146
+ expect(await service.resolveToken(link.token)).toBeNull();
147
+ });
148
+
149
+ it('refuses to resolve an expired token', async () => {
150
+ // Bypass createLink (it refuses past dates) by inserting directly.
151
+ const past = new Date(Date.now() - 60_000).toISOString();
152
+ engine._tables.sys_share_link = [
153
+ {
154
+ id: 'shl_expired',
155
+ token: 'expired-token-xyz-123',
156
+ object_name: 'ai_conversations',
157
+ record_id: 'c1',
158
+ permission: 'view',
159
+ audience: 'link_only',
160
+ expires_at: past,
161
+ revoked_at: null,
162
+ },
163
+ ];
164
+ expect(await service.resolveToken('expired-token-xyz-123')).toBeNull();
165
+ });
166
+ });