Files
Docwell/Server/internal/config/config.go
T
tajniak81andClaude Opus 5.5 631b929433 Add OAuth and OIDC sign-in, two-step codes, audit log, API tokens and webhooks
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>
2026-11-01 09:31:45 +01:00

116 lines
3.5 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
// Package config loads docwell-server settings from environment variables.
package config
import (
"errors"
"os"
"strconv"
"strings"
"time"
)
type Config struct {
// Addr is the listen address of the public API, e.g. ":8080".
Addr string
// PublicURL is the externally reachable base URL of the server.
PublicURL string
// WebURL is the base URL of docwell-web; links in emails open it.
WebURL string
// CORSOrigins lists origins allowed to call the API from a browser.
CORSOrigins []string
// SignupEnabled allows self-service signup (the first account always can).
SignupEnabled bool
// SignupDomains limits signup to these email domains when not empty.
SignupDomains []string
PocketBaseURL string
PocketBaseEmail string
PocketBasePassword string
// DataDir holds docwell-server's own files (the search index).
DataDir string
// MaxUploadMB caps one attachment.
MaxUploadMB int
ShutdownTimeout time.Duration
// AuditRetentionDays is how long audit log entries are kept (0 = forever).
AuditRetentionDays int
// WebhooksAllowLocal lets webhooks call loopback and link-local
// addresses (for development; off in production).
WebhooksAllowLocal bool
}
// Load reads the configuration from the environment and validates it.
func Load() (Config, error) {
return fromLookup(os.LookupEnv)
}
func fromLookup(lookup func(string) (string, bool)) (Config, error) {
get := func(key, def string) string {
if v, ok := lookup(key); ok && v != "" {
return v
}
return def
}
cfg := Config{
Addr: get("DOCWELL_ADDR", ":8080"),
PublicURL: strings.TrimRight(get("DOCWELL_PUBLIC_URL", "http://localhost:8080"), "/"),
WebURL: strings.TrimRight(get("DOCWELL_WEB_URL", "http://localhost:3000"), "/"),
CORSOrigins: splitList(get("DOCWELL_CORS_ORIGINS", "http://localhost:5173,http://localhost:3000")),
PocketBaseURL: strings.TrimRight(get("DOCWELL_PB_URL", "http://127.0.0.1:8090"), "/"),
PocketBaseEmail: get("DOCWELL_PB_EMAIL", ""),
PocketBasePassword: get("DOCWELL_PB_PASSWORD", ""),
SignupEnabled: get("DOCWELL_SIGNUP_ENABLED", "true") == "true",
SignupDomains: splitList(get("DOCWELL_SIGNUP_DOMAINS", "")),
DataDir: get("DOCWELL_DATA_DIR", "./data"),
ShutdownTimeout: 15 * time.Second,
WebhooksAllowLocal: get("DOCWELL_WEBHOOKS_ALLOW_LOCAL", "false") == "true",
}
cfg.AuditRetentionDays = 365
if v := get("DOCWELL_AUDIT_RETENTION_DAYS", ""); v != "" {
n, err := strconv.Atoi(v)
if err != nil || n < 0 {
return Config{}, errors.New("DOCWELL_AUDIT_RETENTION_DAYS must be 0 (keep forever) or more")
}
cfg.AuditRetentionDays = n
}
if v := get("DOCWELL_SHUTDOWN_TIMEOUT", ""); v != "" {
d, err := time.ParseDuration(v)
if err != nil {
return Config{}, errors.New("DOCWELL_SHUTDOWN_TIMEOUT: " + err.Error())
}
cfg.ShutdownTimeout = d
}
cfg.MaxUploadMB = 50
if v := get("DOCWELL_MAX_UPLOAD_MB", ""); v != "" {
n, err := strconv.Atoi(v)
if err != nil || n < 1 || n > 100 {
return Config{}, errors.New("DOCWELL_MAX_UPLOAD_MB must be between 1 and 100")
}
cfg.MaxUploadMB = n
}
if cfg.PocketBaseEmail == "" || cfg.PocketBasePassword == "" {
return Config{}, errors.New("DOCWELL_PB_EMAIL and DOCWELL_PB_PASSWORD are required")
}
return cfg, nil
}
func splitList(s string) []string {
var out []string
for _, part := range strings.Split(s, ",") {
if part = strings.TrimSpace(part); part != "" {
out = append(out, part)
}
}
return out
}