Files
Docwell/Server/internal/access/spaces.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

205 lines
6.3 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package access
import "docwell/server/internal/apperr"
// ---- Spaces and pages ------------------------------------------------------
//
// Space rights cascade from the workspace role:
//
// workspace owner/admin admin of every space in the workspace
// member explicit space role, else the space's default role in open spaces
// guest explicit space role only (guests never see open spaces by default)
//
// Space roles: admin > editor > commenter > viewer. Pages inherit their space's rights.
// Space roles stored in space_members.role.
const (
SpaceAdmin = "admin"
SpaceEditor = "editor"
SpaceCommenter = "commenter"
SpaceViewer = "viewer"
)
// Space visibilities stored in spaces.visibility.
const (
SpaceOpen = "open"
SpacePrivate = "private"
)
var spaceRank = map[string]int{SpaceViewer: 1, SpaceCommenter: 2, SpaceEditor: 3, SpaceAdmin: 4}
// ValidSpaceRole reports whether role is a space role.
func ValidSpaceRole(role string) bool { return spaceRank[role] > 0 }
// ValidDefaultRole reports whether role can be an open space's default role.
func ValidDefaultRole(role string) bool { return ValidSpaceRole(role) && role != SpaceAdmin }
// HigherSpaceRole returns the higher of two space roles ("" ranks lowest).
func HigherSpaceRole(a, b string) string {
if spaceRank[b] > spaceRank[a] {
return b
}
return a
}
// SpaceAtLeast reports whether space role has is at or above need.
func SpaceAtLeast(has, need string) bool { return has != "" && spaceRank[has] >= spaceRank[need] }
// SpaceRole is the effective role in a space for someone with workspace role
// wsRole (already resolved by WorkspaceRole) and explicit space role
// memberRole ("" when not a space member; the highest of the person's own
// and their groups' grants, see HigherSpaceRole). "" = no access.
func SpaceRole(wsRole, visibility, defaultRole, memberRole string) string {
switch {
case wsRole == "":
return ""
case AtLeast(wsRole, WorkspaceAdmin):
return SpaceAdmin
case ValidSpaceRole(memberRole):
return memberRole
case visibility == SpaceOpen && wsRole != WorkspaceGuest:
if ValidDefaultRole(defaultRole) {
return defaultRole
}
return SpaceEditor
}
return ""
}
// CreateSpace and DeleteSpace need workspace admin: spaces are the workspace's structure.
func CreateSpace(wsRole string) error {
if !AtLeast(wsRole, WorkspaceAdmin) {
return apperr.Forbidden("workspace admin role required")
}
return nil
}
func DeleteSpace(wsRole string) error { return CreateSpace(wsRole) }
// ManageSpace (settings and members) needs space admin.
func ManageSpace(spaceRole string) error {
if !SpaceAtLeast(spaceRole, SpaceAdmin) {
return apperr.Forbidden("space admin role required")
}
return nil
}
// ViewSpace needs any access; a space someone cannot see does not exist for them.
func ViewSpace(spaceRole string) error {
if spaceRole == "" {
return apperr.NotFound("space not found")
}
return nil
}
// EditPages (create, rename, move, trash, restore) needs editor.
func EditPages(spaceRole string) error {
if err := ViewSpace(spaceRole); err != nil {
return err
}
if !SpaceAtLeast(spaceRole, SpaceEditor) {
return apperr.Forbidden("editor role required")
}
return nil
}
// CommentPages (writing comments) needs commenter.
func CommentPages(spaceRole string) error {
if err := ViewSpace(spaceRole); err != nil {
return err
}
if !SpaceAtLeast(spaceRole, SpaceCommenter) {
return apperr.Forbidden("commenter role required")
}
return nil
}
// DeleteComment: authors delete their own comments, space admins any.
func DeleteComment(actorID, authorID, spaceRole string) error {
if actorID != "" && actorID == authorID {
return nil
}
if !SpaceAtLeast(spaceRole, SpaceAdmin) {
return apperr.Forbidden("only the author or a space admin can delete this comment")
}
return nil
}
// PurgePage (permanent delete from the trash) needs space admin.
func PurgePage(spaceRole string) error {
if err := ViewSpace(spaceRole); err != nil {
return err
}
return ManageSpace(spaceRole)
}
// SetSpaceMember checks granting role to someone whose workspace role is
// targetWsRole. The target must be able to open the workspace; workspace
// admins are already space admins everywhere, so they are not listed.
func SetSpaceMember(actorRole, targetWsRole, role string) error {
if !ValidSpaceRole(role) {
return apperr.Invalid("role must be admin, editor, commenter or viewer")
}
if err := ManageSpace(actorRole); err != nil {
return err
}
if targetWsRole == "" {
return apperr.Invalid("that user is not a member of this workspace")
}
if AtLeast(targetWsRole, WorkspaceAdmin) {
return apperr.Invalid("workspace admins already manage every space")
}
return nil
}
// SetSpaceGroup checks granting role in a space to a group of the space's
// workspace.
func SetSpaceGroup(actorRole, role string) error {
if !ValidSpaceRole(role) {
return apperr.Invalid("role must be admin, editor, commenter or viewer")
}
return ManageSpace(actorRole)
}
// ManageWebhooks (add, change, remove, test, see deliveries) needs workspace admin.
func ManageWebhooks(wsRole string) error {
if !AtLeast(wsRole, WorkspaceAdmin) {
return apperr.Forbidden("workspace admin role required")
}
return nil
}
// ---- Groups ----------------------------------------------------------------
// ManageGroups (create, rename, delete, change members) needs workspace admin.
func ManageGroups(wsRole string) error {
if !AtLeast(wsRole, WorkspaceAdmin) {
return apperr.Forbidden("workspace admin role required")
}
return nil
}
// ViewGroups needs workspace membership above guest: guests see only what
// was shared with them, not the workspace's structure.
func ViewGroups(wsRole string) error {
if !AtLeast(wsRole, WorkspaceMember) {
return apperr.Forbidden("workspace member role required")
}
return nil
}
// EditComment: only the author changes what they wrote.
func EditComment(actorID, authorID string) error {
if actorID == "" || actorID != authorID {
return apperr.Forbidden("only the author can edit this comment")
}
return nil
}
// ResolveComment and ReactToComment need commenter, like writing one.
func ResolveComment(pageRole string) error { return CommentPages(pageRole) }
func ReactToComment(pageRole string) error { return CommentPages(pageRole) }