2020-09-02 17:13:15 +01:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-09-02 13:47:31 +01:00
|
|
|
package perform
|
2020-06-24 15:06:14 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-06 16:15:24 +01:00
|
|
|
"crypto/ed25519"
|
2020-06-24 15:06:14 +01:00
|
|
|
"fmt"
|
|
|
|
|
2021-11-24 10:45:23 +00:00
|
|
|
federationAPI "github.com/matrix-org/dendrite/federationapi/api"
|
2020-06-24 15:06:14 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-09-02 13:47:31 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/helpers"
|
2020-09-02 17:13:15 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/input"
|
2020-06-24 15:06:14 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/state"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
2022-03-17 17:05:21 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/shared"
|
2020-06-24 15:06:14 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-06-24 15:06:14 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-02-02 13:30:48 +00:00
|
|
|
"github.com/matrix-org/util"
|
2020-06-24 15:06:14 +01:00
|
|
|
)
|
|
|
|
|
2023-05-31 17:33:49 +01:00
|
|
|
type QueryState struct {
|
|
|
|
storage.Database
|
2023-06-14 15:23:46 +01:00
|
|
|
querier api.QuerySenderIDAPI
|
2023-05-31 17:33:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *QueryState) GetAuthEvents(ctx context.Context, event gomatrixserverlib.PDU) (gomatrixserverlib.AuthEventProvider, error) {
|
|
|
|
return helpers.GetAuthEvents(ctx, q.Database, event.Version(), event, event.AuthEventIDs())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *QueryState) GetState(ctx context.Context, roomID spec.RoomID, stateWanted []gomatrixserverlib.StateKeyTuple) ([]gomatrixserverlib.PDU, error) {
|
|
|
|
info, err := q.Database.RoomInfo(ctx, roomID.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to load RoomInfo: %w", err)
|
|
|
|
}
|
|
|
|
if info != nil {
|
2023-06-14 15:23:46 +01:00
|
|
|
roomState := state.NewStateResolution(q.Database, info, q.querier)
|
2023-05-31 17:33:49 +01:00
|
|
|
stateEntries, err := roomState.LoadStateAtSnapshotForStringTuples(
|
|
|
|
ctx, info.StateSnapshotNID(), stateWanted,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
stateNIDs := []types.EventNID{}
|
|
|
|
for _, stateNID := range stateEntries {
|
|
|
|
stateNIDs = append(stateNIDs, stateNID.EventNID)
|
|
|
|
}
|
|
|
|
stateEvents, err := q.Database.Events(ctx, info.RoomVersion, stateNIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to obtain required events: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
events := []gomatrixserverlib.PDU{}
|
|
|
|
for _, event := range stateEvents {
|
|
|
|
events = append(events, event.PDU)
|
|
|
|
}
|
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-09-02 13:47:31 +01:00
|
|
|
type Inviter struct {
|
2020-09-02 17:13:15 +01:00
|
|
|
DB storage.Database
|
|
|
|
Cfg *config.RoomServer
|
2022-05-05 19:30:38 +01:00
|
|
|
FSAPI federationAPI.RoomserverFederationAPI
|
2023-05-31 17:33:49 +01:00
|
|
|
RSAPI api.RoomserverInternalAPI
|
2020-09-02 17:13:15 +01:00
|
|
|
Inputer *input.Inputer
|
2020-09-02 13:47:31 +01:00
|
|
|
}
|
|
|
|
|
2023-05-31 17:33:49 +01:00
|
|
|
func (r *Inviter) IsKnownRoom(ctx context.Context, roomID spec.RoomID) (bool, error) {
|
|
|
|
info, err := r.DB.RoomInfo(ctx, roomID.String())
|
2022-09-26 17:35:35 +01:00
|
|
|
if err != nil {
|
2023-05-31 17:33:49 +01:00
|
|
|
return false, fmt.Errorf("failed to load RoomInfo: %w", err)
|
2022-09-26 17:35:35 +01:00
|
|
|
}
|
2023-05-31 17:33:49 +01:00
|
|
|
return (info != nil && !info.IsStub()), nil
|
|
|
|
}
|
2020-06-24 15:06:14 +01:00
|
|
|
|
2023-05-31 17:33:49 +01:00
|
|
|
func (r *Inviter) StateQuerier() gomatrixserverlib.StateQuerier {
|
|
|
|
return &QueryState{Database: r.DB}
|
|
|
|
}
|
2020-06-24 15:06:14 +01:00
|
|
|
|
2023-05-31 17:33:49 +01:00
|
|
|
func (r *Inviter) ProcessInviteMembership(
|
|
|
|
ctx context.Context, inviteEvent *types.HeaderedEvent,
|
|
|
|
) ([]api.OutputEvent, error) {
|
|
|
|
var outputUpdates []api.OutputEvent
|
|
|
|
var updater *shared.MembershipUpdater
|
2023-06-06 21:55:18 +01:00
|
|
|
|
2023-06-14 15:23:46 +01:00
|
|
|
validRoomID, err := spec.NewRoomID(inviteEvent.RoomID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
userID, err := r.RSAPI.QueryUserIDForSender(ctx, *validRoomID, spec.SenderID(*inviteEvent.StateKey()))
|
2022-06-29 12:32:24 +01:00
|
|
|
if err != nil {
|
2023-05-31 17:33:49 +01:00
|
|
|
return nil, api.ErrInvalidID{Err: fmt.Errorf("the user ID %s is invalid", *inviteEvent.StateKey())}
|
2022-06-29 12:32:24 +01:00
|
|
|
}
|
2023-06-06 21:55:18 +01:00
|
|
|
isTargetLocal := r.Cfg.Matrix.IsLocalServerName(userID.Domain())
|
2023-05-31 17:33:49 +01:00
|
|
|
if updater, err = r.DB.MembershipUpdater(ctx, inviteEvent.RoomID(), *inviteEvent.StateKey(), isTargetLocal, inviteEvent.Version()); err != nil {
|
|
|
|
return nil, fmt.Errorf("r.DB.MembershipUpdater: %w", err)
|
2022-07-22 14:44:04 +01:00
|
|
|
}
|
2023-05-31 17:33:49 +01:00
|
|
|
outputUpdates, err = helpers.UpdateToInviteMembership(updater, &types.Event{
|
|
|
|
EventNID: 0,
|
|
|
|
PDU: inviteEvent.PDU,
|
|
|
|
}, outputUpdates, inviteEvent.Version())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("updateToInviteMembership: %w", err)
|
2020-08-17 11:40:49 +01:00
|
|
|
}
|
2023-05-31 17:33:49 +01:00
|
|
|
if err = updater.Commit(); err != nil {
|
|
|
|
return nil, fmt.Errorf("updater.Commit: %w", err)
|
2020-08-17 11:40:49 +01:00
|
|
|
}
|
2023-05-31 17:33:49 +01:00
|
|
|
return outputUpdates, nil
|
|
|
|
}
|
2020-08-17 11:40:49 +01:00
|
|
|
|
2023-05-31 17:33:49 +01:00
|
|
|
// nolint:gocyclo
|
|
|
|
func (r *Inviter) PerformInvite(
|
|
|
|
ctx context.Context,
|
|
|
|
req *api.PerformInviteRequest,
|
|
|
|
) error {
|
2023-07-06 16:15:24 +01:00
|
|
|
senderID, err := r.RSAPI.QuerySenderIDForUser(ctx, req.InviteInput.RoomID, req.InviteInput.Inviter)
|
2023-06-14 15:23:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-08-02 11:12:14 +01:00
|
|
|
} else if senderID == nil {
|
|
|
|
return fmt.Errorf("sender ID not found for %s in %s", req.InviteInput.Inviter, req.InviteInput.RoomID)
|
2023-06-14 15:23:46 +01:00
|
|
|
}
|
2023-07-06 16:15:24 +01:00
|
|
|
info, err := r.DB.RoomInfo(ctx, req.InviteInput.RoomID.String())
|
2023-05-31 17:33:49 +01:00
|
|
|
if err != nil {
|
2023-07-06 16:15:24 +01:00
|
|
|
return err
|
2023-05-31 17:33:49 +01:00
|
|
|
}
|
2023-07-06 16:15:24 +01:00
|
|
|
|
|
|
|
proto := gomatrixserverlib.ProtoEvent{
|
2023-08-02 11:12:14 +01:00
|
|
|
SenderID: string(*senderID),
|
2023-07-06 16:15:24 +01:00
|
|
|
RoomID: req.InviteInput.RoomID.String(),
|
|
|
|
Type: "m.room.member",
|
2022-07-22 14:44:04 +01:00
|
|
|
}
|
|
|
|
|
2023-07-06 16:15:24 +01:00
|
|
|
content := gomatrixserverlib.MemberContent{
|
|
|
|
Membership: spec.Invite,
|
|
|
|
DisplayName: req.InviteInput.DisplayName,
|
|
|
|
AvatarURL: req.InviteInput.AvatarURL,
|
|
|
|
Reason: req.InviteInput.Reason,
|
|
|
|
IsDirect: req.InviteInput.IsDirect,
|
2020-08-17 17:33:19 +01:00
|
|
|
}
|
2023-07-06 16:15:24 +01:00
|
|
|
|
|
|
|
if err = proto.SetContent(content); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !r.Cfg.Matrix.IsLocalServerName(req.InviteInput.Inviter.Domain()) {
|
|
|
|
return api.ErrInvalidID{Err: fmt.Errorf("the invite must be from a local user")}
|
2020-06-24 15:06:14 +01:00
|
|
|
}
|
|
|
|
|
2023-07-06 16:15:24 +01:00
|
|
|
isTargetLocal := r.Cfg.Matrix.IsLocalServerName(req.InviteInput.Invitee.Domain())
|
|
|
|
|
|
|
|
signingKey := req.InviteInput.PrivateKey
|
|
|
|
if info.RoomVersion == gomatrixserverlib.RoomVersionPseudoIDs {
|
|
|
|
signingKey, err = r.RSAPI.GetOrCreateUserRoomPrivateKey(ctx, req.InviteInput.Inviter, req.InviteInput.RoomID)
|
2023-06-28 19:29:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-07 18:14:35 +01:00
|
|
|
}
|
|
|
|
|
2023-05-31 17:33:49 +01:00
|
|
|
input := gomatrixserverlib.PerformInviteInput{
|
2023-07-06 16:15:24 +01:00
|
|
|
RoomID: req.InviteInput.RoomID,
|
|
|
|
RoomVersion: info.RoomVersion,
|
|
|
|
Inviter: req.InviteInput.Inviter,
|
|
|
|
Invitee: req.InviteInput.Invitee,
|
2023-05-31 17:33:49 +01:00
|
|
|
IsTargetLocal: isTargetLocal,
|
2023-07-06 16:15:24 +01:00
|
|
|
EventTemplate: proto,
|
2023-05-31 17:33:49 +01:00
|
|
|
StrippedState: req.InviteRoomState,
|
2023-07-06 16:15:24 +01:00
|
|
|
KeyID: req.InviteInput.KeyID,
|
|
|
|
SigningKey: signingKey,
|
|
|
|
EventTime: req.InviteInput.EventTime,
|
2023-05-31 17:33:49 +01:00
|
|
|
MembershipQuerier: &api.MembershipQuerier{Roomserver: r.RSAPI},
|
2023-06-14 15:23:46 +01:00
|
|
|
StateQuerier: &QueryState{r.DB, r.RSAPI},
|
|
|
|
UserIDQuerier: func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
|
|
|
return r.RSAPI.QueryUserIDForSender(ctx, roomID, senderID)
|
2023-06-06 21:55:18 +01:00
|
|
|
},
|
2023-08-02 11:12:14 +01:00
|
|
|
SenderIDQuerier: func(roomID spec.RoomID, userID spec.UserID) (*spec.SenderID, error) {
|
2023-07-06 16:15:24 +01:00
|
|
|
return r.RSAPI.QuerySenderIDForUser(ctx, roomID, userID)
|
|
|
|
},
|
|
|
|
SenderIDCreator: func(ctx context.Context, userID spec.UserID, roomID spec.RoomID, roomVersion string) (spec.SenderID, ed25519.PrivateKey, error) {
|
|
|
|
key, keyErr := r.RSAPI.GetOrCreateUserRoomPrivateKey(ctx, userID, roomID)
|
|
|
|
if keyErr != nil {
|
|
|
|
return "", nil, keyErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return spec.SenderIDFromPseudoIDKey(key), key, nil
|
|
|
|
},
|
|
|
|
EventQuerier: func(ctx context.Context, roomID spec.RoomID, eventsNeeded []gomatrixserverlib.StateKeyTuple) (gomatrixserverlib.LatestEvents, error) {
|
|
|
|
req := api.QueryLatestEventsAndStateRequest{RoomID: roomID.String(), StateToFetch: eventsNeeded}
|
|
|
|
res := api.QueryLatestEventsAndStateResponse{}
|
|
|
|
err = r.RSAPI.QueryLatestEventsAndState(ctx, &req, &res)
|
|
|
|
if err != nil {
|
|
|
|
return gomatrixserverlib.LatestEvents{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
stateEvents := []gomatrixserverlib.PDU{}
|
|
|
|
for _, event := range res.StateEvents {
|
|
|
|
stateEvents = append(stateEvents, event.PDU)
|
|
|
|
}
|
|
|
|
return gomatrixserverlib.LatestEvents{
|
|
|
|
RoomExists: res.RoomExists,
|
|
|
|
StateEvents: stateEvents,
|
|
|
|
PrevEventIDs: res.LatestEvents,
|
|
|
|
Depth: res.Depth,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
StoreSenderIDFromPublicID: func(ctx context.Context, senderID spec.SenderID, userIDRaw string, roomID spec.RoomID) error {
|
|
|
|
storeUserID, userErr := spec.NewUserID(userIDRaw, true)
|
|
|
|
if userErr != nil {
|
|
|
|
return userErr
|
|
|
|
}
|
|
|
|
return r.RSAPI.StoreUserRoomPublicKey(ctx, senderID, *storeUserID, roomID)
|
|
|
|
},
|
2023-05-31 17:33:49 +01:00
|
|
|
}
|
2023-07-06 16:15:24 +01:00
|
|
|
|
2023-05-31 17:33:49 +01:00
|
|
|
inviteEvent, err := gomatrixserverlib.PerformInvite(ctx, input, r.FSAPI)
|
2022-02-02 13:30:48 +00:00
|
|
|
if err != nil {
|
2023-05-31 17:33:49 +01:00
|
|
|
switch e := err.(type) {
|
|
|
|
case spec.MatrixError:
|
|
|
|
if e.ErrCode == spec.ErrorForbidden {
|
|
|
|
return api.ErrNotAllowed{Err: fmt.Errorf("%s", e.Err)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
2022-02-02 13:30:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send the invite event to the roomserver input stream. This will
|
|
|
|
// notify existing users in the room about the invite, update the
|
|
|
|
// membership table and ensure that the event is ready and available
|
|
|
|
// to use as an auth event when accepting the invite.
|
|
|
|
// It will NOT notify the invitee of this invite.
|
|
|
|
inputReq := &api.InputRoomEventsRequest{
|
|
|
|
InputRoomEvents: []api.InputRoomEvent{
|
|
|
|
{
|
|
|
|
Kind: api.KindNew,
|
2023-05-31 17:33:49 +01:00
|
|
|
Event: &types.HeaderedEvent{PDU: inviteEvent},
|
2023-07-06 16:15:24 +01:00
|
|
|
Origin: req.InviteInput.Inviter.Domain(),
|
2022-02-02 13:30:48 +00:00
|
|
|
SendAsServer: req.SendAsServer,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
inputRes := &api.InputRoomEventsResponse{}
|
2023-05-09 23:46:49 +01:00
|
|
|
r.Inputer.InputRoomEvents(context.Background(), inputReq, inputRes)
|
2023-05-31 17:33:49 +01:00
|
|
|
if err := inputRes.Err(); err != nil {
|
2023-07-06 16:15:24 +01:00
|
|
|
util.GetLogger(ctx).WithField("event_id", inviteEvent.EventID()).Error("r.InputRoomEvents failed")
|
2023-05-31 17:33:49 +01:00
|
|
|
return api.ErrNotAllowed{Err: err}
|
2022-02-02 13:30:48 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 17:33:49 +01:00
|
|
|
return nil
|
2020-06-24 15:06:14 +01:00
|
|
|
}
|