2017-09-08 16:17:12 +02: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 19:16:53 +02:00
|
|
|
package routing
|
2017-09-08 16:17:12 +02:00
|
|
|
|
|
|
|
import (
|
2017-09-13 12:03:41 +02:00
|
|
|
"context"
|
2017-09-08 16:17:12 +02:00
|
|
|
"encoding/json"
|
2017-09-11 20:08:24 +02:00
|
|
|
"errors"
|
2017-09-08 16:17:12 +02:00
|
|
|
"net/http"
|
2018-08-06 15:09:25 +02:00
|
|
|
"time"
|
2017-09-08 16:17:12 +02:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
2020-03-27 17:28:22 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2023-04-27 13:54:20 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2020-12-02 18:41:00 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-06-16 15:53:19 +02:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2023-05-10 00:46:49 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 16:50:33 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2017-09-08 16:17:12 +02:00
|
|
|
"github.com/matrix-org/util"
|
2017-10-13 15:42:57 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-09-08 16:17:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type invite struct {
|
2019-08-15 19:45:11 +02:00
|
|
|
MXID string `json:"mxid"`
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
Sender string `json:"sender"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
Signed gomatrixserverlib.MemberThirdPartyInviteSigned `json:"signed"`
|
2017-09-08 16:17:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type invites struct {
|
|
|
|
Medium string `json:"medium"`
|
|
|
|
Address string `json:"address"`
|
|
|
|
MXID string `json:"mxid"`
|
|
|
|
Invites []invite `json:"invites"`
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:31:46 +02:00
|
|
|
var (
|
|
|
|
errNotLocalUser = errors.New("the user is not from this server")
|
|
|
|
errNotInRoom = errors.New("the server isn't currently in the room")
|
|
|
|
)
|
2017-09-12 18:15:13 +02:00
|
|
|
|
2017-09-08 16:17:12 +02:00
|
|
|
// CreateInvitesFrom3PIDInvites implements POST /_matrix/federation/v1/3pid/onbind
|
|
|
|
func CreateInvitesFrom3PIDInvites(
|
2022-05-05 20:30:38 +02:00
|
|
|
req *http.Request, rsAPI api.FederationRoomserverAPI,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.FederationAPI,
|
2023-04-24 18:23:25 +02:00
|
|
|
federation fclient.FederationClient,
|
2022-05-05 20:30:38 +02:00
|
|
|
userAPI userapi.FederationUserAPI,
|
2017-09-08 16:17:12 +02:00
|
|
|
) util.JSONResponse {
|
|
|
|
var body invites
|
|
|
|
if reqErr := httputil.UnmarshalJSONRequest(req, &body); reqErr != nil {
|
|
|
|
return *reqErr
|
|
|
|
}
|
|
|
|
|
2023-04-27 13:54:20 +02:00
|
|
|
evs := []*types.HeaderedEvent{}
|
2017-09-08 16:17:12 +02:00
|
|
|
for _, inv := range body.Invites {
|
2023-04-27 13:54:20 +02:00
|
|
|
_, err := rsAPI.QueryRoomVersionForRoom(req.Context(), inv.RoomID)
|
2023-04-27 08:07:13 +02:00
|
|
|
if err != nil {
|
2020-03-27 17:28:22 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.UnsupportedRoomVersion(err.Error()),
|
2020-03-27 17:28:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 12:03:41 +02:00
|
|
|
event, err := createInviteFrom3PIDInvite(
|
2020-06-16 15:53:19 +02:00
|
|
|
req.Context(), rsAPI, cfg, inv, federation, userAPI,
|
2017-09-13 12:03:41 +02:00
|
|
|
)
|
2017-09-08 16:17:12 +02:00
|
|
|
if err != nil {
|
2020-03-02 17:20:44 +01:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("createInviteFrom3PIDInvite failed")
|
2023-05-17 02:33:27 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2017-09-08 16:17:12 +02:00
|
|
|
}
|
|
|
|
if event != nil {
|
2023-05-02 16:03:16 +02:00
|
|
|
evs = append(evs, &types.HeaderedEvent{PDU: event})
|
2017-09-08 16:17:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send all the events
|
2022-11-15 16:05:23 +01:00
|
|
|
if err := api.SendEvents(
|
|
|
|
req.Context(),
|
|
|
|
rsAPI,
|
|
|
|
api.KindNew,
|
|
|
|
evs,
|
|
|
|
cfg.Matrix.ServerName, // TODO: which virtual host?
|
|
|
|
"TODO",
|
|
|
|
cfg.Matrix.ServerName,
|
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
); err != nil {
|
2020-06-10 13:17:54 +02:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("SendEvents failed")
|
2023-05-17 02:33:27 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2017-09-08 16:17:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusOK,
|
2017-09-08 16:17:12 +02:00
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:15:13 +02:00
|
|
|
// ExchangeThirdPartyInvite implements PUT /_matrix/federation/v1/exchange_third_party_invite/{roomID}
|
|
|
|
func ExchangeThirdPartyInvite(
|
|
|
|
httpReq *http.Request,
|
2023-04-19 16:50:33 +02:00
|
|
|
request *fclient.FederationRequest,
|
2017-09-12 18:15:13 +02:00
|
|
|
roomID string,
|
2022-05-05 20:30:38 +02:00
|
|
|
rsAPI api.FederationRoomserverAPI,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.FederationAPI,
|
2023-04-24 18:23:25 +02:00
|
|
|
federation fclient.FederationClient,
|
2017-09-12 18:15:13 +02:00
|
|
|
) util.JSONResponse {
|
2023-05-04 12:17:42 +02:00
|
|
|
var proto gomatrixserverlib.ProtoEvent
|
|
|
|
if err := json.Unmarshal(request.Content(), &proto); err != nil {
|
2017-09-12 18:15:13 +02:00
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.NotJSON("The request body could not be decoded into valid JSON. " + err.Error()),
|
2017-09-12 18:15:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the room ID is correct.
|
2023-05-04 12:17:42 +02:00
|
|
|
if proto.RoomID != roomID {
|
2017-09-12 18:15:13 +02:00
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.BadJSON("The room ID in the request path must match the room ID in the invite event JSON"),
|
2017-09-12 18:15:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 16:23:46 +02:00
|
|
|
validRoomID, err := spec.NewRoomID(roomID)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.BadJSON("Invalid room ID"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
userID, err := rsAPI.QueryUserIDForSender(httpReq.Context(), *validRoomID, spec.SenderID(proto.SenderID))
|
2023-06-07 19:14:35 +02:00
|
|
|
if err != nil || userID == nil {
|
2022-11-15 16:05:23 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-06-07 19:14:35 +02:00
|
|
|
JSON: spec.BadJSON("Invalid sender ID"),
|
2022-11-15 16:05:23 +01:00
|
|
|
}
|
|
|
|
}
|
2023-06-07 19:14:35 +02:00
|
|
|
senderDomain := userID.Domain()
|
2022-11-15 16:05:23 +01:00
|
|
|
|
2017-09-12 18:15:13 +02:00
|
|
|
// Check that the state key is correct.
|
2023-06-14 16:23:46 +02:00
|
|
|
targetUserID, err := rsAPI.QueryUserIDForSender(httpReq.Context(), *validRoomID, spec.SenderID(*proto.StateKey))
|
2023-06-07 19:14:35 +02:00
|
|
|
if err != nil || targetUserID == nil {
|
2017-09-12 18:15:13 +02:00
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.BadJSON("The event's state key isn't a Matrix user ID"),
|
2017-09-12 18:15:13 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-07 19:14:35 +02:00
|
|
|
targetDomain := targetUserID.Domain()
|
2017-09-12 18:15:13 +02:00
|
|
|
|
|
|
|
// Check that the target user is from the requesting homeserver.
|
|
|
|
if targetDomain != request.Origin() {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.BadJSON("The event's state key doesn't have the same domain as the request's origin"),
|
2017-09-12 18:15:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 08:07:13 +02:00
|
|
|
roomVersion, err := rsAPI.QueryRoomVersionForRoom(httpReq.Context(), roomID)
|
|
|
|
if err != nil {
|
2020-03-27 17:28:22 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.UnsupportedRoomVersion(err.Error()),
|
2020-03-27 17:28:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:15:13 +02:00
|
|
|
// Auth and build the event from what the remote server sent us
|
2023-05-04 12:17:42 +02:00
|
|
|
event, err := buildMembershipEvent(httpReq.Context(), &proto, rsAPI, cfg)
|
2017-09-12 18:15:13 +02:00
|
|
|
if err == errNotInRoom {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusNotFound,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.NotFound("Unknown room " + roomID),
|
2017-09-12 18:15:13 +02:00
|
|
|
}
|
|
|
|
} else if err != nil {
|
2020-03-02 17:20:44 +01:00
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("buildMembershipEvent failed")
|
2023-05-17 02:33:27 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2017-09-12 18:15:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the requesting server to sign the newly created event so we know it
|
|
|
|
// acknowledged it
|
2023-04-27 13:54:20 +02:00
|
|
|
inviteReq, err := fclient.NewInviteV2Request(event, nil)
|
2022-05-17 14:23:35 +02:00
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("failed to make invite v2 request")
|
2023-05-17 02:33:27 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2022-05-17 14:23:35 +02:00
|
|
|
}
|
2022-11-15 16:05:23 +01:00
|
|
|
signedEvent, err := federation.SendInviteV2(httpReq.Context(), senderDomain, request.Origin(), inviteReq)
|
2017-09-12 18:15:13 +02:00
|
|
|
if err != nil {
|
2020-03-02 17:20:44 +01:00
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("federation.SendInvite failed")
|
2023-05-17 02:33:27 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2017-09-12 18:15:13 +02:00
|
|
|
}
|
2023-04-27 08:07:13 +02:00
|
|
|
verImpl, err := gomatrixserverlib.GetRoomVersion(roomVersion)
|
2023-04-21 18:06:29 +02:00
|
|
|
if err != nil {
|
2023-04-27 08:07:13 +02:00
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Errorf("unknown room version: %s", roomVersion)
|
2023-05-17 02:33:27 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2023-04-21 18:06:29 +02:00
|
|
|
}
|
|
|
|
inviteEvent, err := verImpl.NewEventFromUntrustedJSON(signedEvent.Event)
|
2022-02-09 21:31:24 +01:00
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("federation.SendInvite failed")
|
2023-05-17 02:33:27 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2022-02-09 21:31:24 +01:00
|
|
|
}
|
2017-09-12 18:15:13 +02:00
|
|
|
|
2023-06-06 15:16:55 +02:00
|
|
|
// Send the event to the Roomserver
|
2020-09-03 16:22:16 +02:00
|
|
|
if err = api.SendEvents(
|
2020-06-10 13:17:54 +02:00
|
|
|
httpReq.Context(), rsAPI,
|
2020-10-19 15:59:13 +02:00
|
|
|
api.KindNew,
|
2023-04-27 13:54:20 +02:00
|
|
|
[]*types.HeaderedEvent{
|
2023-05-02 16:03:16 +02:00
|
|
|
{PDU: inviteEvent},
|
2020-03-27 17:28:22 +01:00
|
|
|
},
|
2022-11-15 16:05:23 +01:00
|
|
|
request.Destination(),
|
2022-01-27 15:29:14 +01:00
|
|
|
request.Origin(),
|
2020-03-27 17:28:22 +01:00
|
|
|
cfg.Matrix.ServerName,
|
|
|
|
nil,
|
2022-01-05 18:44:49 +01:00
|
|
|
false,
|
2017-09-13 14:37:50 +02:00
|
|
|
); err != nil {
|
2020-06-10 13:17:54 +02:00
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("SendEvents failed")
|
2023-05-17 02:33:27 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2017-09-12 18:15:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusOK,
|
2017-09-12 18:15:13 +02:00
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:17:12 +02:00
|
|
|
// createInviteFrom3PIDInvite processes an invite provided by the identity server
|
|
|
|
// and creates a m.room.member event (with "invite" membership) from it.
|
|
|
|
// Returns an error if there was a problem building the event or fetching the
|
|
|
|
// necessary data to do so.
|
|
|
|
func createInviteFrom3PIDInvite(
|
2022-05-05 20:30:38 +02:00
|
|
|
ctx context.Context, rsAPI api.FederationRoomserverAPI,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.FederationAPI,
|
2023-04-24 18:23:25 +02:00
|
|
|
inv invite, federation fclient.FederationClient,
|
2022-05-05 20:30:38 +02:00
|
|
|
userAPI userapi.FederationUserAPI,
|
2023-05-03 11:21:27 +02:00
|
|
|
) (gomatrixserverlib.PDU, error) {
|
2018-08-20 11:45:17 +02:00
|
|
|
_, server, err := gomatrixserverlib.SplitID('@', inv.MXID)
|
2017-09-13 14:31:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if server != cfg.Matrix.ServerName {
|
|
|
|
return nil, errNotLocalUser
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:17:12 +02:00
|
|
|
// Build the event
|
2023-05-04 12:17:42 +02:00
|
|
|
proto := &gomatrixserverlib.ProtoEvent{
|
2017-09-08 16:17:12 +02:00
|
|
|
Type: "m.room.member",
|
2023-06-07 19:14:35 +02:00
|
|
|
SenderID: inv.Sender,
|
2017-09-08 16:17:12 +02:00
|
|
|
RoomID: inv.RoomID,
|
|
|
|
StateKey: &inv.MXID,
|
|
|
|
}
|
|
|
|
|
2023-04-03 20:19:26 +02:00
|
|
|
profile, err := userAPI.QueryProfile(ctx, inv.MXID)
|
2017-09-13 14:31:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-15 19:45:11 +02:00
|
|
|
content := gomatrixserverlib.MemberContent{
|
2023-04-03 20:19:26 +02:00
|
|
|
AvatarURL: profile.AvatarURL,
|
|
|
|
DisplayName: profile.DisplayName,
|
2023-04-19 16:50:33 +02:00
|
|
|
Membership: spec.Invite,
|
2019-08-15 19:45:11 +02:00
|
|
|
ThirdPartyInvite: &gomatrixserverlib.MemberThirdPartyInvite{
|
2017-09-08 16:17:12 +02:00
|
|
|
Signed: inv.Signed,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-04 12:17:42 +02:00
|
|
|
if err = proto.SetContent(content); err != nil {
|
2017-09-08 16:17:12 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-04 12:17:42 +02:00
|
|
|
event, err := buildMembershipEvent(ctx, proto, rsAPI, cfg)
|
2017-09-12 18:15:13 +02:00
|
|
|
if err == errNotInRoom {
|
2023-05-04 12:17:42 +02:00
|
|
|
return nil, sendToRemoteServer(ctx, inv, federation, cfg, *proto)
|
2017-09-13 12:03:41 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-09-12 18:15:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return event, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildMembershipEvent uses a builder for a m.room.member invite event derived
|
|
|
|
// from a third-party invite to auth and build the said event. Returns the said
|
|
|
|
// event.
|
|
|
|
// Returns errNotInRoom if the server is not in the room the invite is for.
|
|
|
|
// Returns an error if something failed during the process.
|
|
|
|
func buildMembershipEvent(
|
2018-08-06 15:09:25 +02:00
|
|
|
ctx context.Context,
|
2023-05-04 12:17:42 +02:00
|
|
|
protoEvent *gomatrixserverlib.ProtoEvent, rsAPI api.FederationRoomserverAPI,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.FederationAPI,
|
2023-05-03 11:21:27 +02:00
|
|
|
) (gomatrixserverlib.PDU, error) {
|
2023-05-04 12:17:42 +02:00
|
|
|
eventsNeeded, err := gomatrixserverlib.StateNeededForProtoEvent(protoEvent)
|
2017-09-08 16:17:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-14 19:36:08 +02:00
|
|
|
if len(eventsNeeded.Tuples()) == 0 {
|
|
|
|
return nil, errors.New("expecting state tuples for event builder, got none")
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:16:55 +02:00
|
|
|
// Ask the Roomserver for information about this room
|
2020-06-16 15:53:19 +02:00
|
|
|
queryReq := api.QueryLatestEventsAndStateRequest{
|
2023-05-04 12:17:42 +02:00
|
|
|
RoomID: protoEvent.RoomID,
|
2017-09-08 16:17:12 +02:00
|
|
|
StateToFetch: eventsNeeded.Tuples(),
|
|
|
|
}
|
2020-06-16 15:53:19 +02:00
|
|
|
var queryRes api.QueryLatestEventsAndStateResponse
|
2020-05-01 11:48:17 +02:00
|
|
|
if err = rsAPI.QueryLatestEventsAndState(ctx, &queryReq, &queryRes); err != nil {
|
2017-09-08 16:17:12 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !queryRes.RoomExists {
|
2017-09-11 20:08:24 +02:00
|
|
|
// Use federation to auth the event
|
2017-09-12 18:15:13 +02:00
|
|
|
return nil, errNotInRoom
|
2017-09-08 16:17:12 +02:00
|
|
|
}
|
|
|
|
|
2017-09-11 20:08:24 +02:00
|
|
|
// Auth the event locally
|
2023-05-04 12:17:42 +02:00
|
|
|
protoEvent.Depth = queryRes.Depth
|
|
|
|
protoEvent.PrevEvents = queryRes.LatestEvents
|
2017-09-08 16:17:12 +02:00
|
|
|
|
|
|
|
authEvents := gomatrixserverlib.NewAuthEvents(nil)
|
|
|
|
|
|
|
|
for i := range queryRes.StateEvents {
|
2023-05-02 16:03:16 +02:00
|
|
|
err = authEvents.AddEvent(queryRes.StateEvents[i].PDU)
|
2017-09-20 11:59:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-08 16:17:12 +02:00
|
|
|
}
|
|
|
|
|
2023-05-04 12:17:42 +02:00
|
|
|
if err = fillDisplayName(protoEvent, authEvents); err != nil {
|
2017-09-08 16:17:12 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
refs, err := eventsNeeded.AuthEventReferences(&authEvents)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-04 12:17:42 +02:00
|
|
|
protoEvent.AuthEvents = refs
|
|
|
|
|
|
|
|
verImpl, err := gomatrixserverlib.GetRoomVersion(queryRes.RoomVersion)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
builder := verImpl.NewEventBuilderFromProtoEvent(protoEvent)
|
2017-09-08 16:17:12 +02:00
|
|
|
|
2018-08-06 15:09:25 +02:00
|
|
|
event, err := builder.Build(
|
2020-03-27 17:28:22 +01:00
|
|
|
time.Now(), cfg.Matrix.ServerName, cfg.Matrix.KeyID,
|
2023-05-04 12:17:42 +02:00
|
|
|
cfg.Matrix.PrivateKey,
|
2018-08-06 15:09:25 +02:00
|
|
|
)
|
2017-09-08 16:17:12 +02:00
|
|
|
|
2020-11-16 16:44:53 +01:00
|
|
|
return event, err
|
2017-09-08 16:17:12 +02:00
|
|
|
}
|
|
|
|
|
2017-09-11 20:08:24 +02:00
|
|
|
// sendToRemoteServer uses federation to send an invite provided by an identity
|
|
|
|
// server to a remote server in case the current server isn't in the room the
|
|
|
|
// invite is for.
|
|
|
|
// Returns an error if it couldn't get the server names to reach or if all of
|
|
|
|
// them responded with an error.
|
|
|
|
func sendToRemoteServer(
|
2017-09-13 12:03:41 +02:00
|
|
|
ctx context.Context, inv invite,
|
2023-04-24 18:23:25 +02:00
|
|
|
federation fclient.FederationClient, cfg *config.FederationAPI,
|
2023-05-04 12:17:42 +02:00
|
|
|
proto gomatrixserverlib.ProtoEvent,
|
2017-09-11 20:08:24 +02:00
|
|
|
) (err error) {
|
2023-04-19 16:50:33 +02:00
|
|
|
remoteServers := make([]spec.ServerName, 2)
|
2017-09-11 20:08:24 +02:00
|
|
|
_, remoteServers[0], err = gomatrixserverlib.SplitID('@', inv.Sender)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Fallback to the room's server if the sender's domain is the same as
|
|
|
|
// the current server's
|
|
|
|
_, remoteServers[1], err = gomatrixserverlib.SplitID('!', inv.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, server := range remoteServers {
|
2023-05-04 12:17:42 +02:00
|
|
|
err = federation.ExchangeThirdPartyInvite(ctx, cfg.Matrix.ServerName, server, proto)
|
2017-09-11 20:08:24 +02:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2018-09-01 10:31:47 +02:00
|
|
|
logrus.WithError(err).Warnf("failed to send 3PID invite via %s", server)
|
2017-09-11 20:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("failed to send 3PID invite via any server")
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:17:12 +02:00
|
|
|
// fillDisplayName looks in a list of auth events for a m.room.third_party_invite
|
|
|
|
// event with the state key matching a given m.room.member event's content's token.
|
|
|
|
// If such an event is found, fills the "display_name" attribute of the
|
|
|
|
// "third_party_invite" structure in the m.room.member event with the display_name
|
|
|
|
// from the m.room.third_party_invite event.
|
|
|
|
// Returns an error if there was a problem parsing the m.room.third_party_invite
|
|
|
|
// event's content or updating the m.room.member event's content.
|
|
|
|
// Returns nil if no m.room.third_party_invite with a matching token could be
|
|
|
|
// found. Returning an error isn't necessary in this case as the event will be
|
|
|
|
// rejected by gomatrixserverlib.
|
|
|
|
func fillDisplayName(
|
2023-05-04 12:17:42 +02:00
|
|
|
builder *gomatrixserverlib.ProtoEvent, authEvents gomatrixserverlib.AuthEvents,
|
2017-09-08 16:17:12 +02:00
|
|
|
) error {
|
2019-08-15 19:45:11 +02:00
|
|
|
var content gomatrixserverlib.MemberContent
|
2017-09-12 18:15:13 +02:00
|
|
|
if err := json.Unmarshal(builder.Content, &content); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:17:12 +02:00
|
|
|
// Look for the m.room.third_party_invite event
|
|
|
|
thirdPartyInviteEvent, _ := authEvents.ThirdPartyInvite(content.ThirdPartyInvite.Signed.Token)
|
|
|
|
|
|
|
|
if thirdPartyInviteEvent == nil {
|
|
|
|
// If the third party invite event doesn't exist then we can't use it to set the display name.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-15 19:45:11 +02:00
|
|
|
var thirdPartyInviteContent gomatrixserverlib.ThirdPartyInviteContent
|
2017-09-08 16:17:12 +02:00
|
|
|
if err := json.Unmarshal(thirdPartyInviteEvent.Content(), &thirdPartyInviteContent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the m.room.third_party_invite event to fill the "displayname" and
|
|
|
|
// update the m.room.member event's content with it
|
|
|
|
content.ThirdPartyInvite.DisplayName = thirdPartyInviteContent.DisplayName
|
2017-09-20 15:15:38 +02:00
|
|
|
return builder.SetContent(content)
|
2017-09-08 16:17:12 +02:00
|
|
|
}
|