Sign-in:
- Google, GitHub, Microsoft and any OpenID Connect provider, set up in the
admin panel (secrets are write-only). PocketBase talks to the provider,
and docwell-server keeps the state and PKCE verifier. Accounts a
provider creates follow the sign-up switch and the domain list; refused
ones are removed again.
- Two-step sign-in with an authenticator app (TOTP), set up under "Your
account", with ten single-use recovery codes. After any first factor
(password, email code, provider) the session is held behind a
five-minute challenge: five tries, each code works once. Turning it off
needs a code. Managers can reset it for someone who lost their phone.
Both the web app and the admin panel ask for the code.
Integrations:
- Personal API tokens ("dw_…") that act as their owner, read-only or
read-write, with optional expiry. Only a hash is stored, and the token is
shown once. Tokens cannot manage tokens or two-step sign-in. Admins see
and can revoke their organization's tokens.
- Workspace webhooks for page, comment and space events. Deliveries are
HMAC-SHA256 signed and retried (10 s, 1 min, 5 min), with a 30-day log
and a test button. Loopback and link-local addresses are refused
(DOCWELL_WEBHOOKS_ALLOW_LOCAL is for development).
Audit log: sign-ins (including failures and second steps, filed under the
account's organization) and every successful change request, by route and
path ids, never bodies. Superadmins see everything and org admins their
organization. It is searchable and exportable as CSV in the admin panel's
new Security page, and kept for DOCWELL_AUDIT_RETENTION_DAYS (365).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
122 lines
5.4 KiB
JavaScript
122 lines
5.4 KiB
JavaScript
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
/// <reference path="../pb_data/types.d.ts" />
|
|
|
|
// Two-step sign-in with an authenticator app (TOTP), personal API tokens,
|
|
// the audit log and workspace webhooks. PocketBase stays private, so the
|
|
// secrets here are only ever read by docwell-server.
|
|
|
|
migrate(
|
|
(app) => {
|
|
const users = app.findCollectionByNameOrId("users");
|
|
const workspaces = app.findCollectionByNameOrId("workspaces");
|
|
const orgs = app.findCollectionByNameOrId("organizations");
|
|
|
|
// totp_pending holds a secret being set up until its first code is confirmed.
|
|
users.fields.add(new TextField({ name: "totp_secret", max: 128 }));
|
|
users.fields.add(new TextField({ name: "totp_pending", max: 128 }));
|
|
users.fields.add(new BoolField({ name: "totp_enabled" }));
|
|
// SHA-256 hashes of the unused recovery codes.
|
|
users.fields.add(new JSONField({ name: "recovery_codes", maxSize: 4000 }));
|
|
app.save(users);
|
|
|
|
app.save(
|
|
new Collection({
|
|
type: "base",
|
|
name: "api_tokens",
|
|
fields: [
|
|
{ name: "user", type: "relation", collectionId: users.id, maxSelect: 1, required: true, cascadeDelete: true },
|
|
{ name: "name", type: "text", required: true, max: 100 },
|
|
{ name: "token_hash", type: "text", required: true, max: 64 },
|
|
// The token's first characters, so people can tell tokens apart.
|
|
{ name: "prefix", type: "text", max: 16 },
|
|
{ name: "scope", type: "select", values: ["read", "write"], maxSelect: 1, required: true },
|
|
{ name: "last_used", type: "date" },
|
|
{ name: "expires", type: "date" },
|
|
{ name: "created", type: "autodate", onCreate: true, onUpdate: false },
|
|
{ name: "updated", type: "autodate", onCreate: true, onUpdate: true },
|
|
],
|
|
indexes: [
|
|
"CREATE UNIQUE INDEX idx_api_tokens_hash ON api_tokens (token_hash)",
|
|
"CREATE INDEX idx_api_tokens_user ON api_tokens (user)",
|
|
],
|
|
}),
|
|
);
|
|
|
|
// Entries outlive what they name: no cascades, and names are copied.
|
|
app.save(
|
|
new Collection({
|
|
type: "base",
|
|
name: "audit_log",
|
|
fields: [
|
|
{ name: "organization", type: "relation", collectionId: orgs.id, maxSelect: 1, cascadeDelete: false },
|
|
{ name: "actor", type: "relation", collectionId: users.id, maxSelect: 1, cascadeDelete: false },
|
|
{ name: "actor_email", type: "text", max: 255 },
|
|
{ name: "action", type: "text", required: true, max: 120 },
|
|
{ name: "target_type", type: "text", max: 40 },
|
|
{ name: "target_id", type: "text", max: 64 },
|
|
{ name: "status", type: "number" },
|
|
{ name: "ip", type: "text", max: 64 },
|
|
{ name: "user_agent", type: "text", max: 300 },
|
|
{ name: "meta", type: "json", maxSize: 4000 },
|
|
{ name: "created", type: "autodate", onCreate: true, onUpdate: false },
|
|
],
|
|
indexes: [
|
|
"CREATE INDEX idx_audit_created ON audit_log (created)",
|
|
"CREATE INDEX idx_audit_org ON audit_log (organization, created)",
|
|
"CREATE INDEX idx_audit_actor ON audit_log (actor, created)",
|
|
],
|
|
}),
|
|
);
|
|
|
|
app.save(
|
|
new Collection({
|
|
type: "base",
|
|
name: "webhooks",
|
|
fields: [
|
|
{ name: "workspace", type: "relation", collectionId: workspaces.id, maxSelect: 1, required: true, cascadeDelete: true },
|
|
{ name: "name", type: "text", max: 100 },
|
|
{ name: "url", type: "url", required: true },
|
|
{ name: "secret", type: "text", required: true, max: 128 },
|
|
{ name: "events", type: "json", maxSize: 2000 },
|
|
{ name: "active", type: "bool" },
|
|
{ name: "created_by", type: "relation", collectionId: users.id, maxSelect: 1, cascadeDelete: false },
|
|
{ name: "created", type: "autodate", onCreate: true, onUpdate: false },
|
|
{ name: "updated", type: "autodate", onCreate: true, onUpdate: true },
|
|
],
|
|
indexes: ["CREATE INDEX idx_webhooks_ws ON webhooks (workspace)"],
|
|
}),
|
|
);
|
|
const webhooks = app.findCollectionByNameOrId("webhooks");
|
|
app.save(
|
|
new Collection({
|
|
type: "base",
|
|
name: "webhook_deliveries",
|
|
fields: [
|
|
{ name: "webhook", type: "relation", collectionId: webhooks.id, maxSelect: 1, required: true, cascadeDelete: true },
|
|
{ name: "event", type: "text", required: true, max: 60 },
|
|
{ name: "delivery_id", type: "text", max: 40 },
|
|
{ name: "status", type: "select", values: ["pending", "success", "failed"], maxSelect: 1 },
|
|
{ name: "attempts", type: "number" },
|
|
{ name: "response_code", type: "number" },
|
|
{ name: "error", type: "text", max: 500 },
|
|
{ name: "duration_ms", type: "number" },
|
|
{ name: "created", type: "autodate", onCreate: true, onUpdate: false },
|
|
{ name: "updated", type: "autodate", onCreate: true, onUpdate: true },
|
|
],
|
|
indexes: ["CREATE INDEX idx_wd_webhook ON webhook_deliveries (webhook, created)"],
|
|
}),
|
|
);
|
|
},
|
|
(app) => {
|
|
for (const name of ["webhook_deliveries", "webhooks", "audit_log", "api_tokens"]) {
|
|
try {
|
|
app.delete(app.findCollectionByNameOrId(name));
|
|
} catch (_) {}
|
|
}
|
|
const users = app.findCollectionByNameOrId("users");
|
|
for (const f of ["recovery_codes", "totp_enabled", "totp_pending", "totp_secret"]) users.fields.removeByName(f);
|
|
app.save(users);
|
|
},
|
|
);
|