2020-09-04 15:16:13 +01:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
2022-12-22 10:54:03 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-09-04 15:16:13 +01:00
|
|
|
"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-09-04 15:16:13 +01:00
|
|
|
"github.com/matrix-org/util"
|
2022-03-03 11:40:53 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-09-04 15:16:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type newPasswordRequest struct {
|
|
|
|
NewPassword string `json:"new_password"`
|
|
|
|
LogoutDevices bool `json:"logout_devices"`
|
|
|
|
Auth newPasswordAuth `json:"auth"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type newPasswordAuth struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Session string `json:"session"`
|
|
|
|
auth.PasswordRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func Password(
|
|
|
|
req *http.Request,
|
2022-05-05 13:17:38 +01:00
|
|
|
userAPI api.ClientUserAPI,
|
2020-09-04 15:16:13 +01:00
|
|
|
device *api.Device,
|
|
|
|
cfg *config.ClientAPI,
|
|
|
|
) util.JSONResponse {
|
|
|
|
// Check that the existing password is right.
|
|
|
|
var r newPasswordRequest
|
|
|
|
r.LogoutDevices = true
|
|
|
|
|
2022-03-03 11:40:53 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"sessionId": device.SessionID,
|
|
|
|
"userId": device.UserID,
|
|
|
|
}).Debug("Changing password")
|
|
|
|
|
2020-09-04 15:16:13 +01:00
|
|
|
// Unmarshal the request.
|
|
|
|
resErr := httputil.UnmarshalJSONRequest(req, &r)
|
|
|
|
if resErr != nil {
|
|
|
|
return *resErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve or generate the sessionID
|
|
|
|
sessionID := r.Auth.Session
|
|
|
|
if sessionID == "" {
|
|
|
|
// Generate a new, random session ID
|
|
|
|
sessionID = util.RandomString(sessionIDLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Require password auth to change the password.
|
|
|
|
if r.Auth.Type != authtypes.LoginTypePassword {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusUnauthorized,
|
|
|
|
JSON: newUserInteractiveResponse(
|
|
|
|
sessionID,
|
|
|
|
[]authtypes.Flow{
|
|
|
|
{
|
|
|
|
Stages: []authtypes.LoginType{authtypes.LoginTypePassword},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the existing password is correct.
|
|
|
|
typePassword := auth.LoginTypePassword{
|
2022-03-24 21:45:44 +00:00
|
|
|
GetAccountByPassword: userAPI.QueryAccountByPassword,
|
2020-09-04 15:16:13 +01:00
|
|
|
Config: cfg,
|
|
|
|
}
|
|
|
|
if _, authErr := typePassword.Login(req.Context(), &r.Auth.PasswordRequest); authErr != nil {
|
|
|
|
return *authErr
|
|
|
|
}
|
2022-02-25 13:33:02 +00:00
|
|
|
sessions.addCompletedSessionStage(sessionID, authtypes.LoginTypePassword)
|
2020-09-04 15:16:13 +01:00
|
|
|
|
|
|
|
// Check the new password strength.
|
2022-12-23 13:11:11 +00:00
|
|
|
if err := internal.ValidatePassword(r.NewPassword); err != nil {
|
|
|
|
return *internal.PasswordResponse(err)
|
2020-09-04 15:16:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the local part.
|
2022-11-11 16:41:37 +00:00
|
|
|
localpart, domain, err := gomatrixserverlib.SplitID('@', device.UserID)
|
2020-09-04 15:16:13 +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-09-04 15:16:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the user API to perform the password change.
|
2021-09-08 17:31:03 +01:00
|
|
|
passwordReq := &api.PerformPasswordUpdateRequest{
|
2022-11-11 16:41:37 +00:00
|
|
|
Localpart: localpart,
|
|
|
|
ServerName: domain,
|
|
|
|
Password: r.NewPassword,
|
2020-09-04 15:16:13 +01:00
|
|
|
}
|
2021-09-08 17:31:03 +01:00
|
|
|
passwordRes := &api.PerformPasswordUpdateResponse{}
|
2020-09-04 15:16:13 +01:00
|
|
|
if err := userAPI.PerformPasswordUpdate(req.Context(), passwordReq, passwordRes); err != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("PerformPasswordUpdate failed")
|
2023-05-17 01:33:27 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2020-09-04 15:16:13 +01:00
|
|
|
}
|
|
|
|
if !passwordRes.PasswordUpdated {
|
|
|
|
util.GetLogger(req.Context()).Error("Expected password to have been updated but wasn't")
|
2023-05-17 01:33:27 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2020-09-04 15:16:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the request asks us to log out all other devices then
|
|
|
|
// ask the user API to do that.
|
|
|
|
if r.LogoutDevices {
|
2021-09-08 17:31:03 +01:00
|
|
|
logoutReq := &api.PerformDeviceDeletionRequest{
|
2020-09-04 15:16:13 +01:00
|
|
|
UserID: device.UserID,
|
|
|
|
DeviceIDs: nil,
|
|
|
|
ExceptDeviceID: device.ID,
|
|
|
|
}
|
2021-09-08 17:31:03 +01:00
|
|
|
logoutRes := &api.PerformDeviceDeletionResponse{}
|
2020-09-04 15:16:13 +01:00
|
|
|
if err := userAPI.PerformDeviceDeletion(req.Context(), logoutReq, logoutRes); err != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("PerformDeviceDeletion failed")
|
2023-05-17 01:33:27 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2020-09-04 15:16:13 +01:00
|
|
|
}
|
2022-03-03 11:40:53 +00:00
|
|
|
|
|
|
|
pushersReq := &api.PerformPusherDeletionRequest{
|
2022-11-11 16:41:37 +00:00
|
|
|
Localpart: localpart,
|
|
|
|
ServerName: domain,
|
|
|
|
SessionID: device.SessionID,
|
2022-03-03 11:40:53 +00:00
|
|
|
}
|
|
|
|
if err := userAPI.PerformPusherDeletion(req.Context(), pushersReq, &struct{}{}); err != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("PerformPusherDeletion failed")
|
2023-05-17 01:33:27 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2022-03-03 11:40:53 +00:00
|
|
|
}
|
2020-09-04 15:16:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return a success code.
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|