Phase 9: application services and admin completion
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
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 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>
This commit is contained in:
1 parent
b507753172
commit
a94794b866
69 files changed
+6922
-124
No files matched your search
@@ -0,0 +1,93 @@
|
||||
package userapi
|
||||
|
||||
import "context"
|
||||
|
||||
// AccountObserver is told when an account is deactivated, after the
|
||||
// account itself has been dealt with. The roomserver's side of a
|
||||
// deactivation — leaving every room — is done through it, because nothing
|
||||
// in this package knows about rooms.
|
||||
//
|
||||
// Like DeviceObserver it runs on the request path and must not fail the
|
||||
// deactivation: the account is already deactivated when it is called.
|
||||
type AccountObserver interface {
|
||||
AccountDeactivated(ctx context.Context, localpart string)
|
||||
}
|
||||
|
||||
// SetAccountObserver connects the observer. It is a setter for the same
|
||||
// reason SetDeviceObserver is: the roomserver is built after the userapi.
|
||||
func (a *API) SetAccountObserver(o AccountObserver) { a.accounts = o }
|
||||
|
||||
// DeactivateAccount deactivates an account: what both the user's own
|
||||
// POST /account/deactivate and the admin API's deactivation do.
|
||||
//
|
||||
// The order is deliberate. The flag goes first, so that every token the
|
||||
// account still holds stops working at once, before anything slower runs;
|
||||
// the profile is erased next, so that the leave events the observer sends
|
||||
// into the user's rooms already carry no name or avatar; the devices go
|
||||
// after that, taking their keys and pushers with them through the device
|
||||
// observer; and the rooms are left last.
|
||||
//
|
||||
// The name stays taken. A deactivated account cannot be registered again,
|
||||
// because a new user with the old user's ID would inherit everything other
|
||||
// servers and clients remember about the old one.
|
||||
func (a *API) DeactivateAccount(ctx context.Context, localpart string, erase bool) error {
|
||||
if err := a.store.SetDeactivated(ctx, localpart, true); err != nil {
|
||||
return err
|
||||
}
|
||||
if erase {
|
||||
if err := a.store.SetDisplayName(ctx, localpart, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := a.store.SetAvatarURL(ctx, localpart, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := a.DeleteAllDevices(ctx, localpart); err != nil {
|
||||
return err
|
||||
}
|
||||
if a.accounts != nil {
|
||||
a.accounts.AccountDeactivated(ctx, localpart)
|
||||
}
|
||||
a.log.Info("deactivated account", "user_id", a.UserID(localpart), "erase", erase)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReactivateAccount clears the deactivated flag, for the admin API. It
|
||||
// gives back nothing a deactivation took — the rooms are left and the
|
||||
// devices gone — only the ability to log in again.
|
||||
func (a *API) ReactivateAccount(ctx context.Context, localpart string) error {
|
||||
if err := a.store.SetDeactivated(ctx, localpart, false); err != nil {
|
||||
return err
|
||||
}
|
||||
a.log.Info("reactivated account", "user_id", a.UserID(localpart))
|
||||
return nil
|
||||
}
|
||||
|
||||
// ChangePassword sets a new password and, when logoutDevices is set, logs
|
||||
// out every device of the user except keepDeviceID (empty keeps none).
|
||||
//
|
||||
// A user changing their own password keeps the session they changed it
|
||||
// from; an admin resetting one keeps none, since whoever is locked out of
|
||||
// the account may be the one holding the sessions.
|
||||
func (a *API) ChangePassword(ctx context.Context, localpart, password string, logoutDevices bool, keepDeviceID string) error {
|
||||
if err := a.SetPassword(ctx, localpart, password); err != nil {
|
||||
return err
|
||||
}
|
||||
if !logoutDevices {
|
||||
return nil
|
||||
}
|
||||
if keepDeviceID == "" {
|
||||
return a.DeleteAllDevices(ctx, localpart)
|
||||
}
|
||||
devices, err := a.store.Devices(ctx, localpart)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
others := make([]string, 0, len(devices))
|
||||
for _, d := range devices {
|
||||
if d.ID != keepDeviceID {
|
||||
others = append(others, d.ID)
|
||||
}
|
||||
}
|
||||
return a.DeleteDevices(ctx, localpart, others)
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
package userapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/dariuszjaniak/warp/server/internal/storage"
|
||||
)
|
||||
|
||||
// Registration token errors.
|
||||
var (
|
||||
ErrNoSuchRegistrationToken = storage.ErrNoSuchRegistrationToken
|
||||
ErrRegistrationTokenExists = storage.ErrRegistrationTokenExists
|
||||
|
||||
// ErrInvalidRegistrationToken is a token an admin asked for that does
|
||||
// not fit the spec's grammar.
|
||||
ErrInvalidRegistrationToken = errors.New("invalid registration token")
|
||||
)
|
||||
|
||||
// MaxRegistrationTokenLength is the spec's limit on a registration token.
|
||||
const MaxRegistrationTokenLength = 64
|
||||
|
||||
// DefaultRegistrationTokenLength is what a generated token is when the
|
||||
// admin does not say.
|
||||
const DefaultRegistrationTokenLength = 16
|
||||
|
||||
// registrationTokenAlphabet is the spec's character set for a token, less
|
||||
// the punctuation: a generated token is read off a screen and typed.
|
||||
const registrationTokenAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||
|
||||
// RegistrationTokens answers the m.login.registration_token stage from two
|
||||
// places: the static tokens of the config file, which never run out, and
|
||||
// the tokens an admin made through the admin API, which may.
|
||||
type RegistrationTokens struct {
|
||||
store storage.AdminStore
|
||||
static []string
|
||||
required bool
|
||||
nowMS func() int64
|
||||
}
|
||||
|
||||
// RegistrationTokensOptions configure them.
|
||||
type RegistrationTokensOptions struct {
|
||||
// Store holds the tokens made through the admin API. Nil means there
|
||||
// are none and the admin endpoints cannot make any.
|
||||
Store storage.AdminStore
|
||||
|
||||
// Static are registration.tokens from the config file.
|
||||
Static []string
|
||||
|
||||
// Required puts the stage into the registration flow even with no
|
||||
// static tokens — registration.require_token, for a server whose
|
||||
// tokens all come from the admin API.
|
||||
Required bool
|
||||
|
||||
NowMS func() int64
|
||||
}
|
||||
|
||||
// NewRegistrationTokens builds them.
|
||||
func NewRegistrationTokens(opts RegistrationTokensOptions) *RegistrationTokens {
|
||||
return &RegistrationTokens{
|
||||
store: opts.Store,
|
||||
static: opts.Static,
|
||||
required: opts.Required || len(opts.Static) > 0,
|
||||
nowMS: opts.NowMS,
|
||||
}
|
||||
}
|
||||
|
||||
// Required reports whether registration needs a token.
|
||||
func (t *RegistrationTokens) Required() bool { return t.required }
|
||||
|
||||
// Valid reports whether a token would be accepted now. It does not spend
|
||||
// it: the stage is checked when it is passed and spent when the account is
|
||||
// created, so a registration abandoned between the two costs no use.
|
||||
func (t *RegistrationTokens) Valid(ctx context.Context, token string) (bool, error) {
|
||||
if t.staticMatch(token) {
|
||||
return true, nil
|
||||
}
|
||||
if t.store == nil || !wellFormedToken(token) {
|
||||
return false, nil
|
||||
}
|
||||
stored, err := t.store.RegistrationToken(ctx, token)
|
||||
switch {
|
||||
case errors.Is(err, storage.ErrNoSuchRegistrationToken):
|
||||
return false, nil
|
||||
case err != nil:
|
||||
return false, err
|
||||
}
|
||||
return stored.Usable(t.nowMS()), nil
|
||||
}
|
||||
|
||||
// Spend takes one use of a token for an account about to be created, and
|
||||
// returns the function that gives it back if the creation then fails. A
|
||||
// static token has no uses to count. A token that has run out or expired
|
||||
// since the stage was passed is ErrNoSuchRegistrationToken.
|
||||
func (t *RegistrationTokens) Spend(ctx context.Context, token string) (release func(), err error) {
|
||||
if t.staticMatch(token) {
|
||||
return func() {}, nil
|
||||
}
|
||||
if t.store == nil {
|
||||
return nil, ErrNoSuchRegistrationToken
|
||||
}
|
||||
if err := t.store.UseRegistrationToken(ctx, token, t.nowMS()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func() {
|
||||
// The request's context may be what failed; giving the use back
|
||||
// must not depend on it.
|
||||
_ = t.store.ReleaseRegistrationToken(context.WithoutCancel(ctx), token)
|
||||
}, nil
|
||||
}
|
||||
|
||||
// staticMatch compares a token against every config file token in
|
||||
// constant time, and does not stop at the first match, so neither the
|
||||
// value nor the position of a token leaks through the time it takes.
|
||||
func (t *RegistrationTokens) staticMatch(token string) bool {
|
||||
found := 0
|
||||
for _, want := range t.static {
|
||||
found |= subtle.ConstantTimeCompare([]byte(token), []byte(want))
|
||||
}
|
||||
return found == 1
|
||||
}
|
||||
|
||||
// NewRegistrationToken is what the admin API asks for.
|
||||
type NewRegistrationToken struct {
|
||||
// Token is the token to make; empty generates one of Length
|
||||
// characters.
|
||||
Token string
|
||||
Length int
|
||||
|
||||
UsesAllowed *int
|
||||
ExpiresAtMS int64
|
||||
}
|
||||
|
||||
// CreateRegistrationToken makes a token through the admin API.
|
||||
func (t *RegistrationTokens) CreateRegistrationToken(ctx context.Context, req NewRegistrationToken) (storage.RegistrationToken, error) {
|
||||
if t.store == nil {
|
||||
return storage.RegistrationToken{}, storage.ErrNotImplemented
|
||||
}
|
||||
token := req.Token
|
||||
if token == "" {
|
||||
length := req.Length
|
||||
if length == 0 {
|
||||
length = DefaultRegistrationTokenLength
|
||||
}
|
||||
if length < 1 || length > MaxRegistrationTokenLength {
|
||||
return storage.RegistrationToken{}, fmt.Errorf("%w: length must be between 1 and %d",
|
||||
ErrInvalidRegistrationToken, MaxRegistrationTokenLength)
|
||||
}
|
||||
var err error
|
||||
if token, err = generateRegistrationToken(length); err != nil {
|
||||
return storage.RegistrationToken{}, err
|
||||
}
|
||||
} else if !wellFormedToken(token) {
|
||||
return storage.RegistrationToken{}, fmt.Errorf(
|
||||
"%w: a token is 1 to %d of the characters A-Z, a-z, 0-9, '.', '_', '~' and '-'",
|
||||
ErrInvalidRegistrationToken, MaxRegistrationTokenLength)
|
||||
}
|
||||
if req.UsesAllowed != nil && *req.UsesAllowed < 0 {
|
||||
return storage.RegistrationToken{}, fmt.Errorf("%w: uses_allowed must not be negative", ErrInvalidRegistrationToken)
|
||||
}
|
||||
if req.ExpiresAtMS < 0 {
|
||||
return storage.RegistrationToken{}, fmt.Errorf("%w: expiry_time must not be negative", ErrInvalidRegistrationToken)
|
||||
}
|
||||
|
||||
stored := storage.RegistrationToken{
|
||||
Token: token,
|
||||
UsesAllowed: req.UsesAllowed,
|
||||
ExpiresAtMS: req.ExpiresAtMS,
|
||||
CreatedAtMS: t.nowMS(),
|
||||
}
|
||||
if err := t.store.CreateRegistrationToken(ctx, stored); err != nil {
|
||||
return storage.RegistrationToken{}, err
|
||||
}
|
||||
return stored, nil
|
||||
}
|
||||
|
||||
// RegistrationToken reads one token made through the admin API.
|
||||
func (t *RegistrationTokens) RegistrationToken(ctx context.Context, token string) (storage.RegistrationToken, error) {
|
||||
if t.store == nil {
|
||||
return storage.RegistrationToken{}, ErrNoSuchRegistrationToken
|
||||
}
|
||||
return t.store.RegistrationToken(ctx, token)
|
||||
}
|
||||
|
||||
// List lists the tokens made through the admin API. The config file's are
|
||||
// not listed: they are in the config file.
|
||||
func (t *RegistrationTokens) List(ctx context.Context) ([]storage.RegistrationToken, error) {
|
||||
if t.store == nil {
|
||||
return []storage.RegistrationToken{}, nil
|
||||
}
|
||||
return t.store.RegistrationTokens(ctx)
|
||||
}
|
||||
|
||||
// Update replaces a token's use limit and expiry.
|
||||
func (t *RegistrationTokens) Update(ctx context.Context, token string, usesAllowed *int, expiresAtMS int64) (storage.RegistrationToken, error) {
|
||||
if t.store == nil {
|
||||
return storage.RegistrationToken{}, ErrNoSuchRegistrationToken
|
||||
}
|
||||
if usesAllowed != nil && *usesAllowed < 0 {
|
||||
return storage.RegistrationToken{}, fmt.Errorf("%w: uses_allowed must not be negative", ErrInvalidRegistrationToken)
|
||||
}
|
||||
if expiresAtMS < 0 {
|
||||
return storage.RegistrationToken{}, fmt.Errorf("%w: expiry_time must not be negative", ErrInvalidRegistrationToken)
|
||||
}
|
||||
if err := t.store.UpdateRegistrationToken(ctx, token, usesAllowed, expiresAtMS); err != nil {
|
||||
return storage.RegistrationToken{}, err
|
||||
}
|
||||
return t.store.RegistrationToken(ctx, token)
|
||||
}
|
||||
|
||||
// Delete removes a token. Accounts registered with it are not affected.
|
||||
func (t *RegistrationTokens) Delete(ctx context.Context, token string) error {
|
||||
if t.store == nil {
|
||||
return ErrNoSuchRegistrationToken
|
||||
}
|
||||
return t.store.DeleteRegistrationToken(ctx, token)
|
||||
}
|
||||
|
||||
// wellFormedToken applies the spec's grammar: 1 to 64 characters from
|
||||
// [A-Za-z0-9._~-].
|
||||
func wellFormedToken(token string) bool {
|
||||
if token == "" || len(token) > MaxRegistrationTokenLength {
|
||||
return false
|
||||
}
|
||||
for i := range len(token) {
|
||||
switch c := token[i]; {
|
||||
case 'A' <= c && c <= 'Z', 'a' <= c && c <= 'z', '0' <= c && c <= '9':
|
||||
case c == '.', c == '_', c == '~', c == '-':
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// generateRegistrationToken makes a random token. Bytes that would bias
|
||||
// the modulo are thrown away, as for media IDs.
|
||||
func generateRegistrationToken(length int) (string, error) {
|
||||
const accept = 256 - 256%len(registrationTokenAlphabet)
|
||||
out := make([]byte, 0, length)
|
||||
buf := make([]byte, length)
|
||||
for len(out) < length {
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
return "", fmt.Errorf("generate a registration token: %w", err)
|
||||
}
|
||||
for _, c := range buf {
|
||||
if int(c) < accept && len(out) < length {
|
||||
out = append(out, registrationTokenAlphabet[int(c)%len(registrationTokenAlphabet)])
|
||||
}
|
||||
}
|
||||
}
|
||||
return string(out), nil
|
||||
}
|
||||
@@ -72,6 +72,9 @@ type API struct {
|
||||
|
||||
// observer hears about deleted and renamed devices; nil hears nothing.
|
||||
observer DeviceObserver
|
||||
|
||||
// accounts hears about deactivated accounts; nil hears nothing.
|
||||
accounts AccountObserver
|
||||
}
|
||||
|
||||
// DeviceObserver is told about changes to devices that something else
|
||||
@@ -393,6 +396,12 @@ type Authenticated struct {
|
||||
Localpart string
|
||||
DeviceID string
|
||||
Admin bool
|
||||
|
||||
// AppService is the ID of the application service making the request,
|
||||
// acting as UserID, or empty for an ordinary access token. A request
|
||||
// made with an application service token has no device: DeviceID is
|
||||
// empty.
|
||||
AppService string
|
||||
}
|
||||
|
||||
// Authenticate resolves an access token.
|
||||
|
||||
Reference in new issue
Block a user