Skip to content

Commit 72f9a51

Browse files
authored
Actually store the identity server in the client when given as an option (#2503)
* Actually store the identity server in the client when given as an option * Update requestRegisterEmailToken to a modern spec version too
1 parent efdda84 commit 72f9a51

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

spec/integ/matrix-client-methods.spec.js

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,19 @@ describe("MatrixClient", function() {
2727
let store = null;
2828
const userId = "@alice:localhost";
2929
const accessToken = "aseukfgwef";
30+
const idServerDomain = "identity.localhost"; // not a real server
31+
const identityAccessToken = "woop-i-am-a-secret";
3032

3133
beforeEach(function() {
3234
store = new MemoryStore();
3335

34-
const testClient = new TestClient(userId, "aliceDevice", accessToken, undefined, { store });
36+
const testClient = new TestClient(userId, "aliceDevice", accessToken, undefined, {
37+
store,
38+
identityServer: {
39+
getAccessToken: () => Promise.resolve(identityAccessToken),
40+
},
41+
idBaseUrl: `https://${idServerDomain}`,
42+
});
3543
httpBackend = testClient.httpBackend;
3644
client = testClient.client;
3745
});
@@ -993,7 +1001,7 @@ describe("MatrixClient", function() {
9931001
};
9941002

9951003
httpBackend.when("GET", "/_matrix/client/versions").respond(200, {
996-
versions: ["r0.5.0"],
1004+
versions: ["r0.6.0"],
9971005
});
9981006

9991007
const prom = client.requestRegisterEmailToken("bob@email", "secret", 1);
@@ -1008,6 +1016,64 @@ describe("MatrixClient", function() {
10081016
expect(await prom).toStrictEqual(response);
10091017
});
10101018
});
1019+
1020+
describe("inviteByThreePid", () => {
1021+
it("should supply an id_access_token", async () => {
1022+
const targetEmail = "[email protected]";
1023+
1024+
httpBackend.when("GET", "/_matrix/client/versions").respond(200, {
1025+
versions: ["r0.6.0"],
1026+
});
1027+
1028+
httpBackend.when("POST", "/invite").check(req => {
1029+
expect(req.data).toStrictEqual({
1030+
id_server: idServerDomain,
1031+
id_access_token: identityAccessToken,
1032+
medium: "email",
1033+
address: targetEmail,
1034+
});
1035+
}).respond(200, {});
1036+
1037+
const prom = client.inviteByThreePid("!room:example.org", "email", targetEmail);
1038+
await httpBackend.flush();
1039+
await prom; // returns empty object, so no validation needed
1040+
});
1041+
});
1042+
1043+
describe("createRoom", () => {
1044+
it("should populate id_access_token on 3pid invites", async () => {
1045+
const targetEmail = "[email protected]";
1046+
const response = {
1047+
room_id: "!room:localhost",
1048+
};
1049+
const input = {
1050+
invite_3pid: [{
1051+
// we intentionally exclude the access token here, so it can be populated for us
1052+
id_server: idServerDomain,
1053+
medium: "email",
1054+
address: targetEmail,
1055+
}],
1056+
};
1057+
1058+
httpBackend.when("GET", "/_matrix/client/versions").respond(200, {
1059+
versions: ["r0.6.0"],
1060+
});
1061+
1062+
httpBackend.when("POST", "/createRoom").check(req => {
1063+
expect(req.data).toMatchObject({
1064+
invite_3pid: expect.arrayContaining([{
1065+
...input.invite_3pid[0],
1066+
id_access_token: identityAccessToken,
1067+
}]),
1068+
});
1069+
expect(req.data.invite_3pid.length).toBe(1);
1070+
}).respond(200, response);
1071+
1072+
const prom = client.createRoom(input);
1073+
await httpBackend.flush();
1074+
expect(await prom).toStrictEqual(response);
1075+
});
1076+
});
10111077
});
10121078

10131079
function withThreadId(event, newThreadId) {

src/client.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
940940

941941
this.baseUrl = opts.baseUrl;
942942
this.idBaseUrl = opts.idBaseUrl;
943+
this.identityServer = opts.identityServer;
943944

944945
this.usingExternalCrypto = opts.usingExternalCrypto;
945946
this.store = opts.store || new StubStore();
@@ -4808,8 +4809,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
48084809
};
48094810

48104811
if (
4811-
this.identityServer &&
4812-
this.identityServer.getAccessToken &&
4812+
this.identityServer?.getAccessToken &&
48134813
await this.doesServerAcceptIdentityAccessToken()
48144814
) {
48154815
const identityAccessToken = await this.identityServer.getAccessToken();
@@ -7196,8 +7196,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
71967196
.filter(i => !i.id_access_token);
71977197
if (
71987198
invitesNeedingToken.length > 0 &&
7199-
this.identityServer &&
7200-
this.identityServer.getAccessToken &&
7199+
this.identityServer?.getAccessToken &&
72017200
await this.doesServerAcceptIdentityAccessToken()
72027201
) {
72037202
const identityAccessToken = await this.identityServer.getAccessToken();

0 commit comments

Comments
 (0)