Authenticate user while fetching rooms from other server

This commit is contained in:
Prateek Sachan 2020-03-26 14:46:10 +05:30
parent cc972a8b94
commit 6ff9882931
2 changed files with 11 additions and 4 deletions

View file

@ -21,6 +21,8 @@ import (
"sync" "sync"
"time" "time"
"github.com/matrix-org/dendrite/clientapi/auth"
"github.com/matrix-org/dendrite/common/config" "github.com/matrix-org/dendrite/common/config"
"github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/httputil"
@ -44,14 +46,19 @@ type filter struct {
// GetPostPublicRooms implements GET and POST /publicRooms // GetPostPublicRooms implements GET and POST /publicRooms
func GetPostPublicRooms( func GetPostPublicRooms(
req *http.Request, cfg *config.Dendrite, server gomatrixserverlib.ServerName, req *http.Request, cfg *config.Dendrite, server gomatrixserverlib.ServerName,
publicRoomDatabase storage.Database, fedClient *gomatrixserverlib.FederationClient, publicRoomDatabase storage.Database, fedClient *gomatrixserverlib.FederationClient, data auth.Data,
) util.JSONResponse { ) util.JSONResponse {
var request PublicRoomReq var request PublicRoomReq
if fillErr := fillPublicRoomsReq(req, &request); fillErr != nil { if fillErr := fillPublicRoomsReq(req, &request); fillErr != nil {
return *fillErr return *fillErr
} }
if server != "" && server != cfg.Matrix.ServerName { if server != "" && server != cfg.Matrix.ServerName {
//TODO Authenticate user before serving rooms from other server // We require requests to be authenticated in order
// to serve public rooms from other server
_, jsonerr := auth.VerifyUserFromRequest(req, data)
if jsonerr != nil {
return *jsonerr
}
fres, err := fedClient.GetPublicRooms(req.Context(), server, int(request.Limit), fres, err := fedClient.GetPublicRooms(req.Context(), server, int(request.Limit),
request.Since, false, "") request.Since, false, "")
if err != nil { if err != nil {

View file

@ -75,14 +75,14 @@ func Setup(
return directory.GetPostPublicRoomsWithExternal(req, publicRoomsDB, fedClient, extRoomsProvider) return directory.GetPostPublicRoomsWithExternal(req, publicRoomsDB, fedClient, extRoomsProvider)
} }
server := gomatrixserverlib.ServerName(req.URL.Query().Get("server")) server := gomatrixserverlib.ServerName(req.URL.Query().Get("server"))
return directory.GetPostPublicRooms(req, cfg, server, publicRoomsDB, fedClient) return directory.GetPostPublicRooms(req, cfg, server, publicRoomsDB, fedClient, authData)
}), }),
).Methods(http.MethodGet, http.MethodPost, http.MethodOptions) ).Methods(http.MethodGet, http.MethodPost, http.MethodOptions)
// Federation - TODO: should this live here or in federation API? It's sure easier if it's here so here it is. // Federation - TODO: should this live here or in federation API? It's sure easier if it's here so here it is.
apiMux.Handle("/_matrix/federation/v1/publicRooms", apiMux.Handle("/_matrix/federation/v1/publicRooms",
common.MakeExternalAPI("federation_public_rooms", func(req *http.Request) util.JSONResponse { common.MakeExternalAPI("federation_public_rooms", func(req *http.Request) util.JSONResponse {
return directory.GetPostPublicRooms(req, cfg, cfg.Matrix.ServerName, publicRoomsDB, fedClient) return directory.GetPostPublicRooms(req, cfg, cfg.Matrix.ServerName, publicRoomsDB, fedClient, authData)
}), }),
).Methods(http.MethodGet) ).Methods(http.MethodGet)
} }