2017-08-04 15:32:10 +00:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-10-11 17:16:53 +00:00
|
|
|
package routing
|
2017-08-04 15:32:10 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-13 12:37:50 +00:00
|
|
|
"context"
|
2017-09-11 18:18:19 +00:00
|
|
|
"errors"
|
2017-08-04 15:32:10 +00:00
|
|
|
"net/http"
|
2018-08-06 13:09:25 +00:00
|
|
|
"time"
|
2017-08-04 15:32:10 +00:00
|
|
|
|
2018-08-20 09:45:17 +00:00
|
|
|
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
|
2017-08-04 15:32:10 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/producers"
|
2017-09-01 09:13:10 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/threepid"
|
2020-05-21 13:40:13 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
|
|
|
"github.com/matrix-org/dendrite/internal/config"
|
2020-03-27 16:28:22 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2018-08-20 09:45:17 +00:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2017-08-04 15:32:10 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
2017-09-11 18:18:19 +00:00
|
|
|
var errMissingUserID = errors.New("'user_id' must be supplied")
|
|
|
|
|
2017-08-04 15:32:10 +00:00
|
|
|
// SendMembership implements PUT /rooms/{roomID}/(join|kick|ban|unban|leave|invite)
|
|
|
|
// by building a m.room.member event then sending it to the room server
|
2020-04-24 15:30:25 +00:00
|
|
|
// TODO: Can we improve the cyclo count here? Separate code paths for invites?
|
|
|
|
// nolint:gocyclo
|
2017-08-04 15:32:10 +00:00
|
|
|
func SendMembership(
|
2020-02-13 17:27:33 +00:00
|
|
|
req *http.Request, accountDB accounts.Database, device *authtypes.Device,
|
2020-02-11 11:18:12 +00:00
|
|
|
roomID string, membership string, cfg *config.Dendrite,
|
2020-05-01 09:48:17 +00:00
|
|
|
rsAPI roomserverAPI.RoomserverInternalAPI, asAPI appserviceAPI.AppServiceQueryAPI,
|
2018-08-20 09:45:17 +00:00
|
|
|
producer *producers.RoomserverProducer,
|
2017-08-04 15:32:10 +00:00
|
|
|
) util.JSONResponse {
|
2020-03-27 16:28:22 +00:00
|
|
|
verReq := api.QueryRoomVersionForRoomRequest{RoomID: roomID}
|
|
|
|
verRes := api.QueryRoomVersionForRoomResponse{}
|
2020-05-01 09:48:17 +00:00
|
|
|
if err := rsAPI.QueryRoomVersionForRoom(req.Context(), &verReq, &verRes); err != nil {
|
2020-03-27 16:28:22 +00:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.UnsupportedRoomVersion(err.Error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 09:13:10 +00:00
|
|
|
var body threepid.MembershipRequest
|
2017-08-29 14:17:26 +00:00
|
|
|
if reqErr := httputil.UnmarshalJSONRequest(req, &body); reqErr != nil {
|
|
|
|
return *reqErr
|
|
|
|
}
|
|
|
|
|
2018-08-22 12:40:25 +00:00
|
|
|
evTime, err := httputil.ParseTSParam(req)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.InvalidArgumentValue(err.Error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 13:29:30 +00:00
|
|
|
inviteStored, jsonErrResp := checkAndProcessThreepid(
|
2020-05-01 09:48:17 +00:00
|
|
|
req, device, &body, cfg, rsAPI, accountDB, producer,
|
2018-08-06 13:09:25 +00:00
|
|
|
membership, roomID, evTime,
|
2017-09-11 18:18:19 +00:00
|
|
|
)
|
2019-07-12 13:29:30 +00:00
|
|
|
if jsonErrResp != nil {
|
|
|
|
return *jsonErrResp
|
2017-08-29 14:17:26 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 18:18:19 +00:00
|
|
|
// If an invite has been stored on an identity server, it means that a
|
|
|
|
// m.room.third_party_invite event has been emitted and that we shouldn't
|
|
|
|
// emit a m.room.member one.
|
|
|
|
if inviteStored {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusOK,
|
2017-09-11 18:18:19 +00:00
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
event, err := buildMembershipEvent(
|
2020-05-07 15:46:11 +00:00
|
|
|
req.Context(), body, accountDB, device, membership,
|
|
|
|
roomID, false, cfg, evTime, rsAPI, asAPI,
|
2017-09-11 18:18:19 +00:00
|
|
|
)
|
|
|
|
if err == errMissingUserID {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusBadRequest,
|
2017-09-11 18:18:19 +00:00
|
|
|
JSON: jsonerror.BadJSON(err.Error()),
|
|
|
|
}
|
2020-05-21 13:40:13 +00:00
|
|
|
} else if err == internal.ErrRoomNoExists {
|
2017-09-11 18:18:19 +00:00
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusNotFound,
|
2017-09-11 18:18:19 +00:00
|
|
|
JSON: jsonerror.NotFound(err.Error()),
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
2020-03-02 16:20:44 +00:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("buildMembershipEvent failed")
|
|
|
|
return jsonerror.InternalServerError()
|
2017-09-11 18:18:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 13:29:30 +00:00
|
|
|
var returnData interface{} = struct{}{}
|
|
|
|
|
2020-04-24 15:30:25 +00:00
|
|
|
switch membership {
|
|
|
|
case gomatrixserverlib.Invite:
|
|
|
|
// Invites need to be handled specially
|
|
|
|
err = producer.SendInvite(
|
|
|
|
req.Context(),
|
|
|
|
event.Headered(verRes.RoomVersion),
|
|
|
|
nil, // ask the roomserver to draw up invite room state for us
|
|
|
|
cfg.Matrix.ServerName,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("producer.SendInvite failed")
|
|
|
|
return jsonerror.InternalServerError()
|
|
|
|
}
|
|
|
|
case gomatrixserverlib.Join:
|
|
|
|
// The join membership requires the room id to be sent in the response
|
2019-07-12 13:29:30 +00:00
|
|
|
returnData = struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
}{roomID}
|
2020-04-28 09:53:07 +00:00
|
|
|
fallthrough
|
2020-04-24 15:30:25 +00:00
|
|
|
default:
|
2020-04-28 09:53:07 +00:00
|
|
|
_, err = producer.SendEvents(
|
|
|
|
req.Context(),
|
|
|
|
[]gomatrixserverlib.HeaderedEvent{event.Headered(verRes.RoomVersion)},
|
|
|
|
cfg.Matrix.ServerName,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("producer.SendEvents failed")
|
|
|
|
return jsonerror.InternalServerError()
|
|
|
|
}
|
2019-07-12 13:29:30 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 18:18:19 +00:00
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusOK,
|
2019-07-12 13:29:30 +00:00
|
|
|
JSON: returnData,
|
2017-09-11 18:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildMembershipEvent(
|
2018-08-06 13:09:25 +00:00
|
|
|
ctx context.Context,
|
2020-02-13 17:27:33 +00:00
|
|
|
body threepid.MembershipRequest, accountDB accounts.Database,
|
2018-08-20 09:45:17 +00:00
|
|
|
device *authtypes.Device,
|
2020-05-07 15:46:11 +00:00
|
|
|
membership, roomID string, isDirect bool,
|
2020-02-11 11:18:12 +00:00
|
|
|
cfg *config.Dendrite, evTime time.Time,
|
2020-05-01 09:48:17 +00:00
|
|
|
rsAPI roomserverAPI.RoomserverInternalAPI, asAPI appserviceAPI.AppServiceQueryAPI,
|
2017-09-11 18:18:19 +00:00
|
|
|
) (*gomatrixserverlib.Event, error) {
|
|
|
|
stateKey, reason, err := getMembershipStateKey(body, device, membership)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-08-04 15:32:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 09:45:17 +00:00
|
|
|
profile, err := loadProfile(ctx, stateKey, cfg, accountDB, asAPI)
|
2017-08-04 15:32:10 +00:00
|
|
|
if err != nil {
|
2017-09-11 18:18:19 +00:00
|
|
|
return nil, err
|
2017-08-04 15:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
builder := gomatrixserverlib.EventBuilder{
|
|
|
|
Sender: device.UserID,
|
|
|
|
RoomID: roomID,
|
|
|
|
Type: "m.room.member",
|
|
|
|
StateKey: &stateKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
// "unban" or "kick" isn't a valid membership value, change it to "leave"
|
|
|
|
if membership == "unban" || membership == "kick" {
|
2019-08-06 14:07:36 +00:00
|
|
|
membership = gomatrixserverlib.Leave
|
2017-08-04 15:32:10 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 17:45:11 +00:00
|
|
|
content := gomatrixserverlib.MemberContent{
|
2017-08-04 15:32:10 +00:00
|
|
|
Membership: membership,
|
|
|
|
DisplayName: profile.DisplayName,
|
|
|
|
AvatarURL: profile.AvatarURL,
|
|
|
|
Reason: reason,
|
2020-05-07 15:46:11 +00:00
|
|
|
IsDirect: isDirect,
|
2017-08-04 15:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = builder.SetContent(content); err != nil {
|
2017-09-11 18:18:19 +00:00
|
|
|
return nil, err
|
2017-08-04 15:32:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 13:40:13 +00:00
|
|
|
return internal.BuildEvent(ctx, &builder, cfg, evTime, rsAPI, nil)
|
2017-08-04 15:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:17:26 +00:00
|
|
|
// loadProfile lookups the profile of a given user from the database and returns
|
|
|
|
// it if the user is local to this server, or returns an empty profile if not.
|
|
|
|
// Returns an error if the retrieval failed or if the first parameter isn't a
|
|
|
|
// valid Matrix ID.
|
2017-09-18 13:15:27 +00:00
|
|
|
func loadProfile(
|
2018-08-20 09:45:17 +00:00
|
|
|
ctx context.Context,
|
|
|
|
userID string,
|
2020-02-11 11:18:12 +00:00
|
|
|
cfg *config.Dendrite,
|
2020-02-13 17:27:33 +00:00
|
|
|
accountDB accounts.Database,
|
2018-08-20 09:45:17 +00:00
|
|
|
asAPI appserviceAPI.AppServiceQueryAPI,
|
2017-09-18 13:15:27 +00:00
|
|
|
) (*authtypes.Profile, error) {
|
2018-08-20 09:45:17 +00:00
|
|
|
_, serverName, err := gomatrixserverlib.SplitID('@', userID)
|
2017-08-29 14:17:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var profile *authtypes.Profile
|
|
|
|
if serverName == cfg.Matrix.ServerName {
|
2019-07-12 15:43:01 +00:00
|
|
|
profile, err = appserviceAPI.RetrieveUserProfile(ctx, userID, asAPI, accountDB)
|
2017-08-29 14:17:26 +00:00
|
|
|
} else {
|
|
|
|
profile = &authtypes.Profile{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return profile, err
|
|
|
|
}
|
|
|
|
|
2017-08-04 15:32:10 +00:00
|
|
|
// getMembershipStateKey extracts the target user ID of a membership change.
|
|
|
|
// For "join" and "leave" this will be the ID of the user making the change.
|
|
|
|
// For "ban", "unban", "kick" and "invite" the target user ID will be in the JSON request body.
|
|
|
|
// In the latter case, if there was an issue retrieving the user ID from the request body,
|
|
|
|
// returns a JSONResponse with a corresponding error code and message.
|
|
|
|
func getMembershipStateKey(
|
2017-09-01 09:13:10 +00:00
|
|
|
body threepid.MembershipRequest, device *authtypes.Device, membership string,
|
2017-09-11 18:18:19 +00:00
|
|
|
) (stateKey string, reason string, err error) {
|
2019-08-06 14:07:36 +00:00
|
|
|
if membership == gomatrixserverlib.Ban || membership == "unban" || membership == "kick" || membership == gomatrixserverlib.Invite {
|
2017-08-04 15:32:10 +00:00
|
|
|
// If we're in this case, the state key is contained in the request body,
|
|
|
|
// possibly along with a reason (for "kick" and "ban") so we need to parse
|
|
|
|
// it
|
2017-08-29 14:17:26 +00:00
|
|
|
if body.UserID == "" {
|
2017-09-11 18:18:19 +00:00
|
|
|
err = errMissingUserID
|
2017-08-04 15:32:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:17:26 +00:00
|
|
|
stateKey = body.UserID
|
|
|
|
reason = body.Reason
|
2017-08-04 15:32:10 +00:00
|
|
|
} else {
|
|
|
|
stateKey = device.UserID
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2019-07-12 13:29:30 +00:00
|
|
|
|
|
|
|
func checkAndProcessThreepid(
|
|
|
|
req *http.Request,
|
|
|
|
device *authtypes.Device,
|
|
|
|
body *threepid.MembershipRequest,
|
2020-02-11 11:18:12 +00:00
|
|
|
cfg *config.Dendrite,
|
2020-05-01 09:48:17 +00:00
|
|
|
rsAPI roomserverAPI.RoomserverInternalAPI,
|
2020-02-13 17:27:33 +00:00
|
|
|
accountDB accounts.Database,
|
2019-07-12 13:29:30 +00:00
|
|
|
producer *producers.RoomserverProducer,
|
|
|
|
membership, roomID string,
|
|
|
|
evTime time.Time,
|
|
|
|
) (inviteStored bool, errRes *util.JSONResponse) {
|
|
|
|
|
|
|
|
inviteStored, err := threepid.CheckAndProcessInvite(
|
2020-05-01 09:48:17 +00:00
|
|
|
req.Context(), device, body, cfg, rsAPI, accountDB, producer,
|
2019-07-12 13:29:30 +00:00
|
|
|
membership, roomID, evTime,
|
|
|
|
)
|
|
|
|
if err == threepid.ErrMissingParameter {
|
|
|
|
return inviteStored, &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON(err.Error()),
|
|
|
|
}
|
|
|
|
} else if err == threepid.ErrNotTrusted {
|
|
|
|
return inviteStored, &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.NotTrusted(body.IDServer),
|
|
|
|
}
|
2020-05-21 13:40:13 +00:00
|
|
|
} else if err == internal.ErrRoomNoExists {
|
2019-07-12 13:29:30 +00:00
|
|
|
return inviteStored, &util.JSONResponse{
|
|
|
|
Code: http.StatusNotFound,
|
|
|
|
JSON: jsonerror.NotFound(err.Error()),
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
2020-03-02 16:20:44 +00:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("threepid.CheckAndProcessInvite failed")
|
|
|
|
er := jsonerror.InternalServerError()
|
2019-07-12 13:29:30 +00:00
|
|
|
return inviteStored, &er
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|