2017-05-25 17:08:28 +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-05-25 17:08:28 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-07-07 15:11:32 +02:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2018-08-22 14:40:25 +02:00
|
|
|
"time"
|
2017-07-07 15:11:32 +02:00
|
|
|
|
2017-05-25 17:08:28 +02:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
2017-07-25 17:10:59 +02:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
2017-05-25 17:08:28 +02:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/producers"
|
2017-10-25 15:44:33 +02:00
|
|
|
"github.com/matrix-org/dendrite/common"
|
2017-06-19 16:21:04 +02:00
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2018-07-17 16:36:04 +02:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2017-05-25 17:08:28 +02:00
|
|
|
"github.com/matrix-org/gomatrix"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// JoinRoomByIDOrAlias implements the "/join/{roomIDOrAlias}" API.
|
|
|
|
// https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-join-roomidoralias
|
|
|
|
func JoinRoomByIDOrAlias(
|
|
|
|
req *http.Request,
|
|
|
|
device *authtypes.Device,
|
|
|
|
roomIDOrAlias string,
|
2020-02-11 12:18:12 +01:00
|
|
|
cfg *config.Dendrite,
|
2017-05-25 17:08:28 +02:00
|
|
|
federation *gomatrixserverlib.FederationClient,
|
|
|
|
producer *producers.RoomserverProducer,
|
2018-07-17 16:36:04 +02:00
|
|
|
queryAPI roomserverAPI.RoomserverQueryAPI,
|
|
|
|
aliasAPI roomserverAPI.RoomserverAliasAPI,
|
2017-05-25 17:08:28 +02:00
|
|
|
keyRing gomatrixserverlib.KeyRing,
|
2017-07-25 17:10:59 +02:00
|
|
|
accountDB *accounts.Database,
|
2017-05-25 17:08:28 +02:00
|
|
|
) util.JSONResponse {
|
|
|
|
var content map[string]interface{} // must be a JSON object
|
|
|
|
if resErr := httputil.UnmarshalJSONRequest(req, &content); resErr != nil {
|
|
|
|
return *resErr
|
|
|
|
}
|
|
|
|
|
2018-08-22 14:40:25 +02:00
|
|
|
evTime, err := httputil.ParseTSParam(req)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.InvalidArgumentValue(err.Error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 17:10:59 +02:00
|
|
|
localpart, _, err := gomatrixserverlib.SplitID('@', device.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return httputil.LogThenError(req, err)
|
|
|
|
}
|
|
|
|
|
2017-09-18 15:15:27 +02:00
|
|
|
profile, err := accountDB.GetProfileByLocalpart(req.Context(), localpart)
|
2017-07-25 17:10:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return httputil.LogThenError(req, err)
|
|
|
|
}
|
|
|
|
|
2019-08-06 16:07:36 +02:00
|
|
|
content["membership"] = gomatrixserverlib.Join
|
2017-07-25 17:10:59 +02:00
|
|
|
content["displayname"] = profile.DisplayName
|
|
|
|
content["avatar_url"] = profile.AvatarURL
|
2017-05-25 17:08:28 +02:00
|
|
|
|
2018-08-22 14:40:25 +02:00
|
|
|
r := joinRoomReq{
|
|
|
|
req, evTime, content, device.UserID, cfg, federation, producer, queryAPI, aliasAPI, keyRing,
|
|
|
|
}
|
2017-05-25 17:08:28 +02:00
|
|
|
|
|
|
|
if strings.HasPrefix(roomIDOrAlias, "!") {
|
2017-09-21 18:00:48 +02:00
|
|
|
return r.joinRoomByID(roomIDOrAlias)
|
2017-05-25 17:08:28 +02:00
|
|
|
}
|
|
|
|
if strings.HasPrefix(roomIDOrAlias, "#") {
|
|
|
|
return r.joinRoomByAlias(roomIDOrAlias)
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2019-07-03 17:38:50 +02:00
|
|
|
JSON: jsonerror.BadJSON(
|
|
|
|
fmt.Sprintf("Invalid first character '%s' for room ID or alias",
|
|
|
|
string([]rune(roomIDOrAlias)[0])), // Wrapping with []rune makes this call UTF-8 safe
|
|
|
|
),
|
2017-05-25 17:08:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type joinRoomReq struct {
|
|
|
|
req *http.Request
|
2018-08-22 14:40:25 +02:00
|
|
|
evTime time.Time
|
2017-05-25 17:08:28 +02:00
|
|
|
content map[string]interface{}
|
|
|
|
userID string
|
2020-02-11 12:18:12 +01:00
|
|
|
cfg *config.Dendrite
|
2017-05-25 17:08:28 +02:00
|
|
|
federation *gomatrixserverlib.FederationClient
|
|
|
|
producer *producers.RoomserverProducer
|
2018-07-17 16:36:04 +02:00
|
|
|
queryAPI roomserverAPI.RoomserverQueryAPI
|
|
|
|
aliasAPI roomserverAPI.RoomserverAliasAPI
|
2017-05-25 17:08:28 +02:00
|
|
|
keyRing gomatrixserverlib.KeyRing
|
|
|
|
}
|
|
|
|
|
|
|
|
// joinRoomByID joins a room by room ID
|
2017-09-21 18:00:48 +02:00
|
|
|
func (r joinRoomReq) joinRoomByID(roomID string) util.JSONResponse {
|
2017-05-25 17:08:28 +02:00
|
|
|
// A client should only join a room by room ID when it has an invite
|
|
|
|
// to the room. If the server is already in the room then we can
|
|
|
|
// lookup the invite and process the request as a normal state event.
|
|
|
|
// If the server is not in the room the we will need to look up the
|
|
|
|
// remote server the invite came from in order to request a join event
|
|
|
|
// from that server.
|
2018-07-17 16:36:04 +02:00
|
|
|
queryReq := roomserverAPI.QueryInvitesForUserRequest{
|
2017-09-21 18:00:48 +02:00
|
|
|
RoomID: roomID, TargetUserID: r.userID,
|
|
|
|
}
|
2018-07-17 16:36:04 +02:00
|
|
|
var queryRes roomserverAPI.QueryInvitesForUserResponse
|
2017-09-21 18:00:48 +02:00
|
|
|
if err := r.queryAPI.QueryInvitesForUser(r.req.Context(), &queryReq, &queryRes); err != nil {
|
|
|
|
return httputil.LogThenError(r.req, err)
|
|
|
|
}
|
2019-01-15 19:20:46 +01:00
|
|
|
|
2017-09-21 18:00:48 +02:00
|
|
|
servers := []gomatrixserverlib.ServerName{}
|
2019-01-15 19:20:46 +01:00
|
|
|
seenInInviterIDs := map[gomatrixserverlib.ServerName]bool{}
|
2017-09-21 18:00:48 +02:00
|
|
|
for _, userID := range queryRes.InviteSenderUserIDs {
|
|
|
|
_, domain, err := gomatrixserverlib.SplitID('@', userID)
|
|
|
|
if err != nil {
|
|
|
|
return httputil.LogThenError(r.req, err)
|
|
|
|
}
|
2019-01-15 19:20:46 +01:00
|
|
|
if !seenInInviterIDs[domain] {
|
2017-09-21 18:00:48 +02:00
|
|
|
servers = append(servers, domain)
|
2019-01-15 19:20:46 +01:00
|
|
|
seenInInviterIDs[domain] = true
|
2017-09-21 18:00:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 19:20:46 +01:00
|
|
|
// Also add the domain extracted from the roomID as a last resort to join
|
|
|
|
// in case the client is erroneously trying to join by ID without an invite
|
|
|
|
// or all previous attempts at domains extracted from the inviter IDs fail
|
|
|
|
// Note: It's no guarantee we'll succeed because a room isn't bound to the domain in its ID
|
|
|
|
_, domain, err := gomatrixserverlib.SplitID('!', roomID)
|
|
|
|
if err != nil {
|
|
|
|
return httputil.LogThenError(r.req, err)
|
|
|
|
}
|
|
|
|
if domain != r.cfg.Matrix.ServerName && !seenInInviterIDs[domain] {
|
|
|
|
servers = append(servers, domain)
|
|
|
|
}
|
|
|
|
|
2017-09-21 18:00:48 +02:00
|
|
|
return r.joinRoomUsingServers(roomID, servers)
|
|
|
|
|
2017-05-25 17:08:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// joinRoomByAlias joins a room using a room alias.
|
|
|
|
func (r joinRoomReq) joinRoomByAlias(roomAlias string) util.JSONResponse {
|
2017-07-07 15:11:32 +02:00
|
|
|
_, domain, err := gomatrixserverlib.SplitID('#', roomAlias)
|
2017-05-25 17:08:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2017-05-25 17:08:28 +02:00
|
|
|
JSON: jsonerror.BadJSON("Room alias must be in the form '#localpart:domain'"),
|
|
|
|
}
|
|
|
|
}
|
2017-06-19 16:21:04 +02:00
|
|
|
if domain == r.cfg.Matrix.ServerName {
|
2018-07-17 16:36:04 +02:00
|
|
|
queryReq := roomserverAPI.GetRoomIDForAliasRequest{Alias: roomAlias}
|
|
|
|
var queryRes roomserverAPI.GetRoomIDForAliasResponse
|
2018-05-30 14:43:13 +02:00
|
|
|
if err = r.aliasAPI.GetRoomIDForAlias(r.req.Context(), &queryReq, &queryRes); err != nil {
|
2017-07-28 16:29:12 +02:00
|
|
|
return httputil.LogThenError(r.req, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(queryRes.RoomID) > 0 {
|
|
|
|
return r.joinRoomUsingServers(queryRes.RoomID, []gomatrixserverlib.ServerName{r.cfg.Matrix.ServerName})
|
|
|
|
}
|
|
|
|
// If the response doesn't contain a non-empty string, return an error
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusNotFound,
|
2017-07-28 16:29:12 +02:00
|
|
|
JSON: jsonerror.NotFound("Room alias " + roomAlias + " not found."),
|
|
|
|
}
|
2017-05-25 17:08:28 +02:00
|
|
|
}
|
2017-07-28 16:29:12 +02:00
|
|
|
// If the room isn't local, use federation to join
|
|
|
|
return r.joinRoomByRemoteAlias(domain, roomAlias)
|
2017-05-25 17:08:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r joinRoomReq) joinRoomByRemoteAlias(
|
|
|
|
domain gomatrixserverlib.ServerName, roomAlias string,
|
|
|
|
) util.JSONResponse {
|
2017-09-13 12:03:41 +02:00
|
|
|
resp, err := r.federation.LookupRoomAlias(r.req.Context(), domain, roomAlias)
|
2017-05-25 17:08:28 +02:00
|
|
|
if err != nil {
|
|
|
|
switch x := err.(type) {
|
|
|
|
case gomatrix.HTTPError:
|
2018-03-13 16:55:45 +01:00
|
|
|
if x.Code == http.StatusNotFound {
|
2017-05-25 17:08:28 +02:00
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusNotFound,
|
2017-05-25 17:08:28 +02:00
|
|
|
JSON: jsonerror.NotFound("Room alias not found"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return httputil.LogThenError(r.req, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.joinRoomUsingServers(resp.RoomID, resp.Servers)
|
|
|
|
}
|
|
|
|
|
2017-09-20 11:59:19 +02:00
|
|
|
func (r joinRoomReq) writeToBuilder(eb *gomatrixserverlib.EventBuilder, roomID string) error {
|
2017-05-25 17:08:28 +02:00
|
|
|
eb.Type = "m.room.member"
|
2017-09-20 11:59:19 +02:00
|
|
|
|
|
|
|
err := eb.SetContent(r.content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = eb.SetUnsigned(struct{}{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-25 17:08:28 +02:00
|
|
|
eb.Sender = r.userID
|
|
|
|
eb.StateKey = &r.userID
|
|
|
|
eb.RoomID = roomID
|
|
|
|
eb.Redacts = ""
|
2017-09-20 11:59:19 +02:00
|
|
|
|
|
|
|
return nil
|
2017-05-25 17:08:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r joinRoomReq) joinRoomUsingServers(
|
|
|
|
roomID string, servers []gomatrixserverlib.ServerName,
|
|
|
|
) util.JSONResponse {
|
|
|
|
var eb gomatrixserverlib.EventBuilder
|
2017-09-20 11:59:19 +02:00
|
|
|
err := r.writeToBuilder(&eb, roomID)
|
|
|
|
if err != nil {
|
|
|
|
return httputil.LogThenError(r.req, err)
|
|
|
|
}
|
2017-05-25 17:08:28 +02:00
|
|
|
|
2018-07-17 16:36:04 +02:00
|
|
|
var queryRes roomserverAPI.QueryLatestEventsAndStateResponse
|
2018-08-22 14:40:25 +02:00
|
|
|
event, err := common.BuildEvent(r.req.Context(), &eb, r.cfg, r.evTime, r.queryAPI, &queryRes)
|
2017-09-13 14:37:50 +02:00
|
|
|
if err == nil {
|
2018-05-26 13:03:35 +02:00
|
|
|
if _, err = r.producer.SendEvents(r.req.Context(), []gomatrixserverlib.Event{*event}, r.cfg.Matrix.ServerName, nil); err != nil {
|
2017-09-13 14:37:50 +02:00
|
|
|
return httputil.LogThenError(r.req, err)
|
2017-07-28 16:29:12 +02:00
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusOK,
|
2017-07-28 16:29:12 +02:00
|
|
|
JSON: struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
}{roomID},
|
|
|
|
}
|
2017-09-13 14:37:50 +02:00
|
|
|
}
|
2017-10-25 15:44:33 +02:00
|
|
|
if err != common.ErrRoomNoExists {
|
2017-08-22 15:14:37 +02:00
|
|
|
return httputil.LogThenError(r.req, err)
|
2017-05-25 17:08:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(servers) == 0 {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusNotFound,
|
2017-05-25 17:08:28 +02:00
|
|
|
JSON: jsonerror.NotFound("No candidate servers found for room"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastErr error
|
|
|
|
for _, server := range servers {
|
|
|
|
var response *util.JSONResponse
|
|
|
|
response, lastErr = r.joinRoomUsingServer(roomID, server)
|
|
|
|
if lastErr != nil {
|
|
|
|
// There was a problem talking to one of the servers.
|
|
|
|
util.GetLogger(r.req.Context()).WithError(lastErr).WithField("server", server).Warn("Failed to join room using server")
|
|
|
|
// Try the next server.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return *response
|
|
|
|
}
|
|
|
|
|
|
|
|
// Every server we tried to join through resulted in an error.
|
|
|
|
// We return the error from the last server.
|
|
|
|
|
|
|
|
// TODO: Generate the correct HTTP status code for all different
|
|
|
|
// kinds of errors that could have happened.
|
|
|
|
// The possible errors include:
|
|
|
|
// 1) We can't connect to the remote servers.
|
|
|
|
// 2) None of the servers we could connect to think we are allowed
|
|
|
|
// to join the room.
|
|
|
|
// 3) The remote server returned something invalid.
|
|
|
|
// 4) We couldn't fetch the public keys needed to verify the
|
|
|
|
// signatures on the state events.
|
|
|
|
// 5) ...
|
|
|
|
return httputil.LogThenError(r.req, lastErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// joinRoomUsingServer tries to join a remote room using a given matrix server.
|
|
|
|
// If there was a failure communicating with the server or the response from the
|
|
|
|
// server was invalid this returns an error.
|
|
|
|
// Otherwise this returns a JSONResponse.
|
|
|
|
func (r joinRoomReq) joinRoomUsingServer(roomID string, server gomatrixserverlib.ServerName) (*util.JSONResponse, error) {
|
2017-09-13 12:03:41 +02:00
|
|
|
respMakeJoin, err := r.federation.MakeJoin(r.req.Context(), server, roomID, r.userID)
|
2017-05-25 17:08:28 +02:00
|
|
|
if err != nil {
|
|
|
|
// TODO: Check if the user was not allowed to join the room.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set all the fields to be what they should be, this should be a no-op
|
|
|
|
// but it's possible that the remote server returned us something "odd"
|
2017-09-20 11:59:19 +02:00
|
|
|
err = r.writeToBuilder(&respMakeJoin.JoinEvent, roomID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-25 17:08:28 +02:00
|
|
|
|
2017-06-19 16:21:04 +02:00
|
|
|
eventID := fmt.Sprintf("$%s:%s", util.RandomString(16), r.cfg.Matrix.ServerName)
|
2017-05-25 17:08:28 +02:00
|
|
|
event, err := respMakeJoin.JoinEvent.Build(
|
2018-08-22 14:40:25 +02:00
|
|
|
eventID, r.evTime, r.cfg.Matrix.ServerName, r.cfg.Matrix.KeyID, r.cfg.Matrix.PrivateKey,
|
2017-05-25 17:08:28 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
res := httputil.LogThenError(r.req, err)
|
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
2017-09-13 12:03:41 +02:00
|
|
|
respSendJoin, err := r.federation.SendJoin(r.req.Context(), server, event)
|
2017-05-25 17:08:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-13 12:03:41 +02:00
|
|
|
if err = respSendJoin.Check(r.req.Context(), r.keyRing, event); err != nil {
|
2017-05-25 17:08:28 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = r.producer.SendEventWithState(
|
2020-01-10 13:11:44 +01:00
|
|
|
r.req.Context(), gomatrixserverlib.RespState(respSendJoin.RespState), event,
|
2017-05-25 17:08:28 +02:00
|
|
|
); err != nil {
|
|
|
|
res := httputil.LogThenError(r.req, err)
|
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusOK,
|
2017-05-25 17:08:28 +02:00
|
|
|
// TODO: Put the response struct somewhere common.
|
|
|
|
JSON: struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
}{roomID},
|
|
|
|
}, nil
|
|
|
|
}
|