aboutsummaryrefslogtreecommitdiff
path: root/userapi
diff options
context:
space:
mode:
authorKegsay <kegan@matrix.org>2020-06-16 14:53:19 +0100
committerGitHub <noreply@github.com>2020-06-16 14:53:19 +0100
commit1942928ee5e0398beed45c8b1c63d7b13e89b646 (patch)
tree0313e9168df851bd1eef314bbc04aed301114e4c /userapi
parent45011579eb65842821dff73fc2028db9d6bf7b93 (diff)
Make federationapi use userapi (#1135)
Removes dependencies on account DB, device DB and ASAPI.
Diffstat (limited to 'userapi')
-rw-r--r--userapi/api/api.go12
-rw-r--r--userapi/internal/api.go16
-rw-r--r--userapi/inthttp/client.go9
-rw-r--r--userapi/inthttp/server.go13
4 files changed, 50 insertions, 0 deletions
diff --git a/userapi/api/api.go b/userapi/api/api.go
index 57b5165a..3ed9252c 100644
--- a/userapi/api/api.go
+++ b/userapi/api/api.go
@@ -20,6 +20,7 @@ import "context"
type UserInternalAPI interface {
QueryProfile(ctx context.Context, req *QueryProfileRequest, res *QueryProfileResponse) error
QueryAccessToken(ctx context.Context, req *QueryAccessTokenRequest, res *QueryAccessTokenResponse) error
+ QueryDevices(ctx context.Context, req *QueryDevicesRequest, res *QueryDevicesResponse) error
}
// QueryAccessTokenRequest is the request for QueryAccessToken
@@ -36,6 +37,17 @@ type QueryAccessTokenResponse struct {
Err error // e.g ErrorForbidden
}
+// QueryDevicesRequest is the request for QueryDevices
+type QueryDevicesRequest struct {
+ UserID string
+}
+
+// QueryDevicesResponse is the response for QueryDevices
+type QueryDevicesResponse struct {
+ UserExists bool
+ Devices []Device
+}
+
// QueryProfileRequest is the request for QueryProfile
type QueryProfileRequest struct {
// The user ID to query
diff --git a/userapi/internal/api.go b/userapi/internal/api.go
index 1f0d5c94..d8dec11a 100644
--- a/userapi/internal/api.go
+++ b/userapi/internal/api.go
@@ -57,6 +57,22 @@ func (a *UserInternalAPI) QueryProfile(ctx context.Context, req *api.QueryProfil
return nil
}
+func (a *UserInternalAPI) QueryDevices(ctx context.Context, req *api.QueryDevicesRequest, res *api.QueryDevicesResponse) error {
+ local, domain, err := gomatrixserverlib.SplitID('@', req.UserID)
+ if err != nil {
+ return err
+ }
+ if domain != a.ServerName {
+ return fmt.Errorf("cannot query devices of remote users: got %s want %s", domain, a.ServerName)
+ }
+ devs, err := a.DeviceDB.GetDevicesByLocalpart(ctx, local)
+ if err != nil {
+ return err
+ }
+ res.Devices = devs
+ return nil
+}
+
func (a *UserInternalAPI) QueryAccessToken(ctx context.Context, req *api.QueryAccessTokenRequest, res *api.QueryAccessTokenResponse) error {
if req.AppServiceUserID != "" {
appServiceDevice, err := a.queryAppServiceToken(ctx, req.AccessToken, req.AppServiceUserID)
diff --git a/userapi/inthttp/client.go b/userapi/inthttp/client.go
index 022243fa..638a7e9b 100644
--- a/userapi/inthttp/client.go
+++ b/userapi/inthttp/client.go
@@ -28,6 +28,7 @@ import (
const (
QueryProfilePath = "/userapi/queryProfile"
QueryAccessTokenPath = "/userapi/queryAccessToken"
+ QueryDevicesPath = "/userapi/queryDevices"
)
// NewUserAPIClient creates a UserInternalAPI implemented by talking to a HTTP POST API.
@@ -73,3 +74,11 @@ func (h *httpUserInternalAPI) QueryAccessToken(
apiURL := h.apiURL + QueryAccessTokenPath
return httputil.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
}
+
+func (h *httpUserInternalAPI) QueryDevices(ctx context.Context, req *api.QueryDevicesRequest, res *api.QueryDevicesResponse) error {
+ span, ctx := opentracing.StartSpanFromContext(ctx, "QueryDevices")
+ defer span.Finish()
+
+ apiURL := h.apiURL + QueryDevicesPath
+ return httputil.PostJSON(ctx, span, h.httpClient, apiURL, req, res)
+}
diff --git a/userapi/inthttp/server.go b/userapi/inthttp/server.go
index 495b161c..19b0e40b 100644
--- a/userapi/inthttp/server.go
+++ b/userapi/inthttp/server.go
@@ -51,4 +51,17 @@ func AddRoutes(internalAPIMux *mux.Router, s api.UserInternalAPI) {
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
}),
)
+ internalAPIMux.Handle(QueryDevicesPath,
+ httputil.MakeInternalAPI("queryDevices", func(req *http.Request) util.JSONResponse {
+ request := api.QueryDevicesRequest{}
+ response := api.QueryDevicesResponse{}
+ if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
+ return util.MessageResponse(http.StatusBadRequest, err.Error())
+ }
+ if err := s.QueryDevices(req.Context(), &request, &response); err != nil {
+ return util.ErrorResponse(err)
+ }
+ return util.JSONResponse{Code: http.StatusOK, JSON: &response}
+ }),
+ )
}