aboutsummaryrefslogtreecommitdiff
path: root/userapi
diff options
context:
space:
mode:
authorKegsay <kegan@matrix.org>2020-06-15 09:54:11 +0100
committerGitHub <noreply@github.com>2020-06-15 09:54:11 +0100
commit6b5996db1736ee962bd081b67b7f38c1591737f8 (patch)
tree49cee6c3bfe4c906616d2cefee4b2a12bce238e6 /userapi
parent0dc4ceaa2d8e46aa0134c1aabe96389ba4c1591d (diff)
Add bare bones user API (#1127)
* Add bare bones user API with tests! * linting
Diffstat (limited to 'userapi')
-rw-r--r--userapi/api/api.go38
-rw-r--r--userapi/internal/api.go53
-rw-r--r--userapi/inthttp/client.go62
-rw-r--r--userapi/inthttp/server.go41
-rw-r--r--userapi/userapi.go41
-rw-r--r--userapi/userapi_test.go138
6 files changed, 373 insertions, 0 deletions
diff --git a/userapi/api/api.go b/userapi/api/api.go
new file mode 100644
index 00000000..8534fb17
--- /dev/null
+++ b/userapi/api/api.go
@@ -0,0 +1,38 @@
+// Copyright 2020 The Matrix.org Foundation C.I.C.
+//
+// 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 api
+
+import "context"
+
+// UserInternalAPI is the internal API for information about users and devices.
+type UserInternalAPI interface {
+ QueryProfile(ctx context.Context, req *QueryProfileRequest, res *QueryProfileResponse) error
+}
+
+// QueryProfileRequest is the request for QueryProfile
+type QueryProfileRequest struct {
+ // The user ID to query
+ UserID string
+}
+
+// QueryProfileResponse is the response for QueryProfile
+type QueryProfileResponse struct {
+ // True if the user has been created. Querying for a profile does not create them.
+ UserExists bool
+ // The current display name if set.
+ DisplayName string
+ // The current avatar URL if set.
+ AvatarURL string
+}
diff --git a/userapi/internal/api.go b/userapi/internal/api.go
new file mode 100644
index 00000000..0144526c
--- /dev/null
+++ b/userapi/internal/api.go
@@ -0,0 +1,53 @@
+// Copyright 2020 The Matrix.org Foundation C.I.C.
+//
+// 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 internal
+
+import (
+ "context"
+ "database/sql"
+ "fmt"
+
+ "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
+ "github.com/matrix-org/dendrite/clientapi/auth/storage/devices"
+ "github.com/matrix-org/dendrite/userapi/api"
+ "github.com/matrix-org/gomatrixserverlib"
+)
+
+type UserInternalAPI struct {
+ AccountDB accounts.Database
+ DeviceDB devices.Database
+ ServerName gomatrixserverlib.ServerName
+}
+
+func (a *UserInternalAPI) QueryProfile(ctx context.Context, req *api.QueryProfileRequest, res *api.QueryProfileResponse) error {
+ local, domain, err := gomatrixserverlib.SplitID('@', req.UserID)
+ if err != nil {
+ return err
+ }
+ if domain != a.ServerName {
+ return fmt.Errorf("cannot query profile of remote users: got %s want %s", domain, a.ServerName)
+ }
+ prof, err := a.AccountDB.GetProfileByLocalpart(ctx, local)
+ if err != nil {
+ if err == sql.ErrNoRows {
+ return nil
+ }
+ return err
+ }
+ res.UserExists = true
+ res.AvatarURL = prof.AvatarURL
+ res.DisplayName = prof.DisplayName
+ return nil
+}
diff --git a/userapi/inthttp/client.go b/userapi/inthttp/client.go
new file mode 100644
index 00000000..90cc54a4
--- /dev/null
+++ b/userapi/inthttp/client.go
@@ -0,0 +1,62 @@
+// Copyright 2020 The Matrix.org Foundation C.I.C.
+//
+// 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 inthttp
+
+import (
+ "context"
+ "errors"
+ "net/http"
+
+ "github.com/matrix-org/dendrite/internal/httputil"
+ "github.com/matrix-org/dendrite/userapi/api"
+ "github.com/opentracing/opentracing-go"
+)
+
+// HTTP paths for the internal HTTP APIs
+const (
+ QueryProfilePath = "/userapi/queryProfile"
+)
+
+// NewUserAPIClient creates a UserInternalAPI implemented by talking to a HTTP POST API.
+// If httpClient is nil an error is returned
+func NewUserAPIClient(
+ apiURL string,
+ httpClient *http.Client,
+) (api.UserInternalAPI, error) {
+ if httpClient == nil {
+ return nil, errors.New("NewUserAPIClient: httpClient is <nil>")
+ }
+ return &httpUserInternalAPI{
+ apiURL: apiURL,
+ httpClient: httpClient,
+ }, nil
+}
+
+type httpUserInternalAPI struct {
+ apiURL string
+ httpClient *http.Client
+}
+
+func (h *httpUserInternalAPI) QueryProfile(
+ ctx context.Context,
+ request *api.QueryProfileRequest,
+ response *api.QueryProfileResponse,
+) error {
+ span, ctx := opentracing.StartSpanFromContext(ctx, "QueryProfile")
+ defer span.Finish()
+
+ apiURL := h.apiURL + QueryProfilePath
+ return httputil.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
+}
diff --git a/userapi/inthttp/server.go b/userapi/inthttp/server.go
new file mode 100644
index 00000000..f3c17ccd
--- /dev/null
+++ b/userapi/inthttp/server.go
@@ -0,0 +1,41 @@
+// Copyright 2020 The Matrix.org Foundation C.I.C.
+//
+// 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 inthttp
+
+import (
+ "encoding/json"
+ "net/http"
+
+ "github.com/gorilla/mux"
+ "github.com/matrix-org/dendrite/internal/httputil"
+ "github.com/matrix-org/dendrite/userapi/api"
+ "github.com/matrix-org/util"
+)
+
+func AddRoutes(internalAPIMux *mux.Router, s api.UserInternalAPI) {
+ internalAPIMux.Handle(QueryProfilePath,
+ httputil.MakeInternalAPI("queryProfile", func(req *http.Request) util.JSONResponse {
+ request := api.QueryProfileRequest{}
+ response := api.QueryProfileResponse{}
+ if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
+ return util.MessageResponse(http.StatusBadRequest, err.Error())
+ }
+ if err := s.QueryProfile(req.Context(), &request, &response); err != nil {
+ return util.ErrorResponse(err)
+ }
+ return util.JSONResponse{Code: http.StatusOK, JSON: &response}
+ }),
+ )
+}
diff --git a/userapi/userapi.go b/userapi/userapi.go
new file mode 100644
index 00000000..32f851cc
--- /dev/null
+++ b/userapi/userapi.go
@@ -0,0 +1,41 @@
+// Copyright 2020 The Matrix.org Foundation C.I.C.
+//
+// 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 userapi
+
+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/userapi/api"
+ "github.com/matrix-org/dendrite/userapi/internal"
+ "github.com/matrix-org/dendrite/userapi/inthttp"
+ "github.com/matrix-org/gomatrixserverlib"
+)
+
+// AddInternalRoutes registers HTTP handlers for the internal API. Invokes functions
+// on the given input API.
+func AddInternalRoutes(router *mux.Router, intAPI api.UserInternalAPI) {
+ inthttp.AddRoutes(router, intAPI)
+}
+
+// NewInternalAPI returns a concerete implementation of the internal API. Callers
+// can call functions directly on the returned API or via an HTTP interface using AddInternalRoutes.
+func NewInternalAPI(accountDB accounts.Database, deviceDB devices.Database, serverName gomatrixserverlib.ServerName) api.UserInternalAPI {
+ return &internal.UserInternalAPI{
+ AccountDB: accountDB,
+ DeviceDB: deviceDB,
+ ServerName: serverName,
+ }
+}
diff --git a/userapi/userapi_test.go b/userapi/userapi_test.go
new file mode 100644
index 00000000..423a8612
--- /dev/null
+++ b/userapi/userapi_test.go
@@ -0,0 +1,138 @@
+package userapi_test
+
+import (
+ "context"
+ "fmt"
+ "net"
+ "net/http"
+ "reflect"
+ "sync"
+ "testing"
+
+ "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/internal/httputil"
+ "github.com/matrix-org/dendrite/userapi"
+ "github.com/matrix-org/dendrite/userapi/api"
+ "github.com/matrix-org/dendrite/userapi/inthttp"
+ "github.com/matrix-org/gomatrixserverlib"
+)
+
+const (
+ serverName = gomatrixserverlib.ServerName("example.com")
+)
+
+func MustMakeInternalAPI(t *testing.T) (api.UserInternalAPI, accounts.Database, devices.Database) {
+ accountDB, err := accounts.NewDatabase("file::memory:", nil, serverName)
+ if err != nil {
+ t.Fatalf("failed to create account DB: %s", err)
+ }
+ deviceDB, err := devices.NewDatabase("file::memory:", nil, serverName)
+ if err != nil {
+ t.Fatalf("failed to create device DB: %s", err)
+ }
+
+ return userapi.NewInternalAPI(accountDB, deviceDB, serverName), accountDB, deviceDB
+}
+
+func TestQueryProfile(t *testing.T) {
+ aliceAvatarURL := "mxc://example.com/alice"
+ aliceDisplayName := "Alice"
+ userAPI, accountDB, _ := MustMakeInternalAPI(t)
+ _, err := accountDB.CreateAccount(context.TODO(), "alice", "foobar", "")
+ if err != nil {
+ t.Fatalf("failed to make account: %s", err)
+ }
+ if err := accountDB.SetAvatarURL(context.TODO(), "alice", aliceAvatarURL); err != nil {
+ t.Fatalf("failed to set avatar url: %s", err)
+ }
+ if err := accountDB.SetDisplayName(context.TODO(), "alice", aliceDisplayName); err != nil {
+ t.Fatalf("failed to set display name: %s", err)
+ }
+
+ testCases := []struct {
+ req api.QueryProfileRequest
+ wantRes api.QueryProfileResponse
+ wantErr error
+ }{
+ {
+ req: api.QueryProfileRequest{
+ UserID: fmt.Sprintf("@alice:%s", serverName),
+ },
+ wantRes: api.QueryProfileResponse{
+ UserExists: true,
+ AvatarURL: aliceAvatarURL,
+ DisplayName: aliceDisplayName,
+ },
+ },
+ {
+ req: api.QueryProfileRequest{
+ UserID: fmt.Sprintf("@bob:%s", serverName),
+ },
+ wantRes: api.QueryProfileResponse{
+ UserExists: false,
+ },
+ },
+ {
+ req: api.QueryProfileRequest{
+ UserID: "@alice:wrongdomain.com",
+ },
+ wantErr: fmt.Errorf("wrong domain"),
+ },
+ }
+
+ runCases := func(testAPI api.UserInternalAPI) {
+ for _, tc := range testCases {
+ var gotRes api.QueryProfileResponse
+ gotErr := testAPI.QueryProfile(context.TODO(), &tc.req, &gotRes)
+ if tc.wantErr == nil && gotErr != nil || tc.wantErr != nil && gotErr == nil {
+ t.Errorf("QueryProfile error, got %s want %s", gotErr, tc.wantErr)
+ continue
+ }
+ if !reflect.DeepEqual(tc.wantRes, gotRes) {
+ t.Errorf("QueryProfile response got %+v want %+v", gotRes, tc.wantRes)
+ }
+ }
+ }
+
+ t.Run("HTTP API", func(t *testing.T) {
+ router := mux.NewRouter().PathPrefix(httputil.InternalPathPrefix).Subrouter()
+ userapi.AddInternalRoutes(router, userAPI)
+ apiURL, cancel := listenAndServe(t, router)
+ defer cancel()
+ httpAPI, err := inthttp.NewUserAPIClient(apiURL, &http.Client{})
+ if err != nil {
+ t.Fatalf("failed to create HTTP client")
+ }
+ runCases(httpAPI)
+ })
+ t.Run("Monolith", func(t *testing.T) {
+ runCases(userAPI)
+ })
+}
+
+func listenAndServe(t *testing.T, router *mux.Router) (apiURL string, cancel func()) {
+ listener, err := net.Listen("tcp", ":0")
+ if err != nil {
+ t.Fatalf("failed to listen: %s", err)
+ }
+ port := listener.Addr().(*net.TCPAddr).Port
+ srv := http.Server{}
+
+ var wg sync.WaitGroup
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ srv.Handler = router
+ err := srv.Serve(listener)
+ if err != nil && err != http.ErrServerClosed {
+ t.Logf("Listen failed: %s", err)
+ }
+ }()
+
+ return fmt.Sprintf("http://localhost:%d", port), func() {
+ srv.Shutdown(context.Background())
+ wg.Wait()
+ }
+}