diff options
author | Kegsay <kegan@matrix.org> | 2020-07-21 14:47:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-21 14:47:53 +0100 |
commit | 1d72ce8b7ab759555503df37af666529749b489c (patch) | |
tree | 06ac331afec50a9a92f05062b80db0870f95ac25 /clientapi/routing/keys.go | |
parent | d76eb1b99491f644be035a631a08b5874065e4d7 (diff) |
Implement claiming one-time keys locally (#1210)
* Add API shape for claiming keys
* Implement claiming one-time keys locally
Fairly boring, nothing too special going on.
Diffstat (limited to 'clientapi/routing/keys.go')
-rw-r--r-- | clientapi/routing/keys.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/clientapi/routing/keys.go b/clientapi/routing/keys.go index 5f7bfb18..ba03a352 100644 --- a/clientapi/routing/keys.go +++ b/clientapi/routing/keys.go @@ -117,3 +117,40 @@ func QueryKeys(req *http.Request, keyAPI api.KeyInternalAPI) util.JSONResponse { }, } } + +type claimKeysRequest struct { + TimeoutMS int `json:"timeout"` + // The keys to be claimed. A map from user ID, to a map from device ID to algorithm name. + OneTimeKeys map[string]map[string]string `json:"one_time_keys"` +} + +func (r *claimKeysRequest) GetTimeout() time.Duration { + if r.TimeoutMS == 0 { + return 10 * time.Second + } + return time.Duration(r.TimeoutMS) * time.Millisecond +} + +func ClaimKeys(req *http.Request, keyAPI api.KeyInternalAPI) util.JSONResponse { + var r claimKeysRequest + resErr := httputil.UnmarshalJSONRequest(req, &r) + if resErr != nil { + return *resErr + } + claimRes := api.PerformClaimKeysResponse{} + keyAPI.PerformClaimKeys(req.Context(), &api.PerformClaimKeysRequest{ + OneTimeKeys: r.OneTimeKeys, + Timeout: r.GetTimeout(), + }, &claimRes) + if claimRes.Error != nil { + util.GetLogger(req.Context()).WithError(claimRes.Error).Error("failed to PerformClaimKeys") + return jsonerror.InternalServerError() + } + return util.JSONResponse{ + Code: 200, + JSON: map[string]interface{}{ + "one_time_keys": claimRes.OneTimeKeys, + "failures": claimRes.Failures, + }, + } +} |