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-08-23 16:13:47 +02:00
|
|
|
|
|
|
|
import (
|
2020-06-25 18:07:40 +02:00
|
|
|
"context"
|
2020-04-03 15:29:06 +02:00
|
|
|
"encoding/json"
|
2020-04-24 17:30:25 +02:00
|
|
|
"fmt"
|
2017-08-23 16:13:47 +02:00
|
|
|
"net/http"
|
|
|
|
|
2020-06-10 13:17:54 +02: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"
|
2017-08-23 16:13:47 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-06 10:55:01 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2023-05-10 00:46:49 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2017-09-04 14:14:01 +02:00
|
|
|
"github.com/matrix-org/util"
|
2017-08-23 16:13:47 +02:00
|
|
|
)
|
|
|
|
|
2020-06-25 18:07:40 +02:00
|
|
|
// InviteV2 implements /_matrix/federation/v2/invite/{roomID}/{eventID}
|
|
|
|
func InviteV2(
|
2017-09-04 14:14:01 +02:00
|
|
|
httpReq *http.Request,
|
2023-04-19 16:50:33 +02:00
|
|
|
request *fclient.FederationRequest,
|
2023-05-31 18:33:49 +02:00
|
|
|
roomID spec.RoomID,
|
2017-08-23 16:13:47 +02:00
|
|
|
eventID string,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.FederationAPI,
|
2022-05-05 20:30:38 +02:00
|
|
|
rsAPI api.FederationRoomserverAPI,
|
2020-06-15 17:57:59 +02:00
|
|
|
keys gomatrixserverlib.JSONVerifier,
|
2017-08-23 16:13:47 +02:00
|
|
|
) util.JSONResponse {
|
2023-04-19 16:50:33 +02:00
|
|
|
inviteReq := fclient.InviteV2Request{}
|
2020-09-24 18:16:59 +02:00
|
|
|
err := json.Unmarshal(request.Content(), &inviteReq)
|
2021-07-26 11:41:58 +02:00
|
|
|
switch e := err.(type) {
|
|
|
|
case gomatrixserverlib.UnsupportedRoomVersionError:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.UnsupportedRoomVersion(
|
2021-07-26 11:41:58 +02:00
|
|
|
fmt.Sprintf("Room version %q is not supported by this server.", e.Version),
|
|
|
|
),
|
|
|
|
}
|
2020-09-24 18:16:59 +02:00
|
|
|
case gomatrixserverlib.BadJSONError:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.BadJSON(err.Error()),
|
2020-09-24 18:16:59 +02:00
|
|
|
}
|
|
|
|
case nil:
|
2023-05-31 18:33:49 +02:00
|
|
|
if inviteReq.Event().StateKey() == nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.BadJSON("The invite event has no state key"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
invitedUser, userErr := spec.NewUserID(*inviteReq.Event().StateKey(), true)
|
|
|
|
if userErr != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.InvalidParam("The user ID is invalid"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !cfg.Matrix.IsLocalServerName(invitedUser.Domain()) {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.InvalidParam("The invited user domain does not belong to this server"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if inviteReq.Event().EventID() != eventID {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.BadJSON("The event ID in the request path must match the event ID in the invite event JSON"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
input := gomatrixserverlib.HandleInviteInput{
|
|
|
|
RoomVersion: inviteReq.RoomVersion(),
|
|
|
|
RoomID: roomID,
|
|
|
|
InvitedUser: *invitedUser,
|
|
|
|
KeyID: cfg.Matrix.KeyID,
|
|
|
|
PrivateKey: cfg.Matrix.PrivateKey,
|
|
|
|
Verifier: keys,
|
|
|
|
RoomQuerier: rsAPI,
|
|
|
|
MembershipQuerier: &api.MembershipQuerier{Roomserver: rsAPI},
|
|
|
|
StateQuerier: rsAPI.StateQuerier(),
|
|
|
|
InviteEvent: inviteReq.Event(),
|
|
|
|
StrippedState: inviteReq.InviteRoomState(),
|
2023-06-07 19:14:35 +02:00
|
|
|
UserIDQuerier: func(roomID string, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-06 22:55:18 +02:00
|
|
|
return rsAPI.QueryUserIDForSender(httpReq.Context(), roomID, senderID)
|
|
|
|
},
|
2023-05-31 18:33:49 +02:00
|
|
|
}
|
|
|
|
event, jsonErr := handleInvite(httpReq.Context(), input, rsAPI)
|
|
|
|
if jsonErr != nil {
|
|
|
|
return *jsonErr
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: fclient.RespInviteV2{Event: event.JSON()},
|
|
|
|
}
|
2020-09-24 18:16:59 +02:00
|
|
|
default:
|
2020-03-27 17:28:22 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.NotJSON("The request body could not be decoded into an invite request. " + err.Error()),
|
2017-08-23 16:13:47 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-25 18:07:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// InviteV1 implements /_matrix/federation/v1/invite/{roomID}/{eventID}
|
|
|
|
func InviteV1(
|
|
|
|
httpReq *http.Request,
|
2023-04-19 16:50:33 +02:00
|
|
|
request *fclient.FederationRequest,
|
2023-05-31 18:33:49 +02:00
|
|
|
roomID spec.RoomID,
|
2020-06-25 18:07:40 +02:00
|
|
|
eventID string,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.FederationAPI,
|
2022-05-05 20:30:38 +02:00
|
|
|
rsAPI api.FederationRoomserverAPI,
|
2020-06-25 18:07:40 +02:00
|
|
|
keys gomatrixserverlib.JSONVerifier,
|
|
|
|
) util.JSONResponse {
|
|
|
|
roomVer := gomatrixserverlib.RoomVersionV1
|
|
|
|
body := request.Content()
|
2023-04-24 12:50:37 +02:00
|
|
|
// roomVer is hardcoded to v1 so we know we won't panic on Must
|
2023-04-21 18:06:29 +02:00
|
|
|
event, err := gomatrixserverlib.MustGetRoomVersion(roomVer).NewEventFromTrustedJSON(body, false)
|
2020-09-24 18:16:59 +02:00
|
|
|
switch err.(type) {
|
|
|
|
case gomatrixserverlib.BadJSONError:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.BadJSON(err.Error()),
|
2020-09-24 18:16:59 +02:00
|
|
|
}
|
|
|
|
case nil:
|
|
|
|
default:
|
2020-06-25 18:07:40 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.NotJSON("The request body could not be decoded into an invite v1 request. " + err.Error()),
|
2020-06-25 18:07:40 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-31 18:33:49 +02:00
|
|
|
var strippedState []gomatrixserverlib.InviteStrippedState
|
|
|
|
if jsonErr := json.Unmarshal(event.Unsigned(), &strippedState); jsonErr != nil {
|
2020-06-25 18:07:40 +02:00
|
|
|
// just warn, they may not have added any.
|
|
|
|
util.GetLogger(httpReq.Context()).Warnf("failed to extract stripped state from invite event")
|
|
|
|
}
|
2017-08-23 16:13:47 +02:00
|
|
|
|
2022-11-15 16:05:23 +01:00
|
|
|
if event.StateKey() == nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.BadJSON("The invite event has no state key"),
|
2022-11-15 16:05:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 18:33:49 +02:00
|
|
|
invitedUser, err := spec.NewUserID(*event.StateKey(), true)
|
2022-11-15 16:05:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-31 18:33:49 +02:00
|
|
|
JSON: spec.InvalidParam("The user ID is invalid"),
|
2022-11-15 16:05:23 +01:00
|
|
|
}
|
|
|
|
}
|
2023-05-31 18:33:49 +02:00
|
|
|
if !cfg.Matrix.IsLocalServerName(invitedUser.Domain()) {
|
2022-07-11 15:31:31 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-31 18:33:49 +02:00
|
|
|
JSON: spec.InvalidParam("The invited user domain does not belong to this server"),
|
2022-07-11 15:31:31 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-31 18:33:49 +02:00
|
|
|
|
|
|
|
if event.EventID() != eventID {
|
2022-09-26 18:35:35 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-31 18:33:49 +02:00
|
|
|
JSON: spec.BadJSON("The event ID in the request path must match the event ID in the invite event JSON"),
|
2022-09-26 18:35:35 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-31 18:33:49 +02:00
|
|
|
|
|
|
|
input := gomatrixserverlib.HandleInviteInput{
|
|
|
|
RoomVersion: roomVer,
|
|
|
|
RoomID: roomID,
|
|
|
|
InvitedUser: *invitedUser,
|
|
|
|
KeyID: cfg.Matrix.KeyID,
|
|
|
|
PrivateKey: cfg.Matrix.PrivateKey,
|
|
|
|
Verifier: keys,
|
|
|
|
RoomQuerier: rsAPI,
|
|
|
|
MembershipQuerier: &api.MembershipQuerier{Roomserver: rsAPI},
|
|
|
|
StateQuerier: rsAPI.StateQuerier(),
|
|
|
|
InviteEvent: event,
|
|
|
|
StrippedState: strippedState,
|
2023-06-07 19:14:35 +02:00
|
|
|
UserIDQuerier: func(roomID string, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-06 22:55:18 +02:00
|
|
|
return rsAPI.QueryUserIDForSender(httpReq.Context(), roomID, senderID)
|
|
|
|
},
|
2017-08-23 16:13:47 +02:00
|
|
|
}
|
2023-05-31 18:33:49 +02:00
|
|
|
event, jsonErr := handleInvite(httpReq.Context(), input, rsAPI)
|
|
|
|
if jsonErr != nil {
|
|
|
|
return *jsonErr
|
2017-08-23 16:13:47 +02:00
|
|
|
}
|
2023-05-31 18:33:49 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: fclient.RespInvite{Event: event.JSON()},
|
2022-05-05 14:17:38 +02:00
|
|
|
}
|
2023-05-31 18:33:49 +02:00
|
|
|
}
|
2023-04-28 17:46:01 +02:00
|
|
|
|
2023-05-31 18:33:49 +02:00
|
|
|
func handleInvite(ctx context.Context, input gomatrixserverlib.HandleInviteInput, rsAPI api.FederationRoomserverAPI) (gomatrixserverlib.PDU, *util.JSONResponse) {
|
|
|
|
inviteEvent, err := gomatrixserverlib.HandleInvite(ctx, input)
|
|
|
|
switch e := err.(type) {
|
|
|
|
case nil:
|
|
|
|
case spec.InternalServerError:
|
|
|
|
util.GetLogger(ctx).WithError(err)
|
|
|
|
return nil, &util.JSONResponse{
|
2020-08-17 12:40:49 +02:00
|
|
|
Code: http.StatusInternalServerError,
|
2023-05-17 02:33:27 +02:00
|
|
|
JSON: spec.InternalServerError{},
|
2020-08-17 12:40:49 +02:00
|
|
|
}
|
2023-05-31 18:33:49 +02:00
|
|
|
case spec.MatrixError:
|
|
|
|
util.GetLogger(ctx).WithError(err)
|
|
|
|
code := http.StatusInternalServerError
|
|
|
|
switch e.ErrCode {
|
|
|
|
case spec.ErrorForbidden:
|
|
|
|
code = http.StatusForbidden
|
|
|
|
case spec.ErrorUnsupportedRoomVersion:
|
|
|
|
fallthrough // http.StatusBadRequest
|
|
|
|
case spec.ErrorBadJSON:
|
|
|
|
code = http.StatusBadRequest
|
2023-04-28 17:46:01 +02:00
|
|
|
}
|
2023-05-31 18:33:49 +02:00
|
|
|
|
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: code,
|
|
|
|
JSON: e,
|
2023-04-28 17:46:01 +02:00
|
|
|
}
|
|
|
|
default:
|
2023-05-31 18:33:49 +02:00
|
|
|
util.GetLogger(ctx).WithError(err)
|
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.Unknown("unknown error"),
|
2023-04-28 17:46:01 +02:00
|
|
|
}
|
2022-05-05 14:17:38 +02:00
|
|
|
}
|
2023-04-28 17:46:01 +02:00
|
|
|
|
2023-05-31 18:33:49 +02:00
|
|
|
headeredInvite := &types.HeaderedEvent{PDU: inviteEvent}
|
|
|
|
if err = rsAPI.HandleInvite(ctx, headeredInvite); err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("HandleInvite failed")
|
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
2022-05-05 14:17:38 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-31 18:33:49 +02:00
|
|
|
return inviteEvent, nil
|
2017-08-23 16:13:47 +02:00
|
|
|
}
|