2020-08-31 14:11:08 +02: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.
|
|
|
|
*/
|
|
|
|
|
2023-03-07 10:53:32 +01:00
|
|
|
import {verifyEd25519Signature, getEd25519Signature, SIGNATURE_ALGORITHM, SignatureVerification} from "./common";
|
2023-02-27 18:13:53 +01:00
|
|
|
import {HistoryVisibility, shouldShareKey, DeviceKey, getDeviceEd25519Key, getDeviceCurve25519Key} from "./common";
|
2022-07-22 17:49:26 +02:00
|
|
|
import {RoomMember} from "../room/members/RoomMember.js";
|
2023-02-24 17:45:56 +01:00
|
|
|
import {getKeyUsage, getKeyEd25519Key, getKeyUserId, KeyUsage} from "../verification/CrossSigning";
|
2023-02-27 18:13:53 +01:00
|
|
|
import {MemberChange} from "../room/members/RoomMember";
|
2023-03-02 15:02:42 +01:00
|
|
|
import type {CrossSigningKey} from "../verification/CrossSigning";
|
2023-02-27 18:13:53 +01:00
|
|
|
import type {HomeServerApi} from "../net/HomeServerApi";
|
|
|
|
import type {ObservableMap} from "../../observable/map";
|
|
|
|
import type {Room} from "../room/Room";
|
|
|
|
import type {ILogItem} from "../../logging/types";
|
|
|
|
import type {Storage} from "../storage/idb/Storage";
|
|
|
|
import type {Transaction} from "../storage/idb/Transaction";
|
|
|
|
import type * as OlmNamespace from "@matrix-org/olm";
|
|
|
|
type Olm = typeof OlmNamespace;
|
2020-08-31 14:11:08 +02:00
|
|
|
|
2023-03-03 11:34:09 +01:00
|
|
|
// tracking status for cross-signing and device keys
|
|
|
|
export enum KeysTrackingStatus {
|
2023-03-02 17:32:46 +01:00
|
|
|
Outdated = 0,
|
|
|
|
UpToDate = 1
|
|
|
|
}
|
2020-08-31 14:11:08 +02:00
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
export type UserIdentity = {
|
|
|
|
userId: string,
|
|
|
|
roomIds: string[],
|
2023-03-03 11:34:09 +01:00
|
|
|
keysTrackingStatus: KeysTrackingStatus,
|
2023-02-27 18:13:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function createUserIdentity(userId: string, initialRoomId?: string): UserIdentity {
|
2022-11-10 17:28:18 +01:00
|
|
|
return {
|
|
|
|
userId: userId,
|
|
|
|
roomIds: initialRoomId ? [initialRoomId] : [],
|
2023-03-03 11:34:09 +01:00
|
|
|
keysTrackingStatus: KeysTrackingStatus.Outdated,
|
2022-11-10 17:28:18 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
function addRoomToIdentity(identity: UserIdentity | undefined, userId: string, roomId: string): UserIdentity | undefined {
|
2021-08-27 19:39:24 +02:00
|
|
|
if (!identity) {
|
2022-11-10 17:28:18 +01:00
|
|
|
identity = createUserIdentity(userId, roomId);
|
2021-08-27 19:39:24 +02:00
|
|
|
return identity;
|
|
|
|
} else {
|
|
|
|
if (!identity.roomIds.includes(roomId)) {
|
|
|
|
identity.roomIds.push(roomId);
|
|
|
|
return identity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 14:11:08 +02:00
|
|
|
export class DeviceTracker {
|
2023-02-27 18:13:53 +01:00
|
|
|
private readonly _storage: Storage;
|
|
|
|
private readonly _getSyncToken: () => string;
|
|
|
|
private readonly _olmUtil: Olm.Utility;
|
|
|
|
private readonly _ownUserId: string;
|
|
|
|
private readonly _ownDeviceId: string;
|
|
|
|
|
|
|
|
constructor(options: {storage: Storage, getSyncToken: () => string, olmUtil: Olm.Utility, ownUserId: string, ownDeviceId: string}) {
|
|
|
|
this._storage = options.storage;
|
|
|
|
this._getSyncToken = options.getSyncToken;
|
|
|
|
this._olmUtil = options.olmUtil;
|
|
|
|
this._ownUserId = options.ownUserId;
|
|
|
|
this._ownDeviceId = options.ownDeviceId;
|
2020-08-31 14:11:08 +02:00
|
|
|
}
|
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
async writeDeviceChanges(changedUserIds: ReadonlyArray<string>, txn: Transaction, log: ILogItem): Promise<void> {
|
2020-08-31 14:11:08 +02:00
|
|
|
const {userIdentities} = txn;
|
2021-02-11 21:08:06 +01:00
|
|
|
// TODO: should we also look at left here to handle this?:
|
|
|
|
// the usual problem here is that you share a room with a user,
|
|
|
|
// go offline, the remote user leaves the room, changes their devices,
|
|
|
|
// then rejoins the room you share (or another room).
|
|
|
|
// At which point you come online, all of this happens in the gap,
|
|
|
|
// and you don't notice that they ever left,
|
|
|
|
// and so the client doesn't invalidate their device cache for the user
|
2023-02-27 18:13:53 +01:00
|
|
|
log.set("changed", changedUserIds.length);
|
|
|
|
await Promise.all(changedUserIds.map(async userId => {
|
2021-02-18 19:56:10 +01:00
|
|
|
const user = await userIdentities.get(userId);
|
|
|
|
if (user) {
|
|
|
|
log.log({l: "outdated", id: userId});
|
2023-03-03 11:34:09 +01:00
|
|
|
user.keysTrackingStatus = KeysTrackingStatus.Outdated;
|
2021-02-18 19:56:10 +01:00
|
|
|
userIdentities.set(user);
|
|
|
|
}
|
|
|
|
}));
|
2020-08-31 14:11:08 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 17:48:26 +02:00
|
|
|
/** @return Promise<{added: string[], removed: string[]}> the user ids for who the room was added or removed to the userIdentity,
|
|
|
|
* and with who a key should be now be shared
|
|
|
|
**/
|
2023-02-27 18:13:53 +01:00
|
|
|
async writeMemberChanges(room: Room, memberChanges: Map<string, MemberChange>, historyVisibility: HistoryVisibility, txn: Transaction): Promise<{added: string[], removed: string[]}> {
|
|
|
|
const added: string[] = [];
|
|
|
|
const removed: string[] = [];
|
2022-07-22 17:48:26 +02:00
|
|
|
await Promise.all(Array.from(memberChanges.values()).map(async memberChange => {
|
|
|
|
// keys should now be shared with this member?
|
|
|
|
// add the room to the userIdentity if so
|
|
|
|
if (shouldShareKey(memberChange.membership, historyVisibility)) {
|
|
|
|
if (await this._addRoomToUserIdentity(memberChange.roomId, memberChange.userId, txn)) {
|
|
|
|
added.push(memberChange.userId);
|
|
|
|
}
|
2022-07-26 16:58:07 +02:00
|
|
|
} else if (shouldShareKey(memberChange.previousMembership, historyVisibility)) {
|
|
|
|
// try to remove room we were previously sharing the key with the member but not anymore
|
2022-07-22 17:48:26 +02:00
|
|
|
const {roomId} = memberChange;
|
|
|
|
// if we left the room, remove room from all user identities in the room
|
|
|
|
if (memberChange.userId === this._ownUserId) {
|
|
|
|
const userIds = await txn.roomMembers.getAllUserIds(roomId);
|
|
|
|
await Promise.all(userIds.map(userId => {
|
|
|
|
return this._removeRoomFromUserIdentity(roomId, userId, txn);
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
await this._removeRoomFromUserIdentity(roomId, memberChange.userId, txn);
|
|
|
|
}
|
|
|
|
removed.push(memberChange.userId);
|
|
|
|
}
|
2020-08-31 14:11:08 +02:00
|
|
|
}));
|
2022-07-22 17:48:26 +02:00
|
|
|
return {added, removed};
|
2020-08-31 14:11:08 +02:00
|
|
|
}
|
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
async trackRoom(room: Room, historyVisibility: HistoryVisibility, log: ILogItem): Promise<void> {
|
2020-09-08 14:24:48 +02:00
|
|
|
if (room.isTrackingMembers || !room.isEncrypted) {
|
2020-08-31 14:11:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-07-27 11:39:50 +02:00
|
|
|
const memberList = await room.loadMemberList(undefined, log);
|
2022-07-26 16:53:02 +02:00
|
|
|
const txn = await this._storage.readWriteTxn([
|
|
|
|
this._storage.storeNames.roomSummary,
|
|
|
|
this._storage.storeNames.userIdentities,
|
2023-02-27 18:13:53 +01:00
|
|
|
this._storage.storeNames.deviceKeys, // to remove all devices in _removeRoomFromUserIdentity
|
2022-07-26 16:53:02 +02:00
|
|
|
]);
|
2020-08-31 14:11:08 +02:00
|
|
|
try {
|
|
|
|
let isTrackingChanges;
|
|
|
|
try {
|
|
|
|
isTrackingChanges = room.writeIsTrackingMembers(true, txn);
|
2023-02-27 18:13:53 +01:00
|
|
|
const members = Array.from((memberList.members as ObservableMap<string, RoomMember>).values());
|
2021-02-23 19:22:59 +01:00
|
|
|
log.set("members", members.length);
|
2022-11-10 17:26:50 +01:00
|
|
|
// TODO: should we remove any userIdentities we should not share the key with??
|
|
|
|
// e.g. as an extra security measure if we had a mistake in other code?
|
2022-07-22 17:48:26 +02:00
|
|
|
await Promise.all(members.map(async member => {
|
|
|
|
if (shouldShareKey(member.membership, historyVisibility)) {
|
|
|
|
await this._addRoomToUserIdentity(member.roomId, member.userId, txn);
|
2022-11-10 17:26:50 +01:00
|
|
|
} else {
|
|
|
|
await this._removeRoomFromUserIdentity(member.roomId, member.userId, txn);
|
2022-07-22 17:48:26 +02:00
|
|
|
}
|
|
|
|
}));
|
2020-08-31 14:11:08 +02:00
|
|
|
} catch (err) {
|
|
|
|
txn.abort();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
await txn.complete();
|
|
|
|
room.applyIsTrackingMembersChanges(isTrackingChanges);
|
|
|
|
} finally {
|
|
|
|
memberList.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 13:42:19 +01:00
|
|
|
async getCrossSigningKeyForUser(userId: string, usage: KeyUsage, hsApi: HomeServerApi | undefined, log: ILogItem): Promise<CrossSigningKey | undefined> {
|
2023-02-27 18:13:53 +01:00
|
|
|
return await log.wrap({l: "DeviceTracker.getCrossSigningKeyForUser", id: userId, usage}, async log => {
|
2023-03-24 13:42:19 +01:00
|
|
|
const txn = await this._storage.readTxn([
|
2023-02-27 18:13:53 +01:00
|
|
|
this._storage.storeNames.userIdentities,
|
|
|
|
this._storage.storeNames.crossSigningKeys,
|
2023-02-14 12:11:59 +01:00
|
|
|
]);
|
2023-03-13 09:15:49 +01:00
|
|
|
const userIdentity = await txn.userIdentities.get(userId);
|
2023-03-03 11:34:09 +01:00
|
|
|
if (userIdentity && userIdentity.keysTrackingStatus !== KeysTrackingStatus.Outdated) {
|
2023-02-27 18:13:53 +01:00
|
|
|
return await txn.crossSigningKeys.get(userId, usage);
|
2023-02-14 16:26:55 +01:00
|
|
|
}
|
2023-03-13 09:15:49 +01:00
|
|
|
// not allowed to access the network, bail out
|
|
|
|
if (!hsApi) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2023-02-14 12:11:59 +01:00
|
|
|
// fetch from hs
|
2023-02-24 17:45:56 +01:00
|
|
|
const keys = await this._queryKeys([userId], hsApi, log);
|
|
|
|
switch (usage) {
|
|
|
|
case KeyUsage.Master:
|
|
|
|
return keys.masterKeys.get(userId);
|
|
|
|
case KeyUsage.SelfSigning:
|
|
|
|
return keys.selfSigningKeys.get(userId);
|
|
|
|
case KeyUsage.UserSigning:
|
|
|
|
return keys.userSigningKeys.get(userId);
|
|
|
|
}
|
2023-02-14 12:11:59 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
async writeHistoryVisibility(room: Room, historyVisibility: HistoryVisibility, syncTxn: Transaction, log: ILogItem): Promise<{added: string[], removed: string[]}> {
|
|
|
|
const added: string[] = [];
|
|
|
|
const removed: string[] = [];
|
2022-07-22 17:49:26 +02:00
|
|
|
if (room.isTrackingMembers && room.isEncrypted) {
|
|
|
|
await log.wrap("rewriting userIdentities", async log => {
|
2023-02-27 18:13:53 +01:00
|
|
|
// TODO: how do we know that we won't fetch the members from the server here and hence close the syncTxn?
|
2022-07-26 16:57:28 +02:00
|
|
|
const memberList = await room.loadMemberList(syncTxn, log);
|
|
|
|
try {
|
2023-02-27 18:13:53 +01:00
|
|
|
const members = Array.from((memberList.members as ObservableMap<string, RoomMember>).values());
|
2022-07-26 16:57:28 +02:00
|
|
|
log.set("members", members.length);
|
|
|
|
await Promise.all(members.map(async member => {
|
|
|
|
if (shouldShareKey(member.membership, historyVisibility)) {
|
|
|
|
if (await this._addRoomToUserIdentity(member.roomId, member.userId, syncTxn)) {
|
|
|
|
added.push(member.userId);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (await this._removeRoomFromUserIdentity(member.roomId, member.userId, syncTxn)) {
|
|
|
|
removed.push(member.userId);
|
|
|
|
}
|
2022-07-22 17:49:26 +02:00
|
|
|
}
|
2022-07-26 16:57:28 +02:00
|
|
|
}));
|
|
|
|
} finally {
|
|
|
|
memberList.release();
|
|
|
|
}
|
2022-07-22 17:49:26 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return {added, removed};
|
2020-08-31 14:11:08 +02:00
|
|
|
}
|
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
async _addRoomToUserIdentity(roomId: string, userId: string, txn: Transaction): Promise<boolean> {
|
2020-08-31 14:11:08 +02:00
|
|
|
const {userIdentities} = txn;
|
2022-07-22 17:48:26 +02:00
|
|
|
const identity = await userIdentities.get(userId);
|
|
|
|
const updatedIdentity = addRoomToIdentity(identity, userId, roomId);
|
2021-08-30 15:05:57 +02:00
|
|
|
if (updatedIdentity) {
|
|
|
|
userIdentities.set(updatedIdentity);
|
2022-07-22 17:48:26 +02:00
|
|
|
return true;
|
2020-08-31 14:11:08 +02:00
|
|
|
}
|
2022-07-22 17:48:26 +02:00
|
|
|
return false;
|
2020-08-31 14:11:08 +02:00
|
|
|
}
|
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
async _removeRoomFromUserIdentity(roomId: string, userId: string, txn: Transaction): Promise<boolean> {
|
|
|
|
const {userIdentities, deviceKeys} = txn;
|
2021-05-06 15:23:58 +02:00
|
|
|
const identity = await userIdentities.get(userId);
|
|
|
|
if (identity) {
|
|
|
|
identity.roomIds = identity.roomIds.filter(id => id !== roomId);
|
|
|
|
// no more encrypted rooms with this user, remove
|
|
|
|
if (identity.roomIds.length === 0) {
|
|
|
|
userIdentities.remove(userId);
|
2023-02-27 18:13:53 +01:00
|
|
|
deviceKeys.removeAllForUser(userId);
|
2021-05-06 15:23:58 +02:00
|
|
|
} else {
|
|
|
|
userIdentities.set(identity);
|
|
|
|
}
|
2022-07-22 17:48:26 +02:00
|
|
|
return true;
|
2021-05-06 15:23:58 +02:00
|
|
|
}
|
2022-07-22 17:48:26 +02:00
|
|
|
return false;
|
2020-08-31 14:11:08 +02:00
|
|
|
}
|
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
async _queryKeys(userIds: string[], hsApi: HomeServerApi, log: ILogItem): Promise<{
|
|
|
|
deviceKeys: Map<string, DeviceKey[]>,
|
|
|
|
masterKeys: Map<string, CrossSigningKey>,
|
|
|
|
selfSigningKeys: Map<string, CrossSigningKey>,
|
|
|
|
userSigningKeys: Map<string, CrossSigningKey>
|
|
|
|
}> {
|
2020-08-31 14:11:08 +02:00
|
|
|
// TODO: we need to handle the race here between /sync and /keys/query just like we need to do for the member list ...
|
|
|
|
// there are multiple requests going out for /keys/query though and only one for /members
|
2022-11-10 17:20:59 +01:00
|
|
|
// So, while doing /keys/query, writeDeviceChanges should add userIds marked as outdated to a list
|
|
|
|
// when /keys/query returns, we should check that list and requery if we queried for a given user.
|
|
|
|
// and then remove the list.
|
2020-08-31 14:11:08 +02:00
|
|
|
|
2020-08-31 14:24:09 +02:00
|
|
|
const deviceKeyResponse = await hsApi.queryKeys({
|
2020-08-31 14:11:08 +02:00
|
|
|
"timeout": 10000,
|
|
|
|
"device_keys": userIds.reduce((deviceKeysMap, userId) => {
|
|
|
|
deviceKeysMap[userId] = [];
|
|
|
|
return deviceKeysMap;
|
|
|
|
}, {}),
|
|
|
|
"token": this._getSyncToken()
|
2021-02-23 19:22:59 +01:00
|
|
|
}, {log}).response();
|
2020-08-31 14:11:08 +02:00
|
|
|
|
2023-03-07 11:00:52 +01:00
|
|
|
const masterKeys = log.wrap("master keys", log => this._filterVerifiedCrossSigningKeys(deviceKeyResponse["master_keys"], KeyUsage.Master, log));
|
|
|
|
const selfSigningKeys = log.wrap("self-signing keys", log => this._filterVerifiedCrossSigningKeys(deviceKeyResponse["self_signing_keys"], KeyUsage.SelfSigning, log));
|
|
|
|
const userSigningKeys = log.wrap("user-signing keys", log => this._filterVerifiedCrossSigningKeys(deviceKeyResponse["user_signing_keys"], KeyUsage.UserSigning, log));
|
2023-02-27 18:13:53 +01:00
|
|
|
const deviceKeys = log.wrap("device keys", log => this._filterVerifiedDeviceKeys(deviceKeyResponse["device_keys"], log));
|
2021-03-04 19:47:02 +01:00
|
|
|
const txn = await this._storage.readWriteTxn([
|
2020-08-31 14:11:08 +02:00
|
|
|
this._storage.storeNames.userIdentities,
|
2023-02-27 18:13:53 +01:00
|
|
|
this._storage.storeNames.deviceKeys,
|
2023-02-24 17:45:56 +01:00
|
|
|
this._storage.storeNames.crossSigningKeys,
|
2020-08-31 14:11:08 +02:00
|
|
|
]);
|
|
|
|
let deviceIdentities;
|
|
|
|
try {
|
2023-02-24 17:45:56 +01:00
|
|
|
for (const key of masterKeys.values()) {
|
|
|
|
txn.crossSigningKeys.set(key);
|
|
|
|
}
|
|
|
|
for (const key of selfSigningKeys.values()) {
|
|
|
|
txn.crossSigningKeys.set(key);
|
|
|
|
}
|
|
|
|
for (const key of userSigningKeys.values()) {
|
|
|
|
txn.crossSigningKeys.set(key);
|
|
|
|
}
|
2023-02-27 18:13:53 +01:00
|
|
|
let totalCount = 0;
|
|
|
|
await Promise.all(Array.from(deviceKeys.keys()).map(async (userId) => {
|
|
|
|
let deviceKeysForUser = deviceKeys.get(userId)!;
|
|
|
|
totalCount += deviceKeysForUser.length;
|
|
|
|
// check for devices that changed their keys and keep the old key
|
|
|
|
deviceKeysForUser = await this._storeQueriedDevicesForUserId(userId, deviceKeysForUser, txn);
|
|
|
|
deviceKeys.set(userId, deviceKeysForUser);
|
2020-08-31 14:11:08 +02:00
|
|
|
}));
|
2023-02-27 18:13:53 +01:00
|
|
|
log.set("devices", totalCount);
|
2020-08-31 14:11:08 +02:00
|
|
|
} catch (err) {
|
|
|
|
txn.abort();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
await txn.complete();
|
2023-02-24 17:45:56 +01:00
|
|
|
return {
|
2023-02-27 18:13:53 +01:00
|
|
|
deviceKeys,
|
2023-02-24 17:45:56 +01:00
|
|
|
masterKeys,
|
|
|
|
selfSigningKeys,
|
|
|
|
userSigningKeys
|
|
|
|
};
|
2020-08-31 14:11:08 +02:00
|
|
|
}
|
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
async _storeQueriedDevicesForUserId(userId: string, deviceKeys: DeviceKey[], txn: Transaction): Promise<DeviceKey[]> {
|
|
|
|
// TODO: we should obsolete (flag) the device keys that have been removed,
|
|
|
|
// but keep them to verify messages encrypted with it?
|
|
|
|
const knownDeviceIds = await txn.deviceKeys.getAllDeviceIds(userId);
|
2020-09-14 15:44:47 +02:00
|
|
|
// delete any devices that we know off but are not in the response anymore.
|
|
|
|
// important this happens before checking if the ed25519 key changed,
|
|
|
|
// otherwise we would end up deleting existing devices with changed keys.
|
|
|
|
for (const deviceId of knownDeviceIds) {
|
2023-02-27 18:13:53 +01:00
|
|
|
if (deviceKeys.every(di => di.device_id !== deviceId)) {
|
|
|
|
txn.deviceKeys.remove(userId, deviceId);
|
2020-09-14 15:44:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// all the device identities as we will have them in storage
|
2023-02-27 18:13:53 +01:00
|
|
|
const allDeviceKeys: DeviceKey[] = [];
|
|
|
|
const deviceKeysToStore: DeviceKey[] = [];
|
2020-09-14 15:44:47 +02:00
|
|
|
// filter out devices that have changed their ed25519 key since last time we queried them
|
2023-02-27 18:13:53 +01:00
|
|
|
await Promise.all(deviceKeys.map(async deviceKey => {
|
|
|
|
if (knownDeviceIds.includes(deviceKey.device_id)) {
|
|
|
|
const existingDevice = await txn.deviceKeys.get(deviceKey.user_id, deviceKey.device_id);
|
|
|
|
if (existingDevice && getDeviceEd25519Key(existingDevice) !== getDeviceEd25519Key(deviceKey)) {
|
|
|
|
allDeviceKeys.push(existingDevice);
|
2022-05-31 13:39:35 +02:00
|
|
|
return;
|
2020-09-14 15:44:47 +02:00
|
|
|
}
|
|
|
|
}
|
2023-02-27 18:13:53 +01:00
|
|
|
allDeviceKeys.push(deviceKey);
|
|
|
|
deviceKeysToStore.push(deviceKey);
|
2020-09-14 15:44:47 +02:00
|
|
|
}));
|
|
|
|
// store devices
|
2023-02-27 18:13:53 +01:00
|
|
|
for (const deviceKey of deviceKeysToStore) {
|
|
|
|
txn.deviceKeys.set(deviceKey);
|
2020-09-14 15:44:47 +02:00
|
|
|
}
|
|
|
|
// mark user identities as up to date
|
2022-11-10 17:28:18 +01:00
|
|
|
let identity = await txn.userIdentities.get(userId);
|
|
|
|
if (!identity) {
|
|
|
|
// create the identity if it doesn't exist, which can happen if
|
|
|
|
// we request devices before tracking the room.
|
|
|
|
// IMPORTANT here that the identity gets created without any roomId!
|
|
|
|
// if we claim that we share and e2ee room with the user without having
|
|
|
|
// checked, we could share keys with that user without them being in the room
|
|
|
|
identity = createUserIdentity(userId);
|
|
|
|
}
|
2023-03-03 11:34:09 +01:00
|
|
|
identity.keysTrackingStatus = KeysTrackingStatus.UpToDate;
|
2020-09-14 15:44:47 +02:00
|
|
|
txn.userIdentities.set(identity);
|
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
return allDeviceKeys;
|
2020-09-14 15:44:47 +02:00
|
|
|
}
|
|
|
|
|
2023-03-07 11:00:52 +01:00
|
|
|
_filterVerifiedCrossSigningKeys(crossSigningKeysResponse: {[userId: string]: CrossSigningKey}, usage: KeyUsage, log: ILogItem): Map<string, CrossSigningKey> {
|
2023-02-27 18:13:53 +01:00
|
|
|
const keys: Map<string, CrossSigningKey> = new Map();
|
2023-02-14 16:26:55 +01:00
|
|
|
if (!crossSigningKeysResponse) {
|
|
|
|
return keys;
|
|
|
|
}
|
2023-02-24 17:45:56 +01:00
|
|
|
for (const [userId, keyInfo] of Object.entries(crossSigningKeysResponse)) {
|
|
|
|
log.wrap({l: userId}, log => {
|
2023-03-07 11:00:52 +01:00
|
|
|
if (this._validateCrossSigningKey(userId, keyInfo, usage, log)) {
|
2023-02-27 18:13:53 +01:00
|
|
|
keys.set(getKeyUserId(keyInfo)!, keyInfo);
|
2023-02-24 17:45:56 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
2023-03-07 11:00:52 +01:00
|
|
|
_validateCrossSigningKey(userId: string, keyInfo: CrossSigningKey, usage: KeyUsage, log: ILogItem): boolean {
|
2023-02-24 17:45:56 +01:00
|
|
|
if (getKeyUserId(keyInfo) !== userId) {
|
|
|
|
log.log({l: "user_id mismatch", userId: keyInfo["user_id"]});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (getKeyUsage(keyInfo) !== usage) {
|
|
|
|
log.log({l: "usage mismatch", usage: keyInfo.usage});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const publicKey = getKeyEd25519Key(keyInfo);
|
|
|
|
if (!publicKey) {
|
|
|
|
log.log({l: "no ed25519 key", keys: keyInfo.keys});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2023-02-14 16:26:55 +01:00
|
|
|
}
|
|
|
|
|
2020-09-14 15:44:47 +02:00
|
|
|
/**
|
|
|
|
* @return {Array<{userId, verifiedKeys: Array<DeviceSection>>}
|
|
|
|
*/
|
2023-02-27 18:13:53 +01:00
|
|
|
_filterVerifiedDeviceKeys(
|
|
|
|
keyQueryDeviceKeysResponse: {[userId: string]: {[deviceId: string]: DeviceKey}},
|
|
|
|
parentLog: ILogItem
|
|
|
|
): Map<string, DeviceKey[]> {
|
|
|
|
const curve25519Keys: Set<string> = new Set();
|
|
|
|
const keys: Map<string, DeviceKey[]> = new Map();
|
|
|
|
if (!keyQueryDeviceKeysResponse) {
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
for (const [userId, keysByDevice] of Object.entries(keyQueryDeviceKeysResponse)) {
|
|
|
|
parentLog.wrap(userId, log => {
|
|
|
|
const verifiedEntries = Object.entries(keysByDevice).filter(([deviceId, deviceKey]) => {
|
|
|
|
return log.wrap(deviceId, log => {
|
|
|
|
if (this._validateDeviceKey(userId, deviceId, deviceKey, log)) {
|
|
|
|
const curve25519Key = getDeviceCurve25519Key(deviceKey);
|
|
|
|
if (curve25519Keys.has(curve25519Key)) {
|
|
|
|
parentLog.log({
|
|
|
|
l: "ignore device with duplicate curve25519 key",
|
|
|
|
keys: deviceKey
|
|
|
|
}, parentLog.level.Warn);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
curve25519Keys.add(curve25519Key);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const verifiedKeys = verifiedEntries.map(([, deviceKeys]) => deviceKeys);
|
|
|
|
keys.set(userId, verifiedKeys);
|
2020-08-31 14:11:08 +02:00
|
|
|
});
|
2023-02-27 18:13:53 +01:00
|
|
|
}
|
|
|
|
return keys;
|
2020-08-31 14:11:08 +02:00
|
|
|
}
|
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
_validateDeviceKey(userIdFromServer: string, deviceIdFromServer: string, deviceKey: DeviceKey, log: ILogItem): boolean {
|
|
|
|
const deviceId = deviceKey["device_id"];
|
|
|
|
const userId = deviceKey["user_id"];
|
|
|
|
if (userId !== userIdFromServer) {
|
|
|
|
log.log("user_id mismatch");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (deviceId !== deviceIdFromServer) {
|
|
|
|
log.log("device_id mismatch");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const ed25519Key = getDeviceEd25519Key(deviceKey);
|
|
|
|
const curve25519Key = getDeviceCurve25519Key(deviceKey);
|
|
|
|
if (typeof ed25519Key !== "string" || typeof curve25519Key !== "string") {
|
|
|
|
log.log("ed25519 and/or curve25519 key invalid").set({deviceKey});
|
|
|
|
return false;
|
|
|
|
}
|
2023-03-07 10:53:32 +01:00
|
|
|
const isValid = verifyEd25519Signature(this._olmUtil, userId, deviceId, ed25519Key, deviceKey, log) === SignatureVerification.Valid;
|
2023-02-27 18:13:53 +01:00
|
|
|
if (!isValid) {
|
|
|
|
log.log({
|
|
|
|
l: "ignore device with invalid signature",
|
|
|
|
keys: deviceKey
|
|
|
|
}, log.level.Warn);
|
|
|
|
}
|
|
|
|
return isValid;
|
2020-08-31 14:11:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gives all the device identities for a room that is already tracked.
|
2022-11-10 17:51:40 +01:00
|
|
|
* Can be used to decide which users to share keys with.
|
2020-08-31 14:11:08 +02:00
|
|
|
* Assumes room is already tracked. Call `trackRoom` first if unsure.
|
|
|
|
* @param {String} roomId [description]
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2023-02-27 18:13:53 +01:00
|
|
|
async devicesForTrackedRoom(roomId: string, hsApi: HomeServerApi, log: ILogItem): Promise<DeviceKey[]> {
|
2021-03-04 19:47:02 +01:00
|
|
|
const txn = await this._storage.readTxn([
|
2020-08-31 14:11:08 +02:00
|
|
|
this._storage.storeNames.roomMembers,
|
|
|
|
this._storage.storeNames.userIdentities,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// because we don't have multiEntry support in IE11, we get a set of userIds that is pretty close to what we
|
|
|
|
// need as a good first filter (given that non-join memberships will be in there). After fetching the identities,
|
|
|
|
// we check which ones have the roomId for the room we're looking at.
|
|
|
|
|
|
|
|
// So, this will also contain non-joined memberships
|
2020-08-31 16:06:31 +02:00
|
|
|
const userIds = await txn.roomMembers.getAllUserIds(roomId);
|
2022-11-10 17:51:40 +01:00
|
|
|
// TODO: check here if userIds is safe? yes it is
|
|
|
|
return await this._devicesForUserIdsInTrackedRoom(roomId, userIds, txn, hsApi, log);
|
2020-09-08 14:24:48 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:51:40 +01:00
|
|
|
/**
|
|
|
|
* Can be used to decide which users to share keys with.
|
|
|
|
* Assumes room is already tracked. Call `trackRoom` first if unsure.
|
2023-03-02 15:02:42 +01:00
|
|
|
* This will not return the device key for our own user, as we don't need to share keys with ourselves.
|
2022-11-10 17:51:40 +01:00
|
|
|
*/
|
2023-02-27 18:13:53 +01:00
|
|
|
async devicesForRoomMembers(roomId: string, userIds: string[], hsApi: HomeServerApi, log: ILogItem): Promise<DeviceKey[]> {
|
2021-03-04 19:47:02 +01:00
|
|
|
const txn = await this._storage.readTxn([
|
2020-09-08 14:24:48 +02:00
|
|
|
this._storage.storeNames.userIdentities,
|
|
|
|
]);
|
2022-11-10 17:51:40 +01:00
|
|
|
return await this._devicesForUserIdsInTrackedRoom(roomId, userIds, txn, hsApi, log);
|
|
|
|
}
|
2022-11-10 17:53:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cannot be used to decide which users to share keys with.
|
|
|
|
* Does not assume membership to any room or whether any room is tracked.
|
2023-03-02 15:02:42 +01:00
|
|
|
* This will return device keys for our own user, including our own device.
|
2022-11-10 17:53:11 +01:00
|
|
|
*/
|
2023-02-27 18:13:53 +01:00
|
|
|
async devicesForUsers(userIds: string[], hsApi: HomeServerApi, log: ILogItem): Promise<DeviceKey[]> {
|
2022-11-10 17:53:11 +01:00
|
|
|
const txn = await this._storage.readTxn([
|
|
|
|
this._storage.storeNames.userIdentities,
|
|
|
|
]);
|
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
const upToDateIdentities: UserIdentity[] = [];
|
|
|
|
const outdatedUserIds: string[] = [];
|
2022-11-10 17:53:11 +01:00
|
|
|
await Promise.all(userIds.map(async userId => {
|
|
|
|
const i = await txn.userIdentities.get(userId);
|
2023-03-03 11:34:09 +01:00
|
|
|
if (i && i.keysTrackingStatus === KeysTrackingStatus.UpToDate) {
|
2022-11-10 17:53:11 +01:00
|
|
|
upToDateIdentities.push(i);
|
2023-03-03 11:34:09 +01:00
|
|
|
} else if (!i || i.keysTrackingStatus === KeysTrackingStatus.Outdated) {
|
2022-11-10 17:53:11 +01:00
|
|
|
// allow fetching for userIdentities we don't know about yet,
|
|
|
|
// as we don't assume the room is tracked here.
|
|
|
|
outdatedUserIds.push(userId);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
return this._devicesForUserIdentities(upToDateIdentities, outdatedUserIds, hsApi, log);
|
2020-09-08 14:24:48 +02:00
|
|
|
}
|
|
|
|
|
2023-03-02 15:02:42 +01:00
|
|
|
/** Gets a single device */
|
2023-02-27 18:13:53 +01:00
|
|
|
async deviceForId(userId: string, deviceId: string, hsApi: HomeServerApi, log: ILogItem) {
|
2022-06-01 15:27:00 +02:00
|
|
|
const txn = await this._storage.readTxn([
|
2023-02-27 18:13:53 +01:00
|
|
|
this._storage.storeNames.deviceKeys,
|
2022-06-01 15:27:00 +02:00
|
|
|
]);
|
2023-02-27 18:13:53 +01:00
|
|
|
let deviceKey = await txn.deviceKeys.get(userId, deviceId);
|
|
|
|
if (deviceKey) {
|
2022-06-01 15:27:00 +02:00
|
|
|
log.set("existingDevice", true);
|
|
|
|
} else {
|
|
|
|
//// BEGIN EXTRACT (deviceKeysMap)
|
|
|
|
const deviceKeyResponse = await hsApi.queryKeys({
|
|
|
|
"timeout": 10000,
|
|
|
|
"device_keys": {
|
|
|
|
[userId]: [deviceId]
|
|
|
|
},
|
|
|
|
"token": this._getSyncToken()
|
|
|
|
}, {log}).response();
|
|
|
|
// verify signature
|
|
|
|
const verifiedKeysPerUser = log.wrap("verify", log => this._filterVerifiedDeviceKeys(deviceKeyResponse["device_keys"], log));
|
|
|
|
//// END EXTRACT
|
2023-02-27 18:13:53 +01:00
|
|
|
const verifiedKey = verifiedKeysPerUser.get(userId)?.find(d => d.device_id === deviceId);
|
2022-06-01 15:55:43 +02:00
|
|
|
// user hasn't uploaded keys for device?
|
2023-02-27 18:13:53 +01:00
|
|
|
if (!verifiedKey) {
|
2022-06-01 15:55:43 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
2022-06-01 15:27:00 +02:00
|
|
|
const txn = await this._storage.readWriteTxn([
|
2023-02-27 18:13:53 +01:00
|
|
|
this._storage.storeNames.deviceKeys,
|
2022-06-01 15:27:00 +02:00
|
|
|
]);
|
|
|
|
// check again we don't have the device already.
|
|
|
|
// when updating all keys for a user we allow updating the
|
|
|
|
// device when the key hasn't changed so the device display name
|
|
|
|
// can be updated, but here we don't.
|
2023-02-27 18:13:53 +01:00
|
|
|
const existingDevice = await txn.deviceKeys.get(userId, deviceId);
|
2022-06-01 15:27:00 +02:00
|
|
|
if (existingDevice) {
|
2023-02-27 18:13:53 +01:00
|
|
|
deviceKey = existingDevice;
|
2022-06-01 15:27:00 +02:00
|
|
|
log.set("existingDeviceAfterFetch", true);
|
|
|
|
} else {
|
|
|
|
try {
|
2023-02-27 18:13:53 +01:00
|
|
|
txn.deviceKeys.set(verifiedKey);
|
|
|
|
deviceKey = verifiedKey;
|
2022-06-01 15:27:00 +02:00
|
|
|
log.set("newDevice", true);
|
|
|
|
} catch (err) {
|
|
|
|
txn.abort();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
await txn.complete();
|
|
|
|
}
|
|
|
|
}
|
2023-02-27 18:13:53 +01:00
|
|
|
return deviceKey;
|
2022-06-01 15:27:00 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:24:48 +02:00
|
|
|
/**
|
2022-11-10 17:51:40 +01:00
|
|
|
* Gets all the device identities with which keys should be shared for a set of users in a tracked room.
|
|
|
|
* If any userIdentities are outdated, it will fetch them from the homeserver.
|
|
|
|
* @param {string} roomId the id of the tracked room to filter users by.
|
|
|
|
* @param {Array<string>} userIds a set of user ids to try and find the identity for.
|
2020-09-08 14:24:48 +02:00
|
|
|
* @param {Transaction} userIdentityTxn to read the user identities
|
|
|
|
* @param {HomeServerApi} hsApi
|
2023-02-27 18:13:53 +01:00
|
|
|
* @return {Array<DeviceKey>} all devices identities for the given users we should share keys with.
|
2020-09-08 14:24:48 +02:00
|
|
|
*/
|
2023-02-27 18:13:53 +01:00
|
|
|
async _devicesForUserIdsInTrackedRoom(roomId: string, userIds: string[], userIdentityTxn: Transaction, hsApi: HomeServerApi, log: ILogItem): Promise<DeviceKey[]> {
|
2020-09-08 14:24:48 +02:00
|
|
|
const allMemberIdentities = await Promise.all(userIds.map(userId => userIdentityTxn.userIdentities.get(userId)));
|
|
|
|
const identities = allMemberIdentities.filter(identity => {
|
2022-11-10 17:51:40 +01:00
|
|
|
// we use roomIds to decide with whom we should share keys for a given room,
|
|
|
|
// taking into account the membership and room history visibility.
|
|
|
|
// so filter out anyone who we shouldn't share keys with.
|
|
|
|
// Given we assume the room is tracked,
|
|
|
|
// also exclude any userId which doesn't have a userIdentity yet.
|
2020-08-31 14:11:08 +02:00
|
|
|
return identity && identity.roomIds.includes(roomId);
|
2023-02-27 18:13:53 +01:00
|
|
|
}) as UserIdentity[]; // undefined has been filter out
|
2023-03-03 11:34:09 +01:00
|
|
|
const upToDateIdentities = identities.filter(i => i.keysTrackingStatus === KeysTrackingStatus.UpToDate);
|
2022-11-10 17:51:40 +01:00
|
|
|
const outdatedUserIds = identities
|
2023-03-03 11:34:09 +01:00
|
|
|
.filter(i => i.keysTrackingStatus === KeysTrackingStatus.Outdated)
|
2022-11-10 17:51:40 +01:00
|
|
|
.map(i => i.userId);
|
|
|
|
let devices = await this._devicesForUserIdentities(upToDateIdentities, outdatedUserIds, hsApi, log);
|
|
|
|
// filter out our own device as we should never share keys with it.
|
|
|
|
devices = devices.filter(device => {
|
2023-02-27 18:13:53 +01:00
|
|
|
const isOwnDevice = device.user_id === this._ownUserId && device.device_id === this._ownDeviceId;
|
2022-11-10 17:51:40 +01:00
|
|
|
return !isOwnDevice;
|
|
|
|
});
|
|
|
|
return devices;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets the device identites for a set of user identities that
|
|
|
|
* are known to be up to date, and a set of userIds that are known
|
|
|
|
* to be absent from our store our outdated. The outdated user ids
|
|
|
|
* will have their keys fetched from the homeserver. */
|
2023-02-27 18:13:53 +01:00
|
|
|
async _devicesForUserIdentities(upToDateIdentities: UserIdentity[], outdatedUserIds: string[], hsApi: HomeServerApi, log: ILogItem): Promise<DeviceKey[]> {
|
2021-02-23 19:22:59 +01:00
|
|
|
log.set("uptodate", upToDateIdentities.length);
|
2022-11-10 17:51:40 +01:00
|
|
|
log.set("outdated", outdatedUserIds.length);
|
2023-02-27 18:13:53 +01:00
|
|
|
let queriedDeviceKeys: Map<string, DeviceKey[]> | undefined;
|
2022-11-10 17:51:40 +01:00
|
|
|
if (outdatedUserIds.length) {
|
2020-08-31 14:11:08 +02:00
|
|
|
// TODO: ignore the race between /sync and /keys/query for now,
|
|
|
|
// where users could get marked as outdated or added/removed from the room while
|
|
|
|
// querying keys
|
2023-02-27 18:13:53 +01:00
|
|
|
const {deviceKeys} = await this._queryKeys(outdatedUserIds, hsApi, log);
|
|
|
|
queriedDeviceKeys = deviceKeys;
|
2020-08-31 14:11:08 +02:00
|
|
|
}
|
|
|
|
|
2021-03-04 19:47:02 +01:00
|
|
|
const deviceTxn = await this._storage.readTxn([
|
2023-02-27 18:13:53 +01:00
|
|
|
this._storage.storeNames.deviceKeys,
|
2020-08-31 14:11:08 +02:00
|
|
|
]);
|
|
|
|
const devicesPerUser = await Promise.all(upToDateIdentities.map(identity => {
|
2023-02-27 18:13:53 +01:00
|
|
|
return deviceTxn.deviceKeys.getAllForUserId(identity.userId);
|
2020-08-31 14:11:08 +02:00
|
|
|
}));
|
|
|
|
let flattenedDevices = devicesPerUser.reduce((all, devicesForUser) => all.concat(devicesForUser), []);
|
2023-02-27 18:13:53 +01:00
|
|
|
if (queriedDeviceKeys && queriedDeviceKeys.size) {
|
|
|
|
for (const deviceKeysForUser of queriedDeviceKeys.values()) {
|
|
|
|
flattenedDevices = flattenedDevices.concat(deviceKeysForUser);
|
|
|
|
}
|
2020-08-31 14:11:08 +02:00
|
|
|
}
|
2022-11-10 17:51:40 +01:00
|
|
|
return flattenedDevices;
|
2020-08-31 14:11:08 +02:00
|
|
|
}
|
2020-09-08 10:50:39 +02:00
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
async getDeviceByCurve25519Key(curve25519Key, txn: Transaction): Promise<DeviceKey | undefined> {
|
|
|
|
return await txn.deviceKeys.getByCurve25519Key(curve25519Key);
|
2020-09-08 10:50:39 +02:00
|
|
|
}
|
2020-08-31 14:11:08 +02:00
|
|
|
}
|
2022-05-31 13:38:34 +02:00
|
|
|
|
|
|
|
import {createMockStorage} from "../../mocks/Storage";
|
|
|
|
import {Instance as NullLoggerInstance} from "../../logging/NullLogger";
|
|
|
|
|
|
|
|
export function tests() {
|
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
function createUntrackedRoomMock(roomId: string, joinedUserIds: string[], invitedUserIds: string[] = []) {
|
2022-05-31 13:38:34 +02:00
|
|
|
return {
|
2022-07-26 17:41:26 +02:00
|
|
|
id: roomId,
|
2022-05-31 13:38:34 +02:00
|
|
|
isTrackingMembers: false,
|
|
|
|
isEncrypted: true,
|
|
|
|
loadMemberList: () => {
|
2022-07-26 16:57:28 +02:00
|
|
|
const joinedMembers = joinedUserIds.map(userId => {return RoomMember.fromUserId(roomId, userId, "join");});
|
|
|
|
const invitedMembers = invitedUserIds.map(userId => {return RoomMember.fromUserId(roomId, userId, "invite");});
|
2022-05-31 13:38:34 +02:00
|
|
|
const members = joinedMembers.concat(invitedMembers);
|
|
|
|
const memberMap = members.reduce((map, member) => {
|
|
|
|
map.set(member.userId, member);
|
|
|
|
return map;
|
|
|
|
}, new Map());
|
|
|
|
return {members: memberMap, release() {}}
|
|
|
|
},
|
|
|
|
writeIsTrackingMembers(isTrackingMembers) {
|
|
|
|
if (this.isTrackingMembers !== isTrackingMembers) {
|
|
|
|
return isTrackingMembers;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
},
|
|
|
|
applyIsTrackingMembersChanges(isTrackingMembers) {
|
|
|
|
if (isTrackingMembers !== undefined) {
|
|
|
|
this.isTrackingMembers = isTrackingMembers;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
function createQueryKeysHSApiMock(createKey = (algorithm, userId, deviceId) => `${algorithm}:${userId}:${deviceId}:key`): HomeServerApi {
|
2022-05-31 13:38:34 +02:00
|
|
|
return {
|
|
|
|
queryKeys(payload) {
|
|
|
|
const {device_keys: deviceKeys} = payload;
|
2023-02-27 18:13:53 +01:00
|
|
|
const userKeys = Object.entries(deviceKeys as {[userId: string]: string[]}).reduce((userKeys, [userId, deviceIds]) => {
|
2022-05-31 13:38:34 +02:00
|
|
|
if (deviceIds.length === 0) {
|
|
|
|
deviceIds = ["device1"];
|
|
|
|
}
|
|
|
|
userKeys[userId] = deviceIds.filter(d => d === "device1").reduce((deviceKeys, deviceId) => {
|
|
|
|
deviceKeys[deviceId] = {
|
|
|
|
"algorithms": [
|
|
|
|
"m.olm.v1.curve25519-aes-sha2",
|
|
|
|
"m.megolm.v1.aes-sha2"
|
|
|
|
],
|
|
|
|
"device_id": deviceId,
|
|
|
|
"keys": {
|
|
|
|
[`curve25519:${deviceId}`]: createKey("curve25519", userId, deviceId),
|
|
|
|
[`ed25519:${deviceId}`]: createKey("ed25519", userId, deviceId),
|
|
|
|
},
|
|
|
|
"signatures": {
|
|
|
|
[userId]: {
|
|
|
|
[`ed25519:${deviceId}`]: `ed25519:${userId}:${deviceId}:signature`
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"unsigned": {
|
|
|
|
"device_display_name": `${userId} Phone`
|
|
|
|
},
|
|
|
|
"user_id": userId
|
|
|
|
};
|
|
|
|
return deviceKeys;
|
|
|
|
}, {});
|
|
|
|
return userKeys;
|
|
|
|
}, {});
|
|
|
|
const response = {device_keys: userKeys};
|
|
|
|
return {
|
|
|
|
async response() {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2023-02-27 18:13:53 +01:00
|
|
|
} as unknown as HomeServerApi;
|
2022-05-31 13:38:34 +02:00
|
|
|
}
|
2022-07-26 16:57:28 +02:00
|
|
|
|
|
|
|
async function writeMemberListToStorage(room, storage) {
|
|
|
|
const txn = await storage.readWriteTxn([
|
|
|
|
storage.storeNames.roomMembers,
|
|
|
|
]);
|
|
|
|
const memberList = await room.loadMemberList(txn);
|
|
|
|
try {
|
|
|
|
for (const member of memberList.members.values()) {
|
|
|
|
txn.roomMembers.set(member.serialize());
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
txn.abort();
|
|
|
|
throw err;
|
|
|
|
} finally {
|
|
|
|
memberList.release();
|
|
|
|
}
|
|
|
|
await txn.complete();
|
|
|
|
}
|
|
|
|
|
2022-05-31 13:38:34 +02:00
|
|
|
const roomId = "!abc:hs.tld";
|
|
|
|
|
|
|
|
return {
|
2022-07-26 16:57:28 +02:00
|
|
|
"trackRoom only writes joined members with history visibility of joined": async assert => {
|
2022-05-31 13:38:34 +02:00
|
|
|
const storage = await createMockStorage();
|
|
|
|
const tracker = new DeviceTracker({
|
|
|
|
storage,
|
|
|
|
getSyncToken: () => "token",
|
2023-02-27 18:13:53 +01:00
|
|
|
olmUtil: {ed25519_verify: () => {}} as unknown as Olm.Utility, // valid if it does not throw
|
2022-05-31 13:38:34 +02:00
|
|
|
ownUserId: "@alice:hs.tld",
|
|
|
|
ownDeviceId: "ABCD",
|
|
|
|
});
|
|
|
|
const room = createUntrackedRoomMock(roomId, ["@alice:hs.tld", "@bob:hs.tld"], ["@charly:hs.tld"]);
|
2022-07-26 16:57:28 +02:00
|
|
|
await tracker.trackRoom(room, HistoryVisibility.Joined, NullLoggerInstance.item);
|
2022-05-31 13:38:34 +02:00
|
|
|
const txn = await storage.readTxn([storage.storeNames.userIdentities]);
|
|
|
|
assert.deepEqual(await txn.userIdentities.get("@alice:hs.tld"), {
|
|
|
|
userId: "@alice:hs.tld",
|
|
|
|
roomIds: [roomId],
|
2023-03-03 11:34:09 +01:00
|
|
|
keysTrackingStatus: KeysTrackingStatus.Outdated
|
2022-05-31 13:38:34 +02:00
|
|
|
});
|
|
|
|
assert.deepEqual(await txn.userIdentities.get("@bob:hs.tld"), {
|
|
|
|
userId: "@bob:hs.tld",
|
|
|
|
roomIds: [roomId],
|
2023-03-03 11:34:09 +01:00
|
|
|
keysTrackingStatus: KeysTrackingStatus.Outdated
|
2022-05-31 13:38:34 +02:00
|
|
|
});
|
|
|
|
assert.equal(await txn.userIdentities.get("@charly:hs.tld"), undefined);
|
|
|
|
},
|
|
|
|
"getting devices for tracked room yields correct keys": async assert => {
|
|
|
|
const storage = await createMockStorage();
|
|
|
|
const tracker = new DeviceTracker({
|
|
|
|
storage,
|
|
|
|
getSyncToken: () => "token",
|
2023-02-27 18:13:53 +01:00
|
|
|
olmUtil: {ed25519_verify: () => {}} as unknown as Olm.Utility, // valid if it does not throw
|
2022-05-31 13:38:34 +02:00
|
|
|
ownUserId: "@alice:hs.tld",
|
|
|
|
ownDeviceId: "ABCD",
|
|
|
|
});
|
|
|
|
const room = createUntrackedRoomMock(roomId, ["@alice:hs.tld", "@bob:hs.tld"]);
|
2022-07-26 16:57:28 +02:00
|
|
|
await tracker.trackRoom(room, HistoryVisibility.Joined, NullLoggerInstance.item);
|
2022-05-31 13:38:34 +02:00
|
|
|
const hsApi = createQueryKeysHSApiMock();
|
|
|
|
const devices = await tracker.devicesForRoomMembers(roomId, ["@alice:hs.tld", "@bob:hs.tld"], hsApi, NullLoggerInstance.item);
|
|
|
|
assert.equal(devices.length, 2);
|
2023-02-27 18:13:53 +01:00
|
|
|
assert.equal(getDeviceEd25519Key(devices.find(d => d.user_id === "@alice:hs.tld")!), "ed25519:@alice:hs.tld:device1:key");
|
|
|
|
assert.equal(getDeviceEd25519Key(devices.find(d => d.user_id === "@bob:hs.tld")!), "ed25519:@bob:hs.tld:device1:key");
|
2022-05-31 13:38:34 +02:00
|
|
|
},
|
2022-05-31 13:39:05 +02:00
|
|
|
"device with changed key is ignored": async assert => {
|
|
|
|
const storage = await createMockStorage();
|
|
|
|
const tracker = new DeviceTracker({
|
|
|
|
storage,
|
|
|
|
getSyncToken: () => "token",
|
2023-02-27 18:13:53 +01:00
|
|
|
olmUtil: {ed25519_verify: () => {}} as unknown as Olm.Utility, // valid if it does not throw
|
2022-05-31 13:39:05 +02:00
|
|
|
ownUserId: "@alice:hs.tld",
|
|
|
|
ownDeviceId: "ABCD",
|
|
|
|
});
|
|
|
|
const room = createUntrackedRoomMock(roomId, ["@alice:hs.tld", "@bob:hs.tld"]);
|
2022-07-26 16:57:28 +02:00
|
|
|
await tracker.trackRoom(room, HistoryVisibility.Joined, NullLoggerInstance.item);
|
2022-05-31 13:39:05 +02:00
|
|
|
const hsApi = createQueryKeysHSApiMock();
|
|
|
|
// query devices first time
|
|
|
|
await tracker.devicesForRoomMembers(roomId, ["@alice:hs.tld", "@bob:hs.tld"], hsApi, NullLoggerInstance.item);
|
|
|
|
const txn = await storage.readWriteTxn([storage.storeNames.userIdentities]);
|
|
|
|
// mark alice as outdated, so keys will be fetched again
|
|
|
|
tracker.writeDeviceChanges(["@alice:hs.tld"], txn, NullLoggerInstance.item);
|
|
|
|
await txn.complete();
|
|
|
|
const hsApiWithChangedAliceKey = createQueryKeysHSApiMock((algo, userId, deviceId) => {
|
|
|
|
return `${algo}:${userId}:${deviceId}:${userId === "@alice:hs.tld" ? "newKey" : "key"}`;
|
|
|
|
});
|
|
|
|
const devices = await tracker.devicesForRoomMembers(roomId, ["@alice:hs.tld", "@bob:hs.tld"], hsApiWithChangedAliceKey, NullLoggerInstance.item);
|
|
|
|
assert.equal(devices.length, 2);
|
2023-02-27 18:13:53 +01:00
|
|
|
assert.equal(getDeviceEd25519Key(devices.find(d => d.user_id === "@alice:hs.tld")!), "ed25519:@alice:hs.tld:device1:key");
|
|
|
|
assert.equal(getDeviceEd25519Key(devices.find(d => d.user_id === "@bob:hs.tld")!), "ed25519:@bob:hs.tld:device1:key");
|
|
|
|
const txn2 = await storage.readTxn([storage.storeNames.deviceKeys]);
|
2022-05-31 13:39:05 +02:00
|
|
|
// also check the modified key was not stored
|
2023-02-27 18:13:53 +01:00
|
|
|
assert.equal(getDeviceEd25519Key((await txn2.deviceKeys.get("@alice:hs.tld", "device1"))!), "ed25519:@alice:hs.tld:device1:key");
|
2022-07-26 16:57:28 +02:00
|
|
|
},
|
|
|
|
"change history visibility from joined to invited adds invitees": async assert => {
|
|
|
|
const storage = await createMockStorage();
|
|
|
|
const tracker = new DeviceTracker({
|
|
|
|
storage,
|
|
|
|
getSyncToken: () => "token",
|
2023-02-27 18:13:53 +01:00
|
|
|
olmUtil: {ed25519_verify: () => {}} as unknown as Olm.Utility, // valid if it does not throw
|
2022-07-26 16:57:28 +02:00
|
|
|
ownUserId: "@alice:hs.tld",
|
|
|
|
ownDeviceId: "ABCD",
|
|
|
|
});
|
|
|
|
// alice is joined, bob is invited
|
|
|
|
const room = await createUntrackedRoomMock(roomId,
|
|
|
|
["@alice:hs.tld"], ["@bob:hs.tld"]);
|
|
|
|
await tracker.trackRoom(room, HistoryVisibility.Joined, NullLoggerInstance.item);
|
2023-02-27 18:13:53 +01:00
|
|
|
const txn = await storage.readWriteTxn([storage.storeNames.userIdentities, storage.storeNames.deviceKeys]);
|
2022-07-26 16:57:28 +02:00
|
|
|
assert.equal(await txn.userIdentities.get("@bob:hs.tld"), undefined);
|
|
|
|
const {added, removed} = await tracker.writeHistoryVisibility(room, HistoryVisibility.Invited, txn, NullLoggerInstance.item);
|
2023-02-27 18:13:53 +01:00
|
|
|
assert.equal((await txn.userIdentities.get("@bob:hs.tld"))!.userId, "@bob:hs.tld");
|
2022-07-26 16:57:28 +02:00
|
|
|
assert.deepEqual(added, ["@bob:hs.tld"]);
|
|
|
|
assert.deepEqual(removed, []);
|
|
|
|
},
|
|
|
|
"change history visibility from invited to joined removes invitees": async assert => {
|
|
|
|
const storage = await createMockStorage();
|
|
|
|
const tracker = new DeviceTracker({
|
|
|
|
storage,
|
|
|
|
getSyncToken: () => "token",
|
2023-02-27 18:13:53 +01:00
|
|
|
olmUtil: {ed25519_verify: () => {}} as unknown as Olm.Utility, // valid if it does not throw
|
2022-07-26 16:57:28 +02:00
|
|
|
ownUserId: "@alice:hs.tld",
|
|
|
|
ownDeviceId: "ABCD",
|
|
|
|
});
|
|
|
|
// alice is joined, bob is invited
|
|
|
|
const room = await createUntrackedRoomMock(roomId,
|
|
|
|
["@alice:hs.tld"], ["@bob:hs.tld"]);
|
|
|
|
await tracker.trackRoom(room, HistoryVisibility.Invited, NullLoggerInstance.item);
|
2023-02-27 18:13:53 +01:00
|
|
|
const txn = await storage.readWriteTxn([storage.storeNames.userIdentities, storage.storeNames.deviceKeys]);
|
|
|
|
assert.equal((await txn.userIdentities.get("@bob:hs.tld"))!.userId, "@bob:hs.tld");
|
2022-07-26 16:57:28 +02:00
|
|
|
const {added, removed} = await tracker.writeHistoryVisibility(room, HistoryVisibility.Joined, txn, NullLoggerInstance.item);
|
|
|
|
assert.equal(await txn.userIdentities.get("@bob:hs.tld"), undefined);
|
|
|
|
assert.deepEqual(added, []);
|
|
|
|
assert.deepEqual(removed, ["@bob:hs.tld"]);
|
|
|
|
},
|
|
|
|
"adding invitee with history visibility of invited adds room to userIdentities": async assert => {
|
|
|
|
const storage = await createMockStorage();
|
|
|
|
const tracker = new DeviceTracker({
|
|
|
|
storage,
|
|
|
|
getSyncToken: () => "token",
|
2023-02-27 18:13:53 +01:00
|
|
|
olmUtil: {ed25519_verify: () => {}} as unknown as Olm.Utility, // valid if it does not throw
|
2022-07-26 16:57:28 +02:00
|
|
|
ownUserId: "@alice:hs.tld",
|
|
|
|
ownDeviceId: "ABCD",
|
|
|
|
});
|
|
|
|
const room = await createUntrackedRoomMock(roomId, ["@alice:hs.tld"]);
|
|
|
|
await tracker.trackRoom(room, HistoryVisibility.Invited, NullLoggerInstance.item);
|
2023-02-27 18:13:53 +01:00
|
|
|
const txn = await storage.readWriteTxn([storage.storeNames.userIdentities, storage.storeNames.deviceKeys]);
|
2022-07-26 16:57:28 +02:00
|
|
|
// inviting a new member
|
|
|
|
const inviteChange = new MemberChange(RoomMember.fromUserId(roomId, "@bob:hs.tld", "invite"));
|
2023-02-27 18:13:53 +01:00
|
|
|
const {added, removed} = await tracker.writeMemberChanges(room, new Map([[inviteChange.userId, inviteChange]]), HistoryVisibility.Invited, txn);
|
2022-07-26 16:57:28 +02:00
|
|
|
assert.deepEqual(added, ["@bob:hs.tld"]);
|
|
|
|
assert.deepEqual(removed, []);
|
2023-02-27 18:13:53 +01:00
|
|
|
assert.equal((await txn.userIdentities.get("@bob:hs.tld"))!.userId, "@bob:hs.tld");
|
2022-07-26 16:57:28 +02:00
|
|
|
},
|
|
|
|
"adding invitee with history visibility of joined doesn't add room": async assert => {
|
|
|
|
const storage = await createMockStorage();
|
|
|
|
const tracker = new DeviceTracker({
|
|
|
|
storage,
|
|
|
|
getSyncToken: () => "token",
|
2023-02-27 18:13:53 +01:00
|
|
|
olmUtil: {ed25519_verify: () => {}} as unknown as Olm.Utility, // valid if it does not throw
|
2022-07-26 16:57:28 +02:00
|
|
|
ownUserId: "@alice:hs.tld",
|
|
|
|
ownDeviceId: "ABCD",
|
|
|
|
});
|
|
|
|
const room = await createUntrackedRoomMock(roomId, ["@alice:hs.tld"]);
|
|
|
|
await tracker.trackRoom(room, HistoryVisibility.Joined, NullLoggerInstance.item);
|
2023-02-27 18:13:53 +01:00
|
|
|
const txn = await storage.readWriteTxn([storage.storeNames.userIdentities, storage.storeNames.deviceKeys]);
|
2022-07-26 16:57:28 +02:00
|
|
|
// inviting a new member
|
|
|
|
const inviteChange = new MemberChange(RoomMember.fromUserId(roomId, "@bob:hs.tld", "invite"));
|
|
|
|
const memberChanges = new Map([[inviteChange.userId, inviteChange]]);
|
|
|
|
const {added, removed} = await tracker.writeMemberChanges(room, memberChanges, HistoryVisibility.Joined, txn);
|
|
|
|
assert.deepEqual(added, []);
|
|
|
|
assert.deepEqual(removed, []);
|
|
|
|
assert.equal(await txn.userIdentities.get("@bob:hs.tld"), undefined);
|
|
|
|
},
|
|
|
|
"getting all devices after changing history visibility now includes invitees": async assert => {
|
|
|
|
const storage = await createMockStorage();
|
|
|
|
const tracker = new DeviceTracker({
|
|
|
|
storage,
|
|
|
|
getSyncToken: () => "token",
|
2023-02-27 18:13:53 +01:00
|
|
|
olmUtil: {ed25519_verify: () => {}} as unknown as Olm.Utility, // valid if it does not throw
|
2022-07-26 16:57:28 +02:00
|
|
|
ownUserId: "@alice:hs.tld",
|
|
|
|
ownDeviceId: "ABCD",
|
|
|
|
});
|
|
|
|
const room = createUntrackedRoomMock(roomId, ["@alice:hs.tld"], ["@bob:hs.tld"]);
|
|
|
|
await tracker.trackRoom(room, HistoryVisibility.Invited, NullLoggerInstance.item);
|
|
|
|
const hsApi = createQueryKeysHSApiMock();
|
|
|
|
// write memberlist from room mock to mock storage,
|
|
|
|
// as devicesForTrackedRoom reads directly from roomMembers store.
|
|
|
|
await writeMemberListToStorage(room, storage);
|
|
|
|
const devices = await tracker.devicesForTrackedRoom(roomId, hsApi, NullLoggerInstance.item);
|
|
|
|
assert.equal(devices.length, 2);
|
2023-02-27 18:13:53 +01:00
|
|
|
assert.equal(getDeviceEd25519Key(devices.find(d => d.user_id === "@alice:hs.tld")!), "ed25519:@alice:hs.tld:device1:key");
|
|
|
|
assert.equal(getDeviceEd25519Key(devices.find(d => d.user_id === "@bob:hs.tld")!), "ed25519:@bob:hs.tld:device1:key");
|
2022-07-26 16:57:28 +02:00
|
|
|
},
|
2022-07-26 16:58:07 +02:00
|
|
|
"rejecting invite with history visibility of invited removes room from user identity": async assert => {
|
|
|
|
const storage = await createMockStorage();
|
|
|
|
const tracker = new DeviceTracker({
|
|
|
|
storage,
|
|
|
|
getSyncToken: () => "token",
|
2023-02-27 18:13:53 +01:00
|
|
|
olmUtil: {ed25519_verify: () => {}} as unknown as Olm.Utility, // valid if it does not throw
|
2022-07-26 16:58:07 +02:00
|
|
|
ownUserId: "@alice:hs.tld",
|
|
|
|
ownDeviceId: "ABCD",
|
|
|
|
});
|
|
|
|
// alice is joined, bob is invited
|
|
|
|
const room = await createUntrackedRoomMock(roomId, ["@alice:hs.tld"], ["@bob:hs.tld"]);
|
|
|
|
await tracker.trackRoom(room, HistoryVisibility.Invited, NullLoggerInstance.item);
|
2023-02-27 18:13:53 +01:00
|
|
|
const txn = await storage.readWriteTxn([storage.storeNames.userIdentities, storage.storeNames.deviceKeys]);
|
2022-07-26 16:58:07 +02:00
|
|
|
// reject invite
|
|
|
|
const inviteChange = new MemberChange(RoomMember.fromUserId(roomId, "@bob:hs.tld", "leave"), "invite");
|
|
|
|
const memberChanges = new Map([[inviteChange.userId, inviteChange]]);
|
|
|
|
const {added, removed} = await tracker.writeMemberChanges(room, memberChanges, HistoryVisibility.Invited, txn);
|
|
|
|
assert.deepEqual(added, []);
|
|
|
|
assert.deepEqual(removed, ["@bob:hs.tld"]);
|
|
|
|
assert.equal(await txn.userIdentities.get("@bob:hs.tld"), undefined);
|
|
|
|
},
|
2022-07-26 17:41:26 +02:00
|
|
|
"remove room from user identity sharing multiple rooms with us preserves other room": async assert => {
|
|
|
|
const storage = await createMockStorage();
|
|
|
|
const tracker = new DeviceTracker({
|
|
|
|
storage,
|
|
|
|
getSyncToken: () => "token",
|
2023-02-27 18:13:53 +01:00
|
|
|
olmUtil: {ed25519_verify: () => {}} as unknown as Olm.Utility, // valid if it does not throw
|
2022-07-26 17:41:26 +02:00
|
|
|
ownUserId: "@alice:hs.tld",
|
|
|
|
ownDeviceId: "ABCD",
|
|
|
|
});
|
|
|
|
// alice is joined, bob is invited
|
|
|
|
const room1 = await createUntrackedRoomMock("!abc:hs.tld", ["@alice:hs.tld", "@bob:hs.tld"]);
|
|
|
|
const room2 = await createUntrackedRoomMock("!def:hs.tld", ["@alice:hs.tld", "@bob:hs.tld"]);
|
|
|
|
await tracker.trackRoom(room1, HistoryVisibility.Joined, NullLoggerInstance.item);
|
|
|
|
await tracker.trackRoom(room2, HistoryVisibility.Joined, NullLoggerInstance.item);
|
|
|
|
const txn1 = await storage.readTxn([storage.storeNames.userIdentities]);
|
2023-02-27 18:13:53 +01:00
|
|
|
assert.deepEqual((await txn1.userIdentities.get("@bob:hs.tld"))!.roomIds, ["!abc:hs.tld", "!def:hs.tld"]);
|
2022-07-26 17:41:26 +02:00
|
|
|
const leaveChange = new MemberChange(RoomMember.fromUserId(room2.id, "@bob:hs.tld", "leave"), "join");
|
|
|
|
const memberChanges = new Map([[leaveChange.userId, leaveChange]]);
|
2023-02-27 18:13:53 +01:00
|
|
|
const txn2 = await storage.readWriteTxn([storage.storeNames.userIdentities, storage.storeNames.deviceKeys]);
|
2022-07-26 17:41:26 +02:00
|
|
|
await tracker.writeMemberChanges(room2, memberChanges, HistoryVisibility.Joined, txn2);
|
|
|
|
await txn2.complete();
|
|
|
|
const txn3 = await storage.readTxn([storage.storeNames.userIdentities]);
|
2023-02-27 18:13:53 +01:00
|
|
|
assert.deepEqual((await txn3.userIdentities.get("@bob:hs.tld"))!.roomIds, ["!abc:hs.tld"]);
|
2022-07-26 17:41:26 +02:00
|
|
|
},
|
|
|
|
"add room to user identity sharing multiple rooms with us preserves other room": async assert => {
|
|
|
|
const storage = await createMockStorage();
|
|
|
|
const tracker = new DeviceTracker({
|
|
|
|
storage,
|
|
|
|
getSyncToken: () => "token",
|
2023-02-27 18:13:53 +01:00
|
|
|
olmUtil: {ed25519_verify: () => {}} as unknown as Olm.Utility, // valid if it does not throw
|
2022-07-26 17:41:26 +02:00
|
|
|
ownUserId: "@alice:hs.tld",
|
|
|
|
ownDeviceId: "ABCD",
|
|
|
|
});
|
|
|
|
// alice is joined, bob is invited
|
|
|
|
const room1 = await createUntrackedRoomMock("!abc:hs.tld", ["@alice:hs.tld", "@bob:hs.tld"]);
|
|
|
|
const room2 = await createUntrackedRoomMock("!def:hs.tld", ["@alice:hs.tld", "@bob:hs.tld"]);
|
|
|
|
await tracker.trackRoom(room1, HistoryVisibility.Joined, NullLoggerInstance.item);
|
|
|
|
const txn1 = await storage.readTxn([storage.storeNames.userIdentities]);
|
2023-02-27 18:13:53 +01:00
|
|
|
assert.deepEqual((await txn1.userIdentities.get("@bob:hs.tld"))!.roomIds, ["!abc:hs.tld"]);
|
2022-07-26 17:41:26 +02:00
|
|
|
await tracker.trackRoom(room2, HistoryVisibility.Joined, NullLoggerInstance.item);
|
|
|
|
const txn2 = await storage.readTxn([storage.storeNames.userIdentities]);
|
2023-02-27 18:13:53 +01:00
|
|
|
assert.deepEqual((await txn2.userIdentities.get("@bob:hs.tld"))!.roomIds, ["!abc:hs.tld", "!def:hs.tld"]);
|
2022-07-26 17:41:26 +02:00
|
|
|
},
|
2022-11-10 17:53:11 +01:00
|
|
|
"devicesForUsers fetches users even though they aren't in any tracked room": async assert => {
|
|
|
|
const storage = await createMockStorage();
|
|
|
|
const tracker = new DeviceTracker({
|
|
|
|
storage,
|
|
|
|
getSyncToken: () => "token",
|
2023-02-27 18:13:53 +01:00
|
|
|
olmUtil: {ed25519_verify: () => {}} as unknown as Olm.Utility, // valid if it does not throw
|
2022-11-10 17:53:11 +01:00
|
|
|
ownUserId: "@alice:hs.tld",
|
|
|
|
ownDeviceId: "ABCD",
|
|
|
|
});
|
|
|
|
const hsApi = createQueryKeysHSApiMock();
|
|
|
|
const devices = await tracker.devicesForUsers(["@bob:hs.tld"], hsApi, NullLoggerInstance.item);
|
|
|
|
assert.equal(devices.length, 1);
|
2023-02-27 18:13:53 +01:00
|
|
|
assert.equal(getDeviceCurve25519Key(devices[0]), "curve25519:@bob:hs.tld:device1:key");
|
2022-11-10 17:53:11 +01:00
|
|
|
const txn1 = await storage.readTxn([storage.storeNames.userIdentities]);
|
2023-02-27 18:13:53 +01:00
|
|
|
assert.deepEqual((await txn1.userIdentities.get("@bob:hs.tld"))!.roomIds, []);
|
2022-11-10 17:53:11 +01:00
|
|
|
},
|
|
|
|
"devicesForUsers doesn't add any roomId when creating userIdentity": async assert => {
|
|
|
|
const storage = await createMockStorage();
|
|
|
|
const tracker = new DeviceTracker({
|
|
|
|
storage,
|
|
|
|
getSyncToken: () => "token",
|
2023-02-27 18:13:53 +01:00
|
|
|
olmUtil: {ed25519_verify: () => {}} as unknown as Olm.Utility, // valid if it does not throw
|
2022-11-10 17:53:11 +01:00
|
|
|
ownUserId: "@alice:hs.tld",
|
|
|
|
ownDeviceId: "ABCD",
|
|
|
|
});
|
|
|
|
const hsApi = createQueryKeysHSApiMock();
|
|
|
|
await tracker.devicesForUsers(["@bob:hs.tld"], hsApi, NullLoggerInstance.item);
|
|
|
|
const txn1 = await storage.readTxn([storage.storeNames.userIdentities]);
|
2023-02-27 18:13:53 +01:00
|
|
|
assert.deepEqual((await txn1.userIdentities.get("@bob:hs.tld"))!.roomIds, []);
|
2022-05-31 13:39:05 +02:00
|
|
|
}
|
2022-05-31 13:38:34 +02:00
|
|
|
}
|
|
|
|
}
|