diff --git a/federationapi/routing/invite.go b/federationapi/routing/invite.go index b1d84f25..4a49463a 100644 --- a/federationapi/routing/invite.go +++ b/federationapi/routing/invite.go @@ -15,6 +15,7 @@ package routing import ( + "context" "encoding/json" "fmt" "net/http" @@ -27,8 +28,8 @@ import ( "github.com/matrix-org/util" ) -// Invite implements /_matrix/federation/v2/invite/{roomID}/{eventID} -func Invite( +// InviteV2 implements /_matrix/federation/v2/invite/{roomID}/{eventID} +func InviteV2( httpReq *http.Request, request *gomatrixserverlib.FederationRequest, roomID string, @@ -44,14 +45,58 @@ func Invite( JSON: jsonerror.NotJSON("The request body could not be decoded into an invite request. " + err.Error()), } } - event := inviteReq.Event() + return processInvite( + httpReq.Context(), inviteReq.Event(), inviteReq.RoomVersion(), inviteReq.InviteRoomState(), roomID, eventID, cfg, rsAPI, keys, + ) +} + +// InviteV1 implements /_matrix/federation/v1/invite/{roomID}/{eventID} +func InviteV1( + httpReq *http.Request, + request *gomatrixserverlib.FederationRequest, + roomID string, + eventID string, + cfg *config.Dendrite, + rsAPI api.RoomserverInternalAPI, + keys gomatrixserverlib.JSONVerifier, +) util.JSONResponse { + roomVer := gomatrixserverlib.RoomVersionV1 + body := request.Content() + event, err := gomatrixserverlib.NewEventFromTrustedJSON(body, false, roomVer) + if err != nil { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.NotJSON("The request body could not be decoded into an invite v1 request: " + err.Error()), + } + } + var strippedState []gomatrixserverlib.InviteV2StrippedState + if err := json.Unmarshal(event.Unsigned(), &strippedState); err != nil { + // just warn, they may not have added any. + util.GetLogger(httpReq.Context()).Warnf("failed to extract stripped state from invite event") + } + return processInvite( + httpReq.Context(), event, roomVer, strippedState, roomID, eventID, cfg, rsAPI, keys, + ) +} + +func processInvite( + ctx context.Context, + event gomatrixserverlib.Event, + roomVer gomatrixserverlib.RoomVersion, + strippedState []gomatrixserverlib.InviteV2StrippedState, + roomID string, + eventID string, + cfg *config.Dendrite, + rsAPI api.RoomserverInternalAPI, + keys gomatrixserverlib.JSONVerifier, +) util.JSONResponse { // Check that we can accept invites for this room version. - if _, err := roomserverVersion.SupportedRoomVersion(inviteReq.RoomVersion()); err != nil { + if _, err := roomserverVersion.SupportedRoomVersion(roomVer); err != nil { return util.JSONResponse{ Code: http.StatusBadRequest, JSON: jsonerror.UnsupportedRoomVersion( - fmt.Sprintf("Room version %q is not supported by this server.", inviteReq.RoomVersion()), + fmt.Sprintf("Room version %q is not supported by this server.", roomVer), ), } } @@ -80,9 +125,9 @@ func Invite( AtTS: event.OriginServerTS(), StrictValidityChecking: true, }} - verifyResults, err := keys.VerifyJSONs(httpReq.Context(), verifyRequests) + verifyResults, err := keys.VerifyJSONs(ctx, verifyRequests) if err != nil { - util.GetLogger(httpReq.Context()).WithError(err).Error("keys.VerifyJSONs failed") + util.GetLogger(ctx).WithError(err).Error("keys.VerifyJSONs failed") return jsonerror.InternalServerError() } if verifyResults[0].Error != nil { @@ -99,13 +144,9 @@ func Invite( // Add the invite event to the roomserver. if perr := api.SendInvite( - httpReq.Context(), rsAPI, - signedEvent.Headered(inviteReq.RoomVersion()), - inviteReq.InviteRoomState(), - event.Origin(), - nil, + ctx, rsAPI, signedEvent.Headered(roomVer), strippedState, event.Origin(), nil, ); perr != nil { - util.GetLogger(httpReq.Context()).WithError(err).Error("producer.SendInvite failed") + util.GetLogger(ctx).WithError(err).Error("producer.SendInvite failed") return perr.JSONResponse() } diff --git a/federationapi/routing/routing.go b/federationapi/routing/routing.go index 645f397d..0afea7d0 100644 --- a/federationapi/routing/routing.go +++ b/federationapi/routing/routing.go @@ -83,10 +83,26 @@ func Setup( }, )).Methods(http.MethodPut, http.MethodOptions) + v1fedmux.Handle("/invite/{roomID}/{eventID}", httputil.MakeFedAPI( + "federation_invite", cfg.Matrix.ServerName, keys, wakeup, + func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest, vars map[string]string) util.JSONResponse { + res := InviteV1( + httpReq, request, vars["roomID"], vars["eventID"], + cfg, rsAPI, keys, + ) + return util.JSONResponse{ + Code: res.Code, + JSON: []interface{}{ + res.Code, res.JSON, + }, + } + }, + )).Methods(http.MethodPut, http.MethodOptions) + v2fedmux.Handle("/invite/{roomID}/{eventID}", httputil.MakeFedAPI( "federation_invite", cfg.Matrix.ServerName, keys, wakeup, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest, vars map[string]string) util.JSONResponse { - return Invite( + return InviteV2( httpReq, request, vars["roomID"], vars["eventID"], cfg, rsAPI, keys, ) diff --git a/go.mod b/go.mod index 57cdb909..7d968b23 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/matrix-org/go-http-js-libp2p v0.0.0-20200518170932-783164aeeda4 github.com/matrix-org/go-sqlite3-js v0.0.0-20200522092705-bc8506ccbcf3 github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26 - github.com/matrix-org/gomatrixserverlib v0.0.0-20200625121044-e5d892cd30c1 + github.com/matrix-org/gomatrixserverlib v0.0.0-20200625153204-0f1026cd05d1 github.com/matrix-org/naffka v0.0.0-20200422140631-181f1ee7401f github.com/matrix-org/util v0.0.0-20190711121626-527ce5ddefc7 github.com/mattn/go-sqlite3 v2.0.2+incompatible diff --git a/go.sum b/go.sum index 973b003c..5a9547ef 100644 --- a/go.sum +++ b/go.sum @@ -373,6 +373,8 @@ github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26 h1:Hr3zjRsq2bh github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26/go.mod h1:3fxX6gUjWyI/2Bt7J1OLhpCzOfO/bB3AiX0cJtEKud0= github.com/matrix-org/gomatrixserverlib v0.0.0-20200625121044-e5d892cd30c1 h1:3yS6hw01X72jpJuAPGVOY+QFD9cpAETR/6Hq2WYKbpU= github.com/matrix-org/gomatrixserverlib v0.0.0-20200625121044-e5d892cd30c1/go.mod h1:JsAzE1Ll3+gDWS9JSUHPJiiyAksvOOnGWF2nXdg4ZzU= +github.com/matrix-org/gomatrixserverlib v0.0.0-20200625153204-0f1026cd05d1 h1:QDOdGCfrzuVLEess3id2a2B29oVZ9JXgJmUfwE7r/iI= +github.com/matrix-org/gomatrixserverlib v0.0.0-20200625153204-0f1026cd05d1/go.mod h1:JsAzE1Ll3+gDWS9JSUHPJiiyAksvOOnGWF2nXdg4ZzU= github.com/matrix-org/naffka v0.0.0-20200422140631-181f1ee7401f h1:pRz4VTiRCO4zPlEMc3ESdUOcW4PXHH4Kj+YDz1XyE+Y= github.com/matrix-org/naffka v0.0.0-20200422140631-181f1ee7401f/go.mod h1:y0oDTjZDv5SM9a2rp3bl+CU+bvTRINQsdb7YlDql5Go= github.com/matrix-org/util v0.0.0-20190711121626-527ce5ddefc7 h1:ntrLa/8xVzeSs8vHFHK25k0C+NV74sYMJnNSg5NoSRo= diff --git a/sytest-whitelist b/sytest-whitelist index 4bc7baef..1628a635 100644 --- a/sytest-whitelist +++ b/sytest-whitelist @@ -243,6 +243,9 @@ User can invite local user to room with version 2 Remote user can backfill in a room with version 2 Inbound federation accepts attempts to join v2 rooms from servers with support Outbound federation can send invites via v2 API +Outbound federation can send invites via v1 API +Inbound federation can receive invites via v1 API +Inbound federation can receive invites via v2 API User can create and send/receive messages in a room with version 3 local user can join room with version 3 Remote user can backfill in a room with version 3