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-03-27 17:28:22 +01:00
|
|
|
"context"
|
2017-08-23 16:13:47 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/producers"
|
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2020-03-27 17:28:22 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2017-08-23 16:13:47 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-09-04 14:14:01 +02:00
|
|
|
"github.com/matrix-org/util"
|
2017-08-23 16:13:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Invite implements /_matrix/federation/v1/invite/{roomID}/{eventID}
|
|
|
|
func Invite(
|
2017-09-04 14:14:01 +02:00
|
|
|
httpReq *http.Request,
|
|
|
|
request *gomatrixserverlib.FederationRequest,
|
2017-08-23 16:13:47 +02:00
|
|
|
roomID string,
|
|
|
|
eventID string,
|
2020-02-11 12:18:12 +01:00
|
|
|
cfg *config.Dendrite,
|
2017-08-23 16:13:47 +02:00
|
|
|
producer *producers.RoomserverProducer,
|
|
|
|
keys gomatrixserverlib.KeyRing,
|
|
|
|
) util.JSONResponse {
|
2020-03-27 17:28:22 +01:00
|
|
|
// Look up the room version for the room.
|
|
|
|
verReq := api.QueryRoomVersionForRoomRequest{RoomID: roomID}
|
|
|
|
verRes := api.QueryRoomVersionForRoomResponse{}
|
|
|
|
if err := producer.QueryAPI.QueryRoomVersionForRoom(context.Background(), &verReq, &verRes); err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.UnsupportedRoomVersion(err.Error()),
|
|
|
|
}
|
|
|
|
}
|
2017-08-23 16:13:47 +02:00
|
|
|
|
|
|
|
// Decode the event JSON from the request.
|
2020-03-27 17:28:22 +01:00
|
|
|
event, err := gomatrixserverlib.NewEventFromUntrustedJSON(request.Content(), verRes.RoomVersion)
|
|
|
|
if err != nil {
|
2017-08-23 16:13:47 +02:00
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2017-08-23 16:13:47 +02:00
|
|
|
JSON: jsonerror.NotJSON("The request body could not be decoded into valid JSON. " + err.Error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the room ID is correct.
|
|
|
|
if event.RoomID() != roomID {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2017-08-23 16:13:47 +02:00
|
|
|
JSON: jsonerror.BadJSON("The room ID in the request path must match the room ID in the invite event JSON"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the event ID is correct.
|
|
|
|
if event.EventID() != eventID {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2017-08-23 16:13:47 +02:00
|
|
|
JSON: jsonerror.BadJSON("The event ID in the request path must match the event ID in the invite event JSON"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the event is from the server sending the request.
|
|
|
|
if event.Origin() != request.Origin() {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusForbidden,
|
2017-08-23 16:13:47 +02:00
|
|
|
JSON: jsonerror.Forbidden("The invite must be sent by the server it originated on"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the event is signed by the server sending the request.
|
2020-03-27 17:28:22 +01:00
|
|
|
redacted := event.Redact()
|
2017-08-23 16:13:47 +02:00
|
|
|
verifyRequests := []gomatrixserverlib.VerifyJSONRequest{{
|
|
|
|
ServerName: event.Origin(),
|
2020-03-27 17:28:22 +01:00
|
|
|
Message: redacted.JSON(),
|
2017-08-23 16:13:47 +02:00
|
|
|
AtTS: event.OriginServerTS(),
|
|
|
|
}}
|
2017-09-13 12:03:41 +02:00
|
|
|
verifyResults, err := keys.VerifyJSONs(httpReq.Context(), verifyRequests)
|
2017-08-23 16:13:47 +02:00
|
|
|
if err != nil {
|
2020-03-02 17:20:44 +01:00
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("keys.VerifyJSONs failed")
|
|
|
|
return jsonerror.InternalServerError()
|
2017-08-23 16:13:47 +02:00
|
|
|
}
|
|
|
|
if verifyResults[0].Error != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusForbidden,
|
2017-08-23 16:13:47 +02:00
|
|
|
JSON: jsonerror.Forbidden("The invite must be signed by the server it originated on"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign the event so that other servers will know that we have received the invite.
|
|
|
|
signedEvent := event.Sign(
|
|
|
|
string(cfg.Matrix.ServerName), cfg.Matrix.KeyID, cfg.Matrix.PrivateKey,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Add the invite event to the roomserver.
|
2017-09-13 14:37:50 +02:00
|
|
|
if err = producer.SendInvite(httpReq.Context(), signedEvent); err != nil {
|
2020-03-02 17:20:44 +01:00
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("producer.SendInvite failed")
|
|
|
|
return jsonerror.InternalServerError()
|
2017-08-23 16:13:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the signed event to the originating server, it should then tell
|
|
|
|
// the other servers in the room that we have been invited.
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusOK,
|
2017-09-11 19:07:12 +02:00
|
|
|
JSON: gomatrixserverlib.RespInvite{Event: signedEvent},
|
2017-08-23 16:13:47 +02:00
|
|
|
}
|
|
|
|
}
|