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>
94 lines
3.4 KiB
Go
94 lines
3.4 KiB
Go
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)
|
|
}
|