@open-pencil/cloud 0.17.0-beta.5 → 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/dist/client.d.ts +560 -93
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1101 -186
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +390 -44
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +366 -3
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +1403 -556
- package/dist/email.js.map +1 -1
- package/dist/errors.js +16 -3
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +878 -37
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +12 -2
- package/dist/index2.d.ts.map +1 -1
- package/dist/listen.mjs +589 -128
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +4 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -1
- package/dist/runtime-cloudflare.js +8689 -6324
- package/dist/runtime-cloudflare.js.map +1 -1
- package/dist/runtime-node.d.mts +10098 -5401
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/server.d.ts +14078 -7909
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +11723 -9412
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +10658 -8649
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +79 -57
- package/dist/server2.mjs.map +1 -1
- package/package.json +13 -4
- package/src/admin/api/routes.ts +263 -190
- package/src/admin/audit/service.ts +9 -1
- package/src/admin/bootstrap.ts +86 -0
- package/src/admin/documents/service.ts +119 -4
- 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 +9 -3
- package/src/admin/schema.ts +6 -5
- package/src/admin/users/service.ts +173 -16
- package/src/client/api.ts +29 -2
- package/src/client/comments.ts +205 -0
- package/src/client/device-authorization.ts +4 -20
- package/src/client/discovery.ts +4 -2
- 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 +3 -6
- package/src/client/portal/account.ts +20 -2
- package/src/client/portal/administration.ts +52 -5
- package/src/client/portal/request.ts +4 -2
- package/src/client/tour.ts +44 -0
- package/src/client/versions.ts +7 -1
- package/src/contract/account.ts +61 -11
- package/src/contract/admin/index.ts +48 -3
- package/src/contract/billing.ts +73 -4
- package/src/contract/comments.ts +223 -0
- package/src/contract/discovery.ts +47 -19
- package/src/contract/index.ts +10 -0
- package/src/contract/links.ts +5 -0
- package/src/contract/relay.ts +3 -1
- package/src/contract/services.ts +44 -4
- package/src/contract/workspaces.ts +5 -0
- package/src/email/locales/ar.json +28 -0
- package/src/email/locales/de.json +28 -0
- package/src/email/locales/es.json +28 -0
- package/src/email/locales/fr.json +28 -0
- package/src/email/locales/it.json +28 -0
- package/src/email/locales/ja.json +28 -0
- package/src/email/locales/pl.json +28 -0
- package/src/email/locales/pt-br.json +28 -0
- package/src/email/locales/ru.json +28 -0
- package/src/email/locales/zh-cn.json +28 -0
- package/src/email/messages.ts +35 -0
- package/src/email/render.ts +18 -0
- package/src/email/samples.ts +28 -1
- package/src/email/schema.ts +29 -3
- package/src/email/templates/BrandEmail.vue +3 -1
- package/src/email/templates/CommentNotification.vue +39 -0
- package/src/email/templates/EnrollmentInvitation.vue +53 -0
- package/src/email/templates/TwoStepChanged.vue +17 -2
- package/src/email/templates/brand.ts +6 -0
- package/src/errors/posthog.ts +28 -4
- package/src/runtime/cloudflare/email.ts +2 -0
- package/src/runtime/cloudflare/relay/index.ts +21 -6
- package/src/runtime/cloudflare/worker.ts +6 -0
- package/src/runtime/cloudflare/xml.ts +12 -0
- package/src/runtime/node/admin.ts +51 -47
- package/src/runtime/node/email/runtime.ts +1 -0
- package/src/runtime/node/email/transport.ts +1 -0
- package/src/runtime/node/server.ts +8 -2
- package/src/runtime/posthog/errors.ts +3 -1
- package/src/server/account/actions.ts +3 -1
- package/src/server/account/deletion.ts +38 -4
- package/src/server/account/routes.ts +67 -46
- package/src/server/account/tokens.ts +3 -3
- package/src/server/api/router.ts +22 -7
- package/src/server/app/checks.ts +6 -1
- package/src/server/app/cors.ts +44 -0
- package/src/server/app/index.ts +76 -76
- package/src/server/app/routes.ts +14 -5
- package/src/server/app/services.ts +49 -15
- package/src/server/auth/access-control.ts +13 -8
- package/src/server/auth/account.ts +13 -12
- package/src/server/auth/activity.ts +7 -2
- package/src/server/auth/adapter.ts +16 -3
- package/src/server/auth/email.ts +29 -17
- package/src/server/auth/endpoints.ts +4 -0
- package/src/server/auth/factory.ts +83 -64
- package/src/server/auth/index.ts +1 -1
- package/src/server/auth/methods.ts +162 -0
- package/src/server/auth/mfa-assurance.ts +147 -10
- package/src/server/auth/rate-limit.ts +31 -0
- package/src/server/auth/runtime.ts +19 -5
- package/src/server/auth/social-providers.ts +70 -24
- package/src/server/billing/index.ts +2 -0
- package/src/server/billing/notify.ts +55 -0
- package/src/server/billing/seats.ts +17 -0
- package/src/server/billing/tokens.ts +1 -1
- 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/collaboration/relay/connection.ts +7 -2
- package/src/server/collaboration/relay/hub.ts +55 -24
- package/src/server/collaboration/routes.ts +8 -5
- 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/routes.ts +6 -6
- package/src/server/config/deployment.ts +19 -6
- package/src/server/config/index.ts +1 -1
- package/src/server/config/public.ts +19 -6
- package/src/server/config/schema.ts +14 -2
- package/src/server/config/toml/schema.ts +27 -4
- package/src/server/db/index.ts +2 -1
- package/src/server/db/migrate.ts +21 -1
- 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/tables.ts +92 -2
- package/src/server/documents/access.ts +17 -0
- package/src/server/documents/images.ts +6 -1
- package/src/server/documents/routes.ts +12 -12
- package/src/server/documents/service.ts +19 -5
- package/src/server/documents/summary.ts +15 -0
- package/src/server/documents/versions/routes.ts +7 -8
- package/src/server/documents/versions/service.ts +5 -2
- package/src/server/email/service.ts +8 -2
- package/src/server/email/types.ts +1 -0
- package/src/server/errors/index.ts +48 -3
- package/src/server/index.ts +15 -1
- 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/pictures/service.ts +3 -1
- package/src/server/policy/workspace.ts +5 -2
- package/src/server/reports/routes.ts +2 -2
- package/src/server/reports/service.ts +3 -1
- package/src/server/services/routes.ts +2 -2
- package/src/server/sharing/capabilities/service.ts +1 -1
- package/src/server/sharing/document-routes.ts +3 -3
- package/src/server/sharing/invitations/service.ts +3 -4
- package/src/server/sharing/routes.ts +15 -15
- package/src/server/validation.ts +33 -25
- package/src/server/workspaces/members.ts +50 -12
- package/src/server/workspaces/repository.ts +23 -12
- package/src/server/workspaces/routes.ts +8 -8
- package/src/services/app.ts +30 -19
- package/src/services/config.ts +47 -8
- package/src/services/metering.ts +15 -27
- package/src/client/billing.ts +0 -80
|
@@ -4,15 +4,17 @@ import type {
|
|
|
4
4
|
CloudIdentityResolver,
|
|
5
5
|
CloudSessionResolver
|
|
6
6
|
} from '#cloud/server/auth'
|
|
7
|
-
import {
|
|
7
|
+
import { enrollmentEmailOptions } from '#cloud/server/auth'
|
|
8
|
+
import { createBillingTokenService, createSeatNotifier } from '#cloud/server/billing'
|
|
8
9
|
import { createCollaborationTicketService } from '#cloud/server/collaboration'
|
|
10
|
+
import { createCommentService } from '#cloud/server/comments'
|
|
9
11
|
import { createCommunityService } from '#cloud/server/community'
|
|
10
12
|
import { CloudConfigError, type CloudServerConfig } from '#cloud/server/config'
|
|
11
13
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
12
14
|
import { createDocumentService } from '#cloud/server/documents'
|
|
13
15
|
import { createDocumentVersionService } from '#cloud/server/documents/versions'
|
|
14
16
|
import type { TransactionalEmailService } from '#cloud/server/email'
|
|
15
|
-
import type
|
|
17
|
+
import { reportCloudError, type CloudErrorReporter } from '#cloud/server/errors'
|
|
16
18
|
import type { InvitationDelivery, InvitationOutbox } from '#cloud/server/invitations'
|
|
17
19
|
import type { ForwardedRequestId } from '#cloud/server/logging'
|
|
18
20
|
import type { ObjectStore } from '#cloud/server/objects'
|
|
@@ -43,6 +45,11 @@ export type CloudServices = {
|
|
|
43
45
|
* documents and deleting the account. Defaults to `resolveSession` when that is given.
|
|
44
46
|
*/
|
|
45
47
|
resolveAccountSession?: CloudSessionResolver
|
|
48
|
+
/**
|
|
49
|
+
* Whether the request's session proved its account's second step, or the account has none.
|
|
50
|
+
* Defaults to the auth adapter's, or to yes when `resolveIdentity` stands in for signing in.
|
|
51
|
+
*/
|
|
52
|
+
twoStepConfirmed?: (request: Request) => Promise<boolean>
|
|
46
53
|
invitationDelivery?: InvitationDelivery
|
|
47
54
|
invitationOutbox?: InvitationOutbox
|
|
48
55
|
transactionalEmail?: TransactionalEmailService
|
|
@@ -62,6 +69,13 @@ export type CloudServices = {
|
|
|
62
69
|
onDocumentTakenDown?: (documentId: string) => void
|
|
63
70
|
/** Tells a document's live rooms its version history changed, on the relay this runtime runs. */
|
|
64
71
|
onDocumentVersionsChanged?: (documentId: string) => void
|
|
72
|
+
/** Tells a document's live rooms one of its comment threads changed, on this runtime's relay. */
|
|
73
|
+
onDocumentCommentsChanged?: (documentId: string, threadId: string) => void
|
|
74
|
+
/**
|
|
75
|
+
* Keeps work a request started running after its answer, as Workers' `waitUntil` does; without
|
|
76
|
+
* it the work runs on its own, as Node lets it.
|
|
77
|
+
*/
|
|
78
|
+
runInBackground?: (task: Promise<unknown>) => void
|
|
65
79
|
/** Logs errors no route handled, and reports them when the deployment configures error tracking. */
|
|
66
80
|
reportError?: CloudErrorReporter
|
|
67
81
|
/** The request ID this runtime trusts from what stands in front of it, such as `cf-ray`. */
|
|
@@ -73,17 +87,37 @@ export function createAppServices(services: CloudServices) {
|
|
|
73
87
|
const { config, database, objects } = services
|
|
74
88
|
const workspaces = createWorkspaceService(database)
|
|
75
89
|
const pictures = createUserPictures(database, { publicURL: config.publicURL })
|
|
90
|
+
const billingTokens =
|
|
91
|
+
services.servicesSigningKey && config.billingEndpoint
|
|
92
|
+
? createBillingTokenService({
|
|
93
|
+
issuer: new URL(config.publicURL).origin,
|
|
94
|
+
signingKey: services.servicesSigningKey,
|
|
95
|
+
lifetimeSeconds: config.servicesTokenLifetimeSeconds
|
|
96
|
+
})
|
|
97
|
+
: undefined
|
|
98
|
+
// Billing charges for the people who can edit, so it hears when they change.
|
|
99
|
+
const seatNotifier =
|
|
100
|
+
billingTokens && config.billingEndpoint
|
|
101
|
+
? createSeatNotifier({ database, endpoint: config.billingEndpoint, tokens: billingTokens })
|
|
102
|
+
: undefined
|
|
103
|
+
const onSeatsChanged = seatNotifier
|
|
104
|
+
? (accountIds: readonly string[]) => {
|
|
105
|
+
const task = seatNotifier.notify(accountIds).catch((error: unknown) => {
|
|
106
|
+
void reportCloudError(services.reportError, 'Telling billing about seats failed', error, {
|
|
107
|
+
source: 'background'
|
|
108
|
+
})
|
|
109
|
+
})
|
|
110
|
+
if (services.runInBackground) services.runInBackground(task)
|
|
111
|
+
}
|
|
112
|
+
: undefined
|
|
76
113
|
const workspaceMembers = createWorkspaceMemberService(database, {
|
|
77
114
|
email: services.transactionalEmail,
|
|
78
|
-
publicURL: config.publicURL
|
|
115
|
+
publicURL: config.publicURL,
|
|
116
|
+
onSeatsChanged
|
|
79
117
|
})
|
|
80
118
|
const enrollment =
|
|
81
119
|
services.enrollment ??
|
|
82
|
-
createEnrollmentService(database,
|
|
83
|
-
publicURL: config.publicURL,
|
|
84
|
-
adminRecipients: config.enrollmentAdminNotificationEmails,
|
|
85
|
-
email: services.transactionalEmail
|
|
86
|
-
})
|
|
120
|
+
createEnrollmentService(database, enrollmentEmailOptions(config, services.transactionalEmail))
|
|
87
121
|
const policy =
|
|
88
122
|
services.policy ??
|
|
89
123
|
(services.entitlementSource
|
|
@@ -150,14 +184,13 @@ export function createAppServices(services: CloudServices) {
|
|
|
150
184
|
lifetimeSeconds: config.servicesTokenLifetimeSeconds
|
|
151
185
|
})
|
|
152
186
|
: undefined
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
lifetimeSeconds: config.servicesTokenLifetimeSeconds
|
|
159
|
-
})
|
|
187
|
+
const comments = createCommentService(database, {
|
|
188
|
+
sharing,
|
|
189
|
+
onCommentsChanged: services.onDocumentCommentsChanged,
|
|
190
|
+
notifier: services.transactionalEmail
|
|
191
|
+
? { email: services.transactionalEmail, publicURL: config.publicURL }
|
|
160
192
|
: undefined
|
|
193
|
+
})
|
|
161
194
|
const reports = createDocumentReportService(database, {
|
|
162
195
|
sharing,
|
|
163
196
|
email: services.transactionalEmail,
|
|
@@ -178,6 +211,7 @@ export function createAppServices(services: CloudServices) {
|
|
|
178
211
|
collaboration,
|
|
179
212
|
servicesTokens,
|
|
180
213
|
billingTokens,
|
|
214
|
+
comments,
|
|
181
215
|
reports
|
|
182
216
|
}
|
|
183
217
|
}
|
|
@@ -3,8 +3,11 @@ import { createAccessControl } from 'better-auth/plugins/access'
|
|
|
3
3
|
|
|
4
4
|
export const accessStatements = {
|
|
5
5
|
workspace: ['read', 'update', 'delete', 'invite', 'manage-members'],
|
|
6
|
-
/**
|
|
7
|
-
|
|
6
|
+
/**
|
|
7
|
+
* `delete` moves a document to Trash and back; `purge` deletes it from Trash for good.
|
|
8
|
+
* `comment` writes comments, which everyone who can read may; `moderate` deletes anyone's.
|
|
9
|
+
*/
|
|
10
|
+
document: ['create', 'read', 'comment', 'edit', 'moderate', 'share', 'delete', 'purge']
|
|
8
11
|
} as const
|
|
9
12
|
|
|
10
13
|
type AccessStatements = typeof accessStatements
|
|
@@ -23,15 +26,15 @@ export const accessControl = createAccessControl(accessStatements)
|
|
|
23
26
|
export const workspaceRoles = {
|
|
24
27
|
admin: accessControl.newRole({
|
|
25
28
|
workspace: ['read', 'update', 'delete', 'invite', 'manage-members'],
|
|
26
|
-
document: ['create', 'read', 'edit', 'share', 'delete', 'purge']
|
|
29
|
+
document: ['create', 'read', 'comment', 'edit', 'moderate', 'share', 'delete', 'purge']
|
|
27
30
|
}),
|
|
28
31
|
editor: accessControl.newRole({
|
|
29
32
|
workspace: ['read'],
|
|
30
|
-
document: ['create', 'read', 'edit', 'share', 'delete']
|
|
33
|
+
document: ['create', 'read', 'comment', 'edit', 'moderate', 'share', 'delete']
|
|
31
34
|
}),
|
|
32
35
|
viewer: accessControl.newRole({
|
|
33
36
|
workspace: ['read'],
|
|
34
|
-
document: ['read']
|
|
37
|
+
document: ['read', 'comment']
|
|
35
38
|
})
|
|
36
39
|
} satisfies Record<WorkspaceRole, unknown>
|
|
37
40
|
|
|
@@ -41,9 +44,11 @@ export const workspaceRoles = {
|
|
|
41
44
|
* owner's document. Deleting from Trash for good is for the owner and workspace administrators.
|
|
42
45
|
*/
|
|
43
46
|
export const documentRoles = {
|
|
44
|
-
owner: accessControl.newRole({
|
|
45
|
-
|
|
46
|
-
|
|
47
|
+
owner: accessControl.newRole({
|
|
48
|
+
document: ['read', 'comment', 'edit', 'moderate', 'share', 'delete', 'purge']
|
|
49
|
+
}),
|
|
50
|
+
editor: accessControl.newRole({ document: ['read', 'comment', 'edit', 'moderate', 'share'] }),
|
|
51
|
+
viewer: accessControl.newRole({ document: ['read', 'comment'] })
|
|
47
52
|
}
|
|
48
53
|
|
|
49
54
|
export type DocumentRole = keyof typeof documentRoles
|
|
@@ -1,29 +1,30 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
isKnownAuthenticationProvider,
|
|
3
|
+
type CloudAuthenticationMethod,
|
|
4
|
+
type CloudSocialProvider
|
|
4
5
|
} from '#cloud/contract/account'
|
|
5
|
-
import {
|
|
6
|
+
import type { CloudServerConfig } from '#cloud/server/config'
|
|
6
7
|
|
|
7
|
-
import type { CloudAuthAdapter } from './adapter'
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
provider === 'credential' || provider === 'google' || provider === 'apple'
|
|
8
|
+
import type { CloudAuthAdapter, SocialLinkStart } from './adapter'
|
|
9
|
+
import { canRemoveSignInMethod, signInProviders } from './methods'
|
|
10
|
+
import { configuredSocialProviders } from './social-providers'
|
|
11
11
|
|
|
12
12
|
export function createAccountAuthenticationService(
|
|
13
13
|
auth: CloudAuthAdapter,
|
|
14
14
|
config: CloudServerConfig
|
|
15
15
|
) {
|
|
16
|
+
const providers = signInProviders(config)
|
|
16
17
|
return {
|
|
17
18
|
async methods(headers: Headers) {
|
|
18
19
|
const accounts = await auth.listAuthenticationMethods(headers)
|
|
19
20
|
const methods = accounts.flatMap((account): CloudAuthenticationMethod[] => {
|
|
20
|
-
if (!
|
|
21
|
+
if (!isKnownAuthenticationProvider(account.providerId)) return []
|
|
21
22
|
return [
|
|
22
23
|
{
|
|
23
24
|
id: account.id,
|
|
24
25
|
provider: account.providerId,
|
|
25
26
|
createdAt: account.createdAt.toISOString(),
|
|
26
|
-
canUnlink: accounts
|
|
27
|
+
canUnlink: canRemoveSignInMethod(account, accounts, providers)
|
|
27
28
|
}
|
|
28
29
|
]
|
|
29
30
|
})
|
|
@@ -42,8 +43,8 @@ export function createAccountAuthenticationService(
|
|
|
42
43
|
},
|
|
43
44
|
async startSocialLink(
|
|
44
45
|
headers: Headers,
|
|
45
|
-
input: { provider:
|
|
46
|
-
): Promise<
|
|
46
|
+
input: { provider: CloudSocialProvider; callbackURL: string }
|
|
47
|
+
): Promise<SocialLinkStart> {
|
|
47
48
|
if (!configuredSocialProviders(config).includes(input.provider)) {
|
|
48
49
|
throw new Error('Authentication provider is unavailable')
|
|
49
50
|
}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
isKnownSocialProvider,
|
|
3
|
+
type CloudAccountSession,
|
|
4
|
+
type CloudSignIn,
|
|
5
|
+
type CloudSignInMethod
|
|
6
|
+
} from '#cloud/contract'
|
|
2
7
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
3
8
|
import { dateString } from '#cloud/server/sharing/contracts'
|
|
4
9
|
import type { BetterAuthPlugin } from 'better-auth'
|
|
@@ -47,7 +52,7 @@ export function signInMethod(
|
|
|
47
52
|
): CloudSignInMethod | null {
|
|
48
53
|
if (isAuthEndpoint(endpoint.path, CORE_AUTH_PATHS.callbackOAuth)) {
|
|
49
54
|
const provider = endpoint.params?.id
|
|
50
|
-
return provider
|
|
55
|
+
return provider && isKnownSocialProvider(provider) ? provider : 'other'
|
|
51
56
|
}
|
|
52
57
|
return methods.find(([path]) => isAuthEndpoint(endpoint.path, path))?.[1] ?? null
|
|
53
58
|
}
|
|
@@ -2,7 +2,8 @@ import type {
|
|
|
2
2
|
CloudAccessToken,
|
|
3
3
|
CloudAccessTokenCreate,
|
|
4
4
|
CloudAccountSession,
|
|
5
|
-
CloudSignIn
|
|
5
|
+
CloudSignIn,
|
|
6
|
+
CloudSocialProvider
|
|
6
7
|
} from '#cloud/contract'
|
|
7
8
|
import type { CloudActor } from '#cloud/server/auth/session'
|
|
8
9
|
|
|
@@ -39,6 +40,12 @@ export type CloudAuthenticationMethodRecord = {
|
|
|
39
40
|
createdAt: Date
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Where a link sends the browser, and the cookies that let the provider's callback match it to this
|
|
45
|
+
* request: the browser has to keep them, or the callback refuses the link.
|
|
46
|
+
*/
|
|
47
|
+
export type SocialLinkStart = { url: string; headers: Headers }
|
|
48
|
+
|
|
42
49
|
export interface CloudAuthAdapter {
|
|
43
50
|
handler(request: Request): Promise<Response>
|
|
44
51
|
resolveIdentity(headers: Headers): Promise<CloudIdentity | null>
|
|
@@ -48,6 +55,12 @@ export interface CloudAuthAdapter {
|
|
|
48
55
|
* terms: enough to leave, by downloading its documents and deleting the account.
|
|
49
56
|
*/
|
|
50
57
|
resolveAccountSession(headers: Headers): Promise<CloudActor | null>
|
|
58
|
+
/**
|
|
59
|
+
* Whether the signed-in session proved its account's second step, or the account has none.
|
|
60
|
+
* Neither resolver above accepts a session that has not: signing in through a linked account
|
|
61
|
+
* such as Google never asks for it.
|
|
62
|
+
*/
|
|
63
|
+
twoStepConfirmed(headers: Headers): Promise<boolean>
|
|
51
64
|
/**
|
|
52
65
|
* The account a personal access token stands for, from `Authorization: Bearer`, once it is held
|
|
53
66
|
* to what a session would be. Only the document API reads tokens.
|
|
@@ -67,8 +80,8 @@ export interface CloudAuthAdapter {
|
|
|
67
80
|
): Promise<void>
|
|
68
81
|
startSocialLink(
|
|
69
82
|
headers: Headers,
|
|
70
|
-
input: { provider:
|
|
71
|
-
): Promise<
|
|
83
|
+
input: { provider: CloudSocialProvider; callbackURL: string }
|
|
84
|
+
): Promise<SocialLinkStart>
|
|
72
85
|
unlinkAuthenticationMethod(headers: Headers, methodId: string): Promise<void>
|
|
73
86
|
banUser(headers: Headers, userId: string, reason?: string): Promise<void>
|
|
74
87
|
unbanUser(headers: Headers, userId: string): Promise<void>
|
package/src/server/auth/email.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { EmailLocale, TwoStepChange } from '#cloud/email'
|
|
2
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
3
|
import type { TransactionalEmailService } from '#cloud/server/email'
|
|
3
4
|
import { sha256 } from '@noble/hashes/sha2.js'
|
|
4
5
|
import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils.js'
|
|
6
|
+
import type { Transaction } from 'kysely'
|
|
5
7
|
|
|
6
8
|
export type AuthenticationEmailService = {
|
|
7
9
|
sendVerification(input: {
|
|
@@ -42,12 +44,17 @@ export type AuthenticationEmailService = {
|
|
|
42
44
|
system: string | null
|
|
43
45
|
desktop: boolean
|
|
44
46
|
}): Promise<void>
|
|
45
|
-
sendTwoStepChanged(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
sendTwoStepChanged(
|
|
48
|
+
input: {
|
|
49
|
+
email: string
|
|
50
|
+
name: string
|
|
51
|
+
userId: string
|
|
52
|
+
change: TwoStepChange
|
|
53
|
+
/** For a removed sign-in method, which one. */
|
|
54
|
+
method?: string
|
|
55
|
+
},
|
|
56
|
+
transaction?: Transaction<CloudDatabase>
|
|
57
|
+
): Promise<void>
|
|
51
58
|
}
|
|
52
59
|
|
|
53
60
|
function tokenHash(value: string): string {
|
|
@@ -112,17 +119,22 @@ export function createAuthenticationEmailService(
|
|
|
112
119
|
}
|
|
113
120
|
})
|
|
114
121
|
},
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
122
|
+
// Queued in the transaction that made the change, so the change and its notice commit together.
|
|
123
|
+
async sendTwoStepChanged(input, transaction) {
|
|
124
|
+
await email.enqueue(
|
|
125
|
+
{
|
|
126
|
+
idempotencyKey: `two-step-changed/${input.userId}/${crypto.randomUUID()}`,
|
|
127
|
+
kind: 'two-step-changed',
|
|
128
|
+
recipientEmail: input.email,
|
|
129
|
+
payload: {
|
|
130
|
+
name: input.name,
|
|
131
|
+
change: input.change,
|
|
132
|
+
...(input.method && { method: input.method }),
|
|
133
|
+
actionURL: new URL('/account/security', publicURL).href
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
transaction
|
|
137
|
+
)
|
|
126
138
|
}
|
|
127
139
|
}
|
|
128
140
|
}
|
|
@@ -23,15 +23,19 @@ export const CORE_AUTH_PATHS: CorePaths<
|
|
|
23
23
|
| 'callbackOAuth'
|
|
24
24
|
| 'deleteUser'
|
|
25
25
|
| 'deleteUserCallback'
|
|
26
|
+
| 'linkSocialAccount'
|
|
26
27
|
| 'requestPasswordReset'
|
|
27
28
|
| 'sendVerificationEmail'
|
|
28
29
|
| 'signInEmail'
|
|
29
30
|
| 'signUpEmail'
|
|
31
|
+
| 'unlinkAccount'
|
|
30
32
|
| 'verifyEmail'
|
|
31
33
|
> = {
|
|
32
34
|
callbackOAuth: '/callback/:id',
|
|
33
35
|
deleteUser: '/delete-user',
|
|
34
36
|
deleteUserCallback: '/delete-user/callback',
|
|
37
|
+
linkSocialAccount: '/link-social',
|
|
38
|
+
unlinkAccount: '/unlink-account',
|
|
35
39
|
requestPasswordReset: '/request-password-reset',
|
|
36
40
|
sendVerificationEmail: '/send-verification-email',
|
|
37
41
|
signInEmail: '/sign-in/email',
|
|
@@ -29,13 +29,11 @@ import type { AuthenticationEmailService } from './email'
|
|
|
29
29
|
import { AUTH_BASE_PATH, CORE_AUTH_PATHS, authRequestPath } from './endpoints'
|
|
30
30
|
import { enrollmentOptions } from './enrollment'
|
|
31
31
|
import { betterAuthLogger } from './logger'
|
|
32
|
-
import {
|
|
33
|
-
|
|
34
|
-
recordResponseAssurance,
|
|
35
|
-
verifySecondFactor
|
|
36
|
-
} from './mfa-assurance'
|
|
32
|
+
import { removeSignInMethod, signInMethodHooks, signInProviders } from './methods'
|
|
33
|
+
import { createMFAAssuranceService, createProofRecorder, verifySecondFactor } from './mfa-assurance'
|
|
37
34
|
import { passwordOptions } from './passwords'
|
|
38
|
-
import {
|
|
35
|
+
import { authRateLimit } from './rate-limit'
|
|
36
|
+
import { APPLE_AUDIENCE, socialProviders, TRUSTED_SOCIAL_PROVIDERS } from './social-providers'
|
|
39
37
|
|
|
40
38
|
const BETTER_AUTH_SCHEMA_VERSION = '1.7.7+mfa.1'
|
|
41
39
|
const DAY_SECONDS = 24 * 60 * 60
|
|
@@ -66,6 +64,7 @@ export function createBetterAuthAdapter(
|
|
|
66
64
|
const activity = createSignInActivity(database, authenticationEmail)
|
|
67
65
|
// A new address is confirmed through a link sent to it, so changing one needs email delivery.
|
|
68
66
|
const emailChangeAvailable = Boolean(config.emailPasswordEnabled && authenticationEmail)
|
|
67
|
+
const providers = signInProviders(config)
|
|
69
68
|
// The plugins are built first, so the options below name their endpoints by the paths they declare.
|
|
70
69
|
const twoStep = twoFactor({
|
|
71
70
|
issuer: 'OpenPencil Cloud',
|
|
@@ -98,24 +97,32 @@ export function createBetterAuthAdapter(
|
|
|
98
97
|
rateLimit: { enabled: false },
|
|
99
98
|
enableSessionForAPIKeys: false
|
|
100
99
|
})
|
|
100
|
+
const joining = enrollmentOptions(
|
|
101
|
+
config,
|
|
102
|
+
database,
|
|
103
|
+
enrollment,
|
|
104
|
+
authenticationEmail,
|
|
105
|
+
emailChangeAvailable
|
|
106
|
+
)
|
|
101
107
|
const auth = betterAuth({
|
|
102
108
|
appName: 'OpenPencil Cloud',
|
|
103
109
|
logger: betterAuthLogger,
|
|
104
110
|
baseURL: config.publicURL,
|
|
105
111
|
basePath: AUTH_BASE_PATH,
|
|
106
|
-
// Clients never reach Better Auth's
|
|
107
|
-
//
|
|
108
|
-
// through its account routes; the server still calls them itself.
|
|
112
|
+
// Clients never reach Better Auth's administration, which `/api/admin` replaces with its own
|
|
113
|
+
// checks, nor the endpoints Cloud's account routes manage; the server still calls them itself.
|
|
109
114
|
disabledPaths: [
|
|
110
115
|
...Object.values(administration.endpoints).map((endpoint) => endpoint.path),
|
|
111
116
|
// Tokens are made, listed, and revoked through `/api/account/tokens`, with a fresh session.
|
|
112
117
|
...Object.values(accessTokens.endpoints).map((endpoint) => endpoint.path),
|
|
113
118
|
passkeys.endpoints.deletePasskey.path,
|
|
114
119
|
twoStep.endpoints.disableTwoFactor.path,
|
|
115
|
-
// Accounts are deleted through `/api/account/delete`,
|
|
116
|
-
// cannot reach either.
|
|
120
|
+
// Accounts are deleted through `/api/account/delete`, closed to accounts awaiting approval.
|
|
117
121
|
CORE_AUTH_PATHS.deleteUser,
|
|
118
|
-
CORE_AUTH_PATHS.deleteUserCallback
|
|
122
|
+
CORE_AUTH_PATHS.deleteUserCallback,
|
|
123
|
+
// Linking and unlinking go through `/api/account/authentication`, with a fresh session.
|
|
124
|
+
CORE_AUTH_PATHS.linkSocialAccount,
|
|
125
|
+
CORE_AUTH_PATHS.unlinkAccount
|
|
119
126
|
],
|
|
120
127
|
secret: config.authSecret,
|
|
121
128
|
database: {
|
|
@@ -128,7 +135,7 @@ export function createBetterAuthAdapter(
|
|
|
128
135
|
accountLinking: {
|
|
129
136
|
enabled: true,
|
|
130
137
|
disableImplicitLinking: false,
|
|
131
|
-
trustedProviders:
|
|
138
|
+
trustedProviders: TRUSTED_SOCIAL_PROVIDERS,
|
|
132
139
|
allowDifferentEmails: false,
|
|
133
140
|
allowUnlinkingAll: false,
|
|
134
141
|
requireLocalEmailVerified: true
|
|
@@ -143,24 +150,9 @@ export function createBetterAuthAdapter(
|
|
|
143
150
|
},
|
|
144
151
|
...(runtime.runInBackground ? { backgroundTasks: { handler: runtime.runInBackground } } : {})
|
|
145
152
|
},
|
|
146
|
-
rateLimit:
|
|
147
|
-
enabled: true,
|
|
148
|
-
storage: 'database',
|
|
149
|
-
window: 60,
|
|
150
|
-
max: 100,
|
|
151
|
-
customRules: {
|
|
152
|
-
[devices.endpoints.deviceCode.path]: { window: 60, max: 10 },
|
|
153
|
-
[devices.endpoints.deviceToken.path]: { window: 60, max: 60 },
|
|
154
|
-
[devices.endpoints.deviceApprove.path]: { window: 60, max: 10 },
|
|
155
|
-
[devices.endpoints.deviceDeny.path]: { window: 60, max: 10 },
|
|
156
|
-
[CORE_AUTH_PATHS.signInEmail]: { window: 10, max: 3 },
|
|
157
|
-
[CORE_AUTH_PATHS.signUpEmail]: { window: 10, max: 3 },
|
|
158
|
-
[CORE_AUTH_PATHS.requestPasswordReset]: { window: 60, max: 3 },
|
|
159
|
-
[CORE_AUTH_PATHS.sendVerificationEmail]: { window: 60, max: 3 }
|
|
160
|
-
}
|
|
161
|
-
},
|
|
153
|
+
rateLimit: authRateLimit(devices.endpoints),
|
|
162
154
|
emailAndPassword: passwordOptions(config, authenticationEmail),
|
|
163
|
-
...
|
|
155
|
+
...joining,
|
|
164
156
|
plugins: [
|
|
165
157
|
...(config.captchaProvider && config.captchaSecretKey
|
|
166
158
|
? [
|
|
@@ -193,6 +185,7 @@ export function createBetterAuthAdapter(
|
|
|
193
185
|
accessTokens
|
|
194
186
|
],
|
|
195
187
|
socialProviders: socialProviders(config),
|
|
188
|
+
databaseHooks: { ...joining.databaseHooks, ...signInMethodHooks },
|
|
196
189
|
trustedOrigins: [
|
|
197
190
|
config.publicURL,
|
|
198
191
|
...config.trustedOrigins,
|
|
@@ -204,18 +197,35 @@ export function createBetterAuthAdapter(
|
|
|
204
197
|
const deploymentRole = (user: { id: string; role?: string | null }) =>
|
|
205
198
|
user.role === 'admin' ? ('admin' as const) : ('user' as const)
|
|
206
199
|
|
|
207
|
-
|
|
200
|
+
/** The signed-in person, and the session they are signed in with. */
|
|
201
|
+
async function signedIn(headers: Headers) {
|
|
208
202
|
const session = await auth.api.getSession({ headers })
|
|
209
203
|
if (!session) return null
|
|
210
204
|
return {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
205
|
+
identity: {
|
|
206
|
+
userId: session.user.id,
|
|
207
|
+
email: session.user.email,
|
|
208
|
+
name: session.user.name,
|
|
209
|
+
image: session.user.image ?? null,
|
|
210
|
+
deploymentRole: deploymentRole(session.user)
|
|
211
|
+
},
|
|
212
|
+
sessionId: session.session.id,
|
|
213
|
+
twoFactorEnabled: 'twoFactorEnabled' in session.user && session.user.twoFactorEnabled === true
|
|
216
214
|
}
|
|
217
215
|
}
|
|
218
216
|
|
|
217
|
+
const resolveIdentity = async (headers: Headers) => (await signedIn(headers))?.identity ?? null
|
|
218
|
+
|
|
219
|
+
async function twoStepConfirmed(headers: Headers): Promise<boolean> {
|
|
220
|
+
const signed = await signedIn(headers)
|
|
221
|
+
if (!signed) return false
|
|
222
|
+
return assurance.confirmed({
|
|
223
|
+
sessionId: signed.sessionId,
|
|
224
|
+
userId: signed.identity.userId,
|
|
225
|
+
twoFactorEnabled: signed.twoFactorEnabled
|
|
226
|
+
})
|
|
227
|
+
}
|
|
228
|
+
|
|
219
229
|
/** The signed-in person, read past the cookie cache, for changes about their own account. */
|
|
220
230
|
const currentSession = (headers: Headers) =>
|
|
221
231
|
auth.api.getSession({ headers, query: { disableCookieCache: true } })
|
|
@@ -259,14 +269,23 @@ export function createBetterAuthAdapter(
|
|
|
259
269
|
return identity
|
|
260
270
|
}
|
|
261
271
|
|
|
262
|
-
/**
|
|
272
|
+
/**
|
|
273
|
+
* The signed-in person while their account is approved and their session proved its second step,
|
|
274
|
+
* before or after accepting the terms.
|
|
275
|
+
*/
|
|
263
276
|
async function resolveAccountSession(headers: Headers) {
|
|
264
|
-
const
|
|
265
|
-
if (!
|
|
277
|
+
const signed = await signedIn(headers)
|
|
278
|
+
if (!signed) return null
|
|
279
|
+
const { identity } = signed
|
|
266
280
|
if (config.enrollmentMode === 'approval' && !(await enrollment.isApproved(identity.email))) {
|
|
267
281
|
return null
|
|
268
282
|
}
|
|
269
|
-
|
|
283
|
+
const confirmed = await assurance.confirmed({
|
|
284
|
+
sessionId: signed.sessionId,
|
|
285
|
+
userId: identity.userId,
|
|
286
|
+
twoFactorEnabled: signed.twoFactorEnabled
|
|
287
|
+
})
|
|
288
|
+
return confirmed ? identity : null
|
|
270
289
|
}
|
|
271
290
|
|
|
272
291
|
/** The account a personal access token in `Authorization: Bearer` stands for. */
|
|
@@ -294,6 +313,9 @@ export function createBetterAuthAdapter(
|
|
|
294
313
|
*/
|
|
295
314
|
const activeAccountPaths = new Set(
|
|
296
315
|
[
|
|
316
|
+
// The desktop session it grants counts as two-step proved; only a proved session may grant one.
|
|
317
|
+
auth.api.deviceApprove,
|
|
318
|
+
auth.api.deviceDeny,
|
|
297
319
|
auth.api.generatePasskeyRegistrationOptions,
|
|
298
320
|
auth.api.listPasskeys,
|
|
299
321
|
auth.api.updatePasskey,
|
|
@@ -316,12 +338,13 @@ export function createBetterAuthAdapter(
|
|
|
316
338
|
auth.api.unlinkAccount
|
|
317
339
|
].map((endpoint) => authRequestPath(endpoint.path))
|
|
318
340
|
)
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
341
|
+
const recordProof = createProofRecorder(database, assurance, {
|
|
342
|
+
totp: authRequestPath(twoStep.endpoints.verifyTOTP.path),
|
|
343
|
+
recoveryCode: authRequestPath(twoStep.endpoints.verifyBackupCode.path),
|
|
344
|
+
passkeySignIn: authRequestPath(passkeys.endpoints.verifyPasskeyAuthentication.path),
|
|
345
|
+
passkeyRegistration: authRequestPath(passkeys.endpoints.verifyPasskeyRegistration.path),
|
|
346
|
+
deviceToken: authRequestPath(devices.endpoints.deviceToken.path)
|
|
347
|
+
})
|
|
325
348
|
|
|
326
349
|
return {
|
|
327
350
|
handler: async (request) => {
|
|
@@ -330,14 +353,13 @@ export function createBetterAuthAdapter(
|
|
|
330
353
|
return Response.json({ error: { code: 'unauthorized' } }, { status: 401 })
|
|
331
354
|
}
|
|
332
355
|
const response = await auth.handler(request)
|
|
333
|
-
|
|
334
|
-
if (response.ok && method)
|
|
335
|
-
await recordResponseAssurance(response, method, database, assurance)
|
|
356
|
+
if (response.ok) await recordProof(path, response, () => currentSession(request.headers))
|
|
336
357
|
return response
|
|
337
358
|
},
|
|
338
359
|
resolveIdentity,
|
|
339
360
|
resolveSession,
|
|
340
361
|
resolveAccountSession,
|
|
362
|
+
twoStepConfirmed,
|
|
341
363
|
resolveAccessToken,
|
|
342
364
|
async listAccessTokens(headers) {
|
|
343
365
|
const session = await currentSession(headers)
|
|
@@ -386,16 +408,13 @@ export function createBetterAuthAdapter(
|
|
|
386
408
|
},
|
|
387
409
|
async startSocialLink(headers, input) {
|
|
388
410
|
await freshSession(headers)
|
|
389
|
-
const
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
}
|
|
397
|
-
})
|
|
398
|
-
return response.url
|
|
411
|
+
const body = { ...input, errorCallbackURL: input.callbackURL, disableRedirect: true }
|
|
412
|
+
const link = await auth.api.linkSocialAccount({ headers, body, returnHeaders: true })
|
|
413
|
+
return { url: link.response.url, headers: link.headers }
|
|
414
|
+
},
|
|
415
|
+
async unlinkAuthenticationMethod(headers, methodId) {
|
|
416
|
+
const session = await freshSession(headers)
|
|
417
|
+
await removeSignInMethod(database, { userId: session.user.id, methodId, providers })
|
|
399
418
|
},
|
|
400
419
|
async deleteAccount(headers, password) {
|
|
401
420
|
const { headers: answer } = await auth.api.deleteUser({
|
|
@@ -405,9 +424,6 @@ export function createBetterAuthAdapter(
|
|
|
405
424
|
})
|
|
406
425
|
return answer
|
|
407
426
|
},
|
|
408
|
-
async unlinkAuthenticationMethod(headers, methodId) {
|
|
409
|
-
await auth.api.unlinkAccount({ headers, body: { accountId: methodId } })
|
|
410
|
-
},
|
|
411
427
|
async banUser(headers, userId, reason) {
|
|
412
428
|
await auth.api.banUser({ headers, body: { userId, banReason: reason } })
|
|
413
429
|
},
|
|
@@ -451,7 +467,8 @@ export function createBetterAuthAdapter(
|
|
|
451
467
|
headers,
|
|
452
468
|
code,
|
|
453
469
|
'totp',
|
|
454
|
-
twoStep.endpoints.verifyTOTP
|
|
470
|
+
twoStep.endpoints.verifyTOTP,
|
|
471
|
+
before ? { id: before.session.id, userId: before.user.id } : null
|
|
455
472
|
)
|
|
456
473
|
// The first code from a new authenticator is what turns it on.
|
|
457
474
|
if (before && !('twoFactorEnabled' in before.user && before.user.twoFactorEnabled === true)) {
|
|
@@ -460,6 +477,7 @@ export function createBetterAuthAdapter(
|
|
|
460
477
|
return response
|
|
461
478
|
},
|
|
462
479
|
async verifyRecoveryCode(headers, code) {
|
|
480
|
+
const current = await currentSession(headers)
|
|
463
481
|
return verifySecondFactor(
|
|
464
482
|
auth,
|
|
465
483
|
config,
|
|
@@ -468,7 +486,8 @@ export function createBetterAuthAdapter(
|
|
|
468
486
|
headers,
|
|
469
487
|
code,
|
|
470
488
|
'recovery-code',
|
|
471
|
-
twoStep.endpoints.verifyBackupCode
|
|
489
|
+
twoStep.endpoints.verifyBackupCode,
|
|
490
|
+
current ? { id: current.session.id, userId: current.user.id } : null
|
|
472
491
|
)
|
|
473
492
|
},
|
|
474
493
|
async generateRecoveryCodes(headers, password) {
|