2020-10-02 17:18:20 +01:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2022-08-05 10:26:59 +01:00
|
|
|
"io"
|
2020-10-02 17:18:20 +01:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth"
|
|
|
|
"github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-05-09 23:46:49 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2020-10-02 17:18:20 +01:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Deactivate handles POST requests to /account/deactivate
|
|
|
|
func Deactivate(
|
|
|
|
req *http.Request,
|
|
|
|
userInteractiveAuth *auth.UserInteractive,
|
2022-05-05 13:17:38 +01:00
|
|
|
accountAPI api.ClientUserAPI,
|
2020-10-02 17:18:20 +01:00
|
|
|
deviceAPI *api.Device,
|
|
|
|
) util.JSONResponse {
|
|
|
|
ctx := req.Context()
|
|
|
|
defer req.Body.Close() // nolint:errcheck
|
2022-08-05 10:26:59 +01:00
|
|
|
bodyBytes, err := io.ReadAll(req.Body)
|
2020-10-02 17:18:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.BadJSON("The request body could not be read: " + err.Error()),
|
2020-10-02 17:18:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
login, errRes := userInteractiveAuth.Verify(ctx, bodyBytes, deviceAPI)
|
|
|
|
if errRes != nil {
|
|
|
|
return *errRes
|
|
|
|
}
|
|
|
|
|
2023-03-31 09:15:01 +01:00
|
|
|
localpart, serverName, err := gomatrixserverlib.SplitID('@', login.Username())
|
2020-10-02 17:18:20 +01:00
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("gomatrixserverlib.SplitID failed")
|
2023-05-17 01:33:27 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2020-10-02 17:18:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var res api.PerformAccountDeactivationResponse
|
2022-03-24 21:45:44 +00:00
|
|
|
err = accountAPI.PerformAccountDeactivation(ctx, &api.PerformAccountDeactivationRequest{
|
2023-03-31 09:15:01 +01:00
|
|
|
Localpart: localpart,
|
|
|
|
ServerName: serverName,
|
2020-10-02 17:18:20 +01:00
|
|
|
}, &res)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("userAPI.PerformAccountDeactivation failed")
|
2023-05-17 01:33:27 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2020-10-02 17:18:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|