Files
Warp/server/internal/clientapi/account.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

112 lines
3.6 KiB
Go

package clientapi
import (
"net/http"
"github.com/dariuszjaniak/warp/server/internal/httputil"
"github.com/dariuszjaniak/warp/server/internal/userapi"
)
// changePasswordRequest is the body of POST /account/password.
type changePasswordRequest struct {
NewPassword string `json:"new_password"`
// LogoutDevices defaults to true, which is why it is a pointer: a
// client that says nothing gets every other session logged out.
LogoutDevices *bool `json:"logout_devices"`
Auth *uiaRequest `json:"auth"`
}
// handleChangePassword implements POST /_matrix/client/v3/account/password.
//
// The current password is asked for again first, through the same
// interactive authentication as deleting a device, and for the same
// reason: a stolen access token must not be enough to lock the owner out.
// The session the change is made from stays logged in; with
// logout_devices, every other one is logged out.
//
// An application service acting as one of its users skips the
// re-authentication: its users have no password to ask for, and the
// service's token is already the strongest credential involved.
func (c *ClientAPI) handleChangePassword(w http.ResponseWriter, r *http.Request, auth userapi.Authenticated) error {
var req changePasswordRequest
if err := httputil.DecodeJSON(w, r, &req); err != nil {
return err
}
sessionID := ""
if auth.AppService == "" {
flows := []uiaFlow{{Stages: []string{stagePassword}}}
var done bool
var err error
sessionID, done, err = c.beginOrAdvanceUIA(w, flows, req.Auth,
func(_ string, stage *uiaRequest) (*httputil.MatrixError, error) {
return c.passwordStage(r, auth, stage)
})
if err != nil || !done {
return err
}
}
if req.NewPassword == "" {
return httputil.MissingParam("new_password is required")
}
if len(req.NewPassword) > userapi.MaxPasswordLength {
return httputil.NewError(http.StatusBadRequest, httputil.ErrCodeWeakPassword,
"Password must be at most %d bytes", userapi.MaxPasswordLength)
}
logout := req.LogoutDevices == nil || *req.LogoutDevices
if err := c.users.ChangePassword(r.Context(), auth.Localpart, req.NewPassword, logout, auth.DeviceID); err != nil {
return err
}
c.uia.remove(sessionID)
httputil.WriteJSON(w, http.StatusOK, struct{}{})
return nil
}
// deactivateRequest is the body of POST /account/deactivate.
type deactivateRequest struct {
// Erase asks for the account's profile to be forgotten as well.
Erase bool `json:"erase"`
Auth *uiaRequest `json:"auth"`
}
// deactivateResponse is its answer. Warp keeps no third-party identifiers
// and talks to no identity server, so there was nothing to unbind.
type deactivateResponse struct {
IDServerUnbindResult string `json:"id_server_unbind_result"`
}
// handleDeactivate implements POST /_matrix/client/v3/account/deactivate:
// the user closing their own account, behind a re-entered password.
func (c *ClientAPI) handleDeactivate(w http.ResponseWriter, r *http.Request, auth userapi.Authenticated) error {
var req deactivateRequest
if err := httputil.DecodeJSON(w, r, &req); err != nil {
return err
}
sessionID := ""
if auth.AppService == "" {
flows := []uiaFlow{{Stages: []string{stagePassword}}}
var done bool
var err error
sessionID, done, err = c.beginOrAdvanceUIA(w, flows, req.Auth,
func(_ string, stage *uiaRequest) (*httputil.MatrixError, error) {
return c.passwordStage(r, auth, stage)
})
if err != nil || !done {
return err
}
}
if err := c.users.DeactivateAccount(r.Context(), auth.Localpart, req.Erase); err != nil {
return err
}
c.uia.remove(sessionID)
httputil.WriteJSON(w, http.StatusOK, deactivateResponse{IDServerUnbindResult: "no-support"})
return nil
}