Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actually store the identity server in the client when given as an option #2503

Merged
merged 3 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 68 additions & 2 deletions spec/integ/matrix-client-methods.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down Expand Up @@ -993,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);
Expand All @@ -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) {
Expand Down
7 changes: 3 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

this.baseUrl = opts.baseUrl;
this.idBaseUrl = opts.idBaseUrl;
this.identityServer = opts.identityServer;

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

if (
this.identityServer &&
this.identityServer.getAccessToken &&
this.identityServer?.getAccessToken &&
await this.doesServerAcceptIdentityAccessToken()
) {
const identityAccessToken = await this.identityServer.getAccessToken();
Expand Down Expand Up @@ -7196,8 +7196,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
.filter(i => !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();
Expand Down