From a327523d2329ebc14ab866627f5d74dd1dbfc69a Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 7 Jul 2022 13:54:14 -0600 Subject: [PATCH 1/2] Actually store the identity server in the client when given as an option --- spec/integ/matrix-client-methods.spec.js | 68 +++++++++++++++++++++++- src/client.ts | 7 ++- 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/spec/integ/matrix-client-methods.spec.js b/spec/integ/matrix-client-methods.spec.js index 5c9855b2ea6..c97e24a9774 100644 --- a/spec/integ/matrix-client-methods.spec.js +++ b/spec/integ/matrix-client-methods.spec.js @@ -27,11 +27,19 @@ describe("MatrixClient", function() { let store = null; const userId = "@alice:localhost"; const accessToken = "aseukfgwef"; + const idServerDomain = "identity.localhost"; // not a real server + const identityAccessToken = "woop-i-am-a-secret"; beforeEach(function() { store = new MemoryStore(); - const testClient = new TestClient(userId, "aliceDevice", accessToken, undefined, { store }); + const testClient = new TestClient(userId, "aliceDevice", accessToken, undefined, { + store, + identityServer: { + getAccessToken: () => Promise.resolve(identityAccessToken), + }, + idBaseUrl: `https://${idServerDomain}`, + }); httpBackend = testClient.httpBackend; client = testClient.client; }); @@ -1008,6 +1016,64 @@ describe("MatrixClient", function() { expect(await prom).toStrictEqual(response); }); }); + + describe("inviteByThreePid", () => { + it("should supply an id_access_token", async () => { + const targetEmail = "gerald@example.org"; + + httpBackend.when("GET", "/_matrix/client/versions").respond(200, { + versions: ["r0.6.0"], + }); + + httpBackend.when("POST", "/invite").check(req => { + expect(req.data).toStrictEqual({ + id_server: idServerDomain, + id_access_token: identityAccessToken, + medium: "email", + address: targetEmail, + }); + }).respond(200, {}); + + const prom = client.inviteByThreePid("!room:example.org", "email", targetEmail); + await httpBackend.flush(); + await prom; // returns empty object, so no validation needed + }); + }); + + describe("createRoom", () => { + it("should populate id_access_token on 3pid invites", async () => { + const targetEmail = "gerald@example.org"; + const response = { + room_id: "!room:localhost", + }; + const input = { + invite_3pid: [{ + // we intentionally exclude the access token here, so it can be populated for us + id_server: idServerDomain, + medium: "email", + address: targetEmail, + }], + }; + + httpBackend.when("GET", "/_matrix/client/versions").respond(200, { + versions: ["r0.6.0"], + }); + + httpBackend.when("POST", "/createRoom").check(req => { + expect(req.data).toMatchObject({ + invite_3pid: expect.arrayContaining([{ + ...input.invite_3pid[0], + id_access_token: identityAccessToken, + }]), + }); + expect(req.data.invite_3pid.length).toBe(1); + }).respond(200, response); + + const prom = client.createRoom(input); + await httpBackend.flush(); + expect(await prom).toStrictEqual(response); + }); + }); }); function withThreadId(event, newThreadId) { diff --git a/src/client.ts b/src/client.ts index e612fd27b56..7c92ba9ea09 100644 --- a/src/client.ts +++ b/src/client.ts @@ -946,6 +946,7 @@ export class MatrixClient extends TypedEventEmitter !i.id_access_token); if ( invitesNeedingToken.length > 0 && - this.identityServer && - this.identityServer.getAccessToken && + this.identityServer?.getAccessToken && await this.doesServerAcceptIdentityAccessToken() ) { const identityAccessToken = await this.identityServer.getAccessToken(); From 53b5e6fde73cfb2bb81321e1aaca10a1123edec9 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 7 Jul 2022 14:28:29 -0600 Subject: [PATCH 2/2] Update requestRegisterEmailToken to a modern spec version too --- spec/integ/matrix-client-methods.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/integ/matrix-client-methods.spec.js b/spec/integ/matrix-client-methods.spec.js index c97e24a9774..0dd33a02c3d 100644 --- a/spec/integ/matrix-client-methods.spec.js +++ b/spec/integ/matrix-client-methods.spec.js @@ -1001,7 +1001,7 @@ describe("MatrixClient", function() { }; httpBackend.when("GET", "/_matrix/client/versions").respond(200, { - versions: ["r0.5.0"], + versions: ["r0.6.0"], }); const prom = client.requestRegisterEmailToken("bob@email", "secret", 1);