Files
Warp/server/internal/setup/appservices.go
T
tajniak81andClaude Opus 5.5 a94794b866
CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Test (windows-latest) (push) Canceled after 0s
CI / Storage tests on PostgreSQL (push) Canceled after 0s
CI / Complement (federation off) (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
CI / Smoke test the binary (push) Canceled after 0s
Phase 9: application services and admin completion
Implements PLAN.md Phase 9. internal/appservice reads registration files
(appservice.config_files), matches namespaces the way Synapse does
(anchored at the start), and delivers events to each service as
transactions read from the roomserver's event stream at a cursor kept in
appservice_state. A transaction in flight is recorded, so after a restart
it is resent under the same ID; failures back off from 2s to 5m, and a
successful ping cuts the backoff short.

In the Client-Server API an as_token acts as the service's sender or, with
?user_id=, as any existing user in its namespace. Services register and
log in their users with m.login.application_service; exclusive user and
alias namespaces are refused to everybody else with M_EXCLUSIVE. Unknown
aliases and users in a namespace are asked of the service first. The
ping endpoint reports M_URL_NOT_SET, M_BAD_STATUS, M_CONNECTION_FAILED
and M_CONNECTION_TIMEOUT. Services' own requests, and their users' when
rate_limited is false, skip the rate limiter.

Users can change their password (POST /account/password) and deactivate
their account (POST /account/deactivate), both behind the password.
Deactivation revokes tokens, deletes devices, optionally erases the
profile and leaves every room through a new userapi.AccountObserver.

The admin API is complete: user detail, the admin flag, deactivation and
reactivation, password resets, devices, media quotas
(media.max_user_storage_bytes plus per-user overrides), registration
tokens with use limits and expiry (registration.require_token), server
notices (internal/servernotices, server_notices.*), an editor for the
federation allow/deny lists persisted in server_settings, a delivery view
of the application services, and an audit log of every change.

Migration 0011 adds the registration token, server notice room, media
quota, audit log and appservice cursor tables. Policy lists are now
returned sorted. The Complement entrypoint loads the registrations
Complement mounts in /complement/appservice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 12:58:28 +02:00

73 lines
2.7 KiB
Go

package setup
import (
"context"
"errors"
"fmt"
"log/slog"
"github.com/dariuszjaniak/warp/server/internal/appservice"
"github.com/dariuszjaniak/warp/server/internal/config"
"github.com/dariuszjaniak/warp/server/internal/matrix/event"
"github.com/dariuszjaniak/warp/server/internal/roomserver"
"github.com/dariuszjaniak/warp/server/internal/userapi"
)
// loadAppServices reads the registration files the config names.
func loadAppServices(cfg *config.Config) (*appservice.Registry, error) {
regs := make([]*appservice.Registration, 0, len(cfg.AppService.ConfigFiles))
for _, path := range cfg.AppService.ConfigFiles {
reg, err := appservice.LoadRegistration(path, cfg.Global.ServerName)
if err != nil {
return nil, err
}
regs = append(regs, reg)
}
return appservice.NewRegistry(cfg.Global.ServerName, regs)
}
// ensureAppServiceSenders creates the sender user of every application
// service that does not have one yet. A service acts as its sender
// whenever it does not ask to act as somebody else, so the account has to
// exist before the first request; it has no password, because nobody logs
// in as it.
func ensureAppServiceSenders(ctx context.Context, users *userapi.API, registry *appservice.Registry, log *slog.Logger) error {
for _, svc := range registry.Services() {
_, err := users.CreateAccount(ctx, userapi.NewAccountRequest{Localpart: svc.SenderLocalpart})
switch {
case err == nil:
log.Info("created the sender of an application service", "appservice", svc.ID, "user_id", svc.SenderUserID())
case errors.Is(err, userapi.ErrUserExists):
default:
return fmt.Errorf("create the sender of application service %q: %w", svc.ID, err)
}
}
return nil
}
// roomLeaver is the roomserver's side of a deactivation: the user leaves
// every room they are in, rejects every invite and withdraws every knock.
// A room that refuses is logged and skipped; the account is already
// deactivated, and one stuck room must not keep the user in all the
// others.
type roomLeaver struct {
rooms *roomserver.API
log *slog.Logger
}
// AccountDeactivated implements userapi.AccountObserver.
func (l roomLeaver) AccountDeactivated(ctx context.Context, localpart string) {
userID := "@" + localpart + ":" + l.rooms.ServerName()
memberships, err := l.rooms.UserMemberships(ctx, userID,
[]string{event.MembershipJoin, event.MembershipInvite, event.MembershipKnock})
if err != nil {
l.log.Error("could not list the rooms of a deactivated user", "user_id", userID, "error", err)
return
}
for _, m := range memberships {
if err := l.rooms.LeaveRoom(ctx, m.RoomID, userID, ""); err != nil {
l.log.Warn("a deactivated user could not leave a room", "user_id", userID, "room_id", m.RoomID, "error", err)
}
}
}