2020-07-14 12:59:07 +01:00
|
|
|
// 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.
|
|
|
|
|
2020-07-13 16:02:35 +01:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2020-07-14 12:59:07 +01:00
|
|
|
"bytes"
|
2020-07-13 16:02:35 +01:00
|
|
|
"context"
|
2020-07-15 18:40:41 +01:00
|
|
|
"encoding/json"
|
2020-07-14 12:59:07 +01:00
|
|
|
"fmt"
|
2020-07-21 17:46:47 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
2020-07-13 16:02:35 +01:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/keyserver/api"
|
2020-07-23 16:41:36 +01:00
|
|
|
"github.com/matrix-org/dendrite/keyserver/producers"
|
2020-07-14 12:59:07 +01:00
|
|
|
"github.com/matrix-org/dendrite/keyserver/storage"
|
2020-07-22 17:04:57 +01:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2020-07-15 18:40:41 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-07-22 17:04:57 +01:00
|
|
|
"github.com/matrix-org/util"
|
2020-07-14 12:59:07 +01:00
|
|
|
"github.com/tidwall/gjson"
|
2020-07-15 18:40:41 +01:00
|
|
|
"github.com/tidwall/sjson"
|
2020-07-13 16:02:35 +01:00
|
|
|
)
|
|
|
|
|
2020-07-14 12:59:07 +01:00
|
|
|
type KeyInternalAPI struct {
|
2020-07-15 18:40:41 +01:00
|
|
|
DB storage.Database
|
|
|
|
ThisServer gomatrixserverlib.ServerName
|
2020-07-21 17:46:47 +01:00
|
|
|
FedClient *gomatrixserverlib.FederationClient
|
2020-07-22 17:04:57 +01:00
|
|
|
UserAPI userapi.UserInternalAPI
|
2020-07-23 16:41:36 +01:00
|
|
|
Producer *producers.KeyChange
|
2020-08-06 17:48:10 +01:00
|
|
|
Updater *DeviceListUpdater
|
2020-07-14 12:59:07 +01:00
|
|
|
}
|
2020-07-13 16:02:35 +01:00
|
|
|
|
2020-07-30 18:00:56 +01:00
|
|
|
func (a *KeyInternalAPI) SetUserAPI(i userapi.UserInternalAPI) {
|
|
|
|
a.UserAPI = i
|
|
|
|
}
|
|
|
|
|
2020-08-05 13:41:16 +01:00
|
|
|
func (a *KeyInternalAPI) InputDeviceListUpdate(
|
|
|
|
ctx context.Context, req *api.InputDeviceListUpdateRequest, res *api.InputDeviceListUpdateResponse,
|
|
|
|
) {
|
2020-08-06 17:48:10 +01:00
|
|
|
err := a.Updater.Update(ctx, req.Event)
|
2020-08-05 13:41:16 +01:00
|
|
|
if err != nil {
|
|
|
|
res.Error = &api.KeyError{
|
2020-08-06 17:48:10 +01:00
|
|
|
Err: fmt.Sprintf("failed to update device list: %s", err),
|
2020-08-05 13:41:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 18:25:16 +01:00
|
|
|
func (a *KeyInternalAPI) QueryKeyChanges(ctx context.Context, req *api.QueryKeyChangesRequest, res *api.QueryKeyChangesResponse) {
|
2020-07-30 11:15:46 +01:00
|
|
|
if req.Partition < 0 {
|
|
|
|
req.Partition = a.Producer.DefaultPartition()
|
|
|
|
}
|
2020-07-30 14:52:21 +01:00
|
|
|
userIDs, latest, err := a.DB.KeyChanges(ctx, req.Partition, req.Offset, req.ToOffset)
|
2020-07-28 18:25:16 +01:00
|
|
|
if err != nil {
|
|
|
|
res.Error = &api.KeyError{
|
|
|
|
Err: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.Offset = latest
|
2020-07-30 11:15:46 +01:00
|
|
|
res.Partition = req.Partition
|
2020-07-28 18:25:16 +01:00
|
|
|
res.UserIDs = userIDs
|
|
|
|
}
|
|
|
|
|
2020-07-13 16:02:35 +01:00
|
|
|
func (a *KeyInternalAPI) PerformUploadKeys(ctx context.Context, req *api.PerformUploadKeysRequest, res *api.PerformUploadKeysResponse) {
|
2020-07-14 12:59:07 +01:00
|
|
|
res.KeyErrors = make(map[string]map[string]*api.KeyError)
|
2020-08-03 17:07:06 +01:00
|
|
|
a.uploadLocalDeviceKeys(ctx, req, res)
|
2020-07-14 12:59:07 +01:00
|
|
|
a.uploadOneTimeKeys(ctx, req, res)
|
2020-07-13 16:02:35 +01:00
|
|
|
}
|
2020-07-21 14:47:53 +01:00
|
|
|
|
2020-07-13 16:02:35 +01:00
|
|
|
func (a *KeyInternalAPI) PerformClaimKeys(ctx context.Context, req *api.PerformClaimKeysRequest, res *api.PerformClaimKeysResponse) {
|
2020-07-21 14:47:53 +01:00
|
|
|
res.OneTimeKeys = make(map[string]map[string]map[string]json.RawMessage)
|
|
|
|
res.Failures = make(map[string]interface{})
|
|
|
|
// wrap request map in a top-level by-domain map
|
|
|
|
domainToDeviceKeys := make(map[string]map[string]map[string]string)
|
|
|
|
for userID, val := range req.OneTimeKeys {
|
|
|
|
_, serverName, err := gomatrixserverlib.SplitID('@', userID)
|
|
|
|
if err != nil {
|
|
|
|
continue // ignore invalid users
|
|
|
|
}
|
|
|
|
nested, ok := domainToDeviceKeys[string(serverName)]
|
|
|
|
if !ok {
|
|
|
|
nested = make(map[string]map[string]string)
|
|
|
|
}
|
|
|
|
nested[userID] = val
|
|
|
|
domainToDeviceKeys[string(serverName)] = nested
|
|
|
|
}
|
|
|
|
// claim local keys
|
|
|
|
if local, ok := domainToDeviceKeys[string(a.ThisServer)]; ok {
|
|
|
|
keys, err := a.DB.ClaimKeys(ctx, local)
|
|
|
|
if err != nil {
|
|
|
|
res.Error = &api.KeyError{
|
|
|
|
Err: fmt.Sprintf("failed to ClaimKeys locally: %s", err),
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 17:04:57 +01:00
|
|
|
util.GetLogger(ctx).WithField("keys_claimed", len(keys)).WithField("num_users", len(local)).Info("Claimed local keys")
|
|
|
|
for _, key := range keys {
|
|
|
|
_, ok := res.OneTimeKeys[key.UserID]
|
|
|
|
if !ok {
|
|
|
|
res.OneTimeKeys[key.UserID] = make(map[string]map[string]json.RawMessage)
|
|
|
|
}
|
|
|
|
_, ok = res.OneTimeKeys[key.UserID][key.DeviceID]
|
|
|
|
if !ok {
|
|
|
|
res.OneTimeKeys[key.UserID][key.DeviceID] = make(map[string]json.RawMessage)
|
|
|
|
}
|
|
|
|
for keyID, keyJSON := range key.KeyJSON {
|
|
|
|
res.OneTimeKeys[key.UserID][key.DeviceID][keyID] = keyJSON
|
|
|
|
}
|
|
|
|
}
|
2020-07-21 14:47:53 +01:00
|
|
|
delete(domainToDeviceKeys, string(a.ThisServer))
|
|
|
|
}
|
2020-07-22 17:04:57 +01:00
|
|
|
if len(domainToDeviceKeys) > 0 {
|
|
|
|
a.claimRemoteKeys(ctx, req.Timeout, res, domainToDeviceKeys)
|
|
|
|
}
|
2020-07-21 17:46:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *KeyInternalAPI) claimRemoteKeys(
|
|
|
|
ctx context.Context, timeout time.Duration, res *api.PerformClaimKeysResponse, domainToDeviceKeys map[string]map[string]map[string]string,
|
|
|
|
) {
|
|
|
|
resultCh := make(chan *gomatrixserverlib.RespClaimKeys, len(domainToDeviceKeys))
|
|
|
|
// allows us to wait until all federation servers have been poked
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(domainToDeviceKeys))
|
|
|
|
// mutex for failures
|
|
|
|
var failMu sync.Mutex
|
2020-07-22 17:04:57 +01:00
|
|
|
util.GetLogger(ctx).WithField("num_servers", len(domainToDeviceKeys)).Info("Claiming remote keys from servers")
|
2020-07-21 17:46:47 +01:00
|
|
|
|
|
|
|
// fan out
|
|
|
|
for d, k := range domainToDeviceKeys {
|
|
|
|
go func(domain string, keysToClaim map[string]map[string]string) {
|
|
|
|
defer wg.Done()
|
|
|
|
fedCtx, cancel := context.WithTimeout(ctx, timeout)
|
|
|
|
defer cancel()
|
|
|
|
claimKeyRes, err := a.FedClient.ClaimKeys(fedCtx, gomatrixserverlib.ServerName(domain), keysToClaim)
|
|
|
|
if err != nil {
|
2020-07-22 17:04:57 +01:00
|
|
|
util.GetLogger(ctx).WithError(err).WithField("server", domain).Error("ClaimKeys failed")
|
2020-07-21 17:46:47 +01:00
|
|
|
failMu.Lock()
|
|
|
|
res.Failures[domain] = map[string]interface{}{
|
|
|
|
"message": err.Error(),
|
|
|
|
}
|
|
|
|
failMu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resultCh <- &claimKeyRes
|
|
|
|
}(d, k)
|
|
|
|
}
|
2020-07-13 16:02:35 +01:00
|
|
|
|
2020-07-21 17:46:47 +01:00
|
|
|
// Close the result channel when the goroutines have quit so the for .. range exits
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(resultCh)
|
|
|
|
}()
|
|
|
|
|
2020-07-22 17:04:57 +01:00
|
|
|
keysClaimed := 0
|
2020-07-21 17:46:47 +01:00
|
|
|
for result := range resultCh {
|
|
|
|
for userID, nest := range result.OneTimeKeys {
|
|
|
|
res.OneTimeKeys[userID] = make(map[string]map[string]json.RawMessage)
|
|
|
|
for deviceID, nest2 := range nest {
|
|
|
|
res.OneTimeKeys[userID][deviceID] = make(map[string]json.RawMessage)
|
|
|
|
for keyIDWithAlgo, otk := range nest2 {
|
|
|
|
keyJSON, err := json.Marshal(otk)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res.OneTimeKeys[userID][deviceID][keyIDWithAlgo] = keyJSON
|
2020-07-22 17:04:57 +01:00
|
|
|
keysClaimed++
|
2020-07-21 17:46:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 17:04:57 +01:00
|
|
|
util.GetLogger(ctx).WithField("num_keys", keysClaimed).Info("Claimed remote keys")
|
2020-07-13 16:02:35 +01:00
|
|
|
}
|
2020-07-21 14:47:53 +01:00
|
|
|
|
2020-08-03 12:29:58 +01:00
|
|
|
func (a *KeyInternalAPI) QueryOneTimeKeys(ctx context.Context, req *api.QueryOneTimeKeysRequest, res *api.QueryOneTimeKeysResponse) {
|
|
|
|
count, err := a.DB.OneTimeKeysCount(ctx, req.UserID, req.DeviceID)
|
|
|
|
if err != nil {
|
|
|
|
res.Error = &api.KeyError{
|
|
|
|
Err: fmt.Sprintf("Failed to query OTK counts: %s", err),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res.Count = *count
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:32:14 +01:00
|
|
|
func (a *KeyInternalAPI) QueryDeviceMessages(ctx context.Context, req *api.QueryDeviceMessagesRequest, res *api.QueryDeviceMessagesResponse) {
|
|
|
|
msgs, err := a.DB.DeviceKeysForUser(ctx, req.UserID, nil)
|
|
|
|
if err != nil {
|
|
|
|
res.Error = &api.KeyError{
|
|
|
|
Err: fmt.Sprintf("failed to query DB for device keys: %s", err),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
maxStreamID := 0
|
|
|
|
for _, m := range msgs {
|
|
|
|
if m.StreamID > maxStreamID {
|
|
|
|
maxStreamID = m.StreamID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.Devices = msgs
|
|
|
|
res.StreamID = maxStreamID
|
|
|
|
}
|
|
|
|
|
2020-07-13 16:02:35 +01:00
|
|
|
func (a *KeyInternalAPI) QueryKeys(ctx context.Context, req *api.QueryKeysRequest, res *api.QueryKeysResponse) {
|
2020-07-15 18:40:41 +01:00
|
|
|
res.DeviceKeys = make(map[string]map[string]json.RawMessage)
|
|
|
|
res.Failures = make(map[string]interface{})
|
|
|
|
// make a map from domain to device keys
|
2020-07-21 17:46:47 +01:00
|
|
|
domainToDeviceKeys := make(map[string]map[string][]string)
|
2020-07-15 18:40:41 +01:00
|
|
|
for userID, deviceIDs := range req.UserToDevices {
|
|
|
|
_, serverName, err := gomatrixserverlib.SplitID('@', userID)
|
|
|
|
if err != nil {
|
|
|
|
continue // ignore invalid users
|
|
|
|
}
|
|
|
|
domain := string(serverName)
|
|
|
|
// query local devices
|
|
|
|
if serverName == a.ThisServer {
|
|
|
|
deviceKeys, err := a.DB.DeviceKeysForUser(ctx, userID, deviceIDs)
|
|
|
|
if err != nil {
|
|
|
|
res.Error = &api.KeyError{
|
|
|
|
Err: fmt.Sprintf("failed to query local device keys: %s", err),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-07-22 17:04:57 +01:00
|
|
|
|
|
|
|
// pull out display names after we have the keys so we handle wildcards correctly
|
|
|
|
var dids []string
|
|
|
|
for _, dk := range deviceKeys {
|
|
|
|
dids = append(dids, dk.DeviceID)
|
|
|
|
}
|
|
|
|
var queryRes userapi.QueryDeviceInfosResponse
|
|
|
|
err = a.UserAPI.QueryDeviceInfos(ctx, &userapi.QueryDeviceInfosRequest{
|
|
|
|
DeviceIDs: dids,
|
|
|
|
}, &queryRes)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).Warnf("Failed to QueryDeviceInfos for device IDs, display names will be missing")
|
|
|
|
}
|
|
|
|
|
2020-07-15 18:40:41 +01:00
|
|
|
if res.DeviceKeys[userID] == nil {
|
|
|
|
res.DeviceKeys[userID] = make(map[string]json.RawMessage)
|
|
|
|
}
|
|
|
|
for _, dk := range deviceKeys {
|
2020-07-31 14:40:45 +01:00
|
|
|
if len(dk.KeyJSON) == 0 {
|
|
|
|
continue // don't include blank keys
|
|
|
|
}
|
2020-07-22 17:04:57 +01:00
|
|
|
// inject display name if known
|
|
|
|
dk.KeyJSON, _ = sjson.SetBytes(dk.KeyJSON, "unsigned", struct {
|
|
|
|
DisplayName string `json:"device_display_name,omitempty"`
|
|
|
|
}{queryRes.DeviceInfo[dk.DeviceID].DisplayName})
|
2020-07-15 18:40:41 +01:00
|
|
|
res.DeviceKeys[userID][dk.DeviceID] = dk.KeyJSON
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-21 17:46:47 +01:00
|
|
|
domainToDeviceKeys[domain] = make(map[string][]string)
|
|
|
|
domainToDeviceKeys[domain][userID] = append(domainToDeviceKeys[domain][userID], deviceIDs...)
|
2020-07-15 18:40:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: set device display names when they are known
|
2020-07-21 17:46:47 +01:00
|
|
|
|
|
|
|
// perform key queries for remote devices
|
|
|
|
a.queryRemoteKeys(ctx, req.Timeout, res, domainToDeviceKeys)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *KeyInternalAPI) queryRemoteKeys(
|
|
|
|
ctx context.Context, timeout time.Duration, res *api.QueryKeysResponse, domainToDeviceKeys map[string]map[string][]string,
|
|
|
|
) {
|
|
|
|
resultCh := make(chan *gomatrixserverlib.RespQueryKeys, len(domainToDeviceKeys))
|
|
|
|
// allows us to wait until all federation servers have been poked
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(domainToDeviceKeys))
|
|
|
|
// mutex for failures
|
|
|
|
var failMu sync.Mutex
|
|
|
|
|
|
|
|
// fan out
|
|
|
|
for domain, deviceKeys := range domainToDeviceKeys {
|
|
|
|
go func(serverName string, devKeys map[string][]string) {
|
|
|
|
defer wg.Done()
|
|
|
|
fedCtx, cancel := context.WithTimeout(ctx, timeout)
|
|
|
|
defer cancel()
|
|
|
|
queryKeysResp, err := a.FedClient.QueryKeys(fedCtx, gomatrixserverlib.ServerName(serverName), devKeys)
|
|
|
|
if err != nil {
|
|
|
|
failMu.Lock()
|
|
|
|
res.Failures[serverName] = map[string]interface{}{
|
|
|
|
"message": err.Error(),
|
|
|
|
}
|
|
|
|
failMu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resultCh <- &queryKeysResp
|
|
|
|
}(domain, deviceKeys)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the result channel when the goroutines have quit so the for .. range exits
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(resultCh)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for result := range resultCh {
|
|
|
|
for userID, nest := range result.DeviceKeys {
|
|
|
|
res.DeviceKeys[userID] = make(map[string]json.RawMessage)
|
|
|
|
for deviceID, deviceKey := range nest {
|
|
|
|
keyJSON, err := json.Marshal(deviceKey)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res.DeviceKeys[userID][deviceID] = keyJSON
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-13 16:02:35 +01:00
|
|
|
}
|
2020-07-14 12:59:07 +01:00
|
|
|
|
2020-08-03 17:07:06 +01:00
|
|
|
func (a *KeyInternalAPI) uploadLocalDeviceKeys(ctx context.Context, req *api.PerformUploadKeysRequest, res *api.PerformUploadKeysResponse) {
|
|
|
|
var keysToStore []api.DeviceMessage
|
2020-07-14 12:59:07 +01:00
|
|
|
// assert that the user ID / device ID are not lying for each key
|
|
|
|
for _, key := range req.DeviceKeys {
|
2020-08-03 17:07:06 +01:00
|
|
|
_, serverName, err := gomatrixserverlib.SplitID('@', key.UserID)
|
|
|
|
if err != nil {
|
|
|
|
continue // ignore invalid users
|
|
|
|
}
|
|
|
|
if serverName != a.ThisServer {
|
|
|
|
continue // ignore remote users
|
|
|
|
}
|
2020-07-30 18:00:56 +01:00
|
|
|
if len(key.KeyJSON) == 0 {
|
2020-08-03 17:07:06 +01:00
|
|
|
keysToStore = append(keysToStore, key.WithStreamID(0))
|
2020-07-30 18:00:56 +01:00
|
|
|
continue // deleted keys don't need sanity checking
|
|
|
|
}
|
2020-07-14 12:59:07 +01:00
|
|
|
gotUserID := gjson.GetBytes(key.KeyJSON, "user_id").Str
|
|
|
|
gotDeviceID := gjson.GetBytes(key.KeyJSON, "device_id").Str
|
|
|
|
if gotUserID == key.UserID && gotDeviceID == key.DeviceID {
|
2020-08-03 17:07:06 +01:00
|
|
|
keysToStore = append(keysToStore, key.WithStreamID(0))
|
2020-07-14 12:59:07 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
res.KeyError(key.UserID, key.DeviceID, &api.KeyError{
|
2020-07-15 12:02:34 +01:00
|
|
|
Err: fmt.Sprintf(
|
2020-07-14 12:59:07 +01:00
|
|
|
"user_id or device_id mismatch: users: %s - %s, devices: %s - %s",
|
|
|
|
gotUserID, key.UserID, gotDeviceID, key.DeviceID,
|
|
|
|
),
|
|
|
|
})
|
|
|
|
}
|
2020-07-30 18:00:56 +01:00
|
|
|
|
2020-07-14 12:59:07 +01:00
|
|
|
// get existing device keys so we can check for changes
|
2020-08-03 17:07:06 +01:00
|
|
|
existingKeys := make([]api.DeviceMessage, len(keysToStore))
|
2020-07-14 12:59:07 +01:00
|
|
|
for i := range keysToStore {
|
2020-08-03 17:07:06 +01:00
|
|
|
existingKeys[i] = api.DeviceMessage{
|
|
|
|
DeviceKeys: api.DeviceKeys{
|
|
|
|
UserID: keysToStore[i].UserID,
|
|
|
|
DeviceID: keysToStore[i].DeviceID,
|
|
|
|
},
|
2020-07-14 12:59:07 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-15 12:02:34 +01:00
|
|
|
if err := a.DB.DeviceKeysJSON(ctx, existingKeys); err != nil {
|
2020-07-14 12:59:07 +01:00
|
|
|
res.Error = &api.KeyError{
|
2020-07-15 12:02:34 +01:00
|
|
|
Err: fmt.Sprintf("failed to query existing device keys: %s", err.Error()),
|
2020-07-14 12:59:07 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// store the device keys and emit changes
|
2020-08-05 13:41:16 +01:00
|
|
|
err := a.DB.StoreLocalDeviceKeys(ctx, keysToStore)
|
2020-08-03 17:07:06 +01:00
|
|
|
if err != nil {
|
2020-07-14 12:59:07 +01:00
|
|
|
res.Error = &api.KeyError{
|
2020-07-15 12:02:34 +01:00
|
|
|
Err: fmt.Sprintf("failed to store device keys: %s", err.Error()),
|
2020-07-14 12:59:07 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-08-03 17:07:06 +01:00
|
|
|
err = a.emitDeviceKeyChanges(existingKeys, keysToStore)
|
2020-07-23 16:41:36 +01:00
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).Errorf("Failed to emitDeviceKeyChanges: %s", err)
|
|
|
|
}
|
2020-07-14 12:59:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *KeyInternalAPI) uploadOneTimeKeys(ctx context.Context, req *api.PerformUploadKeysRequest, res *api.PerformUploadKeysResponse) {
|
|
|
|
for _, key := range req.OneTimeKeys {
|
|
|
|
// grab existing keys based on (user/device/algorithm/key ID)
|
|
|
|
keyIDsWithAlgorithms := make([]string, len(key.KeyJSON))
|
|
|
|
i := 0
|
|
|
|
for keyIDWithAlgo := range key.KeyJSON {
|
|
|
|
keyIDsWithAlgorithms[i] = keyIDWithAlgo
|
|
|
|
i++
|
|
|
|
}
|
2020-07-15 12:02:34 +01:00
|
|
|
existingKeys, err := a.DB.ExistingOneTimeKeys(ctx, key.UserID, key.DeviceID, keyIDsWithAlgorithms)
|
2020-07-14 12:59:07 +01:00
|
|
|
if err != nil {
|
|
|
|
res.KeyError(key.UserID, key.DeviceID, &api.KeyError{
|
2020-07-15 12:02:34 +01:00
|
|
|
Err: "failed to query existing one-time keys: " + err.Error(),
|
2020-07-14 12:59:07 +01:00
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for keyIDWithAlgo := range existingKeys {
|
|
|
|
// if keys exist and the JSON doesn't match, error out as the key already exists
|
|
|
|
if !bytes.Equal(existingKeys[keyIDWithAlgo], key.KeyJSON[keyIDWithAlgo]) {
|
|
|
|
res.KeyError(key.UserID, key.DeviceID, &api.KeyError{
|
2020-07-15 12:02:34 +01:00
|
|
|
Err: fmt.Sprintf("%s device %s: algorithm / key ID %s one-time key already exists", key.UserID, key.DeviceID, keyIDWithAlgo),
|
2020-07-14 12:59:07 +01:00
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// store one-time keys
|
2020-07-15 12:02:34 +01:00
|
|
|
counts, err := a.DB.StoreOneTimeKeys(ctx, key)
|
|
|
|
if err != nil {
|
2020-07-14 12:59:07 +01:00
|
|
|
res.KeyError(key.UserID, key.DeviceID, &api.KeyError{
|
2020-07-15 12:02:34 +01:00
|
|
|
Err: fmt.Sprintf("%s device %s : failed to store one-time keys: %s", key.UserID, key.DeviceID, err.Error()),
|
2020-07-14 12:59:07 +01:00
|
|
|
})
|
2020-07-15 12:02:34 +01:00
|
|
|
continue
|
2020-07-14 12:59:07 +01:00
|
|
|
}
|
2020-07-15 12:02:34 +01:00
|
|
|
// collect counts
|
|
|
|
res.OneTimeKeyCounts = append(res.OneTimeKeyCounts, *counts)
|
2020-07-14 12:59:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-03 17:07:06 +01:00
|
|
|
func (a *KeyInternalAPI) emitDeviceKeyChanges(existing, new []api.DeviceMessage) error {
|
2020-07-23 16:41:36 +01:00
|
|
|
// find keys in new that are not in existing
|
2020-08-03 17:07:06 +01:00
|
|
|
var keysAdded []api.DeviceMessage
|
2020-07-23 16:41:36 +01:00
|
|
|
for _, newKey := range new {
|
|
|
|
exists := false
|
|
|
|
for _, existingKey := range existing {
|
2020-07-30 18:00:56 +01:00
|
|
|
// Do not treat the absence of keys as equal, or else we will not emit key changes
|
|
|
|
// when users delete devices which never had a key to begin with as both KeyJSONs are nil.
|
|
|
|
if bytes.Equal(existingKey.KeyJSON, newKey.KeyJSON) && len(existingKey.KeyJSON) > 0 {
|
2020-07-23 16:41:36 +01:00
|
|
|
exists = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
keysAdded = append(keysAdded, newKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a.Producer.ProduceKeyChanges(keysAdded)
|
2020-07-14 12:59:07 +01:00
|
|
|
}
|