diff options
author | Kegsay <kegan@matrix.org> | 2020-07-15 12:02:34 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-15 12:02:34 +0100 |
commit | 9dd2ed7f6513e8fa677dee8d7dafa33f9c7afdfc (patch) | |
tree | 5c09582128d156aa2b6629cdaae626b44357b48d /clientapi/routing/keys.go | |
parent | b4c07995d68dbeffa2161920cb4cd61ea2be8389 (diff) |
Implement key uploads (#1202)
* Add storage layer for postgres/sqlite
* Return OTK counts when inserting new keys
* Hook up the key DB and make a test pass
* Convert postgres queries to be sqlite queries
* Blacklist test due to requiring rejected events
* Unbreak tests
* Update blacklist
Diffstat (limited to 'clientapi/routing/keys.go')
-rw-r--r-- | clientapi/routing/keys.go | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/clientapi/routing/keys.go b/clientapi/routing/keys.go index 5c1a657f..8c5c5bbe 100644 --- a/clientapi/routing/keys.go +++ b/clientapi/routing/keys.go @@ -15,8 +15,13 @@ package routing import ( + "encoding/json" "net/http" + "github.com/matrix-org/dendrite/clientapi/httputil" + "github.com/matrix-org/dendrite/clientapi/jsonerror" + "github.com/matrix-org/dendrite/keyserver/api" + userapi "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/util" ) @@ -32,9 +37,55 @@ func QueryKeys( } } -func UploadKeys(req *http.Request) util.JSONResponse { +type uploadKeysRequest struct { + DeviceKeys json.RawMessage `json:"device_keys"` + OneTimeKeys map[string]json.RawMessage `json:"one_time_keys"` +} + +func UploadKeys(req *http.Request, keyAPI api.KeyInternalAPI, device *userapi.Device) util.JSONResponse { + var r uploadKeysRequest + resErr := httputil.UnmarshalJSONRequest(req, &r) + if resErr != nil { + return *resErr + } + + uploadReq := &api.PerformUploadKeysRequest{} + if r.DeviceKeys != nil { + uploadReq.DeviceKeys = []api.DeviceKeys{ + { + DeviceID: device.ID, + UserID: device.UserID, + KeyJSON: r.DeviceKeys, + }, + } + } + if r.OneTimeKeys != nil { + uploadReq.OneTimeKeys = []api.OneTimeKeys{ + { + DeviceID: device.ID, + UserID: device.UserID, + KeyJSON: r.OneTimeKeys, + }, + } + } + + var uploadRes api.PerformUploadKeysResponse + keyAPI.PerformUploadKeys(req.Context(), uploadReq, &uploadRes) + if uploadRes.Error != nil { + util.GetLogger(req.Context()).WithError(uploadRes.Error).Error("Failed to PerformUploadKeys") + return jsonerror.InternalServerError() + } + if len(uploadRes.KeyErrors) > 0 { + util.GetLogger(req.Context()).WithField("key_errors", uploadRes.KeyErrors).Error("Failed to upload one or more keys") + return util.JSONResponse{ + Code: 400, + JSON: uploadRes.KeyErrors, + } + } return util.JSONResponse{ Code: 200, - JSON: struct{}{}, + JSON: struct { + OTKCounts interface{} `json:"one_time_key_counts"` + }{uploadRes.OneTimeKeyCounts[0].KeyCount}, } } |