2017-09-08 09:17:12 -05: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 12:16:53 -05:00
|
|
|
package routing
|
2017-09-08 09:17:12 -05:00
|
|
|
|
|
|
|
import (
|
2017-09-13 05:03:41 -05:00
|
|
|
"context"
|
2017-09-08 09:17:12 -05:00
|
|
|
"encoding/json"
|
2017-09-11 13:08:24 -05:00
|
|
|
"errors"
|
2017-09-08 09:17:12 -05:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2018-08-06 08:09:25 -05:00
|
|
|
"time"
|
2017-09-08 09:17:12 -05:00
|
|
|
|
2018-08-20 04:45:17 -05:00
|
|
|
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
|
2017-09-13 07:31:46 -05:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
2017-09-08 09:17:12 -05:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
2017-09-12 11:15:13 -05:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
2017-09-08 09:17:12 -05:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/producers"
|
|
|
|
"github.com/matrix-org/dendrite/common"
|
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2018-08-20 04:45:17 -05:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2017-09-08 09:17:12 -05:00
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
2017-09-11 13:08:24 -05:00
|
|
|
|
2017-10-13 08:42:57 -05:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-09-08 09:17:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type invite struct {
|
|
|
|
MXID string `json:"mxid"`
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
Sender string `json:"sender"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
Signed common.TPInviteSigned `json:"signed"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type invites struct {
|
|
|
|
Medium string `json:"medium"`
|
|
|
|
Address string `json:"address"`
|
|
|
|
MXID string `json:"mxid"`
|
|
|
|
Invites []invite `json:"invites"`
|
|
|
|
}
|
|
|
|
|
2017-09-13 07:31:46 -05:00
|
|
|
var (
|
|
|
|
errNotLocalUser = errors.New("the user is not from this server")
|
|
|
|
errNotInRoom = errors.New("the server isn't currently in the room")
|
|
|
|
)
|
2017-09-12 11:15:13 -05:00
|
|
|
|
2017-09-08 09:17:12 -05:00
|
|
|
// CreateInvitesFrom3PIDInvites implements POST /_matrix/federation/v1/3pid/onbind
|
|
|
|
func CreateInvitesFrom3PIDInvites(
|
2018-08-20 04:45:17 -05:00
|
|
|
req *http.Request, queryAPI roomserverAPI.RoomserverQueryAPI,
|
|
|
|
asAPI appserviceAPI.AppServiceQueryAPI, cfg config.Dendrite,
|
2017-09-11 13:08:24 -05:00
|
|
|
producer *producers.RoomserverProducer, federation *gomatrixserverlib.FederationClient,
|
2017-09-13 07:31:46 -05:00
|
|
|
accountDB *accounts.Database,
|
2017-09-08 09:17:12 -05:00
|
|
|
) util.JSONResponse {
|
|
|
|
var body invites
|
|
|
|
if reqErr := httputil.UnmarshalJSONRequest(req, &body); reqErr != nil {
|
|
|
|
return *reqErr
|
|
|
|
}
|
|
|
|
|
|
|
|
evs := []gomatrixserverlib.Event{}
|
|
|
|
for _, inv := range body.Invites {
|
2017-09-13 05:03:41 -05:00
|
|
|
event, err := createInviteFrom3PIDInvite(
|
2018-08-20 04:45:17 -05:00
|
|
|
req.Context(), queryAPI, asAPI, cfg, inv, federation, accountDB,
|
2017-09-13 05:03:41 -05:00
|
|
|
)
|
2017-09-08 09:17:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return httputil.LogThenError(req, err)
|
|
|
|
}
|
|
|
|
if event != nil {
|
|
|
|
evs = append(evs, *event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send all the events
|
2018-05-26 06:03:35 -05:00
|
|
|
if _, err := producer.SendEvents(req.Context(), evs, cfg.Matrix.ServerName, nil); err != nil {
|
2017-09-08 09:17:12 -05:00
|
|
|
return httputil.LogThenError(req, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusOK,
|
2017-09-08 09:17:12 -05:00
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 11:15:13 -05:00
|
|
|
// ExchangeThirdPartyInvite implements PUT /_matrix/federation/v1/exchange_third_party_invite/{roomID}
|
|
|
|
func ExchangeThirdPartyInvite(
|
|
|
|
httpReq *http.Request,
|
|
|
|
request *gomatrixserverlib.FederationRequest,
|
|
|
|
roomID string,
|
2018-08-20 04:45:17 -05:00
|
|
|
queryAPI roomserverAPI.RoomserverQueryAPI,
|
2017-09-12 11:15:13 -05:00
|
|
|
cfg config.Dendrite,
|
|
|
|
federation *gomatrixserverlib.FederationClient,
|
|
|
|
producer *producers.RoomserverProducer,
|
|
|
|
) util.JSONResponse {
|
|
|
|
var builder gomatrixserverlib.EventBuilder
|
|
|
|
if err := json.Unmarshal(request.Content(), &builder); err != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusBadRequest,
|
2017-09-12 11:15:13 -05:00
|
|
|
JSON: jsonerror.NotJSON("The request body could not be decoded into valid JSON. " + err.Error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the room ID is correct.
|
|
|
|
if builder.RoomID != roomID {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusBadRequest,
|
2017-09-12 11:15:13 -05:00
|
|
|
JSON: jsonerror.BadJSON("The room ID in the request path must match the room ID in the invite event JSON"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the state key is correct.
|
|
|
|
_, targetDomain, err := gomatrixserverlib.SplitID('@', *builder.StateKey)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusBadRequest,
|
2017-09-12 11:15:13 -05:00
|
|
|
JSON: jsonerror.BadJSON("The event's state key isn't a Matrix user ID"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the target user is from the requesting homeserver.
|
|
|
|
if targetDomain != request.Origin() {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusBadRequest,
|
2017-09-12 11:15:13 -05:00
|
|
|
JSON: jsonerror.BadJSON("The event's state key doesn't have the same domain as the request's origin"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auth and build the event from what the remote server sent us
|
2018-08-06 08:09:25 -05:00
|
|
|
event, err := buildMembershipEvent(httpReq.Context(), &builder, queryAPI, cfg)
|
2017-09-12 11:15:13 -05:00
|
|
|
if err == errNotInRoom {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusNotFound,
|
2017-09-12 11:15:13 -05:00
|
|
|
JSON: jsonerror.NotFound("Unknown room " + roomID),
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return httputil.LogThenError(httpReq, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the requesting server to sign the newly created event so we know it
|
|
|
|
// acknowledged it
|
2017-09-13 05:03:41 -05:00
|
|
|
signedEvent, err := federation.SendInvite(httpReq.Context(), request.Origin(), *event)
|
2017-09-12 11:15:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return httputil.LogThenError(httpReq, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the event to the roomserver
|
2018-05-26 06:03:35 -05:00
|
|
|
if _, err = producer.SendEvents(
|
2017-12-04 12:07:52 -06:00
|
|
|
httpReq.Context(), []gomatrixserverlib.Event{signedEvent.Event}, cfg.Matrix.ServerName, nil,
|
2017-09-13 07:37:50 -05:00
|
|
|
); err != nil {
|
2017-09-12 11:15:13 -05:00
|
|
|
return httputil.LogThenError(httpReq, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusOK,
|
2017-09-12 11:15:13 -05:00
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 09:17:12 -05:00
|
|
|
// createInviteFrom3PIDInvite processes an invite provided by the identity server
|
|
|
|
// and creates a m.room.member event (with "invite" membership) from it.
|
|
|
|
// Returns an error if there was a problem building the event or fetching the
|
|
|
|
// necessary data to do so.
|
|
|
|
func createInviteFrom3PIDInvite(
|
2018-08-20 04:45:17 -05:00
|
|
|
ctx context.Context, queryAPI roomserverAPI.RoomserverQueryAPI,
|
|
|
|
asAPI appserviceAPI.AppServiceQueryAPI, cfg config.Dendrite,
|
2017-09-13 05:03:41 -05:00
|
|
|
inv invite, federation *gomatrixserverlib.FederationClient,
|
2017-09-13 07:31:46 -05:00
|
|
|
accountDB *accounts.Database,
|
2017-09-08 09:17:12 -05:00
|
|
|
) (*gomatrixserverlib.Event, error) {
|
2018-08-20 04:45:17 -05:00
|
|
|
_, server, err := gomatrixserverlib.SplitID('@', inv.MXID)
|
2017-09-13 07:31:46 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if server != cfg.Matrix.ServerName {
|
|
|
|
return nil, errNotLocalUser
|
|
|
|
}
|
|
|
|
|
2017-09-08 09:17:12 -05:00
|
|
|
// Build the event
|
|
|
|
builder := &gomatrixserverlib.EventBuilder{
|
|
|
|
Type: "m.room.member",
|
|
|
|
Sender: inv.Sender,
|
|
|
|
RoomID: inv.RoomID,
|
|
|
|
StateKey: &inv.MXID,
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:43:01 -05:00
|
|
|
profile, err := appserviceAPI.RetrieveUserProfile(ctx, inv.MXID, asAPI, accountDB)
|
2017-09-13 07:31:46 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-08 09:17:12 -05:00
|
|
|
content := common.MemberContent{
|
2017-09-13 07:31:46 -05:00
|
|
|
AvatarURL: profile.AvatarURL,
|
|
|
|
DisplayName: profile.DisplayName,
|
|
|
|
Membership: "invite",
|
2017-09-08 09:17:12 -05:00
|
|
|
ThirdPartyInvite: &common.TPInvite{
|
|
|
|
Signed: inv.Signed,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-09-13 07:31:46 -05:00
|
|
|
if err = builder.SetContent(content); err != nil {
|
2017-09-08 09:17:12 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-08-06 08:09:25 -05:00
|
|
|
event, err := buildMembershipEvent(ctx, builder, queryAPI, cfg)
|
2017-09-12 11:15:13 -05:00
|
|
|
if err == errNotInRoom {
|
2018-08-06 08:09:25 -05:00
|
|
|
return nil, sendToRemoteServer(ctx, inv, federation, cfg, *builder)
|
2017-09-13 05:03:41 -05:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-09-12 11:15:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return event, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildMembershipEvent uses a builder for a m.room.member invite event derived
|
|
|
|
// from a third-party invite to auth and build the said event. Returns the said
|
|
|
|
// event.
|
|
|
|
// Returns errNotInRoom if the server is not in the room the invite is for.
|
|
|
|
// Returns an error if something failed during the process.
|
|
|
|
func buildMembershipEvent(
|
2018-08-06 08:09:25 -05:00
|
|
|
ctx context.Context,
|
2018-08-20 04:45:17 -05:00
|
|
|
builder *gomatrixserverlib.EventBuilder, queryAPI roomserverAPI.RoomserverQueryAPI,
|
2017-09-12 11:15:13 -05:00
|
|
|
cfg config.Dendrite,
|
|
|
|
) (*gomatrixserverlib.Event, error) {
|
2017-09-08 09:17:12 -05:00
|
|
|
eventsNeeded, err := gomatrixserverlib.StateNeededForEventBuilder(builder)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the roomserver for information about this room
|
2018-08-20 04:45:17 -05:00
|
|
|
queryReq := roomserverAPI.QueryLatestEventsAndStateRequest{
|
2017-09-08 09:17:12 -05:00
|
|
|
RoomID: builder.RoomID,
|
|
|
|
StateToFetch: eventsNeeded.Tuples(),
|
|
|
|
}
|
2018-08-20 04:45:17 -05:00
|
|
|
var queryRes roomserverAPI.QueryLatestEventsAndStateResponse
|
2018-08-06 08:09:25 -05:00
|
|
|
if err = queryAPI.QueryLatestEventsAndState(ctx, &queryReq, &queryRes); err != nil {
|
2017-09-08 09:17:12 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !queryRes.RoomExists {
|
2017-09-11 13:08:24 -05:00
|
|
|
// Use federation to auth the event
|
2017-09-12 11:15:13 -05:00
|
|
|
return nil, errNotInRoom
|
2017-09-08 09:17:12 -05:00
|
|
|
}
|
|
|
|
|
2017-09-11 13:08:24 -05:00
|
|
|
// Auth the event locally
|
2017-09-08 09:17:12 -05:00
|
|
|
builder.Depth = queryRes.Depth
|
|
|
|
builder.PrevEvents = queryRes.LatestEvents
|
|
|
|
|
|
|
|
authEvents := gomatrixserverlib.NewAuthEvents(nil)
|
|
|
|
|
|
|
|
for i := range queryRes.StateEvents {
|
2017-09-20 04:59:19 -05:00
|
|
|
err = authEvents.AddEvent(&queryRes.StateEvents[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-08 09:17:12 -05:00
|
|
|
}
|
|
|
|
|
2017-09-12 11:15:13 -05:00
|
|
|
if err = fillDisplayName(builder, authEvents); err != nil {
|
2017-09-08 09:17:12 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
refs, err := eventsNeeded.AuthEventReferences(&authEvents)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
builder.AuthEvents = refs
|
|
|
|
|
|
|
|
eventID := fmt.Sprintf("$%s:%s", util.RandomString(16), cfg.Matrix.ServerName)
|
2018-08-06 08:09:25 -05:00
|
|
|
event, err := builder.Build(
|
|
|
|
eventID, time.Now(), cfg.Matrix.ServerName, cfg.Matrix.KeyID, cfg.Matrix.PrivateKey,
|
|
|
|
)
|
2017-09-08 09:17:12 -05:00
|
|
|
|
2017-09-12 11:15:13 -05:00
|
|
|
return &event, err
|
2017-09-08 09:17:12 -05:00
|
|
|
}
|
|
|
|
|
2017-09-11 13:08:24 -05:00
|
|
|
// sendToRemoteServer uses federation to send an invite provided by an identity
|
|
|
|
// server to a remote server in case the current server isn't in the room the
|
|
|
|
// invite is for.
|
|
|
|
// Returns an error if it couldn't get the server names to reach or if all of
|
|
|
|
// them responded with an error.
|
|
|
|
func sendToRemoteServer(
|
2017-09-13 05:03:41 -05:00
|
|
|
ctx context.Context, inv invite,
|
2017-11-15 04:25:48 -06:00
|
|
|
federation *gomatrixserverlib.FederationClient, _ config.Dendrite,
|
2017-09-11 13:08:24 -05:00
|
|
|
builder gomatrixserverlib.EventBuilder,
|
|
|
|
) (err error) {
|
|
|
|
remoteServers := make([]gomatrixserverlib.ServerName, 2)
|
|
|
|
_, remoteServers[0], err = gomatrixserverlib.SplitID('@', inv.Sender)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Fallback to the room's server if the sender's domain is the same as
|
|
|
|
// the current server's
|
|
|
|
_, remoteServers[1], err = gomatrixserverlib.SplitID('!', inv.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, server := range remoteServers {
|
2017-09-13 05:03:41 -05:00
|
|
|
err = federation.ExchangeThirdPartyInvite(ctx, server, builder)
|
2017-09-11 13:08:24 -05:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2018-09-01 03:31:47 -05:00
|
|
|
logrus.WithError(err).Warnf("failed to send 3PID invite via %s", server)
|
2017-09-11 13:08:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("failed to send 3PID invite via any server")
|
|
|
|
}
|
|
|
|
|
2017-09-08 09:17:12 -05:00
|
|
|
// fillDisplayName looks in a list of auth events for a m.room.third_party_invite
|
|
|
|
// event with the state key matching a given m.room.member event's content's token.
|
|
|
|
// If such an event is found, fills the "display_name" attribute of the
|
|
|
|
// "third_party_invite" structure in the m.room.member event with the display_name
|
|
|
|
// from the m.room.third_party_invite event.
|
|
|
|
// Returns an error if there was a problem parsing the m.room.third_party_invite
|
|
|
|
// event's content or updating the m.room.member event's content.
|
|
|
|
// Returns nil if no m.room.third_party_invite with a matching token could be
|
|
|
|
// found. Returning an error isn't necessary in this case as the event will be
|
|
|
|
// rejected by gomatrixserverlib.
|
|
|
|
func fillDisplayName(
|
2017-09-12 11:15:13 -05:00
|
|
|
builder *gomatrixserverlib.EventBuilder, authEvents gomatrixserverlib.AuthEvents,
|
2017-09-08 09:17:12 -05:00
|
|
|
) error {
|
2017-09-12 11:15:13 -05:00
|
|
|
var content common.MemberContent
|
|
|
|
if err := json.Unmarshal(builder.Content, &content); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-08 09:17:12 -05:00
|
|
|
// Look for the m.room.third_party_invite event
|
|
|
|
thirdPartyInviteEvent, _ := authEvents.ThirdPartyInvite(content.ThirdPartyInvite.Signed.Token)
|
|
|
|
|
|
|
|
if thirdPartyInviteEvent == nil {
|
|
|
|
// If the third party invite event doesn't exist then we can't use it to set the display name.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var thirdPartyInviteContent common.ThirdPartyInviteContent
|
|
|
|
if err := json.Unmarshal(thirdPartyInviteEvent.Content(), &thirdPartyInviteContent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the m.room.third_party_invite event to fill the "displayname" and
|
|
|
|
// update the m.room.member event's content with it
|
|
|
|
content.ThirdPartyInvite.DisplayName = thirdPartyInviteContent.DisplayName
|
2017-09-20 08:15:38 -05:00
|
|
|
return builder.SetContent(content)
|
2017-09-08 09:17:12 -05:00
|
|
|
}
|