Fix UserExists flag, device check

This commit is contained in:
Neil Alexander 2022-02-16 13:11:51 +00:00
parent fee6d342d2
commit 1357ae5248
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
2 changed files with 11 additions and 16 deletions

View file

@ -558,6 +558,12 @@ func (a *KeyInternalAPI) uploadLocalDeviceKeys(ctx context.Context, req *api.Per
} }
return return
} }
if !uapidevices.UserExists {
res.Error = &api.KeyError{
Err: fmt.Sprintf("user %q does not exist", req.UserID),
}
return
}
// Get all of the user existing device keys so we can check for changes. // Get all of the user existing device keys so we can check for changes.
existingKeys, err := a.DB.DeviceKeysForUser(ctx, req.UserID, nil) existingKeys, err := a.DB.DeviceKeysForUser(ctx, req.UserID, nil)
@ -568,8 +574,8 @@ func (a *KeyInternalAPI) uploadLocalDeviceKeys(ctx context.Context, req *api.Per
return return
} }
existingDeviceMap := make(map[string]struct{}, len(existingKeys)) existingDeviceMap := make(map[string]struct{}, len(existingKeys))
for _, k := range existingKeys { for _, key := range uapidevices.Devices {
existingDeviceMap[k.DeviceID] = struct{}{} existingDeviceMap[key.ID] = struct{}{}
} }
// Work out whether we have device keys in the keyserver for devices that // Work out whether we have device keys in the keyserver for devices that
@ -577,13 +583,7 @@ func (a *KeyInternalAPI) uploadLocalDeviceKeys(ctx context.Context, req *api.Per
// that we keep some integrity between the two. // that we keep some integrity between the two.
var toClean []gomatrixserverlib.KeyID var toClean []gomatrixserverlib.KeyID
for _, k := range existingKeys { for _, k := range existingKeys {
found := false if _, ok := existingDeviceMap[k.DeviceID]; !ok {
for _, d := range uapidevices.Devices {
if k.UserID == d.UserID && k.DeviceID == d.ID {
found = true
}
}
if !found {
toClean = append(toClean, gomatrixserverlib.KeyID(k.DeviceID)) toClean = append(toClean, gomatrixserverlib.KeyID(k.DeviceID))
} }
} }
@ -613,13 +613,7 @@ func (a *KeyInternalAPI) uploadLocalDeviceKeys(ctx context.Context, req *api.Per
} }
// check that the device in question actually exists in the user // check that the device in question actually exists in the user
// API before we try and store a key for it // API before we try and store a key for it
foundDevice := false if _, ok := existingDeviceMap[key.DeviceID]; !ok {
for _, d := range uapidevices.Devices {
if d.ID == key.DeviceID {
foundDevice = true
}
}
if !foundDevice {
continue continue
} }
gotUserID := gjson.GetBytes(key.KeyJSON, "user_id").Str gotUserID := gjson.GetBytes(key.KeyJSON, "user_id").Str

View file

@ -315,6 +315,7 @@ func (a *UserInternalAPI) QueryDevices(ctx context.Context, req *api.QueryDevice
if err != nil { if err != nil {
return err return err
} }
res.UserExists = true
res.Devices = devs res.Devices = devs
return nil return nil
} }