Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Add cross-signing sigs to the keys object
Browse files Browse the repository at this point in the history
All the callers want this info in the same place, so let's reduce the
duplication by doing it here.
  • Loading branch information
richvdh committed Sep 2, 2020
1 parent bbb0cfd commit bd18b8f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 33 deletions.
1 change: 1 addition & 0 deletions changelog.d/8234.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactor queries for device keys and cross-signatures.
12 changes: 3 additions & 9 deletions synapse/storage/databases/main/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,9 @@ async def _get_device_update_edus_by_remote(
prev_id = stream_id

if device is not None:
key_json = device.key_json
if key_json:
result["keys"] = db_to_json(key_json)

if device.signatures:
for sig_user_id, sigs in device.signatures.items():
result["keys"].setdefault("signatures", {}).setdefault(
sig_user_id, {}
).update(sigs)
keys = device.keys
if keys:
result["keys"] = keys

device_display_name = device.display_name
if device_display_name:
Expand Down
38 changes: 14 additions & 24 deletions synapse/storage/databases/main/end_to_end_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,8 @@ class DeviceKeyLookupResult:

# the key data from e2e_device_keys_json. Typically includes fields like
# "algorithm", "keys" (including the curve25519 identity key and the ed25519 signing
# key) and "signatures" (a signature of the structure by the ed25519 key)
key_json = attr.ib(type=Optional[str])

# cross-signing sigs on this device.
# dict from (signing user_id)->(signing device_id)->sig
signatures = attr.ib(type=Optional[Dict[str, Dict[str, str]]], factory=dict)
# key) and signatures" (a map from (user id) to (key id/device_id) to signature.)
keys = attr.ib(type=Optional[JsonDict])


class EndToEndKeyWorkerStore(SQLBaseStore):
Expand All @@ -70,15 +66,9 @@ async def get_e2e_device_keys_for_federation_query(
for device_id, device in user_devices.items():
result = {"device_id": device_id}

key_json = device.key_json
if key_json:
result["keys"] = db_to_json(key_json)

if device.signatures:
for sig_user_id, sigs in device.signatures.items():
result["keys"].setdefault("signatures", {}).setdefault(
sig_user_id, {}
).update(sigs)
keys = device.keys
if keys:
result["keys"] = keys

device_display_name = device.display_name
if device_display_name:
Expand Down Expand Up @@ -114,16 +104,11 @@ async def get_e2e_device_keys_for_cs_api(
for user_id, device_keys in results.items():
rv[user_id] = {}
for device_id, device_info in device_keys.items():
r = db_to_json(device_info.key_json)
r = device_info.keys
r["unsigned"] = {}
display_name = device_info.display_name
if display_name is not None:
r["unsigned"]["device_display_name"] = display_name
if device_info.signatures:
for sig_user_id, sigs in device_info.signatures.items():
r.setdefault("signatures", {}).setdefault(
sig_user_id, {}
).update(sigs)
rv[user_id][device_id] = r

return rv
Expand All @@ -137,6 +122,9 @@ async def get_e2e_device_keys_and_signatures(
) -> Dict[str, Dict[str, Optional[DeviceKeyLookupResult]]]:
"""Fetch a list of device keys, together with their cross-signatures.
The cross-signatures are added to the `signatures` field within the `keys`
object in the response.
Args:
query_list: List of pairs of user_ids and device_ids. Device id can be None
to indicate "all devices for this user"
Expand Down Expand Up @@ -167,7 +155,7 @@ async def get_e2e_device_keys_and_signatures(
(user_id, device_id)
for user_id, dev in result.items()
for device_id, d in dev.items()
if d is not None
if d is not None and d.keys is not None
)

for batch in batch_iter(signature_query, 50):
Expand All @@ -186,7 +174,9 @@ async def get_e2e_device_keys_and_signatures(
signature = row["signature"]

target_device_result = result[target_user_id][target_device_id]
target_device_signatures = target_device_result.signatures
target_device_signatures = target_device_result.keys.setdefault(
"signatures", {}
)

signing_user_signatures = target_device_signatures.setdefault(
signing_user_id, {}
Expand Down Expand Up @@ -243,7 +233,7 @@ def _get_e2e_device_keys_txn(
if include_deleted_devices:
deleted_devices.remove((user_id, device_id))
result.setdefault(user_id, {})[device_id] = DeviceKeyLookupResult(
display_name, key_json
display_name, db_to_json(key_json) if key_json else None
)

if include_deleted_devices:
Expand Down

0 comments on commit bd18b8f

Please sign in to comment.