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
Implements PLAN.md Phase 5: the Server-Server API. Warp joins rooms on other servers and lets their users join its rooms, invites across servers with the invitee's server countersigning, rejects remote invites (locally if the inviter is unreachable), and exchanges events, typing, receipts and to-device messages with other homeservers — all behind the runtime switch, which now starts and stops real outbound traffic. The protocol lives in internal/federation with no knowledge of rooms: X-Matrix signing (fuzzed parser), the spec's discovery algorithm keeping the dial address and the TLS name apart, a signed client that refuses every request while the switch is off or the policy denies the destination, and a key ring that caches other servers' keys for at most a week, with optional notaries. The roomserver takes events from elsewhere through the spec's intake: signatures and hashes (a hash mismatch keeps the redacted form), the rules against the event's own auth events, against the state before it — with /get_missing_events for small gaps and /state_ids for wide ones — and against the current state, where a failure is a soft failure. Events held without their state are outliers: a column on roomserver_events, because the current state of a room joined over federation is almost all outliers, and excluded from every timeline query. Forks are resolved at the room's head. Membership across servers is make_join/send_join, make_leave/send_leave and countersigned invites, in both directions; m.room.server_acl is enforced on everything another server sends. History before a remote join is backfilled when /messages pages past it, stored at negative stream positions. internal/federationapi is the running component: the inbound endpoints, and an outbound queue that persists what it owes each destination, sends it in transactions of 50 PDUs and 100 EDUs, backs off from ten seconds to six hours, survives restarts, and drops events persisted while federation is off rather than delivering a backlog in one burst. Its Module is what the switch starts and stops. Also: a TLS listener for port 8448, trusted key servers, key validity and an outbound CA bundle in the config; per-destination health and backoff reset in the admin API; remote profiles, aliases and directories in the Client-Server API; a Complement federation profile. See docs/federation.md, including its known gaps (no partial-state joins, no key rotation). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
196 lines
6.8 KiB
Go
196 lines
6.8 KiB
Go
package clientapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/dariuszjaniak/warp/server/internal/federation"
|
|
"github.com/dariuszjaniak/warp/server/internal/httputil"
|
|
"github.com/dariuszjaniak/warp/server/internal/matrix/id"
|
|
"github.com/dariuszjaniak/warp/server/internal/storage"
|
|
"github.com/dariuszjaniak/warp/server/internal/userapi"
|
|
)
|
|
|
|
// profileResponse is the body of GET /profile/{userId}. Both fields are
|
|
// omitted when unset: the spec distinguishes "no display name" from an empty
|
|
// one, and clients fall back to the localpart for the former.
|
|
type profileResponse struct {
|
|
DisplayName string `json:"displayname,omitempty"`
|
|
AvatarURL string `json:"avatar_url,omitempty"`
|
|
}
|
|
|
|
// MaxDisplayNameLength and MaxAvatarURLLength bound the profile fields.
|
|
// The spec sets no limit; these are what keeps a member list from carrying a
|
|
// megabyte of display name per user once rooms exist.
|
|
const (
|
|
MaxDisplayNameLength = 256
|
|
MaxAvatarURLLength = 1024
|
|
)
|
|
|
|
// handleGetProfile implements GET /_matrix/client/v3/profile/{userId}.
|
|
func (c *ClientAPI) handleGetProfile(w http.ResponseWriter, r *http.Request) error {
|
|
account, err := c.lookupProfile(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
httputil.WriteJSON(w, http.StatusOK, profileResponse{
|
|
DisplayName: account.DisplayName,
|
|
AvatarURL: account.AvatarURL,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
// handleGetDisplayName implements GET /profile/{userId}/displayname.
|
|
func (c *ClientAPI) handleGetDisplayName(w http.ResponseWriter, r *http.Request) error {
|
|
account, err := c.lookupProfile(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
httputil.WriteJSON(w, http.StatusOK, profileResponse{DisplayName: account.DisplayName})
|
|
return nil
|
|
}
|
|
|
|
// handleGetAvatarURL implements GET /profile/{userId}/avatar_url.
|
|
func (c *ClientAPI) handleGetAvatarURL(w http.ResponseWriter, r *http.Request) error {
|
|
account, err := c.lookupProfile(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
httputil.WriteJSON(w, http.StatusOK, profileResponse{AvatarURL: account.AvatarURL})
|
|
return nil
|
|
}
|
|
|
|
// RemoteProfiles asks another server for a profile. *federation.Client
|
|
// satisfies it.
|
|
type RemoteProfiles interface {
|
|
QueryProfile(ctx context.Context, destination, userID, field string) (map[string]any, error)
|
|
}
|
|
|
|
// lookupProfile resolves the user ID in the path to an account: a local
|
|
// one from the database, a remote one by asking its server.
|
|
//
|
|
// A user ID on another server is where the federation switch shows up in the
|
|
// Client-Server API: with federation off there is no way to answer and the
|
|
// spec's M_FORBIDDEN is the honest reply, as PLAN.md section 3 requires.
|
|
func (c *ClientAPI) lookupProfile(r *http.Request) (storage.Account, error) {
|
|
userID := r.PathValue("userID")
|
|
|
|
localpart, err := c.users.ParseLocalUserID(userID)
|
|
switch {
|
|
case errors.Is(err, userapi.ErrForeignServer):
|
|
if !c.federation.Enabled() || c.remoteProfile == nil {
|
|
return storage.Account{}, federation.RejectClientRequest(
|
|
"looking up the profile of a user on another server")
|
|
}
|
|
return c.lookupRemoteProfile(r.Context(), userID)
|
|
case err != nil:
|
|
return storage.Account{}, httputil.InvalidParam("%s is not a valid user ID", userID)
|
|
}
|
|
|
|
account, err := c.users.Account(r.Context(), localpart)
|
|
switch {
|
|
case errors.Is(err, userapi.ErrNoSuchUser):
|
|
return storage.Account{}, httputil.NotFound("Profile not found")
|
|
case err != nil:
|
|
return storage.Account{}, err
|
|
}
|
|
return account, nil
|
|
}
|
|
|
|
// putDisplayNameRequest is the body of PUT /profile/{userId}/displayname.
|
|
type putDisplayNameRequest struct {
|
|
DisplayName string `json:"displayname"`
|
|
}
|
|
|
|
// putAvatarURLRequest is the body of PUT /profile/{userId}/avatar_url.
|
|
type putAvatarURLRequest struct {
|
|
AvatarURL string `json:"avatar_url"`
|
|
}
|
|
|
|
// handlePutDisplayName implements PUT /profile/{userId}/displayname.
|
|
func (c *ClientAPI) handlePutDisplayName(w http.ResponseWriter, r *http.Request, auth userapi.Authenticated) error {
|
|
localpart, err := c.requireSelf(auth, r.PathValue("userID"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var req putDisplayNameRequest
|
|
if err := httputil.DecodeJSON(w, r, &req); err != nil {
|
|
return err
|
|
}
|
|
if len(req.DisplayName) > MaxDisplayNameLength {
|
|
return httputil.NewError(http.StatusBadRequest, httputil.ErrCodeTooLarge,
|
|
"Display names must be at most %d bytes", MaxDisplayNameLength)
|
|
}
|
|
|
|
if err := c.users.SetDisplayName(r.Context(), localpart, req.DisplayName); err != nil {
|
|
return profileWriteError(err)
|
|
}
|
|
// Once rooms exist this also has to rewrite the m.room.member event in
|
|
// every room the user is in; that belongs to the roomserver in Phase 3.
|
|
httputil.WriteJSON(w, http.StatusOK, struct{}{})
|
|
return nil
|
|
}
|
|
|
|
// handlePutAvatarURL implements PUT /profile/{userId}/avatar_url.
|
|
func (c *ClientAPI) handlePutAvatarURL(w http.ResponseWriter, r *http.Request, auth userapi.Authenticated) error {
|
|
localpart, err := c.requireSelf(auth, r.PathValue("userID"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var req putAvatarURLRequest
|
|
if err := httputil.DecodeJSON(w, r, &req); err != nil {
|
|
return err
|
|
}
|
|
if len(req.AvatarURL) > MaxAvatarURLLength {
|
|
return httputil.NewError(http.StatusBadRequest, httputil.ErrCodeTooLarge,
|
|
"Avatar URLs must be at most %d bytes", MaxAvatarURLLength)
|
|
}
|
|
|
|
if err := c.users.SetAvatarURL(r.Context(), localpart, req.AvatarURL); err != nil {
|
|
return profileWriteError(err)
|
|
}
|
|
httputil.WriteJSON(w, http.StatusOK, struct{}{})
|
|
return nil
|
|
}
|
|
|
|
// profileWriteError maps a failed profile update. An admin may write to
|
|
// another user's profile, so "no such user" is reachable here.
|
|
func profileWriteError(err error) error {
|
|
if errors.Is(err, userapi.ErrNoSuchUser) {
|
|
return httputil.NotFound("Profile not found")
|
|
}
|
|
return err
|
|
}
|
|
|
|
// lookupRemoteProfile asks a user's own server for their profile.
|
|
//
|
|
// Nothing is cached. A profile changes whenever its owner likes, and a
|
|
// cached copy would need invalidating by an EDU this server does not
|
|
// receive; the membership events of the rooms a remote user shares with
|
|
// ours carry their display name already, which is where clients look for
|
|
// it most of the time.
|
|
func (c *ClientAPI) lookupRemoteProfile(ctx context.Context, userID string) (storage.Account, error) {
|
|
server, err := id.ServerNameOf(userID)
|
|
if err != nil {
|
|
return storage.Account{}, httputil.InvalidParam("%s is not a valid user ID", userID)
|
|
}
|
|
profile, err := c.remoteProfile.QueryProfile(ctx, server, userID, "")
|
|
switch {
|
|
case federation.IsNotFound(err):
|
|
return storage.Account{}, httputil.NotFound("Profile not found")
|
|
case err != nil:
|
|
c.log.Info("remote profile lookup failed", "user_id", userID, "error", err)
|
|
return storage.Account{}, httputil.NewError(http.StatusBadGateway, httputil.ErrCodeUnknown,
|
|
"%s could not be asked for the profile", server)
|
|
}
|
|
account := storage.Account{}
|
|
if name, ok := profile["displayname"].(string); ok {
|
|
account.DisplayName = name
|
|
}
|
|
if avatar, ok := profile["avatar_url"].(string); ok {
|
|
account.AvatarURL = avatar
|
|
}
|
|
return account, nil
|
|
}
|