2017-06-27 13:37:25 +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-06-27 13:37:25 +02:00
|
|
|
|
|
|
|
import (
|
2018-08-08 17:17:09 +02:00
|
|
|
"fmt"
|
2017-07-07 15:11:32 +02:00
|
|
|
"net/http"
|
|
|
|
|
2017-06-27 13:37:25 +02:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2018-08-08 17:17:09 +02:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2017-06-27 13:37:25 +02:00
|
|
|
"github.com/matrix-org/gomatrix"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DirectoryRoom looks up a room alias
|
|
|
|
func DirectoryRoom(
|
|
|
|
req *http.Request,
|
|
|
|
roomAlias string,
|
|
|
|
federation *gomatrixserverlib.FederationClient,
|
|
|
|
cfg *config.Dendrite,
|
2018-08-08 17:17:09 +02:00
|
|
|
rsAPI roomserverAPI.RoomserverAliasAPI,
|
2017-06-27 13:37:25 +02:00
|
|
|
) util.JSONResponse {
|
2017-07-07 15:11:32 +02:00
|
|
|
_, domain, err := gomatrixserverlib.SplitID('#', roomAlias)
|
2017-06-27 13:37:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2017-06-27 13:37:25 +02:00
|
|
|
JSON: jsonerror.BadJSON("Room alias must be in the form '#localpart:domain'"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if domain == cfg.Matrix.ServerName {
|
2018-08-08 17:17:09 +02:00
|
|
|
// Query the roomserver API to check if the alias exists locally
|
|
|
|
queryReq := roomserverAPI.GetRoomIDForAliasRequest{Alias: roomAlias}
|
|
|
|
var queryRes roomserverAPI.GetRoomIDForAliasResponse
|
|
|
|
if err = rsAPI.GetRoomIDForAlias(req.Context(), &queryReq, &queryRes); err != nil {
|
2017-07-28 12:31:43 +02:00
|
|
|
return httputil.LogThenError(req, err)
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:17:09 +02:00
|
|
|
// List any roomIDs found associated with this alias
|
2017-07-28 12:31:43 +02:00
|
|
|
if len(queryRes.RoomID) > 0 {
|
|
|
|
return util.JSONResponse{
|
2018-08-08 17:17:09 +02:00
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: queryRes,
|
2017-07-28 12:31:43 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-27 13:37:25 +02:00
|
|
|
} else {
|
2018-08-08 17:17:09 +02:00
|
|
|
// Query the federation for this room alias
|
|
|
|
resp, err := federation.LookupRoomAlias(req.Context(), domain, roomAlias)
|
2017-06-27 13:37:25 +02:00
|
|
|
if err != nil {
|
2018-08-08 17:17:09 +02:00
|
|
|
switch err.(type) {
|
2017-06-27 13:37:25 +02:00
|
|
|
case gomatrix.HTTPError:
|
2018-08-08 17:17:09 +02:00
|
|
|
default:
|
|
|
|
// TODO: Return 502 if the remote server errored.
|
|
|
|
// TODO: Return 504 if the remote server timed out.
|
|
|
|
return httputil.LogThenError(req, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(resp.RoomID) > 0 {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: resp,
|
2017-06-27 13:37:25 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-28 12:31:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-08-08 17:17:09 +02:00
|
|
|
Code: http.StatusNotFound,
|
|
|
|
JSON: jsonerror.NotFound(
|
|
|
|
fmt.Sprintf("Room alias %s not found", roomAlias),
|
|
|
|
),
|
2017-07-28 12:31:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLocalAlias implements PUT /directory/room/{roomAlias}
|
|
|
|
// TODO: Check if the user has the power level to set an alias
|
|
|
|
func SetLocalAlias(
|
|
|
|
req *http.Request,
|
|
|
|
device *authtypes.Device,
|
|
|
|
alias string,
|
|
|
|
cfg *config.Dendrite,
|
2018-08-08 17:17:09 +02:00
|
|
|
aliasAPI roomserverAPI.RoomserverAliasAPI,
|
2017-07-28 12:31:43 +02:00
|
|
|
) util.JSONResponse {
|
|
|
|
_, domain, err := gomatrixserverlib.SplitID('#', alias)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2017-07-28 12:31:43 +02:00
|
|
|
JSON: jsonerror.BadJSON("Room alias must be in the form '#localpart:domain'"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if domain != cfg.Matrix.ServerName {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusForbidden,
|
2017-07-28 12:31:43 +02:00
|
|
|
JSON: jsonerror.Forbidden("Alias must be on local homeserver"),
|
|
|
|
}
|
|
|
|
}
|
2017-06-27 13:37:25 +02:00
|
|
|
|
2018-06-29 13:09:00 +02:00
|
|
|
// Check that the alias does not fall within an exclusive namespace of an
|
|
|
|
// application service
|
2018-07-06 12:29:15 +02:00
|
|
|
// TODO: This code should eventually be refactored with:
|
|
|
|
// 1. The new method for checking for things matching an AS's namespace
|
|
|
|
// 2. Using an overall Regex object for all AS's just like we did for usernames
|
2018-06-29 13:09:00 +02:00
|
|
|
for _, appservice := range cfg.Derived.ApplicationServices {
|
2019-07-12 15:36:17 +02:00
|
|
|
// Don't prevent AS from creating aliases in its own namespace
|
|
|
|
// Note that Dendrite uses SenderLocalpart as UserID for AS users
|
|
|
|
if device.UserID != appservice.SenderLocalpart {
|
|
|
|
if aliasNamespaces, ok := appservice.NamespaceMap["aliases"]; ok {
|
|
|
|
for _, namespace := range aliasNamespaces {
|
|
|
|
if namespace.Exclusive && namespace.RegexpObject.MatchString(alias) {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.ASExclusive("Alias is reserved by an application service"),
|
|
|
|
}
|
2018-06-29 13:09:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 12:31:43 +02:00
|
|
|
var r struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
}
|
|
|
|
if resErr := httputil.UnmarshalJSONRequest(req, &r); resErr != nil {
|
|
|
|
return *resErr
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:17:09 +02:00
|
|
|
queryReq := roomserverAPI.SetRoomAliasRequest{
|
2017-07-28 12:31:43 +02:00
|
|
|
UserID: device.UserID,
|
|
|
|
RoomID: r.RoomID,
|
|
|
|
Alias: alias,
|
|
|
|
}
|
2018-08-08 17:17:09 +02:00
|
|
|
var queryRes roomserverAPI.SetRoomAliasResponse
|
2017-09-13 14:37:50 +02:00
|
|
|
if err := aliasAPI.SetRoomAlias(req.Context(), &queryReq, &queryRes); err != nil {
|
2017-07-28 12:31:43 +02:00
|
|
|
return httputil.LogThenError(req, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if queryRes.AliasExists {
|
2017-06-27 13:37:25 +02:00
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusConflict,
|
2017-07-28 12:31:43 +02:00
|
|
|
JSON: jsonerror.Unknown("The alias " + alias + " already exists."),
|
2017-06-27 13:37:25 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-28 12:31:43 +02:00
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusOK,
|
2017-07-28 12:31:43 +02:00
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveLocalAlias implements DELETE /directory/room/{roomAlias}
|
|
|
|
// TODO: Check if the user has the power level to remove an alias
|
|
|
|
func RemoveLocalAlias(
|
|
|
|
req *http.Request,
|
|
|
|
device *authtypes.Device,
|
|
|
|
alias string,
|
2018-08-08 17:17:09 +02:00
|
|
|
aliasAPI roomserverAPI.RoomserverAliasAPI,
|
2017-07-28 12:31:43 +02:00
|
|
|
) util.JSONResponse {
|
2018-08-08 17:17:09 +02:00
|
|
|
queryReq := roomserverAPI.RemoveRoomAliasRequest{
|
2017-07-28 12:31:43 +02:00
|
|
|
Alias: alias,
|
|
|
|
UserID: device.UserID,
|
|
|
|
}
|
2018-08-08 17:17:09 +02:00
|
|
|
var queryRes roomserverAPI.RemoveRoomAliasResponse
|
2017-09-13 14:37:50 +02:00
|
|
|
if err := aliasAPI.RemoveRoomAlias(req.Context(), &queryReq, &queryRes); err != nil {
|
2017-07-28 12:31:43 +02:00
|
|
|
return httputil.LogThenError(req, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusOK,
|
2017-07-28 12:31:43 +02:00
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
2017-06-27 13:37:25 +02:00
|
|
|
}
|