@open-pencil/cloud 0.17.0-beta.4 → 0.17.0-beta.6
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/README.md +2 -1
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/services.mjs +1 -1
- package/dist/bin/services.mjs.map +1 -1
- package/dist/client.d.ts +783 -47
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1651 -382
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +701 -45
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +2138 -34
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +4343 -306
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +61 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +1338 -44
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +45 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +958 -340
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +93 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +18032 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +12615 -5249
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/server.d.ts +17367 -8567
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +13587 -7333
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +11601 -5401
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +84 -62
- package/dist/server2.mjs.map +1 -1
- package/package.json +31 -5
- package/src/admin/api/routes.ts +263 -190
- package/src/admin/audit/service.ts +11 -3
- package/src/admin/bootstrap.ts +86 -0
- package/src/admin/documents/service.ts +195 -10
- package/src/admin/email/service.ts +2 -1
- package/src/admin/enrollment/email.ts +17 -3
- package/src/admin/enrollment/service.ts +71 -10
- package/src/admin/errors.ts +5 -0
- package/src/admin/index.ts +1 -0
- package/src/admin/reports/service.ts +15 -3
- package/src/admin/schema.ts +6 -5
- package/src/admin/users/service.ts +173 -16
- package/src/client/api.ts +52 -473
- package/src/client/auth/client.ts +22 -3
- package/src/client/comments.ts +205 -0
- package/src/client/community.ts +40 -0
- package/src/client/device-authorization.ts +4 -20
- package/src/client/discovery.ts +39 -11
- package/src/client/documents.ts +160 -0
- package/src/client/files/archive.ts +60 -0
- package/src/client/files/checksum.ts +7 -0
- package/src/client/files/download.ts +122 -0
- package/src/client/files/images.ts +89 -0
- package/src/client/files/index.ts +15 -0
- package/src/client/files/objects.ts +173 -0
- package/src/client/files/save.ts +79 -0
- package/src/client/index.ts +4 -0
- package/src/client/portal/account.ts +73 -3
- package/src/client/portal/administration.ts +52 -5
- package/src/client/portal/request.ts +6 -2
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +23 -18
- package/src/client/sharing.ts +262 -0
- package/src/client/tour.ts +44 -0
- package/src/client/versions.ts +110 -0
- package/src/contract/account.ts +111 -11
- package/src/contract/admin/index.ts +48 -3
- package/src/contract/billing.ts +235 -0
- package/src/contract/collaboration.ts +5 -0
- package/src/contract/comments.ts +223 -0
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +94 -5
- package/src/contract/entitlements.ts +10 -0
- package/src/contract/index.ts +64 -5
- package/src/contract/links.ts +62 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/relay.ts +3 -1
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +62 -20
- package/src/contract/workspaces.ts +5 -0
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +169 -0
- package/src/email/locales/de.json +169 -0
- package/src/email/locales/es.json +169 -0
- package/src/email/locales/fr.json +169 -0
- package/src/email/locales/it.json +169 -0
- package/src/email/locales/ja.json +169 -0
- package/src/email/locales/pl.json +169 -0
- package/src/email/locales/pt-br.json +169 -0
- package/src/email/locales/ru.json +169 -0
- package/src/email/locales/zh-cn.json +169 -0
- package/src/email/messages.ts +235 -0
- package/src/email/render.ts +160 -276
- package/src/email/samples.ts +142 -0
- package/src/email/schema.ts +98 -8
- package/src/email/templates/BrandEmail.vue +56 -0
- package/src/email/templates/CommentNotification.vue +39 -0
- package/src/email/templates/DocumentBlocked.vue +45 -0
- package/src/email/templates/DocumentInvitation.vue +24 -0
- package/src/email/templates/DocumentReport.vue +32 -0
- package/src/email/templates/DocumentReportDecided.vue +36 -0
- package/src/email/templates/DocumentReportReceived.vue +18 -0
- package/src/email/templates/DocumentRestored.vue +24 -0
- package/src/email/templates/EmailChangeRequested.vue +24 -0
- package/src/email/templates/EmailRoot.vue +19 -0
- package/src/email/templates/EmailText.vue +9 -0
- package/src/email/templates/EmailVerification.vue +38 -0
- package/src/email/templates/EnrollmentInvitation.vue +53 -0
- package/src/email/templates/EnrollmentNotification.vue +24 -0
- package/src/email/templates/EnrollmentUpdate.vue +52 -0
- package/src/email/templates/NewSignIn.vue +37 -0
- package/src/email/templates/PasswordChanged.vue +22 -0
- package/src/email/templates/PasswordReset.vue +23 -0
- package/src/email/templates/StorageLimitReached.vue +28 -0
- package/src/email/templates/TwoStepChanged.vue +47 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +15 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/{server/errors → errors}/posthog.ts +28 -4
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/email.ts +2 -0
- package/src/runtime/cloudflare/relay/index.ts +28 -7
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +14 -1
- package/src/runtime/cloudflare/xml.ts +12 -0
- package/src/runtime/node/admin.ts +51 -47
- package/src/runtime/node/cleanup.ts +5 -1
- package/src/runtime/node/email/runtime.ts +1 -0
- package/src/runtime/node/email/transport.ts +14 -10
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/server.ts +20 -4
- package/src/runtime/node/services/main.ts +1 -1
- package/src/runtime/posthog/errors.ts +5 -2
- package/src/runtime/s3/objects.ts +17 -0
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +17 -1
- package/src/server/account/actions.ts +3 -1
- package/src/server/account/deletion.ts +40 -4
- package/src/server/account/routes.ts +305 -253
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +44 -6
- package/src/server/app/checks.ts +89 -0
- package/src/server/app/cors.ts +44 -0
- package/src/server/app/editors.ts +20 -0
- package/src/server/app/index.ts +128 -0
- package/src/server/app/indexing.ts +40 -0
- package/src/server/app/limiters.ts +53 -0
- package/src/server/app/routes.ts +123 -0
- package/src/server/app/services.ts +219 -0
- package/src/server/auth/access-control.ts +13 -8
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/account.ts +13 -12
- package/src/server/auth/activity.ts +7 -2
- package/src/server/auth/adapter.ts +36 -3
- package/src/server/auth/email.ts +33 -18
- package/src/server/auth/endpoints.ts +4 -0
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +165 -189
- package/src/server/auth/index.ts +1 -1
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/methods.ts +162 -0
- package/src/server/auth/mfa-assurance.ts +190 -2
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/rate-limit.ts +31 -0
- package/src/server/auth/runtime.ts +19 -5
- package/src/server/auth/session.ts +2 -0
- package/src/server/auth/social-providers.ts +70 -24
- package/src/server/billing/index.ts +8 -0
- package/src/server/billing/notify.ts +55 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/seats.ts +17 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +16 -3
- package/src/server/cleanup/documents.ts +5 -2
- package/src/server/cleanup/revisions.ts +6 -3
- package/src/server/cleanup/uploads.ts +2 -1
- package/src/server/cleanup/worker.ts +10 -1
- package/src/server/collaboration/relay/connection.ts +7 -2
- package/src/server/collaboration/relay/hub.ts +80 -166
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/routes.ts +8 -5
- package/src/server/collaboration/service.ts +21 -2
- package/src/server/comments/index.ts +9 -0
- package/src/server/comments/notify.ts +277 -0
- package/src/server/comments/routes.ts +352 -0
- package/src/server/comments/rows.ts +249 -0
- package/src/server/comments/service.ts +570 -0
- package/src/server/community/index.ts +2 -0
- package/src/server/community/routes.ts +93 -0
- package/src/server/community/service.ts +134 -0
- package/src/server/config/deployment.ts +35 -10
- package/src/server/config/index.ts +2 -2
- package/src/server/config/public.ts +25 -6
- package/src/server/config/schema.ts +53 -293
- package/src/server/config/toml/schema.ts +330 -0
- package/src/server/db/index.ts +3 -1
- package/src/server/db/migrate.ts +33 -1
- package/src/server/db/migrations/009_access_tokens.ts +47 -0
- package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
- package/src/server/db/migrations/011_account_ids.ts +30 -0
- package/src/server/db/migrations/012_email_locales.ts +34 -0
- package/src/server/db/migrations/013_document_block_emails.ts +40 -0
- package/src/server/db/migrations/014_report_replies.ts +28 -0
- package/src/server/db/migrations/015_comments.ts +57 -0
- package/src/server/db/migrations/016_comment_notifications.ts +76 -0
- package/src/server/db/migrations/017_invitation_emails.ts +19 -0
- package/src/server/db/migrations/018_enrollment_source.ts +17 -0
- package/src/server/db/migrations/schema.ts +26 -1
- package/src/server/db/tables.ts +122 -3
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/access.ts +17 -0
- package/src/server/documents/images.ts +6 -1
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +7 -1
- package/src/server/documents/routes.ts +12 -12
- package/src/server/documents/service.ts +31 -407
- package/src/server/documents/summary.ts +15 -0
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/routes.ts +7 -8
- package/src/server/documents/versions/service.ts +86 -26
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +39 -6
- package/src/server/email/types.ts +13 -0
- package/src/server/errors/index.ts +49 -12
- package/src/server/index.ts +27 -6
- package/src/server/internal/routes.ts +8 -2
- package/src/server/internal/service.ts +10 -0
- package/src/server/invitations/budget.ts +3 -1
- package/src/server/invitations/index.ts +1 -0
- package/src/server/invitations/links.ts +6 -0
- package/src/server/invitations/types.ts +5 -1
- package/src/server/objects/types.ts +5 -0
- package/src/server/pictures/service.ts +3 -1
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +13 -2
- package/src/server/policy/policy.ts +39 -12
- package/src/server/policy/service.ts +71 -52
- package/src/server/policy/storage.ts +33 -0
- package/src/server/policy/workspace.ts +5 -2
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/storage.ts +5 -0
- package/src/server/rate-limit/middleware.ts +4 -0
- package/src/server/reports/index.ts +1 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +5 -3
- package/src/server/reports/service.ts +27 -3
- package/src/server/services/routes.ts +2 -2
- package/src/server/sharing/capabilities/service.ts +3 -4
- package/src/server/sharing/document-routes.ts +3 -3
- package/src/server/sharing/invitations/service.ts +17 -8
- package/src/server/sharing/routes.ts +15 -15
- package/src/server/validation.ts +35 -24
- package/src/server/workspaces/members.ts +57 -31
- package/src/server/workspaces/repository.ts +23 -12
- package/src/server/workspaces/routes.ts +8 -8
- package/src/services/app.ts +31 -22
- package/src/services/config.ts +48 -8
- package/src/services/metering.ts +15 -27
- package/src/services/usage.ts +6 -14
- package/src/email/templates/document-invitation.ts +0 -35
- package/src/email/templates/enrollment.ts +0 -27
- package/src/email/templates/layout.ts +0 -18
- package/src/server/app.ts +0 -453
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { cloudAccessTokenCreateSchema, cloudAccessTokenRevokeSchema } from '#cloud/contract'
|
|
2
|
+
import type { CloudAPIEnvironment } from '#cloud/server/api'
|
|
3
|
+
import type { CloudAuthAdapter, CloudSessionResolver } from '#cloud/server/auth'
|
|
4
|
+
import { validatedJSON } from '#cloud/server/validation'
|
|
5
|
+
import { Hono, type MiddlewareHandler } from 'hono'
|
|
6
|
+
|
|
7
|
+
import { createAccountActions } from './actions'
|
|
8
|
+
|
|
9
|
+
export type AccessTokenRouteDependencies = {
|
|
10
|
+
auth: CloudAuthAdapter
|
|
11
|
+
resolveSession: CloudSessionResolver
|
|
12
|
+
/** A session's, never a token's: account routes resolve sessions alone. */
|
|
13
|
+
requireActiveSession: MiddlewareHandler<CloudAPIEnvironment>
|
|
14
|
+
authenticatedMutationLimiter: MiddlewareHandler<CloudAPIEnvironment>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The account's personal access tokens: listed, made, and revoked from a signed-in session only,
|
|
19
|
+
* so a token never sees or makes others. Making one needs a recent sign-in.
|
|
20
|
+
*/
|
|
21
|
+
export function createAccessTokenRoutes(services: AccessTokenRouteDependencies) {
|
|
22
|
+
const { accountAction } = createAccountActions(services.resolveSession)
|
|
23
|
+
return new Hono<CloudAPIEnvironment>()
|
|
24
|
+
.use('/api/account/tokens', services.requireActiveSession)
|
|
25
|
+
.use('/api/account/tokens/*', services.requireActiveSession)
|
|
26
|
+
.use('/api/account/tokens/*', services.authenticatedMutationLimiter)
|
|
27
|
+
.get('/api/account/tokens', async (context) => {
|
|
28
|
+
const tokens = await services.auth.listAccessTokens(context.req.raw.headers)
|
|
29
|
+
if (!tokens) return context.json({ error: { code: 'unauthorized' as const } }, 401)
|
|
30
|
+
return context.json({ tokens })
|
|
31
|
+
})
|
|
32
|
+
.post(
|
|
33
|
+
'/api/account/tokens/create',
|
|
34
|
+
validatedJSON(cloudAccessTokenCreateSchema),
|
|
35
|
+
async (context) => {
|
|
36
|
+
const result = await accountAction(
|
|
37
|
+
() => services.auth.createAccessToken(context.req.raw.headers, context.req.valid('json')),
|
|
38
|
+
context
|
|
39
|
+
)
|
|
40
|
+
return result instanceof Response ? result : context.json(result, 201)
|
|
41
|
+
}
|
|
42
|
+
)
|
|
43
|
+
.post(
|
|
44
|
+
'/api/account/tokens/revoke',
|
|
45
|
+
validatedJSON(cloudAccessTokenRevokeSchema),
|
|
46
|
+
async (context) => {
|
|
47
|
+
const result = await accountAction(
|
|
48
|
+
() =>
|
|
49
|
+
services.auth.revokeAccessToken(context.req.raw.headers, context.req.valid('json').id),
|
|
50
|
+
context
|
|
51
|
+
)
|
|
52
|
+
return result instanceof Response ? result : context.json({ ok: true as const })
|
|
53
|
+
}
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type CloudAccessTokenRoutes = ReturnType<typeof createAccessTokenRoutes>
|
package/src/server/api/router.ts
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import type { CloudAPIEnvironment } from '#cloud/server/api/types'
|
|
2
2
|
import type { CloudSessionResolver } from '#cloud/server/auth'
|
|
3
|
+
import { createBillingRoutes, type BillingTokenService } from '#cloud/server/billing'
|
|
3
4
|
import type { CollaborationTicketService } from '#cloud/server/collaboration'
|
|
4
5
|
import {
|
|
5
6
|
createCollaborationRoutes,
|
|
6
7
|
createPublicCollaborationRoutes
|
|
7
8
|
} from '#cloud/server/collaboration'
|
|
9
|
+
import {
|
|
10
|
+
createCommentRoutes,
|
|
11
|
+
createSharedCommentRoutes,
|
|
12
|
+
type CommentService
|
|
13
|
+
} from '#cloud/server/comments'
|
|
14
|
+
import { createCommunityRoutes, type CommunityService } from '#cloud/server/community'
|
|
8
15
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
9
16
|
import type { DocumentService } from '#cloud/server/documents'
|
|
10
|
-
import { createDocumentRoutes } from '#cloud/server/documents'
|
|
17
|
+
import { createDocumentRoutes, DocumentNotFoundError } from '#cloud/server/documents'
|
|
11
18
|
import {
|
|
12
19
|
createDocumentVersionRoutes,
|
|
13
20
|
type DocumentVersionService
|
|
@@ -33,6 +40,9 @@ export type CloudAPIServices = {
|
|
|
33
40
|
collaboration: CollaborationTicketService
|
|
34
41
|
documents: DocumentService
|
|
35
42
|
versions: DocumentVersionService
|
|
43
|
+
comments: CommentService
|
|
44
|
+
/** The community gallery, which anyone may read. */
|
|
45
|
+
community: CommunityService
|
|
36
46
|
sharing: DocumentSharingService
|
|
37
47
|
/** Reports about documents shared by link. */
|
|
38
48
|
reports: DocumentReportService
|
|
@@ -40,6 +50,8 @@ export type CloudAPIServices = {
|
|
|
40
50
|
entitlements?: EntitlementService
|
|
41
51
|
/** Signs service tokens; without it the deployment offers no services. */
|
|
42
52
|
servicesTokens?: ServicesTokenService
|
|
53
|
+
/** Signs billing tokens; without it the deployment sells nothing. */
|
|
54
|
+
billingTokens?: BillingTokenService
|
|
43
55
|
workspaces: WorkspaceService
|
|
44
56
|
workspaceMembers: WorkspaceMemberService
|
|
45
57
|
}
|
|
@@ -52,22 +64,38 @@ export function createPublicCloudAPIRouter(services: CloudAPIServices) {
|
|
|
52
64
|
'/',
|
|
53
65
|
createPublicDocumentRoutes(services.sharing, services.documents, services.resolveSession)
|
|
54
66
|
)
|
|
67
|
+
.route('/', createSharedCommentRoutes(services.comments, services.resolveSession))
|
|
55
68
|
.route('/', createPublicCollaborationRoutes(services.collaboration, services.resolveSession))
|
|
69
|
+
.route('/', createCommunityRoutes(services.community))
|
|
56
70
|
}
|
|
57
71
|
|
|
58
72
|
export function createCloudAPIRouter(services: CloudAPIServices) {
|
|
59
73
|
return new Hono<CloudAPIEnvironment>()
|
|
60
74
|
.route('/public', createPublicCloudAPIRouter(services))
|
|
61
|
-
.get('/
|
|
75
|
+
.get('/account/entitlements', async (context) => {
|
|
62
76
|
if (!services.entitlements)
|
|
63
77
|
return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
64
78
|
return context.json({
|
|
65
|
-
entitlements: await services.entitlements.
|
|
66
|
-
context.get('actor').userId,
|
|
67
|
-
context.req.valid('param').workspaceId
|
|
68
|
-
)
|
|
79
|
+
entitlements: await services.entitlements.account(context.get('actor').userId)
|
|
69
80
|
})
|
|
70
81
|
})
|
|
82
|
+
.get('/workspaces/:workspaceId/entitlements', workspaceParams, async (context) => {
|
|
83
|
+
if (!services.entitlements)
|
|
84
|
+
return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
85
|
+
try {
|
|
86
|
+
return context.json({
|
|
87
|
+
entitlements: await services.entitlements.workspace(
|
|
88
|
+
context.get('actor').userId,
|
|
89
|
+
context.req.valid('param').workspaceId
|
|
90
|
+
)
|
|
91
|
+
})
|
|
92
|
+
} catch (error) {
|
|
93
|
+
// A workspace the account left, or one that has gone, since the editor listed it.
|
|
94
|
+
if (error instanceof DocumentNotFoundError)
|
|
95
|
+
return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
96
|
+
throw error
|
|
97
|
+
}
|
|
98
|
+
})
|
|
71
99
|
.get('/session', (context) => context.json({ user: context.get('actor') }))
|
|
72
100
|
.route(
|
|
73
101
|
'/',
|
|
@@ -78,6 +106,15 @@ export function createCloudAPIRouter(services: CloudAPIServices) {
|
|
|
78
106
|
: undefined
|
|
79
107
|
)
|
|
80
108
|
)
|
|
109
|
+
.route(
|
|
110
|
+
'/',
|
|
111
|
+
createBillingRoutes(
|
|
112
|
+
services.billingTokens,
|
|
113
|
+
services.database && services.rateLimitSecret
|
|
114
|
+
? { database: services.database, secret: services.rateLimitSecret }
|
|
115
|
+
: undefined
|
|
116
|
+
)
|
|
117
|
+
)
|
|
81
118
|
.route(
|
|
82
119
|
'/',
|
|
83
120
|
createCollaborationRoutes(
|
|
@@ -97,6 +134,7 @@ export function createCloudAPIRouter(services: CloudAPIServices) {
|
|
|
97
134
|
)
|
|
98
135
|
)
|
|
99
136
|
.route('/', createDocumentVersionRoutes(services.versions))
|
|
137
|
+
.route('/', createCommentRoutes(services.comments))
|
|
100
138
|
.route(
|
|
101
139
|
'/',
|
|
102
140
|
createDocumentSharingRoutes(
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { createCloudIdentityResolver, createCloudSessionResolver } from '#cloud/server/auth'
|
|
2
|
+
import type { Context } from 'hono'
|
|
3
|
+
|
|
4
|
+
import type { CloudServices } from './services'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The routes a personal access token reaches: workspaces and their documents, uploads, versions,
|
|
8
|
+
* sharing, and Trash, and who it signs in as. Anything else, such as services tokens that spend
|
|
9
|
+
* credits or administration, takes a session, including routes added later.
|
|
10
|
+
*/
|
|
11
|
+
const ACCESS_TOKEN_API_PATHS = [
|
|
12
|
+
'/api/session',
|
|
13
|
+
'/api/workspaces',
|
|
14
|
+
'/api/documents',
|
|
15
|
+
'/api/uploads',
|
|
16
|
+
'/api/shared-documents',
|
|
17
|
+
'/api/trash'
|
|
18
|
+
]
|
|
19
|
+
const acceptsAccessToken = (path: string) =>
|
|
20
|
+
ACCESS_TOKEN_API_PATHS.some((prefix) => path === prefix || path.startsWith(`${prefix}/`))
|
|
21
|
+
|
|
22
|
+
/** Who a request comes from: anyone signed in, an account that may use the server, or may leave. */
|
|
23
|
+
export function createRequestResolvers(services: CloudServices) {
|
|
24
|
+
return {
|
|
25
|
+
resolveIdentity: services.resolveIdentity ?? createCloudIdentityResolver(services.auth),
|
|
26
|
+
resolveSession: services.resolveSession ?? createCloudSessionResolver(services.auth),
|
|
27
|
+
resolveAccountSession:
|
|
28
|
+
services.resolveAccountSession ??
|
|
29
|
+
(services.resolveSession
|
|
30
|
+
? services.resolveSession
|
|
31
|
+
: (request: Request) => services.auth.resolveAccountSession(request.headers)),
|
|
32
|
+
twoStepConfirmed:
|
|
33
|
+
services.twoStepConfirmed ??
|
|
34
|
+
(services.resolveIdentity
|
|
35
|
+
? async () => true
|
|
36
|
+
: (request: Request) => services.auth.twoStepConfirmed(request.headers))
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The checks routes put in front of themselves to name the request's actor. */
|
|
41
|
+
export function createRequestChecks(
|
|
42
|
+
services: CloudServices,
|
|
43
|
+
resolvers: ReturnType<typeof createRequestResolvers>
|
|
44
|
+
) {
|
|
45
|
+
const { resolveSession, resolveAccountSession } = resolvers
|
|
46
|
+
return {
|
|
47
|
+
requireActiveSession: async (context: Context, next: () => Promise<void>) => {
|
|
48
|
+
// A route that lets an account leave before accepting the terms named it already.
|
|
49
|
+
if (context.get('actor')) return next()
|
|
50
|
+
const actor = await resolveSession(context.req.raw)
|
|
51
|
+
if (!actor) return context.json({ error: { code: 'unauthorized' as const } }, 401)
|
|
52
|
+
context.set('actor', actor)
|
|
53
|
+
return next()
|
|
54
|
+
},
|
|
55
|
+
/**
|
|
56
|
+
* The API's caller: a session, or else a personal access token on the routes that take one
|
|
57
|
+
* (`ACCESS_TOKEN_API_PATHS`); account routes never get here.
|
|
58
|
+
*/
|
|
59
|
+
requireActiveActor: async (context: Context, next: () => Promise<void>) => {
|
|
60
|
+
if (context.get('actor')) return next()
|
|
61
|
+
const actor =
|
|
62
|
+
(await resolveSession(context.req.raw)) ??
|
|
63
|
+
(acceptsAccessToken(context.req.path)
|
|
64
|
+
? await services.auth.resolveAccessToken(context.req.raw.headers)
|
|
65
|
+
: null)
|
|
66
|
+
if (!actor) return context.json({ error: { code: 'unauthorized' as const } }, 401)
|
|
67
|
+
context.set('actor', actor)
|
|
68
|
+
return next()
|
|
69
|
+
},
|
|
70
|
+
/**
|
|
71
|
+
* An account that may leave: one that has terms to accept can still download its documents
|
|
72
|
+
* and delete itself, which nothing else lets it do until it accepts.
|
|
73
|
+
*/
|
|
74
|
+
requireAccountSession: async (context: Context, next: () => Promise<void>) => {
|
|
75
|
+
const actor = await resolveAccountSession(context.req.raw)
|
|
76
|
+
if (!actor) return context.json({ error: { code: 'unauthorized' as const } }, 401)
|
|
77
|
+
context.set('actor', actor)
|
|
78
|
+
return next()
|
|
79
|
+
},
|
|
80
|
+
/** Reading one document, which downloading everything before leaving needs, without terms. */
|
|
81
|
+
allowLeavingRead: async (context: Context, next: () => Promise<void>) => {
|
|
82
|
+
if (context.req.method === 'GET') {
|
|
83
|
+
const actor = await resolveAccountSession(context.req.raw)
|
|
84
|
+
if (actor) context.set('actor', actor)
|
|
85
|
+
}
|
|
86
|
+
return next()
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { CLOUD_DISCOVERY_PATH } from '#cloud/contract'
|
|
2
|
+
import type { MiddlewareHandler } from 'hono'
|
|
3
|
+
import { cors } from 'hono/cors'
|
|
4
|
+
|
|
5
|
+
/** The community gallery's addresses, which anyone may read from any page. */
|
|
6
|
+
const COMMUNITY_API_PATH = '/api/community'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Whether an address is a public read any page may make: the server's discovery, and the
|
|
10
|
+
* community gallery's list, documents, and thumbnails. They need no account, so they carry none.
|
|
11
|
+
*/
|
|
12
|
+
function isOpenRead(method: string, path: string): boolean {
|
|
13
|
+
if (method !== 'GET' && method !== 'HEAD' && method !== 'OPTIONS') return false
|
|
14
|
+
return (
|
|
15
|
+
path === CLOUD_DISCOVERY_PATH ||
|
|
16
|
+
path === COMMUNITY_API_PATH ||
|
|
17
|
+
path.startsWith(`${COMMUNITY_API_PATH}/`)
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Cross-origin access to the server. Its own pages and the origins it trusts send their account
|
|
23
|
+
* with every request. Any other page, such as the documentation site's landing showing the tour,
|
|
24
|
+
* may read the discovery and the gallery without one, and nothing else.
|
|
25
|
+
*/
|
|
26
|
+
export function createCloudCORS(allowedOrigins: ReadonlySet<string>): MiddlewareHandler {
|
|
27
|
+
const trusted = cors({
|
|
28
|
+
origin: (origin) => (allowedOrigins.has(origin) ? origin : null),
|
|
29
|
+
allowMethods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
|
|
30
|
+
allowHeaders: ['Content-Type', 'Authorization'],
|
|
31
|
+
credentials: true,
|
|
32
|
+
maxAge: 600
|
|
33
|
+
})
|
|
34
|
+
const openRead = cors({ origin: '*', allowMethods: ['GET', 'HEAD'], maxAge: 600 })
|
|
35
|
+
return (context, next) => {
|
|
36
|
+
const origin = context.req.header('Origin')
|
|
37
|
+
const anyPage = origin !== undefined && !allowedOrigins.has(origin)
|
|
38
|
+
// A preflight names the method it asks for; a request is its own.
|
|
39
|
+
const method = context.req.header('Access-Control-Request-Method') ?? context.req.method
|
|
40
|
+
return anyPage && isOpenRead(method, context.req.path)
|
|
41
|
+
? openRead(context, next)
|
|
42
|
+
: trusted(context, next)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { CLOUD_EDITOR_VERSION_HEADER, isEditorVersionAtLeast } from '#cloud/contract'
|
|
2
|
+
import type { MiddlewareHandler } from 'hono'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Refuses requests from a desktop editor older than the server's minimum, which would otherwise
|
|
6
|
+
* keep writing with a model the server no longer accepts. Requests that name no version, from
|
|
7
|
+
* browser editors, the CLI or MCP, pass.
|
|
8
|
+
*/
|
|
9
|
+
export function requireMinimumEditor(minimum: string | undefined): MiddlewareHandler {
|
|
10
|
+
return async (context, next) => {
|
|
11
|
+
const version = context.req.header(CLOUD_EDITOR_VERSION_HEADER)
|
|
12
|
+
if (minimum && version && !isEditorVersionAtLeast(version, minimum)) {
|
|
13
|
+
return context.json(
|
|
14
|
+
{ error: { code: 'editor_outdated' as const }, minimumEditorVersion: minimum },
|
|
15
|
+
426
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
return next()
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CLOUD_API_MAX_BODY_BYTES,
|
|
3
|
+
CLOUD_DISCOVERY_PATH,
|
|
4
|
+
CLOUD_PROTOCOL_VERSION,
|
|
5
|
+
CLOUD_SERVICES_JWKS_PATH
|
|
6
|
+
} from '#cloud/contract'
|
|
7
|
+
import type { CloudAPIEnvironment } from '#cloud/server/api'
|
|
8
|
+
import { AUTH_BASE_PATH } from '#cloud/server/auth'
|
|
9
|
+
import { createCommunityThumbnailRoutes } from '#cloud/server/community'
|
|
10
|
+
import { cloudDiscoveryFromConfig } from '#cloud/server/config'
|
|
11
|
+
import { cloudErrorHandler } from '#cloud/server/errors'
|
|
12
|
+
import { cloudRequestId } from '#cloud/server/logging'
|
|
13
|
+
import { createPictureRoutes } from '#cloud/server/pictures'
|
|
14
|
+
import { Hono } from 'hono'
|
|
15
|
+
import { bodyLimit } from 'hono/body-limit'
|
|
16
|
+
|
|
17
|
+
import { createRequestChecks, createRequestResolvers } from './checks'
|
|
18
|
+
import { createCloudCORS } from './cors'
|
|
19
|
+
import { requireMinimumEditor } from './editors'
|
|
20
|
+
import { robotsText, shouldPreventIndexing } from './indexing'
|
|
21
|
+
import { createAppRateLimiters } from './limiters'
|
|
22
|
+
import { createAppRouters } from './routes'
|
|
23
|
+
import { createAppServices, type CloudServices } from './services'
|
|
24
|
+
|
|
25
|
+
export { shouldPreventIndexing, withIndexingPolicy } from './indexing'
|
|
26
|
+
export type { CloudServices } from './services'
|
|
27
|
+
|
|
28
|
+
/** Where administration is mounted. */
|
|
29
|
+
const ADMIN_API_PATH = '/api/admin'
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The Cloud server over a runtime's services. The order the routes and checks are registered in
|
|
33
|
+
* is what guards them: rate limiters come before the routes they count, the public API before
|
|
34
|
+
* the checks that require an account, and administration before the general write limiter.
|
|
35
|
+
*/
|
|
36
|
+
export function createCloudApp(services: CloudServices) {
|
|
37
|
+
const { config } = services
|
|
38
|
+
const discovery = cloudDiscoveryFromConfig(config)
|
|
39
|
+
const allowedOrigins = new Set(
|
|
40
|
+
[config.publicURL, ...config.trustedOrigins].map((origin) => new URL(origin).origin)
|
|
41
|
+
)
|
|
42
|
+
const cloudCORS = createCloudCORS(allowedOrigins)
|
|
43
|
+
const app = createAppServices(services)
|
|
44
|
+
const resolvers = createRequestResolvers(services)
|
|
45
|
+
const checks = createRequestChecks(services, resolvers)
|
|
46
|
+
const limiters = createAppRateLimiters(services.database, config)
|
|
47
|
+
const routers = createAppRouters(services, app, resolvers, checks, limiters, allowedOrigins)
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
new Hono<CloudAPIEnvironment>()
|
|
51
|
+
.onError(cloudErrorHandler(services.reportError))
|
|
52
|
+
.use('*', cloudRequestId(services.forwardedRequestId))
|
|
53
|
+
// Discovery is public, so any editor's page may read it without credentials: an editor on
|
|
54
|
+
// another address learns whether the server lists it before it signs in.
|
|
55
|
+
.use(CLOUD_DISCOVERY_PATH, cloudCORS)
|
|
56
|
+
.use('/api/*', cloudCORS)
|
|
57
|
+
.use('/api/*', requireMinimumEditor(config.minimumEditorVersion))
|
|
58
|
+
.use(
|
|
59
|
+
'/api/*',
|
|
60
|
+
bodyLimit({
|
|
61
|
+
maxSize: CLOUD_API_MAX_BODY_BYTES,
|
|
62
|
+
onError: (context) => context.json({ error: { code: 'invalid_request' as const } }, 413)
|
|
63
|
+
})
|
|
64
|
+
)
|
|
65
|
+
.use('*', async (context, next) => {
|
|
66
|
+
await next()
|
|
67
|
+
if (shouldPreventIndexing(context.req.path, config.indexingPolicy)) {
|
|
68
|
+
context.header('X-Robots-Tag', 'noindex, nofollow, noarchive')
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
.get('/robots.txt', (context) => {
|
|
72
|
+
context.header('Content-Type', 'text/plain; charset=utf-8')
|
|
73
|
+
return context.body(robotsText(config.indexingPolicy))
|
|
74
|
+
})
|
|
75
|
+
.get('/health', (context) =>
|
|
76
|
+
context.json({
|
|
77
|
+
status: 'ok' as const,
|
|
78
|
+
protocolVersion: CLOUD_PROTOCOL_VERSION
|
|
79
|
+
})
|
|
80
|
+
)
|
|
81
|
+
.get('/ready', async (context) => {
|
|
82
|
+
try {
|
|
83
|
+
await services.database.selectFrom('workspace').select('id').limit(1).execute()
|
|
84
|
+
const objects = await services.objects.checkReadiness()
|
|
85
|
+
if (!objects.ok) {
|
|
86
|
+
return context.json(
|
|
87
|
+
{ status: 'unavailable' as const, dependency: 'object_storage' as const },
|
|
88
|
+
503
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
return context.json({
|
|
92
|
+
status: 'ready' as const,
|
|
93
|
+
objectStorage: {
|
|
94
|
+
checksumVerification: objects.checksumVerification,
|
|
95
|
+
multipartUpload: services.objects.capabilities.multipartUpload
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
} catch {
|
|
99
|
+
return context.json({ status: 'unavailable' as const }, 503)
|
|
100
|
+
}
|
|
101
|
+
})
|
|
102
|
+
.get(CLOUD_DISCOVERY_PATH, (context) => context.json(discovery))
|
|
103
|
+
.get(CLOUD_SERVICES_JWKS_PATH, async (context) =>
|
|
104
|
+
app.servicesTokens ? context.json(app.servicesTokens.jwks()) : context.notFound()
|
|
105
|
+
)
|
|
106
|
+
.on(['GET', 'POST'], `${AUTH_BASE_PATH}/*`, (context) =>
|
|
107
|
+
services.auth.handler(context.req.raw)
|
|
108
|
+
)
|
|
109
|
+
.route('/', routers.account)
|
|
110
|
+
.route('/', routers.accessTokens)
|
|
111
|
+
.route('/', createPictureRoutes(app.pictures))
|
|
112
|
+
.use('/api/shares/:shareId/collaboration-ticket', limiters.publicCollaborationLimiter)
|
|
113
|
+
.use('/api/public/*', limiters.publicRateLimiter)
|
|
114
|
+
.use('/api/shares/*', limiters.publicRateLimiter)
|
|
115
|
+
.use('/api/invitations/*', limiters.publicRateLimiter)
|
|
116
|
+
.use('/api/community/*', limiters.communityLimiter, limiters.publicRateLimiter)
|
|
117
|
+
.route('/', createCommunityThumbnailRoutes(app.community))
|
|
118
|
+
.route('/api', routers.publicCloudAPI)
|
|
119
|
+
.route('/api/internal', routers.internalAPI)
|
|
120
|
+
.use('/api/documents/:documentId', checks.allowLeavingRead)
|
|
121
|
+
.use('/api/*', checks.requireActiveActor)
|
|
122
|
+
.route(ADMIN_API_PATH, routers.admin)
|
|
123
|
+
.use('/api/*', limiters.authenticatedMutationLimiter)
|
|
124
|
+
.route('/api', routers.cloudAPI)
|
|
125
|
+
)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export type CloudApp = ReturnType<typeof createCloudApp>
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { CLOUD_DISCOVERY_PATH } from '#cloud/contract'
|
|
2
|
+
|
|
3
|
+
export function shouldPreventIndexing(path: string, policy: 'allow' | 'deny'): boolean {
|
|
4
|
+
return (
|
|
5
|
+
policy === 'deny' ||
|
|
6
|
+
path.startsWith('/api/') ||
|
|
7
|
+
path.startsWith('/admin') ||
|
|
8
|
+
path.startsWith('/auth') ||
|
|
9
|
+
path.startsWith('/account') ||
|
|
10
|
+
path === '/app' ||
|
|
11
|
+
path === '/sign-in' ||
|
|
12
|
+
path === '/sign-up' ||
|
|
13
|
+
path === '/join' ||
|
|
14
|
+
path === CLOUD_DISCOVERY_PATH ||
|
|
15
|
+
// A community document in the editor; its page at `/community/:id` is the one to index.
|
|
16
|
+
/^\/community\/[^/]+\/open\/?$/.test(path)
|
|
17
|
+
)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function withIndexingPolicy(
|
|
21
|
+
response: Response,
|
|
22
|
+
path: string,
|
|
23
|
+
policy: 'allow' | 'deny'
|
|
24
|
+
): Response {
|
|
25
|
+
if (!shouldPreventIndexing(path, policy)) return response
|
|
26
|
+
const headers = new Headers(response.headers)
|
|
27
|
+
headers.set('X-Robots-Tag', 'noindex, nofollow, noarchive')
|
|
28
|
+
return new Response(response.body, {
|
|
29
|
+
status: response.status,
|
|
30
|
+
statusText: response.statusText,
|
|
31
|
+
headers
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** What `/robots.txt` answers: everything closed, or only the server's own pages and API. */
|
|
36
|
+
export function robotsText(policy: 'allow' | 'deny'): string {
|
|
37
|
+
return policy === 'deny'
|
|
38
|
+
? 'User-agent: *\nDisallow: /\n'
|
|
39
|
+
: 'User-agent: *\nDisallow: /admin\nDisallow: /account\nDisallow: /app\nDisallow: /api\nDisallow: /auth\nDisallow: /sign-in\nDisallow: /sign-up\nDisallow: /join\nDisallow: /.well-known\n'
|
|
40
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { CloudServerConfig } from '#cloud/server/config'
|
|
2
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
3
|
+
import {
|
|
4
|
+
CLOUD_RATE_LIMITS,
|
|
5
|
+
createActorRateLimiter,
|
|
6
|
+
createTrustedIPRateLimiter,
|
|
7
|
+
routeHasOwnBudget,
|
|
8
|
+
type RateLimitPolicy
|
|
9
|
+
} from '#cloud/server/rate-limit'
|
|
10
|
+
import type { Kysely } from 'kysely'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The app's rate limiters. The public ones count by trusted client IP and answer an exhausted
|
|
14
|
+
* budget as not found, so a capability's existence never leaks; the authenticated one counts by
|
|
15
|
+
* actor and leaves reads and routes with a budget of their own alone.
|
|
16
|
+
*/
|
|
17
|
+
export function createAppRateLimiters(database: Kysely<CloudDatabase>, config: CloudServerConfig) {
|
|
18
|
+
/** A limiter for requests without an account, by trusted client IP. */
|
|
19
|
+
const publicLimiter = (
|
|
20
|
+
policy: RateLimitPolicy,
|
|
21
|
+
options: Parameters<typeof createTrustedIPRateLimiter>[5]
|
|
22
|
+
) =>
|
|
23
|
+
createTrustedIPRateLimiter(
|
|
24
|
+
database,
|
|
25
|
+
config.authSecret,
|
|
26
|
+
policy,
|
|
27
|
+
{ trustedHeaders: config.authTrustedIPHeaders },
|
|
28
|
+
(context) => context.json({ error: { code: 'not_found' as const } }, 404),
|
|
29
|
+
options
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
publicRateLimiter: publicLimiter(CLOUD_RATE_LIMITS.publicCapability, {
|
|
34
|
+
resource: (context) => context.req.path,
|
|
35
|
+
// Guests refresh collaboration tickets on their own budget, `publicCollaborationLimiter`.
|
|
36
|
+
skip: routeHasOwnBudget
|
|
37
|
+
}),
|
|
38
|
+
publicCollaborationLimiter: publicLimiter(CLOUD_RATE_LIMITS.collaborationTicket, {
|
|
39
|
+
resource: (context) => context.req.path,
|
|
40
|
+
route: true
|
|
41
|
+
}),
|
|
42
|
+
// The gallery's own budget, across its paths, in place of the per-path public one.
|
|
43
|
+
communityLimiter: publicLimiter(CLOUD_RATE_LIMITS.communityRead, { route: true }),
|
|
44
|
+
authenticatedMutationLimiter: createActorRateLimiter(
|
|
45
|
+
database,
|
|
46
|
+
config.authSecret,
|
|
47
|
+
CLOUD_RATE_LIMITS.authenticatedMutation,
|
|
48
|
+
// Reads are free, and a route with a budget of its own, such as creating documents, counts
|
|
49
|
+
// against that instead.
|
|
50
|
+
(context) => context.req.method === 'GET' || routeHasOwnBudget(context)
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createAdminAuditService,
|
|
3
|
+
createAdminDocumentService,
|
|
4
|
+
createAdminEmailService,
|
|
5
|
+
createAdminOperationsService,
|
|
6
|
+
createAdminReportService,
|
|
7
|
+
createAdminUserService,
|
|
8
|
+
createCloudAdminRoutes
|
|
9
|
+
} from '#cloud/admin'
|
|
10
|
+
import { createAccountDeletionService } from '#cloud/server/account/deletion'
|
|
11
|
+
import { createAccountRoutes } from '#cloud/server/account/routes'
|
|
12
|
+
import { createTermsService } from '#cloud/server/account/terms'
|
|
13
|
+
import { createAccessTokenRoutes } from '#cloud/server/account/tokens'
|
|
14
|
+
import { createCloudAPIRouter, createPublicCloudAPIRouter } from '#cloud/server/api'
|
|
15
|
+
import { createAuthenticationEmailService } from '#cloud/server/auth'
|
|
16
|
+
import { signInProviders } from '#cloud/server/auth/methods'
|
|
17
|
+
import { createInternalRoutes, createInternalService } from '#cloud/server/internal'
|
|
18
|
+
import { Hono } from 'hono'
|
|
19
|
+
|
|
20
|
+
import type { createRequestChecks, createRequestResolvers } from './checks'
|
|
21
|
+
import type { createAppRateLimiters } from './limiters'
|
|
22
|
+
import type { CloudAppServices, CloudServices } from './services'
|
|
23
|
+
|
|
24
|
+
/** The routers the app mounts, each over the services, checks, and limiters it needs. */
|
|
25
|
+
export function createAppRouters(
|
|
26
|
+
services: CloudServices,
|
|
27
|
+
app: CloudAppServices,
|
|
28
|
+
resolvers: ReturnType<typeof createRequestResolvers>,
|
|
29
|
+
checks: ReturnType<typeof createRequestChecks>,
|
|
30
|
+
limiters: ReturnType<typeof createAppRateLimiters>,
|
|
31
|
+
allowedOrigins: ReadonlySet<string>
|
|
32
|
+
) {
|
|
33
|
+
const { config, database } = services
|
|
34
|
+
const { resolveIdentity, resolveSession, twoStepConfirmed } = resolvers
|
|
35
|
+
const admin = createCloudAdminRoutes(
|
|
36
|
+
{
|
|
37
|
+
email: createAdminEmailService(database, services.transactionalEmail, {
|
|
38
|
+
publicURL: config.publicURL
|
|
39
|
+
}),
|
|
40
|
+
enrollment: app.enrollment,
|
|
41
|
+
users: createAdminUserService(database, services.auth, {
|
|
42
|
+
requireMFA: config.deploymentAdminMFARequired,
|
|
43
|
+
approval: config.enrollmentMode === 'approval',
|
|
44
|
+
signInProviders: signInProviders(config),
|
|
45
|
+
pictures: app.pictures,
|
|
46
|
+
notify: services.transactionalEmail
|
|
47
|
+
? createAuthenticationEmailService(services.transactionalEmail, config.publicURL)
|
|
48
|
+
: undefined
|
|
49
|
+
}),
|
|
50
|
+
audit: createAdminAuditService(database),
|
|
51
|
+
operations: createAdminOperationsService(database, config),
|
|
52
|
+
documents: createAdminDocumentService(database, {
|
|
53
|
+
closeRooms: services.onDocumentTakenDown,
|
|
54
|
+
purgeGraceMs: config.documentPurgeGraceMs,
|
|
55
|
+
communityWorkspaceId: config.community?.workspaceId ?? null,
|
|
56
|
+
objects: services.objects,
|
|
57
|
+
documents: app.documents,
|
|
58
|
+
email: services.transactionalEmail,
|
|
59
|
+
publicURL: config.publicURL,
|
|
60
|
+
contactEmail: config.legal?.abuseEmail
|
|
61
|
+
}),
|
|
62
|
+
reports: createAdminReportService(
|
|
63
|
+
database,
|
|
64
|
+
{ email: services.transactionalEmail, contactEmail: config.legal?.abuseEmail },
|
|
65
|
+
{ communityWorkspaceId: config.community?.workspaceId ?? null }
|
|
66
|
+
)
|
|
67
|
+
},
|
|
68
|
+
allowedOrigins,
|
|
69
|
+
{
|
|
70
|
+
database,
|
|
71
|
+
secret: config.authSecret,
|
|
72
|
+
auth: services.auth,
|
|
73
|
+
requireMFA: config.deploymentAdminMFARequired
|
|
74
|
+
}
|
|
75
|
+
)
|
|
76
|
+
const account = createAccountRoutes({
|
|
77
|
+
auth: services.auth,
|
|
78
|
+
terms: createTermsService(database, config),
|
|
79
|
+
pictures: app.pictures,
|
|
80
|
+
config,
|
|
81
|
+
enrollment: app.enrollment,
|
|
82
|
+
workspaces: app.workspaces,
|
|
83
|
+
deletion: createAccountDeletionService(database),
|
|
84
|
+
resolveIdentity,
|
|
85
|
+
resolveSession,
|
|
86
|
+
twoStepConfirmed,
|
|
87
|
+
requireActiveSession: checks.requireActiveSession,
|
|
88
|
+
requireAccountSession: checks.requireAccountSession,
|
|
89
|
+
authenticatedMutationLimiter: limiters.authenticatedMutationLimiter
|
|
90
|
+
})
|
|
91
|
+
const accessTokens = createAccessTokenRoutes({
|
|
92
|
+
auth: services.auth,
|
|
93
|
+
resolveSession,
|
|
94
|
+
requireActiveSession: checks.requireActiveSession,
|
|
95
|
+
authenticatedMutationLimiter: limiters.authenticatedMutationLimiter
|
|
96
|
+
})
|
|
97
|
+
const shared = {
|
|
98
|
+
collaboration: app.collaboration,
|
|
99
|
+
documents: app.documents,
|
|
100
|
+
versions: app.versions,
|
|
101
|
+
comments: app.comments,
|
|
102
|
+
community: app.community,
|
|
103
|
+
entitlements: app.entitlements,
|
|
104
|
+
sharing: app.sharing,
|
|
105
|
+
reports: app.reports,
|
|
106
|
+
workspaces: app.workspaces,
|
|
107
|
+
workspaceMembers: app.workspaceMembers,
|
|
108
|
+
resolveSession
|
|
109
|
+
}
|
|
110
|
+
const cloudAPI = createCloudAPIRouter({
|
|
111
|
+
...shared,
|
|
112
|
+
database,
|
|
113
|
+
rateLimitSecret: config.authSecret,
|
|
114
|
+
servicesTokens: app.servicesTokens,
|
|
115
|
+
billingTokens: app.billingTokens
|
|
116
|
+
})
|
|
117
|
+
const publicCloudAPI = createPublicCloudAPIRouter(shared)
|
|
118
|
+
// Services such as billing authenticate with their own token, not a session.
|
|
119
|
+
const internalAPI = config.internalServiceToken
|
|
120
|
+
? createInternalRoutes(createInternalService(database), config.internalServiceToken)
|
|
121
|
+
: new Hono()
|
|
122
|
+
return { admin, account, accessTokens, cloudAPI, publicCloudAPI, internalAPI }
|
|
123
|
+
}
|