diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-api-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-api-server/main.go index 91c55191..d843c143 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-api-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-api-server/main.go @@ -26,6 +26,7 @@ func main() { defer base.Close() // nolint: errcheck accountDB := base.CreateAccountsDB() + deviceDB := base.CreateDeviceDB() keyDB := base.CreateKeyDB() federation := base.CreateFederationClient() keyRing := keydb.CreateKeyRing(federation.Client, keyDB) @@ -33,7 +34,7 @@ func main() { alias, input, query := base.CreateHTTPRoomserverAPIs() federationapi.SetupFederationAPIComponent( - base, accountDB, federation, &keyRing, + base, accountDB, deviceDB, federation, &keyRing, alias, input, query, ) diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go index 2d3bc09e..3ffc833e 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go @@ -61,7 +61,7 @@ func main() { federation, &keyRing, alias, input, query, transactions.New(), ) - federationapi.SetupFederationAPIComponent(base, accountDB, federation, &keyRing, alias, input, query) + federationapi.SetupFederationAPIComponent(base, accountDB, deviceDB, federation, &keyRing, alias, input, query) federationsender.SetupFederationSenderComponent(base, federation, query) mediaapi.SetupMediaAPIComponent(base, deviceDB) publicroomsapi.SetupPublicRoomsAPIComponent(base, deviceDB) diff --git a/src/github.com/matrix-org/dendrite/federationapi/federationapi.go b/src/github.com/matrix-org/dendrite/federationapi/federationapi.go index c8bbf0df..4c8d5de3 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/federationapi.go +++ b/src/github.com/matrix-org/dendrite/federationapi/federationapi.go @@ -16,6 +16,7 @@ package federationapi import ( "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" + "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" "github.com/matrix-org/dendrite/common/basecomponent" "github.com/matrix-org/dendrite/roomserver/api" // TODO: Are we really wanting to pull in the producer from clientapi @@ -29,6 +30,7 @@ import ( func SetupFederationAPIComponent( base *basecomponent.BaseDendrite, accountsDB *accounts.Database, + deviceDB *devices.Database, federation *gomatrixserverlib.FederationClient, keyRing *gomatrixserverlib.KeyRing, aliasAPI api.RoomserverAliasAPI, @@ -39,6 +41,6 @@ func SetupFederationAPIComponent( routing.Setup( base.APIMux, *base.Cfg, queryAPI, aliasAPI, - roomserverProducer, *keyRing, federation, accountsDB, + roomserverProducer, *keyRing, federation, accountsDB, deviceDB, ) } diff --git a/src/github.com/matrix-org/dendrite/federationapi/routing/devices.go b/src/github.com/matrix-org/dendrite/federationapi/routing/devices.go new file mode 100644 index 00000000..ba8af7a9 --- /dev/null +++ b/src/github.com/matrix-org/dendrite/federationapi/routing/devices.go @@ -0,0 +1,53 @@ +// 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. + +package routing + +import ( + "net/http" + + "github.com/matrix-org/dendrite/clientapi/auth/authtypes" + "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" + "github.com/matrix-org/dendrite/clientapi/httputil" + "github.com/matrix-org/dendrite/clientapi/jsonerror" + "github.com/matrix-org/dendrite/clientapi/userutil" + "github.com/matrix-org/util" +) + +type userDevicesResponse struct { + Devices []authtypes.Device `json:"devices"` +} + +// GetUserDevices for the given user id +func GetUserDevices( + req *http.Request, + deviceDB *devices.Database, + userID string, +) util.JSONResponse { + localpart, err := userutil.ParseUsernameParam(userID, nil) + if err != nil { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.InvalidArgumentValue("Invalid user ID"), + } + } + + devs, err := deviceDB.GetDevicesByLocalpart(req.Context(), localpart) + if err != nil { + return httputil.LogThenError(req, err) + } + + return util.JSONResponse{ + Code: 200, + JSON: userDevicesResponse{devs}, + } +} diff --git a/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go b/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go index 7faa5d7c..eede7f48 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go @@ -20,6 +20,7 @@ import ( "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" + "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" "github.com/matrix-org/dendrite/clientapi/producers" "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/common/config" @@ -43,6 +44,7 @@ func Setup( keys gomatrixserverlib.KeyRing, federation *gomatrixserverlib.FederationClient, accountDB *accounts.Database, + deviceDB *devices.Database, ) { v2keysmux := apiMux.PathPrefix(pathPrefixV2Keys).Subrouter() v1fedmux := apiMux.PathPrefix(pathPrefixV1Federation).Subrouter() @@ -146,6 +148,16 @@ func Setup( }, )).Methods(http.MethodGet) + v1fedmux.Handle("/query/user_devices/{userID}", common.MakeFedAPI( + "federation_query_user_devices", cfg.Matrix.ServerName, keys, + func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { + vars := mux.Vars(httpReq) + return GetUserDevices( + httpReq, deviceDB, vars["userID"], + ) + }, + )).Methods(http.MethodGet) + v1fedmux.Handle("/make_join/{roomID}/{userID}", common.MakeFedAPI( "federation_make_join", cfg.Matrix.ServerName, keys, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {