@pairdraw/cloud 0.17.0 → 0.19.0-beta.1
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.d.ts +0 -5
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +0 -7
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +8 -5
- package/dist/contract2.js.map +1 -1
- package/dist/email.js.map +1 -1
- package/dist/index.d.ts +9 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/listen.mjs +202 -110
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-node.d.mts +2299 -661
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/server.d.ts +3319 -985
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +415 -190
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +243 -79
- package/dist/server.mjs.map +1 -1
- package/package.json +2 -2
- package/src/client/api.ts +0 -15
- package/src/contract/documents.ts +8 -0
- package/src/contract/index.ts +1 -3
- package/src/contract/sharing.ts +0 -9
- package/src/runtime/cloudflare/relay.ts +5 -1
- package/src/runtime/cloudflare/worker.ts +4 -1
- package/src/runtime/node/bootstrap.ts +13 -3
- package/src/runtime/node/cleanup.ts +5 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/server.ts +12 -2
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/site/server.ts +19 -2
- package/src/server/api/router.ts +16 -8
- package/src/server/api/types.ts +7 -0
- package/src/server/app.ts +9 -0
- package/src/server/auth/factory.ts +3 -0
- package/src/server/collaboration/relay/hub.ts +42 -3
- package/src/server/collaboration/routes.ts +21 -14
- package/src/server/config/schema.ts +3 -0
- package/src/server/db/expressions.ts +13 -1
- package/src/server/db/migrate.ts +3 -1
- package/src/server/db/migrations/028_invitation_sends.ts +31 -0
- package/src/server/db/tables.ts +9 -0
- package/src/server/documents/access.ts +4 -2
- package/src/server/documents/routes.ts +81 -57
- package/src/server/documents/versions/routes.ts +27 -16
- package/src/server/index.ts +2 -0
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/profiles/routes.ts +16 -12
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/index.ts +4 -1
- package/src/server/rate-limit/middleware.ts +98 -11
- package/src/server/sharing/capabilities/service.ts +27 -2
- package/src/server/sharing/document-routes.ts +30 -22
- package/src/server/sharing/grants/service.ts +13 -7
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +2 -0
- package/src/server/sharing/routes.ts +79 -64
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +76 -0
- package/src/server/workspaces/members.ts +2 -0
- package/src/server/workspaces/routes.ts +56 -35
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type { CloudAPIEnvironment } from '#cloud/server/api/types'
|
|
1
|
+
import type { CloudAPIEnvironment, RateLimitUse } from '#cloud/server/api/types'
|
|
2
2
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
3
|
-
import type { Context, Env, Input } from 'hono'
|
|
3
|
+
import type { Context, Env, Input, MiddlewareHandler } from 'hono'
|
|
4
4
|
import { rateLimiter, type RateLimitExceededEventHandler } from 'hono-rate-limiter'
|
|
5
|
+
import { every } from 'hono/combine'
|
|
5
6
|
import { matchedRoutes } from 'hono/route'
|
|
6
7
|
import type { Kysely } from 'kysely'
|
|
7
8
|
|
|
@@ -14,18 +15,39 @@ export type RateLimitPolicy = {
|
|
|
14
15
|
limit: number
|
|
15
16
|
}
|
|
16
17
|
|
|
18
|
+
/**
|
|
19
|
+
* A budget counted per resource, such as a document, with `actorLimit` capping one actor across
|
|
20
|
+
* every resource, so new resources do not add budget without end.
|
|
21
|
+
*/
|
|
22
|
+
export type ResourceRateLimitPolicy = RateLimitPolicy & { actorLimit: number }
|
|
23
|
+
|
|
17
24
|
export const CLOUD_RATE_LIMITS = {
|
|
18
25
|
publicCapability: { namespace: 'public-capability', windowMs: 60_000, limit: 30 },
|
|
19
|
-
collaborationTicket: {
|
|
20
|
-
|
|
26
|
+
collaborationTicket: {
|
|
27
|
+
namespace: 'collaboration-ticket',
|
|
28
|
+
windowMs: 60_000,
|
|
29
|
+
limit: 60,
|
|
30
|
+
actorLimit: 300
|
|
31
|
+
},
|
|
32
|
+
invitationMutation: {
|
|
33
|
+
namespace: 'invitation-mutation',
|
|
34
|
+
windowMs: 60_000,
|
|
35
|
+
limit: 30,
|
|
36
|
+
actorLimit: 60
|
|
37
|
+
},
|
|
21
38
|
workspaceCreation: { namespace: 'workspace-creation', windowMs: 60_000, limit: 30 },
|
|
22
|
-
documentCreation: {
|
|
23
|
-
|
|
39
|
+
documentCreation: {
|
|
40
|
+
namespace: 'document-creation',
|
|
41
|
+
windowMs: 60_000,
|
|
42
|
+
limit: 60,
|
|
43
|
+
actorLimit: 120
|
|
44
|
+
},
|
|
45
|
+
uploadCreation: { namespace: 'upload-creation', windowMs: 60_000, limit: 120, actorLimit: 600 },
|
|
24
46
|
authenticatedMutation: { namespace: 'authenticated-mutation', windowMs: 60_000, limit: 120 },
|
|
25
47
|
aiToken: { namespace: 'ai-token', windowMs: 60_000, limit: 30 },
|
|
26
48
|
adminRead: { namespace: 'admin-read', windowMs: 60_000, limit: 300 },
|
|
27
49
|
adminMutation: { namespace: 'admin-mutation', windowMs: 60_000, limit: 30 }
|
|
28
|
-
} as const satisfies Record<string, RateLimitPolicy>
|
|
50
|
+
} as const satisfies Record<string, RateLimitPolicy | ResourceRateLimitPolicy>
|
|
29
51
|
|
|
30
52
|
/**
|
|
31
53
|
* The limiters routes mount for their own budget, each with which requests it counts. A general
|
|
@@ -123,15 +145,80 @@ export function createActorRateLimiter(
|
|
|
123
145
|
}
|
|
124
146
|
|
|
125
147
|
/**
|
|
126
|
-
* A limiter a route mounts for its own budget, such as
|
|
148
|
+
* A limiter a route mounts for its own budget, such as workspace creation; general limiters leave
|
|
127
149
|
* the requests it counts to it.
|
|
128
150
|
*/
|
|
129
151
|
export function createRouteRateLimiter(
|
|
130
152
|
database: Kysely<CloudDatabase>,
|
|
131
153
|
secret: string,
|
|
132
154
|
policy: RateLimitPolicy,
|
|
133
|
-
skip?: (context: Context) => boolean
|
|
134
|
-
|
|
155
|
+
skip?: (context: Context) => boolean
|
|
156
|
+
) {
|
|
157
|
+
return ownBudget(createActorRateLimiter(database, secret, policy, skip), skip)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* A route's own budget counted per resource, such as uploads per document, and capped per actor
|
|
162
|
+
* across resources by `actorLimit`; general limiters leave the requests it counts to it.
|
|
163
|
+
*/
|
|
164
|
+
export function createResourceRateLimiter(
|
|
165
|
+
database: Kysely<CloudDatabase>,
|
|
166
|
+
secret: string,
|
|
167
|
+
policy: ResourceRateLimitPolicy,
|
|
168
|
+
resource: (context: Context<CloudAPIEnvironment, '*'>) => string,
|
|
169
|
+
skip?: (context: Context) => boolean
|
|
135
170
|
) {
|
|
136
|
-
|
|
171
|
+
const perResource = createActorRateLimiter(database, secret, policy, skip, resource)
|
|
172
|
+
const perActor = createActorRateLimiter(
|
|
173
|
+
database,
|
|
174
|
+
secret,
|
|
175
|
+
{ namespace: `${policy.namespace}-actor`, windowMs: policy.windowMs, limit: policy.actorLimit },
|
|
176
|
+
skip
|
|
177
|
+
)
|
|
178
|
+
return ownBudget(
|
|
179
|
+
every(perResource, keepResourceUse, perActor, reportTighterBudget(policy.windowMs)),
|
|
180
|
+
skip
|
|
181
|
+
)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Keeps the resource budget's use, which the actor-wide limiter would record over. */
|
|
185
|
+
const keepResourceUse: MiddlewareHandler<CloudAPIEnvironment> = async (context, next) => {
|
|
186
|
+
context.set('resourceRateLimit', context.get('rateLimit'))
|
|
187
|
+
await next()
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Reports whichever budget has fewer requests left in the `RateLimit` headers, as both limiters
|
|
192
|
+
* write them and the last would otherwise win. A refused request keeps the headers of the budget
|
|
193
|
+
* that refused it.
|
|
194
|
+
*/
|
|
195
|
+
function reportTighterBudget(windowMs: number): MiddlewareHandler<CloudAPIEnvironment> {
|
|
196
|
+
return async (context, next) => {
|
|
197
|
+
const resource = context.get('resourceRateLimit')
|
|
198
|
+
const actor = context.get('rateLimit')
|
|
199
|
+
const tighter = resource && actor && resource.remaining < actor.remaining ? resource : actor
|
|
200
|
+
if (tighter) setBudgetHeaders(context, tighter, windowMs)
|
|
201
|
+
await next()
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** The draft-7 `RateLimit` headers `hono-rate-limiter` writes, for one budget's use. */
|
|
206
|
+
function setBudgetHeaders(context: Context, use: RateLimitUse, windowMs: number): void {
|
|
207
|
+
const windowSeconds = Math.ceil(windowMs / 1000)
|
|
208
|
+
const reset = use.resetTime
|
|
209
|
+
? Math.max(0, Math.ceil((use.resetTime.getTime() - Date.now()) / 1000))
|
|
210
|
+
: windowSeconds
|
|
211
|
+
context.header('RateLimit-Policy', `${use.limit};w=${windowSeconds}`)
|
|
212
|
+
context.header('RateLimit', `limit=${use.limit}, remaining=${use.remaining}, reset=${reset}`)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* A limiter's resource named by a route parameter, such as the document a request uploads to. It
|
|
217
|
+
* runs before the route's param validator, and the value only names the limiter's counter, which
|
|
218
|
+
* `rateLimitKey` hashes; it never reaches a query.
|
|
219
|
+
*/
|
|
220
|
+
export function routeParamResource(name: string) {
|
|
221
|
+
return (context: Context): string =>
|
|
222
|
+
// eslint-disable-next-line pairdraw/no-unvalidated-route-params -- a counter name, hashed, never queried.
|
|
223
|
+
context.req.param(name) ?? 'unknown'
|
|
137
224
|
}
|
|
@@ -12,6 +12,7 @@ import { DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
|
12
12
|
import { CLOUD_FEATURE_KEYS } from '#cloud/server/policy/keys'
|
|
13
13
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
14
14
|
import type { Kysely, Transaction, UpdateObject } from 'kysely'
|
|
15
|
+
import * as v from 'valibot'
|
|
15
16
|
|
|
16
17
|
import { randomHex } from '@pairdraw/scene-graph/random'
|
|
17
18
|
|
|
@@ -27,6 +28,27 @@ import { createSharingPolicy } from '../policy'
|
|
|
27
28
|
|
|
28
29
|
const SHARE_SECRET_BYTES = 24
|
|
29
30
|
|
|
31
|
+
/** An account ID as the user table keys it; other IDs in `createdBy` name no account. */
|
|
32
|
+
const accountIdSchema = v.pipe(v.string(), v.uuid())
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Whether any of these people is banned, for a ban that has not run out. A link stops working
|
|
36
|
+
* while whoever made it, or made its document, is banned, so what a banned person put up is no
|
|
37
|
+
* longer public. The IDs are matched as the user table keys them, so the lookup uses its index.
|
|
38
|
+
*/
|
|
39
|
+
async function anyBanned(database: Kysely<CloudDatabase>, userIds: string[]): Promise<boolean> {
|
|
40
|
+
const accounts = userIds.filter((id) => v.is(accountIdSchema, id))
|
|
41
|
+
if (accounts.length === 0) return false
|
|
42
|
+
const banned = await database
|
|
43
|
+
.selectFrom('user')
|
|
44
|
+
.select('id')
|
|
45
|
+
.where('id', 'in', accounts)
|
|
46
|
+
.where('banned', '=', true)
|
|
47
|
+
.where((user) => user.or([user('banExpires', 'is', null), user('banExpires', '>', new Date())]))
|
|
48
|
+
.executeTakeFirst()
|
|
49
|
+
return banned !== undefined
|
|
50
|
+
}
|
|
51
|
+
|
|
30
52
|
export type DocumentShareCapability = {
|
|
31
53
|
share: DocumentShare
|
|
32
54
|
secret: string
|
|
@@ -257,7 +279,9 @@ export function createCapabilityService(
|
|
|
257
279
|
'documentShare.secretHash',
|
|
258
280
|
'documentShare.roomEpoch',
|
|
259
281
|
'documentShare.expiresAt',
|
|
260
|
-
'documentShare.revisionId'
|
|
282
|
+
'documentShare.revisionId',
|
|
283
|
+
'documentShare.createdBy as linkMaker',
|
|
284
|
+
'document.createdBy as documentMaker'
|
|
261
285
|
])
|
|
262
286
|
.where('documentShare.id', '=', shareId)
|
|
263
287
|
.where('documentShare.revokedAt', 'is', null)
|
|
@@ -266,7 +290,8 @@ export function createCapabilityService(
|
|
|
266
290
|
if (
|
|
267
291
|
!row ||
|
|
268
292
|
!capabilityHashMatches(row.secretHash, input.secret) ||
|
|
269
|
-
(row.expiresAt && new Date(row.expiresAt).getTime() <= Date.now())
|
|
293
|
+
(row.expiresAt && new Date(row.expiresAt).getTime() <= Date.now()) ||
|
|
294
|
+
(await anyBanned(database, [row.linkMaker, row.documentMaker]))
|
|
270
295
|
) {
|
|
271
296
|
throw new DocumentShareInvalidError()
|
|
272
297
|
}
|
|
@@ -3,7 +3,7 @@ import type { CloudSessionResolver } from '#cloud/server/auth'
|
|
|
3
3
|
import type { DocumentService } from '#cloud/server/documents'
|
|
4
4
|
import type { DocumentSharingService } from '#cloud/server/sharing'
|
|
5
5
|
import { sharingRoute } from '#cloud/server/sharing/http'
|
|
6
|
-
import { validatedJSON } from '#cloud/server/validation'
|
|
6
|
+
import { shareParams, validatedJSON } from '#cloud/server/validation'
|
|
7
7
|
import { Hono } from 'hono'
|
|
8
8
|
|
|
9
9
|
import { shareActor } from './actor'
|
|
@@ -14,28 +14,36 @@ export function createPublicDocumentRoutes(
|
|
|
14
14
|
resolveSession?: CloudSessionResolver
|
|
15
15
|
) {
|
|
16
16
|
return new Hono()
|
|
17
|
-
.post(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
17
|
+
.post(
|
|
18
|
+
'/shares/:shareId/document',
|
|
19
|
+
shareParams,
|
|
20
|
+
validatedJSON(parseResolveDocumentShare),
|
|
21
|
+
(context) =>
|
|
22
|
+
sharingRoute(context, async () => {
|
|
23
|
+
const resolution = await sharing.resolveShare(
|
|
24
|
+
context.req.param('shareId'),
|
|
25
|
+
context.req.valid('json'),
|
|
26
|
+
await shareActor(context.req.raw, resolveSession)
|
|
27
|
+
)
|
|
28
|
+
return context.json({
|
|
29
|
+
resolution,
|
|
30
|
+
document: await documents.downloadShared(resolution.documentId, resolution.revisionId)
|
|
31
|
+
})
|
|
27
32
|
})
|
|
28
|
-
})
|
|
29
33
|
)
|
|
30
|
-
.post(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
.post(
|
|
35
|
+
'/shares/:shareId/images',
|
|
36
|
+
shareParams,
|
|
37
|
+
validatedJSON(parseRequestSharedImageLinks),
|
|
38
|
+
(context) =>
|
|
39
|
+
sharingRoute(context, async () => {
|
|
40
|
+
const { checksums, ...share } = context.req.valid('json')
|
|
41
|
+
const resolution = await sharing.resolveShare(
|
|
42
|
+
context.req.param('shareId'),
|
|
43
|
+
share,
|
|
44
|
+
await shareActor(context.req.raw, resolveSession)
|
|
45
|
+
)
|
|
46
|
+
return context.json(await documents.imageLinksShared(resolution.documentId, checksums))
|
|
47
|
+
})
|
|
40
48
|
)
|
|
41
49
|
}
|
|
@@ -27,6 +27,9 @@ export function createGrantService(database: Kysely<CloudDatabase>) {
|
|
|
27
27
|
input: PutDocumentGrantInput
|
|
28
28
|
): Promise<DocumentGrant> {
|
|
29
29
|
await requireSharingAccess(database, userId, documentId)
|
|
30
|
+
// A target with no grant row is granted now. One that already has a live grant is changed,
|
|
31
|
+
// and one whose grant was revoked stays revoked rather than coming back by itself, so the
|
|
32
|
+
// conflict branch touches nothing and the update returns no row.
|
|
30
33
|
const row = await database
|
|
31
34
|
.insertInto('documentGrant')
|
|
32
35
|
.values({
|
|
@@ -38,12 +41,14 @@ export function createGrantService(database: Kysely<CloudDatabase>) {
|
|
|
38
41
|
revokedAt: null
|
|
39
42
|
})
|
|
40
43
|
.onConflict((conflict) =>
|
|
41
|
-
conflict
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
conflict
|
|
45
|
+
.columns(['documentId', 'userId'])
|
|
46
|
+
.doUpdateSet({
|
|
47
|
+
permission: input.permission,
|
|
48
|
+
createdBy: userId,
|
|
49
|
+
updatedAt: new Date()
|
|
50
|
+
})
|
|
51
|
+
.where('documentGrant.revokedAt', 'is', null)
|
|
47
52
|
)
|
|
48
53
|
.returning([
|
|
49
54
|
'id',
|
|
@@ -54,7 +59,8 @@ export function createGrantService(database: Kysely<CloudDatabase>) {
|
|
|
54
59
|
'createdAt',
|
|
55
60
|
'updatedAt'
|
|
56
61
|
])
|
|
57
|
-
.
|
|
62
|
+
.executeTakeFirst()
|
|
63
|
+
if (!row) throw new DocumentNotFoundError()
|
|
58
64
|
return grantContract(row)
|
|
59
65
|
},
|
|
60
66
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DocumentForbiddenError, DocumentNotFoundError } from '#cloud/server/documents'
|
|
2
|
+
import { InvitationLimitError } from '#cloud/server/invitations'
|
|
2
3
|
import {
|
|
3
4
|
DocumentShareInvalidError,
|
|
4
5
|
DocumentShareKindError,
|
|
@@ -25,6 +26,9 @@ function sharingDomainError(context: Context, error: unknown): Response | null {
|
|
|
25
26
|
if (error instanceof InvitationDeliveryError) {
|
|
26
27
|
return context.json({ error: { code: 'invitation_delivery_failed' as const } }, 502)
|
|
27
28
|
}
|
|
29
|
+
if (error instanceof InvitationLimitError) {
|
|
30
|
+
return context.json({ error: { code: 'rate_limited' as const } }, 429)
|
|
31
|
+
}
|
|
28
32
|
if (error instanceof DocumentForbiddenError) {
|
|
29
33
|
return context.json({ error: { code: 'forbidden' as const } }, 403)
|
|
30
34
|
}
|
|
@@ -12,6 +12,7 @@ import type { CloudActor } from '#cloud/server/auth'
|
|
|
12
12
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
13
13
|
import { DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
14
14
|
import type { InvitationDelivery, InvitationOutbox } from '#cloud/server/invitations'
|
|
15
|
+
import { spendInvitationBudget } from '#cloud/server/invitations'
|
|
15
16
|
import type { Kysely } from 'kysely'
|
|
16
17
|
|
|
17
18
|
import { randomHex } from '@pairdraw/scene-graph/random'
|
|
@@ -161,6 +162,7 @@ export function createInvitationService(
|
|
|
161
162
|
}
|
|
162
163
|
: null
|
|
163
164
|
const row = await database.transaction().execute(async (transaction) => {
|
|
165
|
+
await spendInvitationBudget(transaction, { senderId: userId, email: input.email })
|
|
164
166
|
const inserted = await transaction
|
|
165
167
|
.insertInto('documentInvitation')
|
|
166
168
|
.values({
|
|
@@ -2,7 +2,6 @@ import {
|
|
|
2
2
|
parseAcceptDocumentInvitation,
|
|
3
3
|
parseCreateInvitationContinuation,
|
|
4
4
|
parseCreateDocumentInvitation,
|
|
5
|
-
parseLookupCloudUser,
|
|
6
5
|
parseCreateDocumentShare,
|
|
7
6
|
parsePutDocumentGrant,
|
|
8
7
|
parseResolveDocumentShare,
|
|
@@ -10,10 +9,18 @@ import {
|
|
|
10
9
|
} from '#cloud/contract'
|
|
11
10
|
import type { CloudActor, CloudSessionResolver } from '#cloud/server/auth'
|
|
12
11
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
13
|
-
import { CLOUD_RATE_LIMITS,
|
|
12
|
+
import { CLOUD_RATE_LIMITS, createResourceRateLimiter } from '#cloud/server/rate-limit'
|
|
14
13
|
import { sharingRoute } from '#cloud/server/sharing/http'
|
|
15
14
|
import type { DocumentSharingService } from '#cloud/server/sharing/service'
|
|
16
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
documentInvitationParams,
|
|
17
|
+
documentParams,
|
|
18
|
+
documentShareParams,
|
|
19
|
+
documentUserParams,
|
|
20
|
+
invitationParams,
|
|
21
|
+
shareParams,
|
|
22
|
+
validatedJSON
|
|
23
|
+
} from '#cloud/server/validation'
|
|
17
24
|
import { Hono } from 'hono'
|
|
18
25
|
import type { Kysely } from 'kysely'
|
|
19
26
|
|
|
@@ -31,28 +38,17 @@ export function createDocumentSharingRoutes(
|
|
|
31
38
|
if (rateLimit) {
|
|
32
39
|
router.use(
|
|
33
40
|
'/documents/:documentId/invitations',
|
|
34
|
-
|
|
41
|
+
createResourceRateLimiter(
|
|
35
42
|
rateLimit.database,
|
|
36
43
|
rateLimit.secret,
|
|
37
44
|
CLOUD_RATE_LIMITS.invitationMutation,
|
|
38
|
-
(context) => context.req.
|
|
39
|
-
(context) => context.req.
|
|
45
|
+
(context) => context.req.param('documentId') ?? 'unknown',
|
|
46
|
+
(context) => context.req.method !== 'POST'
|
|
40
47
|
)
|
|
41
48
|
)
|
|
42
49
|
}
|
|
43
50
|
return router
|
|
44
|
-
.
|
|
45
|
-
sharingRoute(context, async () =>
|
|
46
|
-
context.json({
|
|
47
|
-
user: await service.lookupUser(
|
|
48
|
-
context.get('actor').userId,
|
|
49
|
-
context.req.param('documentId'),
|
|
50
|
-
context.req.valid('json')
|
|
51
|
-
)
|
|
52
|
-
})
|
|
53
|
-
)
|
|
54
|
-
)
|
|
55
|
-
.get('/documents/:documentId/users/:userId', (context) =>
|
|
51
|
+
.get('/documents/:documentId/users/:userId', documentUserParams, (context) =>
|
|
56
52
|
sharingRoute(context, async () =>
|
|
57
53
|
context.json({
|
|
58
54
|
user: await service.userProfile(
|
|
@@ -63,7 +59,7 @@ export function createDocumentSharingRoutes(
|
|
|
63
59
|
})
|
|
64
60
|
)
|
|
65
61
|
)
|
|
66
|
-
.get('/documents/:documentId/shares', (context) =>
|
|
62
|
+
.get('/documents/:documentId/shares', documentParams, (context) =>
|
|
67
63
|
sharingRoute(context, async () =>
|
|
68
64
|
context.json({
|
|
69
65
|
shares: await service.listShares(
|
|
@@ -73,20 +69,25 @@ export function createDocumentSharingRoutes(
|
|
|
73
69
|
})
|
|
74
70
|
)
|
|
75
71
|
)
|
|
76
|
-
.post(
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
72
|
+
.post(
|
|
73
|
+
'/documents/:documentId/shares',
|
|
74
|
+
documentParams,
|
|
75
|
+
validatedJSON(parseCreateDocumentShare),
|
|
76
|
+
(context) =>
|
|
77
|
+
sharingRoute(context, async () =>
|
|
78
|
+
context.json(
|
|
79
|
+
await service.createShare(
|
|
80
|
+
context.get('actor').userId,
|
|
81
|
+
context.req.param('documentId'),
|
|
82
|
+
context.req.valid('json')
|
|
83
|
+
),
|
|
84
|
+
201
|
|
85
|
+
)
|
|
85
86
|
)
|
|
86
|
-
)
|
|
87
87
|
)
|
|
88
88
|
.patch(
|
|
89
89
|
'/documents/:documentId/shares/:shareId',
|
|
90
|
+
documentShareParams,
|
|
90
91
|
validatedJSON(parseUpdateDocumentShare),
|
|
91
92
|
(context) =>
|
|
92
93
|
sharingRoute(context, async () =>
|
|
@@ -100,7 +101,7 @@ export function createDocumentSharingRoutes(
|
|
|
100
101
|
})
|
|
101
102
|
)
|
|
102
103
|
)
|
|
103
|
-
.post('/documents/:documentId/shares/:shareId/rotate', (context) =>
|
|
104
|
+
.post('/documents/:documentId/shares/:shareId/rotate', documentShareParams, (context) =>
|
|
104
105
|
sharingRoute(context, async () =>
|
|
105
106
|
context.json(
|
|
106
107
|
await service.rotateShare(
|
|
@@ -111,7 +112,7 @@ export function createDocumentSharingRoutes(
|
|
|
111
112
|
)
|
|
112
113
|
)
|
|
113
114
|
)
|
|
114
|
-
.delete('/documents/:documentId/shares/:shareId', (context) =>
|
|
115
|
+
.delete('/documents/:documentId/shares/:shareId', documentShareParams, (context) =>
|
|
115
116
|
sharingRoute(context, async () => {
|
|
116
117
|
await service.revokeShare(
|
|
117
118
|
context.get('actor').userId,
|
|
@@ -121,7 +122,7 @@ export function createDocumentSharingRoutes(
|
|
|
121
122
|
return context.body(null, 204)
|
|
122
123
|
})
|
|
123
124
|
)
|
|
124
|
-
.get('/documents/:documentId/grants', (context) =>
|
|
125
|
+
.get('/documents/:documentId/grants', documentParams, (context) =>
|
|
125
126
|
sharingRoute(context, async () =>
|
|
126
127
|
context.json({
|
|
127
128
|
grants: await service.listGrants(
|
|
@@ -131,19 +132,23 @@ export function createDocumentSharingRoutes(
|
|
|
131
132
|
})
|
|
132
133
|
)
|
|
133
134
|
)
|
|
134
|
-
.put(
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
135
|
+
.put(
|
|
136
|
+
'/documents/:documentId/grants/:userId',
|
|
137
|
+
documentUserParams,
|
|
138
|
+
validatedJSON(parsePutDocumentGrant),
|
|
139
|
+
(context) =>
|
|
140
|
+
sharingRoute(context, async () =>
|
|
141
|
+
context.json({
|
|
142
|
+
grant: await service.putGrant(
|
|
143
|
+
context.get('actor').userId,
|
|
144
|
+
context.req.param('documentId'),
|
|
145
|
+
context.req.param('userId'),
|
|
146
|
+
context.req.valid('json')
|
|
147
|
+
)
|
|
148
|
+
})
|
|
149
|
+
)
|
|
145
150
|
)
|
|
146
|
-
.delete('/documents/:documentId/grants/:userId', (context) =>
|
|
151
|
+
.delete('/documents/:documentId/grants/:userId', documentUserParams, (context) =>
|
|
147
152
|
sharingRoute(context, async () => {
|
|
148
153
|
await service.revokeGrant(
|
|
149
154
|
context.get('actor').userId,
|
|
@@ -153,7 +158,7 @@ export function createDocumentSharingRoutes(
|
|
|
153
158
|
return context.body(null, 204)
|
|
154
159
|
})
|
|
155
160
|
)
|
|
156
|
-
.get('/documents/:documentId/invitations', (context) =>
|
|
161
|
+
.get('/documents/:documentId/invitations', documentParams, (context) =>
|
|
157
162
|
sharingRoute(context, async () =>
|
|
158
163
|
context.json({
|
|
159
164
|
invitations: await service.listInvitations(
|
|
@@ -165,6 +170,7 @@ export function createDocumentSharingRoutes(
|
|
|
165
170
|
)
|
|
166
171
|
.post(
|
|
167
172
|
'/documents/:documentId/invitations',
|
|
173
|
+
documentParams,
|
|
168
174
|
validatedJSON(parseCreateDocumentInvitation),
|
|
169
175
|
(context) =>
|
|
170
176
|
sharingRoute(context, async () =>
|
|
@@ -180,6 +186,7 @@ export function createDocumentSharingRoutes(
|
|
|
180
186
|
)
|
|
181
187
|
.post(
|
|
182
188
|
'/invitations/:invitationId/accept',
|
|
189
|
+
invitationParams,
|
|
183
190
|
validatedJSON(parseAcceptDocumentInvitation),
|
|
184
191
|
(context) =>
|
|
185
192
|
sharingRoute(context, async () =>
|
|
@@ -192,15 +199,18 @@ export function createDocumentSharingRoutes(
|
|
|
192
199
|
})
|
|
193
200
|
)
|
|
194
201
|
)
|
|
195
|
-
.delete(
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
202
|
+
.delete(
|
|
203
|
+
'/documents/:documentId/invitations/:invitationId',
|
|
204
|
+
documentInvitationParams,
|
|
205
|
+
(context) =>
|
|
206
|
+
sharingRoute(context, async () => {
|
|
207
|
+
await service.revokeInvitation(
|
|
208
|
+
context.get('actor').userId,
|
|
209
|
+
context.req.param('documentId'),
|
|
210
|
+
context.req.param('invitationId')
|
|
211
|
+
)
|
|
212
|
+
return context.body(null, 204)
|
|
213
|
+
})
|
|
204
214
|
)
|
|
205
215
|
}
|
|
206
216
|
|
|
@@ -226,6 +236,7 @@ export function createPublicSharingRoutes(
|
|
|
226
236
|
)
|
|
227
237
|
.post(
|
|
228
238
|
'/invitations/:invitationId/preview',
|
|
239
|
+
invitationParams,
|
|
229
240
|
validatedJSON(parseAcceptDocumentInvitation),
|
|
230
241
|
(context) =>
|
|
231
242
|
sharingRoute(context, async () =>
|
|
@@ -237,15 +248,19 @@ export function createPublicSharingRoutes(
|
|
|
237
248
|
})
|
|
238
249
|
)
|
|
239
250
|
)
|
|
240
|
-
.post(
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
251
|
+
.post(
|
|
252
|
+
'/shares/:shareId/resolve',
|
|
253
|
+
shareParams,
|
|
254
|
+
validatedJSON(parseResolveDocumentShare),
|
|
255
|
+
(context) =>
|
|
256
|
+
sharingRoute(context, async () =>
|
|
257
|
+
context.json({
|
|
258
|
+
resolution: await service.resolveShare(
|
|
259
|
+
context.req.param('shareId'),
|
|
260
|
+
context.req.valid('json'),
|
|
261
|
+
await shareActor(context.req.raw, resolveSession)
|
|
262
|
+
)
|
|
263
|
+
})
|
|
264
|
+
)
|
|
250
265
|
)
|
|
251
266
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CloudUserProfile
|
|
1
|
+
import type { CloudUserProfile } from '#cloud/contract'
|
|
2
2
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
3
3
|
import type { InvitationDelivery, InvitationOutbox } from '#cloud/server/invitations'
|
|
4
4
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
@@ -35,22 +35,6 @@ export function createDocumentSharingService(
|
|
|
35
35
|
const invitations = createInvitationService(database, options)
|
|
36
36
|
|
|
37
37
|
return {
|
|
38
|
-
async lookupUser(
|
|
39
|
-
userId: string,
|
|
40
|
-
documentId: string,
|
|
41
|
-
input: LookupCloudUserInput
|
|
42
|
-
): Promise<CloudUserProfile | null> {
|
|
43
|
-
await requireSharingAccess(database, userId, documentId)
|
|
44
|
-
const user = await database
|
|
45
|
-
.selectFrom('user')
|
|
46
|
-
.select(['id', 'name', 'email', 'image'])
|
|
47
|
-
.where((expression) =>
|
|
48
|
-
expression(expression.fn('lower', [expression.ref('email')]), '=', input.email)
|
|
49
|
-
)
|
|
50
|
-
.executeTakeFirst()
|
|
51
|
-
return user ?? null
|
|
52
|
-
},
|
|
53
|
-
|
|
54
38
|
async userProfile(
|
|
55
39
|
userId: string,
|
|
56
40
|
documentId: string,
|