diff --git a/src/github.com/matrix-org/dendrite/federationapi/routing/join.go b/src/github.com/matrix-org/dendrite/federationapi/routing/join.go index d06b243b9..99e8d82de 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/routing/join.go +++ b/src/github.com/matrix-org/dendrite/federationapi/routing/join.go @@ -16,6 +16,7 @@ package routing import ( "context" + "encoding/json" "net/http" "time" @@ -89,5 +90,62 @@ func SendJoin( keys gomatrixserverlib.KeyRing, roomID, eventID string, ) util.JSONResponse { + var event gomatrixserverlib.Event + if err := json.Unmarshal(request.Content(), &event); err != nil { + return util.JSONResponse{ + Code: 400, + 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{ + Code: 400, + JSON: jsonerror.BadJSON("The room ID in the request path must match the room ID in the join event JSON"), + } + } + + // Check that the event ID is correct. + if event.EventID() != eventID { + return util.JSONResponse{ + Code: 400, + JSON: jsonerror.BadJSON("The event ID in the request path must match the event ID in the join event JSON"), + } + } + + // Check that the event is from the server sending the request. + if event.Origin() != request.Origin() { + return util.JSONResponse{ + Code: 403, + JSON: jsonerror.Forbidden("The join must be sent by the server it originated on"), + } + } + + // Check that the event is signed by the server sending the request. + verifyRequests := []gomatrixserverlib.VerifyJSONRequest{{ + ServerName: event.Origin(), + Message: event.Redact().JSON(), + AtTS: event.OriginServerTS(), + }} + verifyResults, err := keys.VerifyJSONs(httpReq.Context(), verifyRequests) + if err != nil { + return httputil.LogThenError(httpReq, err) + } + if verifyResults[0].Error != nil { + return util.JSONResponse{ + Code: 403, + JSON: jsonerror.Forbidden("The join must be signed by the server it originated on"), + } + } + + // TODO: Fetch state and auth events + + return util.JSONResponse{ + Code: 200, + JSON: map[string]interface{}{ + "state": []gomatrixserverlib.Event{}, + "auth_chain": []gomatrixserverlib.Event{}, + }, + } } diff --git a/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go b/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go index 5d921b3c5..4f2ff4e52 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go @@ -136,6 +136,18 @@ func Setup( }, )).Methods("GET") + v1fedmux.Handle("/send_join/{roomID}/{userID}", common.MakeFedAPI( + "federation_send_join", cfg.Matrix.ServerName, keys, + func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { + vars := mux.Vars(httpReq) + roomID := vars["roomID"] + userID := vars["userID"] + return MakeJoin( + httpReq.Context(), httpReq, request, cfg, query, time.Now(), keys, roomID, userID, + ) + }, + )).Methods("PUT") + v1fedmux.Handle("/version", common.MakeExternalAPI( "federation_version", func(httpReq *http.Request) util.JSONResponse {