Skip to content

Commit

Permalink
Expose a new 'userHasCrossSigningKeys' method (#2950)
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh authored Dec 8, 2022
1 parent 224e592 commit 39cf212
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
66 changes: 65 additions & 1 deletion spec/unit/crypto/cross-signing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import HttpBackend from "matrix-mock-request";
import * as olmlib from "../../../src/crypto/olmlib";
import { MatrixError } from '../../../src/http-api';
import { logger } from '../../../src/logger';
import { ICrossSigningKey, ICreateClientOpts, ISignedKey } from '../../../src/client';
import { ICrossSigningKey, ICreateClientOpts, ISignedKey, MatrixClient } from '../../../src/client';
import { CryptoEvent, IBootstrapCrossSigningOpts } from '../../../src/crypto';
import { IDevice } from '../../../src/crypto/deviceinfo';
import { TestClient } from '../../TestClient';
Expand Down Expand Up @@ -1137,3 +1137,67 @@ describe("Cross Signing", function() {
alice.stopClient();
});
});

describe("userHasCrossSigningKeys", function() {
if (!global.Olm) {
return;
}

beforeAll(() => {
return global.Olm.init();
});

let aliceClient: MatrixClient;
let httpBackend: HttpBackend;
beforeEach(async () => {
const testClient = await makeTestClient({ userId: "@alice:example.com", deviceId: "Osborne2" });
aliceClient = testClient.client;
httpBackend = testClient.httpBackend;
});

afterEach(() => {
aliceClient.stopClient();
});

it("should download devices and return true if one is a cross-signing key", async () => {
httpBackend
.when("POST", "/keys/query")
.respond(200, {
"master_keys": {
"@alice:example.com": {
user_id: "@alice:example.com",
usage: ["master"],
keys: {
"ed25519:nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk":
"nqOvzeuGWT/sRx3h7+MHoInYj3Uk2LD/unI9kDYcHwk",
},
},
},
});

let result: boolean;
await Promise.all([
httpBackend.flush("/keys/query"),
aliceClient.userHasCrossSigningKeys().then((res) => {result = res;}),
]);
expect(result!).toBeTruthy();
});

it("should download devices and return false if there is no cross-signing key", async () => {
httpBackend
.when("POST", "/keys/query")
.respond(200, {});

let result: boolean;
await Promise.all([
httpBackend.flush("/keys/query"),
aliceClient.userHasCrossSigningKeys().then((res) => {result = res;}),
]);
expect(result!).toBeFalsy();
});

it("throws an error if crypto is disabled", () => {
aliceClient.crypto = undefined;
expect(() => aliceClient.userHasCrossSigningKeys()).toThrowError("encryption disabled");
});
});
13 changes: 13 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2473,6 +2473,19 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
this.crypto.prepareToEncrypt(room);
}

/**
* Checks if the user has previously published cross-signing keys
*
* This means downloading the devicelist for the user and checking if the list includes
* the cross-signing pseudo-device.
*/
public userHasCrossSigningKeys(): Promise<boolean> {
if (!this.crypto) {
throw new Error("End-to-end encryption disabled");
}
return this.crypto.userHasCrossSigningKeys();
}

/**
* Checks whether cross signing:
* - is enabled on this account and trusted by this device
Expand Down
13 changes: 13 additions & 0 deletions src/crypto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,19 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
}
}

/**
* Checks if the user has previously published cross-signing keys
*
* This means downloading the devicelist for the user and checking if the list includes
* the cross-signing pseudo-device.
*
* @internal
*/
public async userHasCrossSigningKeys(): Promise<boolean> {
await this.downloadKeys([this.userId]);
return this.deviceList.getStoredCrossSigningForUser(this.userId) !== null;
}

/**
* Checks whether cross signing:
* - is enabled on this account and trusted by this device
Expand Down

0 comments on commit 39cf212

Please sign in to comment.