@open-pencil/cloud 0.0.0-stage → 0.17.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/LICENSE +202 -0
- package/LICENSE-MIT +21 -0
- package/NOTICE +7 -0
- package/README.md +37 -2
- package/dist/bin/ai-relay.d.mts +1 -0
- package/dist/bin/ai-relay.mjs +31 -0
- package/dist/bin/ai-relay.mjs.map +1 -0
- package/dist/bin/server.d.mts +1 -0
- package/dist/bin/server.mjs +29 -0
- package/dist/bin/server.mjs.map +1 -0
- package/dist/client.d.ts +1520 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +1917 -0
- package/dist/client.js.map +1 -0
- package/dist/contract.d.ts +2 -0
- package/dist/contract.js +2 -0
- package/dist/contract2.js +1453 -0
- package/dist/contract2.js.map +1 -0
- package/dist/email.d.ts +169 -0
- package/dist/email.d.ts.map +1 -0
- package/dist/email.js +670 -0
- package/dist/email.js.map +1 -0
- package/dist/index.d.ts +1755 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/listen.mjs +1881 -0
- package/dist/listen.mjs.map +1 -0
- package/dist/runtime-node.d.mts +10040 -0
- package/dist/runtime-node.d.mts.map +1 -0
- package/dist/runtime-node.mjs +32 -0
- package/dist/runtime-node.mjs.map +1 -0
- package/dist/server.d.ts +12792 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +8475 -0
- package/dist/server.js.map +1 -0
- package/dist/server.mjs +8324 -0
- package/dist/server.mjs.map +1 -0
- package/dist/server2.mjs +505 -0
- package/dist/server2.mjs.map +1 -0
- package/package.json +124 -4
- package/src/admin/api/routes.ts +223 -0
- package/src/admin/api/security.ts +12 -0
- package/src/admin/audit/service.ts +138 -0
- package/src/admin/email/service.ts +140 -0
- package/src/admin/enrollment/email.ts +77 -0
- package/src/admin/enrollment/service.ts +308 -0
- package/src/admin/errors.ts +20 -0
- package/src/admin/index.ts +19 -0
- package/src/admin/list.ts +61 -0
- package/src/admin/operations/service.ts +70 -0
- package/src/admin/schema.ts +28 -0
- package/src/admin/users/service.ts +369 -0
- package/src/ai-relay/app.ts +251 -0
- package/src/ai-relay/config.ts +120 -0
- package/src/ai-relay/db/index.ts +10 -0
- package/src/ai-relay/db/migrate.ts +32 -0
- package/src/ai-relay/db/migrations/001_usage.ts +26 -0
- package/src/ai-relay/db/schema.ts +20 -0
- package/src/ai-relay/index.ts +18 -0
- package/src/ai-relay/metering.ts +68 -0
- package/src/ai-relay/usage.ts +115 -0
- package/src/client/ai.ts +96 -0
- package/src/client/api.ts +528 -0
- package/src/client/auth/client.ts +188 -0
- package/src/client/auth/failure.ts +54 -0
- package/src/client/auth/navigation.ts +45 -0
- package/src/client/device-authorization.ts +134 -0
- package/src/client/discovery.ts +105 -0
- package/src/client/images.ts +81 -0
- package/src/client/index.ts +53 -0
- package/src/client/portal/account.ts +183 -0
- package/src/client/portal/administration.ts +130 -0
- package/src/client/portal/client.ts +18 -0
- package/src/client/portal/request.ts +115 -0
- package/src/client/response.ts +51 -0
- package/src/client/workspaces.ts +142 -0
- package/src/contract/account.ts +204 -0
- package/src/contract/admin/index.ts +187 -0
- package/src/contract/ai.ts +150 -0
- package/src/contract/collaboration.ts +45 -0
- package/src/contract/discovery.ts +81 -0
- package/src/contract/documents.ts +265 -0
- package/src/contract/entitlements.ts +28 -0
- package/src/contract/index.ts +204 -0
- package/src/contract/links.ts +87 -0
- package/src/contract/portal.ts +11 -0
- package/src/contract/relay.ts +368 -0
- package/src/contract/sharing.ts +191 -0
- package/src/contract/versions.ts +87 -0
- package/src/contract/workspaces.ts +100 -0
- package/src/email/index.ts +14 -0
- package/src/email/render.ts +276 -0
- package/src/email/schema.ts +144 -0
- package/src/email/templates/brand.ts +105 -0
- package/src/email/templates/document-invitation.ts +35 -0
- package/src/email/templates/enrollment.ts +27 -0
- package/src/email/templates/layout.ts +18 -0
- package/src/runtime/cloudflare/email.ts +70 -0
- package/src/runtime/cloudflare/environment.ts +57 -0
- package/src/runtime/cloudflare/index.ts +3 -0
- package/src/runtime/cloudflare/relay.ts +136 -0
- package/src/runtime/cloudflare/site.ts +13 -0
- package/src/runtime/cloudflare/tsconfig.json +21 -0
- package/src/runtime/cloudflare/worker.ts +160 -0
- package/src/runtime/node/admin.ts +59 -0
- package/src/runtime/node/ai-relay/main.ts +35 -0
- package/src/runtime/node/ai-relay/server.ts +63 -0
- package/src/runtime/node/application.ts +50 -0
- package/src/runtime/node/bootstrap.ts +22 -0
- package/src/runtime/node/cleanup.ts +24 -0
- package/src/runtime/node/command.ts +17 -0
- package/src/runtime/node/config.ts +16 -0
- package/src/runtime/node/email/runtime.ts +43 -0
- package/src/runtime/node/email/transport.ts +112 -0
- package/src/runtime/node/entitlements/main.ts +41 -0
- package/src/runtime/node/entitlements/refusal.ts +15 -0
- package/src/runtime/node/index.ts +25 -0
- package/src/runtime/node/listen.ts +35 -0
- package/src/runtime/node/main.ts +29 -0
- package/src/runtime/node/migrate.ts +13 -0
- package/src/runtime/node/quota-reconcile.ts +9 -0
- package/src/runtime/node/relay.ts +54 -0
- package/src/runtime/node/server.ts +121 -0
- package/src/runtime/node/site.ts +64 -0
- package/src/runtime/postgres/database.ts +19 -0
- package/src/runtime/s3/objects.ts +244 -0
- package/src/runtime/site/server.ts +82 -0
- package/src/server/account/actions.ts +66 -0
- package/src/server/account/routes.ts +300 -0
- package/src/server/ai/errors.ts +7 -0
- package/src/server/ai/index.ts +4 -0
- package/src/server/ai/keys.ts +43 -0
- package/src/server/ai/routes.ts +43 -0
- package/src/server/ai/service.ts +70 -0
- package/src/server/api/index.ts +8 -0
- package/src/server/api/router.ts +117 -0
- package/src/server/api/types.ts +7 -0
- package/src/server/app.ts +375 -0
- package/src/server/auth/account.ts +73 -0
- package/src/server/auth/activity.ts +213 -0
- package/src/server/auth/adapter.ts +79 -0
- package/src/server/auth/email.ts +125 -0
- package/src/server/auth/endpoints.ts +57 -0
- package/src/server/auth/factory.ts +583 -0
- package/src/server/auth/index.ts +17 -0
- package/src/server/auth/mfa-assurance.ts +55 -0
- package/src/server/auth/runtime.ts +26 -0
- package/src/server/auth/session.ts +19 -0
- package/src/server/auth/social-providers.ts +55 -0
- package/src/server/auth/user-agent.ts +16 -0
- package/src/server/auth/workspace-access.ts +23 -0
- package/src/server/cleanup/configured.ts +38 -0
- package/src/server/cleanup/documents.ts +231 -0
- package/src/server/cleanup/index.ts +29 -0
- package/src/server/cleanup/objects.ts +27 -0
- package/src/server/cleanup/revisions.ts +232 -0
- package/src/server/cleanup/uploads.ts +129 -0
- package/src/server/cleanup/worker.ts +79 -0
- package/src/server/collaboration/index.ts +28 -0
- package/src/server/collaboration/relay/authorize.ts +53 -0
- package/src/server/collaboration/relay/awareness.ts +104 -0
- package/src/server/collaboration/relay/documents.ts +39 -0
- package/src/server/collaboration/relay/hub.ts +524 -0
- package/src/server/collaboration/relay/index.ts +15 -0
- package/src/server/collaboration/relay/persistence.ts +48 -0
- package/src/server/collaboration/routes.ts +77 -0
- package/src/server/collaboration/service.ts +188 -0
- package/src/server/config/deployment.ts +178 -0
- package/src/server/config/index.ts +20 -0
- package/src/server/config/limits.ts +8 -0
- package/src/server/config/public.ts +66 -0
- package/src/server/config/schema.ts +512 -0
- package/src/server/db/database.ts +15 -0
- package/src/server/db/expressions.ts +13 -0
- package/src/server/db/index.ts +21 -0
- package/src/server/db/migrate.ts +177 -0
- package/src/server/db/migrations/001_foundation.ts +164 -0
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +34 -0
- package/src/server/db/migrations/003_document_cleanup_claims.ts +23 -0
- package/src/server/db/migrations/004_document_sharing.ts +82 -0
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +13 -0
- package/src/server/db/migrations/006_invitation_continuation.ts +27 -0
- package/src/server/db/migrations/007_collaboration_state.ts +25 -0
- package/src/server/db/migrations/008_storage_reservations.ts +37 -0
- package/src/server/db/migrations/009_workspace_entitlements.ts +22 -0
- package/src/server/db/migrations/010_upload_finalization.ts +22 -0
- package/src/server/db/migrations/011_upload_finalization_lease.ts +12 -0
- package/src/server/db/migrations/012_transactional_email.ts +48 -0
- package/src/server/db/migrations/013_admin_enrollment.ts +54 -0
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +30 -0
- package/src/server/db/migrations/015_cloud_rate_limit.ts +22 -0
- package/src/server/db/migrations/016_auth_schema.ts +16 -0
- package/src/server/db/migrations/017_authentication_email_kinds.ts +32 -0
- package/src/server/db/migrations/018_authentication_mfa.ts +78 -0
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +42 -0
- package/src/server/db/migrations/020_workspace_invitations.ts +57 -0
- package/src/server/db/migrations/021_document_versions.ts +66 -0
- package/src/server/db/migrations/022_entitlement_external_version.ts +17 -0
- package/src/server/db/migrations/023_document_images.ts +61 -0
- package/src/server/db/migrations/024_account_activity.ts +66 -0
- package/src/server/db/migrations/025_table_names.ts +58 -0
- package/src/server/db/migrations/026_user_picture.ts +30 -0
- package/src/server/db/migrations/027_document_kind.ts +19 -0
- package/src/server/db/migrations/helpers.ts +78 -0
- package/src/server/db/tables.ts +394 -0
- package/src/server/documents/access.ts +103 -0
- package/src/server/documents/errors.ts +16 -0
- package/src/server/documents/images.ts +407 -0
- package/src/server/documents/index.ts +14 -0
- package/src/server/documents/limits.ts +57 -0
- package/src/server/documents/repository.ts +141 -0
- package/src/server/documents/routes.ts +239 -0
- package/src/server/documents/service.ts +597 -0
- package/src/server/documents/summary.ts +37 -0
- package/src/server/documents/types.ts +14 -0
- package/src/server/documents/versions/checkpoint.ts +98 -0
- package/src/server/documents/versions/history.ts +25 -0
- package/src/server/documents/versions/index.ts +4 -0
- package/src/server/documents/versions/routes.ts +112 -0
- package/src/server/documents/versions/service.ts +397 -0
- package/src/server/email/crypto.ts +57 -0
- package/src/server/email/index.ts +18 -0
- package/src/server/email/invitations.ts +30 -0
- package/src/server/email/service.ts +245 -0
- package/src/server/email/types.ts +56 -0
- package/src/server/email/worker.ts +33 -0
- package/src/server/index.ts +279 -0
- package/src/server/internal/index.ts +8 -0
- package/src/server/internal/routes.ts +41 -0
- package/src/server/internal/schema.ts +10 -0
- package/src/server/internal/service.ts +44 -0
- package/src/server/invitations/index.ts +6 -0
- package/src/server/invitations/types.ts +30 -0
- package/src/server/objects/index.ts +16 -0
- package/src/server/objects/types.ts +81 -0
- package/src/server/pictures/index.ts +9 -0
- package/src/server/pictures/provider.ts +61 -0
- package/src/server/pictures/routes.ts +21 -0
- package/src/server/pictures/service.ts +235 -0
- package/src/server/policy/configured.ts +23 -0
- package/src/server/policy/database.ts +108 -0
- package/src/server/policy/entitlements.ts +29 -0
- package/src/server/policy/index.ts +17 -0
- package/src/server/policy/keys.ts +23 -0
- package/src/server/policy/policy.ts +117 -0
- package/src/server/policy/provider.ts +67 -0
- package/src/server/policy/schema.ts +69 -0
- package/src/server/policy/service.ts +85 -0
- package/src/server/policy/static.ts +62 -0
- package/src/server/quota/index.ts +7 -0
- package/src/server/quota/reconcile.ts +61 -0
- package/src/server/quota/storage.ts +165 -0
- package/src/server/rate-limit/cleanup.ts +16 -0
- package/src/server/rate-limit/index.ts +12 -0
- package/src/server/rate-limit/keys.ts +38 -0
- package/src/server/rate-limit/middleware.ts +137 -0
- package/src/server/rate-limit/postgres.ts +79 -0
- package/src/server/sharing/access.ts +29 -0
- package/src/server/sharing/actor.ts +12 -0
- package/src/server/sharing/capabilities/service.ts +262 -0
- package/src/server/sharing/continuation.ts +85 -0
- package/src/server/sharing/contracts.ts +80 -0
- package/src/server/sharing/crypto.ts +48 -0
- package/src/server/sharing/document-routes.ts +41 -0
- package/src/server/sharing/errors.ts +12 -0
- package/src/server/sharing/grants/service.ts +79 -0
- package/src/server/sharing/http.ts +41 -0
- package/src/server/sharing/index.ts +39 -0
- package/src/server/sharing/invitations/service.ts +315 -0
- package/src/server/sharing/policy.ts +44 -0
- package/src/server/sharing/routes.ts +251 -0
- package/src/server/sharing/service.ts +117 -0
- package/src/server/validation.ts +24 -0
- package/src/server/worker/interval.ts +23 -0
- package/src/server/worker/periodic.ts +42 -0
- package/src/server/workspaces/index.ts +16 -0
- package/src/server/workspaces/members.ts +502 -0
- package/src/server/workspaces/repository.ts +91 -0
- package/src/server/workspaces/routes.ts +198 -0
- package/src/server/workspaces/service.ts +84 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CreateDocumentShareInput,
|
|
3
|
+
DocumentPermission,
|
|
4
|
+
DocumentShare,
|
|
5
|
+
ResolveDocumentShareInput,
|
|
6
|
+
UpdateDocumentShareInput
|
|
7
|
+
} from '#cloud/contract'
|
|
8
|
+
import type { CloudActor } from '#cloud/server/auth'
|
|
9
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
10
|
+
import { DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
11
|
+
import { CLOUD_FEATURE_KEYS } from '#cloud/server/policy/keys'
|
|
12
|
+
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
13
|
+
import type { Kysely, UpdateObject } from 'kysely'
|
|
14
|
+
|
|
15
|
+
import { randomHex } from '@open-pencil/scene-graph/random'
|
|
16
|
+
|
|
17
|
+
import { advanceCollaborationEpoch, requireSharingAccess } from '../access'
|
|
18
|
+
import { shareContract } from '../contracts'
|
|
19
|
+
import { capabilityHashMatches, hashCapability } from '../crypto'
|
|
20
|
+
import { DocumentShareInvalidError, DocumentShareSignInRequiredError } from '../errors'
|
|
21
|
+
import { createSharingPolicy } from '../policy'
|
|
22
|
+
|
|
23
|
+
const SHARE_SECRET_BYTES = 24
|
|
24
|
+
|
|
25
|
+
export type DocumentShareCapability = {
|
|
26
|
+
share: DocumentShare
|
|
27
|
+
secret: string
|
|
28
|
+
path: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type ResolvedSharePrincipal =
|
|
32
|
+
| { kind: 'user'; userId: string; name: string; email: string }
|
|
33
|
+
| { kind: 'guest'; guestId: string; name: string }
|
|
34
|
+
|
|
35
|
+
export type ResolvedDocumentShare = {
|
|
36
|
+
documentId: string
|
|
37
|
+
permission: DocumentPermission
|
|
38
|
+
principal: ResolvedSharePrincipal
|
|
39
|
+
roomEpoch: number
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export type CapabilityServiceOptions = {
|
|
43
|
+
policy?: CloudPolicy
|
|
44
|
+
deploymentMode?: 'official' | 'self-hosted'
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function createCapabilityService(
|
|
48
|
+
database: Kysely<CloudDatabase>,
|
|
49
|
+
options: CapabilityServiceOptions
|
|
50
|
+
) {
|
|
51
|
+
const sharingPolicy = createSharingPolicy(database, options)
|
|
52
|
+
|
|
53
|
+
async function updateActiveShare(
|
|
54
|
+
userId: string,
|
|
55
|
+
documentId: string,
|
|
56
|
+
shareId: string,
|
|
57
|
+
changes: UpdateObject<CloudDatabase, 'documentShare'>
|
|
58
|
+
) {
|
|
59
|
+
await requireSharingAccess(database, userId, documentId)
|
|
60
|
+
const row = await database
|
|
61
|
+
.updateTable('documentShare')
|
|
62
|
+
.set(changes)
|
|
63
|
+
.where('id', '=', shareId)
|
|
64
|
+
.where('documentId', '=', documentId)
|
|
65
|
+
.where('revokedAt', 'is', null)
|
|
66
|
+
.returningAll()
|
|
67
|
+
.executeTakeFirst()
|
|
68
|
+
if (!row) throw new DocumentNotFoundError()
|
|
69
|
+
return row
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
async listShares(userId: string, documentId: string): Promise<DocumentShare[]> {
|
|
74
|
+
await requireSharingAccess(database, userId, documentId)
|
|
75
|
+
const rows = await database
|
|
76
|
+
.selectFrom('documentShare')
|
|
77
|
+
.selectAll()
|
|
78
|
+
.where('documentId', '=', documentId)
|
|
79
|
+
.where('revokedAt', 'is', null)
|
|
80
|
+
.orderBy('createdAt', 'desc')
|
|
81
|
+
.execute()
|
|
82
|
+
return rows.map(shareContract)
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
async createShare(
|
|
86
|
+
userId: string,
|
|
87
|
+
documentId: string,
|
|
88
|
+
input: CreateDocumentShareInput
|
|
89
|
+
): Promise<DocumentShareCapability> {
|
|
90
|
+
await requireSharingAccess(database, userId, documentId)
|
|
91
|
+
await sharingPolicy.requireCapabilityLink({ actorId: userId, documentId })
|
|
92
|
+
const id = crypto.randomUUID()
|
|
93
|
+
const secret = randomHex(SHARE_SECRET_BYTES)
|
|
94
|
+
const row = await database.transaction().execute(async (transaction) => {
|
|
95
|
+
const document = await transaction
|
|
96
|
+
.selectFrom('document')
|
|
97
|
+
.select('collaborationEpoch')
|
|
98
|
+
.where('id', '=', documentId)
|
|
99
|
+
.forUpdate()
|
|
100
|
+
.executeTakeFirstOrThrow()
|
|
101
|
+
return transaction
|
|
102
|
+
.insertInto('documentShare')
|
|
103
|
+
.values({
|
|
104
|
+
id,
|
|
105
|
+
documentId,
|
|
106
|
+
permission: input.permission,
|
|
107
|
+
secretHash: hashCapability(secret),
|
|
108
|
+
roomEpoch: document.collaborationEpoch,
|
|
109
|
+
createdBy: userId,
|
|
110
|
+
expiresAt: input.expiresAt ? new Date(input.expiresAt) : null,
|
|
111
|
+
revokedAt: null,
|
|
112
|
+
lastUsedAt: null
|
|
113
|
+
})
|
|
114
|
+
.returningAll()
|
|
115
|
+
.executeTakeFirstOrThrow()
|
|
116
|
+
})
|
|
117
|
+
return { share: shareContract(row), secret, path: `/share/${id}#${secret}` }
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
async updateShare(
|
|
121
|
+
userId: string,
|
|
122
|
+
documentId: string,
|
|
123
|
+
shareId: string,
|
|
124
|
+
input: UpdateDocumentShareInput
|
|
125
|
+
): Promise<DocumentShare> {
|
|
126
|
+
// Authorization comes first, so a caller without access learns nothing about the share
|
|
127
|
+
// or the document's link policy.
|
|
128
|
+
await requireSharingAccess(database, userId, documentId)
|
|
129
|
+
const current = await database
|
|
130
|
+
.selectFrom('documentShare')
|
|
131
|
+
.select('id')
|
|
132
|
+
.where('id', '=', shareId)
|
|
133
|
+
.where('documentId', '=', documentId)
|
|
134
|
+
.where('revokedAt', 'is', null)
|
|
135
|
+
.executeTakeFirst()
|
|
136
|
+
if (!current) throw new DocumentNotFoundError()
|
|
137
|
+
await sharingPolicy.requireCapabilityLink({ actorId: userId, documentId })
|
|
138
|
+
const changes: UpdateObject<CloudDatabase, 'documentShare'> = { updatedAt: new Date() }
|
|
139
|
+
if (input.permission) changes.permission = input.permission
|
|
140
|
+
if (input.expiresAt !== undefined) {
|
|
141
|
+
changes.expiresAt = input.expiresAt ? new Date(input.expiresAt) : null
|
|
142
|
+
}
|
|
143
|
+
const row = await updateActiveShare(userId, documentId, shareId, changes)
|
|
144
|
+
return shareContract(row)
|
|
145
|
+
},
|
|
146
|
+
|
|
147
|
+
async rotateShare(
|
|
148
|
+
userId: string,
|
|
149
|
+
documentId: string,
|
|
150
|
+
shareId: string
|
|
151
|
+
): Promise<DocumentShareCapability> {
|
|
152
|
+
const secret = randomHex(SHARE_SECRET_BYTES)
|
|
153
|
+
const row = await database.transaction().execute(async (transaction) => {
|
|
154
|
+
await requireSharingAccess(transaction, userId, documentId)
|
|
155
|
+
const document = await transaction
|
|
156
|
+
.selectFrom('document')
|
|
157
|
+
.select('collaborationEpoch')
|
|
158
|
+
.where('id', '=', documentId)
|
|
159
|
+
.forUpdate()
|
|
160
|
+
.executeTakeFirstOrThrow()
|
|
161
|
+
const updated = await transaction
|
|
162
|
+
.updateTable('documentShare')
|
|
163
|
+
.set({
|
|
164
|
+
secretHash: hashCapability(secret),
|
|
165
|
+
roomEpoch: document.collaborationEpoch,
|
|
166
|
+
updatedAt: new Date()
|
|
167
|
+
})
|
|
168
|
+
.where('id', '=', shareId)
|
|
169
|
+
.where('documentId', '=', documentId)
|
|
170
|
+
.where('revokedAt', 'is', null)
|
|
171
|
+
.returningAll()
|
|
172
|
+
.executeTakeFirst()
|
|
173
|
+
if (!updated) throw new DocumentNotFoundError()
|
|
174
|
+
return updated
|
|
175
|
+
})
|
|
176
|
+
return { share: shareContract(row), secret, path: `/share/${shareId}#${secret}` }
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
async revokeShare(userId: string, documentId: string, shareId: string): Promise<void> {
|
|
180
|
+
await database.transaction().execute(async (transaction) => {
|
|
181
|
+
await requireSharingAccess(transaction, userId, documentId)
|
|
182
|
+
const result = await transaction
|
|
183
|
+
.updateTable('documentShare')
|
|
184
|
+
.set({ revokedAt: new Date(), updatedAt: new Date() })
|
|
185
|
+
.where('id', '=', shareId)
|
|
186
|
+
.where('documentId', '=', documentId)
|
|
187
|
+
.where('revokedAt', 'is', null)
|
|
188
|
+
.executeTakeFirst()
|
|
189
|
+
if (Number(result.numUpdatedRows) === 0) throw new DocumentNotFoundError()
|
|
190
|
+
await advanceCollaborationEpoch(transaction, documentId)
|
|
191
|
+
})
|
|
192
|
+
},
|
|
193
|
+
|
|
194
|
+
async resolveShare(
|
|
195
|
+
shareId: string,
|
|
196
|
+
input: ResolveDocumentShareInput,
|
|
197
|
+
actor?: CloudActor
|
|
198
|
+
): Promise<ResolvedDocumentShare> {
|
|
199
|
+
const row = await database
|
|
200
|
+
.selectFrom('documentShare')
|
|
201
|
+
.innerJoin('document', 'document.id', 'documentShare.documentId')
|
|
202
|
+
.select([
|
|
203
|
+
'documentShare.id',
|
|
204
|
+
'documentShare.documentId',
|
|
205
|
+
'documentShare.permission',
|
|
206
|
+
'documentShare.secretHash',
|
|
207
|
+
'documentShare.roomEpoch',
|
|
208
|
+
'documentShare.expiresAt'
|
|
209
|
+
])
|
|
210
|
+
.where('documentShare.id', '=', shareId)
|
|
211
|
+
.where('documentShare.revokedAt', 'is', null)
|
|
212
|
+
.where('document.deletedAt', 'is', null)
|
|
213
|
+
.executeTakeFirst()
|
|
214
|
+
if (
|
|
215
|
+
!row ||
|
|
216
|
+
!capabilityHashMatches(row.secretHash, input.secret) ||
|
|
217
|
+
(row.expiresAt && new Date(row.expiresAt).getTime() <= Date.now())
|
|
218
|
+
) {
|
|
219
|
+
throw new DocumentShareInvalidError()
|
|
220
|
+
}
|
|
221
|
+
if (!actor && options.policy) {
|
|
222
|
+
const document = await database
|
|
223
|
+
.selectFrom('document')
|
|
224
|
+
.select('workspaceId')
|
|
225
|
+
.where('id', '=', row.documentId)
|
|
226
|
+
.executeTakeFirstOrThrow()
|
|
227
|
+
const featureKey =
|
|
228
|
+
row.permission === 'edit' ? CLOUD_FEATURE_KEYS.guestEdit : CLOUD_FEATURE_KEYS.guestView
|
|
229
|
+
if (
|
|
230
|
+
!(await options.policy.boolean(featureKey, false, {
|
|
231
|
+
targetingKey: document.workspaceId,
|
|
232
|
+
workspaceId: document.workspaceId,
|
|
233
|
+
documentId: row.documentId,
|
|
234
|
+
deploymentMode: options.deploymentMode ?? 'self-hosted'
|
|
235
|
+
}))
|
|
236
|
+
) {
|
|
237
|
+
// Guests are not let in here; people signed in to the server are.
|
|
238
|
+
throw new DocumentShareSignInRequiredError()
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
await database
|
|
242
|
+
.updateTable('documentShare')
|
|
243
|
+
.set({ lastUsedAt: new Date() })
|
|
244
|
+
.where('id', '=', shareId)
|
|
245
|
+
.execute()
|
|
246
|
+
return {
|
|
247
|
+
documentId: row.documentId,
|
|
248
|
+
permission: row.permission,
|
|
249
|
+
principal: actor
|
|
250
|
+
? { kind: 'user', userId: actor.userId, name: actor.name, email: actor.email }
|
|
251
|
+
: {
|
|
252
|
+
kind: 'guest',
|
|
253
|
+
guestId: input.guestId ?? crypto.randomUUID(),
|
|
254
|
+
name: input.guestName ?? 'Guest'
|
|
255
|
+
},
|
|
256
|
+
roomEpoch: row.roomEpoch
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export type CapabilityService = ReturnType<typeof createCapabilityService>
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { InvitationPreview } from '#cloud/contract'
|
|
2
|
+
import type { CloudActor } from '#cloud/server/auth'
|
|
3
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
4
|
+
import { DocumentShareInvalidError } from '#cloud/server/sharing/errors'
|
|
5
|
+
import type { Kysely } from 'kysely'
|
|
6
|
+
|
|
7
|
+
import { capabilityHashMatches, decryptContinuationToken, encryptContinuationToken } from './crypto'
|
|
8
|
+
|
|
9
|
+
const CONTINUATION_LIFETIME_MS = 10 * 60_000
|
|
10
|
+
|
|
11
|
+
export type InvitationContinuationService = ReturnType<typeof createInvitationContinuationService>
|
|
12
|
+
|
|
13
|
+
export function createInvitationContinuationService(
|
|
14
|
+
database: Kysely<CloudDatabase>,
|
|
15
|
+
secret: string,
|
|
16
|
+
validateInvitation: (invitationId: string, token: string) => Promise<InvitationPreview>
|
|
17
|
+
) {
|
|
18
|
+
return {
|
|
19
|
+
async create(input: { invitationId: string; token: string }): Promise<{ id: string }> {
|
|
20
|
+
await validateInvitation(input.invitationId, input.token)
|
|
21
|
+
const id = crypto.randomUUID()
|
|
22
|
+
await database
|
|
23
|
+
.insertInto('invitationContinuation')
|
|
24
|
+
.values({
|
|
25
|
+
id,
|
|
26
|
+
invitationId: input.invitationId,
|
|
27
|
+
tokenEncrypted: await encryptContinuationToken(secret, input.token),
|
|
28
|
+
expiresAt: new Date(Date.now() + CONTINUATION_LIFETIME_MS),
|
|
29
|
+
consumedAt: null
|
|
30
|
+
})
|
|
31
|
+
.execute()
|
|
32
|
+
return { id }
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
async consume(id: string): Promise<{ invitationId: string; token: string }> {
|
|
36
|
+
return database.transaction().execute(async (transaction) => {
|
|
37
|
+
const row = await transaction
|
|
38
|
+
.selectFrom('invitationContinuation')
|
|
39
|
+
.select(['invitationId', 'tokenEncrypted', 'expiresAt', 'consumedAt'])
|
|
40
|
+
.where('id', '=', id)
|
|
41
|
+
.forUpdate()
|
|
42
|
+
.executeTakeFirst()
|
|
43
|
+
if (!row || row.consumedAt || new Date(row.expiresAt).getTime() <= Date.now()) {
|
|
44
|
+
throw new DocumentShareInvalidError()
|
|
45
|
+
}
|
|
46
|
+
await transaction
|
|
47
|
+
.updateTable('invitationContinuation')
|
|
48
|
+
.set({ consumedAt: new Date() })
|
|
49
|
+
.where('id', '=', id)
|
|
50
|
+
.execute()
|
|
51
|
+
return {
|
|
52
|
+
invitationId: row.invitationId,
|
|
53
|
+
token: await decryptContinuationToken(secret, row.tokenEncrypted)
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function invitationTokenIsActive(
|
|
61
|
+
invitation: {
|
|
62
|
+
tokenHash: string
|
|
63
|
+
expiresAt: Date | string
|
|
64
|
+
acceptedAt: Date | string | null
|
|
65
|
+
revokedAt: Date | string | null
|
|
66
|
+
},
|
|
67
|
+
token: string
|
|
68
|
+
): boolean {
|
|
69
|
+
return (
|
|
70
|
+
capabilityHashMatches(invitation.tokenHash, token) &&
|
|
71
|
+
!invitation.acceptedAt &&
|
|
72
|
+
!invitation.revokedAt &&
|
|
73
|
+
new Date(invitation.expiresAt).getTime() > Date.now()
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function recipientHint(email: string): string {
|
|
78
|
+
const [local = '', domain = ''] = email.split('@')
|
|
79
|
+
const visible = local.slice(0, 2)
|
|
80
|
+
return `${visible}${'*'.repeat(Math.max(3, local.length - visible.length))}@${domain}`
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function invitationMatchesActor(actor: CloudActor, email: string): boolean {
|
|
84
|
+
return email === actor.email.trim().toLowerCase()
|
|
85
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DocumentGrant,
|
|
3
|
+
DocumentInvitation,
|
|
4
|
+
DocumentPermission,
|
|
5
|
+
DocumentShare
|
|
6
|
+
} from '#cloud/contract'
|
|
7
|
+
|
|
8
|
+
export function dateString(value: Date | string | null): string | null {
|
|
9
|
+
if (value === null) return null
|
|
10
|
+
return value instanceof Date ? value.toISOString() : value
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function shareContract(row: {
|
|
14
|
+
id: string
|
|
15
|
+
documentId: string
|
|
16
|
+
permission: DocumentPermission
|
|
17
|
+
roomEpoch: number
|
|
18
|
+
createdBy: string
|
|
19
|
+
createdAt: Date | string
|
|
20
|
+
updatedAt: Date | string
|
|
21
|
+
expiresAt: Date | string | null
|
|
22
|
+
revokedAt: Date | string | null
|
|
23
|
+
lastUsedAt: Date | string | null
|
|
24
|
+
}): DocumentShare {
|
|
25
|
+
// Rows carry the secret's hash; responses name each field so it never goes out.
|
|
26
|
+
return {
|
|
27
|
+
id: row.id,
|
|
28
|
+
documentId: row.documentId,
|
|
29
|
+
permission: row.permission,
|
|
30
|
+
roomEpoch: row.roomEpoch,
|
|
31
|
+
createdBy: row.createdBy,
|
|
32
|
+
createdAt: dateString(row.createdAt) ?? '',
|
|
33
|
+
updatedAt: dateString(row.updatedAt) ?? '',
|
|
34
|
+
expiresAt: dateString(row.expiresAt),
|
|
35
|
+
revokedAt: dateString(row.revokedAt),
|
|
36
|
+
lastUsedAt: dateString(row.lastUsedAt)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function grantContract(row: {
|
|
41
|
+
id: string
|
|
42
|
+
documentId: string
|
|
43
|
+
userId: string
|
|
44
|
+
permission: DocumentPermission
|
|
45
|
+
createdBy: string
|
|
46
|
+
createdAt: Date | string
|
|
47
|
+
updatedAt: Date | string
|
|
48
|
+
}): DocumentGrant {
|
|
49
|
+
return {
|
|
50
|
+
id: row.id,
|
|
51
|
+
documentId: row.documentId,
|
|
52
|
+
userId: row.userId,
|
|
53
|
+
permission: row.permission,
|
|
54
|
+
createdBy: row.createdBy,
|
|
55
|
+
createdAt: dateString(row.createdAt) ?? '',
|
|
56
|
+
updatedAt: dateString(row.updatedAt) ?? ''
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function invitationContract(row: {
|
|
61
|
+
id: string
|
|
62
|
+
documentId: string
|
|
63
|
+
emailNormalized: string
|
|
64
|
+
permission: DocumentPermission
|
|
65
|
+
invitedBy: string
|
|
66
|
+
invitedAt: Date | string
|
|
67
|
+
expiresAt: Date | string
|
|
68
|
+
acceptedAt: Date | string | null
|
|
69
|
+
}): DocumentInvitation {
|
|
70
|
+
return {
|
|
71
|
+
id: row.id,
|
|
72
|
+
documentId: row.documentId,
|
|
73
|
+
email: row.emailNormalized,
|
|
74
|
+
permission: row.permission,
|
|
75
|
+
invitedBy: row.invitedBy,
|
|
76
|
+
invitedAt: dateString(row.invitedAt) ?? '',
|
|
77
|
+
expiresAt: dateString(row.expiresAt) ?? '',
|
|
78
|
+
acceptedAt: dateString(row.acceptedAt)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { hkdf } from '@noble/hashes/hkdf.js'
|
|
2
|
+
import { sha256 } from '@noble/hashes/sha2.js'
|
|
3
|
+
import { bytesToHex, hexToBytes, utf8ToBytes } from '@noble/hashes/utils.js'
|
|
4
|
+
import { compactDecrypt, CompactEncrypt } from 'jose'
|
|
5
|
+
|
|
6
|
+
const CONTINUATION_SALT = utf8ToBytes('openpencil-cloud-invitation-continuation')
|
|
7
|
+
const CONTINUATION_HEADER = { alg: 'dir', enc: 'A256GCM' } as const
|
|
8
|
+
|
|
9
|
+
export function hashCapability(value: string): string {
|
|
10
|
+
return bytesToHex(sha256(utf8ToBytes(value)))
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function capabilityHashMatches(stored: string, value: string): boolean {
|
|
14
|
+
let expected: Uint8Array
|
|
15
|
+
try {
|
|
16
|
+
expected = hexToBytes(stored)
|
|
17
|
+
} catch {
|
|
18
|
+
return false
|
|
19
|
+
}
|
|
20
|
+
const actual = sha256(utf8ToBytes(value))
|
|
21
|
+
if (expected.length !== actual.length) return false
|
|
22
|
+
let difference = 0
|
|
23
|
+
for (const [index, expectedByte] of expected.entries()) {
|
|
24
|
+
difference |= expectedByte ^ (actual[index] ?? 0)
|
|
25
|
+
}
|
|
26
|
+
return difference === 0
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function continuationKey(secret: string): Uint8Array {
|
|
30
|
+
return hkdf(sha256, utf8ToBytes(secret), CONTINUATION_SALT, new Uint8Array(), 32)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function encryptContinuationToken(secret: string, token: string): Promise<string> {
|
|
34
|
+
return new CompactEncrypt(utf8ToBytes(token))
|
|
35
|
+
.setProtectedHeader(CONTINUATION_HEADER)
|
|
36
|
+
.encrypt(continuationKey(secret))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function decryptContinuationToken(secret: string, value: string): Promise<string> {
|
|
40
|
+
const { plaintext, protectedHeader } = await compactDecrypt(value, continuationKey(secret))
|
|
41
|
+
if (
|
|
42
|
+
protectedHeader.alg !== CONTINUATION_HEADER.alg ||
|
|
43
|
+
protectedHeader.enc !== CONTINUATION_HEADER.enc
|
|
44
|
+
) {
|
|
45
|
+
throw new Error('Unsupported invitation continuation encryption')
|
|
46
|
+
}
|
|
47
|
+
return new TextDecoder().decode(plaintext)
|
|
48
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { parseRequestSharedImageLinks, parseResolveDocumentShare } from '#cloud/contract'
|
|
2
|
+
import type { CloudSessionResolver } from '#cloud/server/auth'
|
|
3
|
+
import type { DocumentService } from '#cloud/server/documents'
|
|
4
|
+
import type { DocumentSharingService } from '#cloud/server/sharing'
|
|
5
|
+
import { sharingRoute } from '#cloud/server/sharing/http'
|
|
6
|
+
import { validatedJSON } from '#cloud/server/validation'
|
|
7
|
+
import { Hono } from 'hono'
|
|
8
|
+
|
|
9
|
+
import { shareActor } from './actor'
|
|
10
|
+
|
|
11
|
+
export function createPublicDocumentRoutes(
|
|
12
|
+
sharing: DocumentSharingService,
|
|
13
|
+
documents: DocumentService,
|
|
14
|
+
resolveSession?: CloudSessionResolver
|
|
15
|
+
) {
|
|
16
|
+
return new Hono()
|
|
17
|
+
.post('/shares/:shareId/document', validatedJSON(parseResolveDocumentShare), (context) =>
|
|
18
|
+
sharingRoute(context, async () => {
|
|
19
|
+
const resolution = await sharing.resolveShare(
|
|
20
|
+
context.req.param('shareId'),
|
|
21
|
+
context.req.valid('json'),
|
|
22
|
+
await shareActor(context.req.raw, resolveSession)
|
|
23
|
+
)
|
|
24
|
+
return context.json({
|
|
25
|
+
resolution,
|
|
26
|
+
document: await documents.downloadShared(resolution.documentId)
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
)
|
|
30
|
+
.post('/shares/:shareId/images', validatedJSON(parseRequestSharedImageLinks), (context) =>
|
|
31
|
+
sharingRoute(context, async () => {
|
|
32
|
+
const { checksums, ...share } = context.req.valid('json')
|
|
33
|
+
const resolution = await sharing.resolveShare(
|
|
34
|
+
context.req.param('shareId'),
|
|
35
|
+
share,
|
|
36
|
+
await shareActor(context.req.raw, resolveSession)
|
|
37
|
+
)
|
|
38
|
+
return context.json(await documents.imageLinksShared(resolution.documentId, checksums))
|
|
39
|
+
})
|
|
40
|
+
)
|
|
41
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export class InvitationDeliveryError extends Error {
|
|
2
|
+
override readonly name = 'InvitationDeliveryError'
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export class DocumentShareInvalidError extends Error {
|
|
6
|
+
override readonly name = 'DocumentShareInvalidError'
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** A valid link this server opens only for people signed in to it. */
|
|
10
|
+
export class DocumentShareSignInRequiredError extends Error {
|
|
11
|
+
override readonly name = 'DocumentShareSignInRequiredError'
|
|
12
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { DocumentGrant, PutDocumentGrantInput } from '#cloud/contract'
|
|
2
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
3
|
+
import { DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
4
|
+
import type { Kysely } from 'kysely'
|
|
5
|
+
|
|
6
|
+
import { advanceCollaborationEpoch, requireSharingAccess } from '../access'
|
|
7
|
+
import { grantContract } from '../contracts'
|
|
8
|
+
|
|
9
|
+
export function createGrantService(database: Kysely<CloudDatabase>) {
|
|
10
|
+
return {
|
|
11
|
+
async listGrants(userId: string, documentId: string): Promise<DocumentGrant[]> {
|
|
12
|
+
await requireSharingAccess(database, userId, documentId)
|
|
13
|
+
const rows = await database
|
|
14
|
+
.selectFrom('documentGrant')
|
|
15
|
+
.select(['id', 'documentId', 'userId', 'permission', 'createdBy', 'createdAt', 'updatedAt'])
|
|
16
|
+
.where('documentId', '=', documentId)
|
|
17
|
+
.where('revokedAt', 'is', null)
|
|
18
|
+
.orderBy('createdAt')
|
|
19
|
+
.execute()
|
|
20
|
+
return rows.map(grantContract)
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
async putGrant(
|
|
24
|
+
userId: string,
|
|
25
|
+
documentId: string,
|
|
26
|
+
targetUserId: string,
|
|
27
|
+
input: PutDocumentGrantInput
|
|
28
|
+
): Promise<DocumentGrant> {
|
|
29
|
+
await requireSharingAccess(database, userId, documentId)
|
|
30
|
+
const row = await database
|
|
31
|
+
.insertInto('documentGrant')
|
|
32
|
+
.values({
|
|
33
|
+
id: crypto.randomUUID(),
|
|
34
|
+
documentId,
|
|
35
|
+
userId: targetUserId,
|
|
36
|
+
permission: input.permission,
|
|
37
|
+
createdBy: userId,
|
|
38
|
+
revokedAt: null
|
|
39
|
+
})
|
|
40
|
+
.onConflict((conflict) =>
|
|
41
|
+
conflict.columns(['documentId', 'userId']).doUpdateSet({
|
|
42
|
+
permission: input.permission,
|
|
43
|
+
createdBy: userId,
|
|
44
|
+
revokedAt: null,
|
|
45
|
+
updatedAt: new Date()
|
|
46
|
+
})
|
|
47
|
+
)
|
|
48
|
+
.returning([
|
|
49
|
+
'id',
|
|
50
|
+
'documentId',
|
|
51
|
+
'userId',
|
|
52
|
+
'permission',
|
|
53
|
+
'createdBy',
|
|
54
|
+
'createdAt',
|
|
55
|
+
'updatedAt'
|
|
56
|
+
])
|
|
57
|
+
.executeTakeFirstOrThrow()
|
|
58
|
+
return grantContract(row)
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
/** Revoking access advances the collaboration epoch, so tickets and room keys issued before stop working. */
|
|
62
|
+
async revokeGrant(userId: string, documentId: string, targetUserId: string): Promise<void> {
|
|
63
|
+
await database.transaction().execute(async (transaction) => {
|
|
64
|
+
await requireSharingAccess(transaction, userId, documentId)
|
|
65
|
+
const result = await transaction
|
|
66
|
+
.updateTable('documentGrant')
|
|
67
|
+
.set({ revokedAt: new Date(), updatedAt: new Date() })
|
|
68
|
+
.where('documentId', '=', documentId)
|
|
69
|
+
.where('userId', '=', targetUserId)
|
|
70
|
+
.where('revokedAt', 'is', null)
|
|
71
|
+
.executeTakeFirst()
|
|
72
|
+
if (Number(result.numUpdatedRows) === 0) throw new DocumentNotFoundError()
|
|
73
|
+
await advanceCollaborationEpoch(transaction, documentId)
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export type GrantService = ReturnType<typeof createGrantService>
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { DocumentForbiddenError, DocumentNotFoundError } from '#cloud/server/documents'
|
|
2
|
+
import {
|
|
3
|
+
DocumentShareInvalidError,
|
|
4
|
+
DocumentShareSignInRequiredError,
|
|
5
|
+
InvitationDeliveryError
|
|
6
|
+
} from '#cloud/server/sharing/errors'
|
|
7
|
+
import { WorkspaceInvitationInvalidError } from '#cloud/server/workspaces/members'
|
|
8
|
+
import type { Context } from 'hono'
|
|
9
|
+
|
|
10
|
+
export function sharingDomainError(context: Context, error: unknown): Response | null {
|
|
11
|
+
if (
|
|
12
|
+
error instanceof DocumentNotFoundError ||
|
|
13
|
+
error instanceof DocumentShareInvalidError ||
|
|
14
|
+
error instanceof WorkspaceInvitationInvalidError
|
|
15
|
+
) {
|
|
16
|
+
return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
17
|
+
}
|
|
18
|
+
if (error instanceof DocumentShareSignInRequiredError) {
|
|
19
|
+
return context.json({ error: { code: 'unauthorized' as const } }, 401)
|
|
20
|
+
}
|
|
21
|
+
if (error instanceof InvitationDeliveryError) {
|
|
22
|
+
return context.json({ error: { code: 'invitation_delivery_failed' as const } }, 502)
|
|
23
|
+
}
|
|
24
|
+
if (error instanceof DocumentForbiddenError) {
|
|
25
|
+
return context.json({ error: { code: 'forbidden' as const } }, 403)
|
|
26
|
+
}
|
|
27
|
+
return null
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function sharingRoute<T>(
|
|
31
|
+
context: Context,
|
|
32
|
+
action: () => Promise<T>
|
|
33
|
+
): Promise<T | Response> {
|
|
34
|
+
try {
|
|
35
|
+
return await action()
|
|
36
|
+
} catch (error) {
|
|
37
|
+
const response = sharingDomainError(context, error)
|
|
38
|
+
if (response) return response
|
|
39
|
+
throw error
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createCapabilityService,
|
|
3
|
+
type CapabilityService,
|
|
4
|
+
type CapabilityServiceOptions
|
|
5
|
+
} from './capabilities/service'
|
|
6
|
+
|
|
7
|
+
export {
|
|
8
|
+
capabilityHashMatches,
|
|
9
|
+
decryptContinuationToken,
|
|
10
|
+
encryptContinuationToken,
|
|
11
|
+
hashCapability
|
|
12
|
+
} from './crypto'
|
|
13
|
+
export {
|
|
14
|
+
DocumentShareInvalidError,
|
|
15
|
+
DocumentShareSignInRequiredError,
|
|
16
|
+
InvitationDeliveryError
|
|
17
|
+
} from './errors'
|
|
18
|
+
export { createGrantService, type GrantService } from './grants/service'
|
|
19
|
+
|
|
20
|
+
export { createPublicDocumentRoutes } from './document-routes'
|
|
21
|
+
export {
|
|
22
|
+
createInvitationService,
|
|
23
|
+
type InvitationService,
|
|
24
|
+
type InvitationServiceOptions
|
|
25
|
+
} from './invitations/service'
|
|
26
|
+
|
|
27
|
+
export {
|
|
28
|
+
createDocumentSharingRoutes,
|
|
29
|
+
createPublicSharingRoutes,
|
|
30
|
+
type SharingRouteEnvironment
|
|
31
|
+
} from './routes'
|
|
32
|
+
export {
|
|
33
|
+
createDocumentSharingService,
|
|
34
|
+
type DocumentShareCapability,
|
|
35
|
+
type DocumentSharingServiceOptions,
|
|
36
|
+
type DocumentSharingService,
|
|
37
|
+
type ResolvedDocumentShare,
|
|
38
|
+
type ResolvedSharePrincipal
|
|
39
|
+
} from './service'
|