Skip to content

Commit

Permalink
Send authenticated /versions request (#3968)
Browse files Browse the repository at this point in the history
* Send authenticated /versions request

Implements [MSC4026](matrix-org/matrix-spec-proposals#4026).

I believe this probably is as simple as this: it will mean that the versions
response can obviously change after logging in, but since the client is
constructed again with an access token, this should just work (?)

A remaining question is whether this needs to be optional. Opening the PR
to prompt the discussion. Apps might not expect it, but it's just the same
auth that we're sending to other endpoints on the same server.

* Fix tests

* Clear /versions cache on access token set
  • Loading branch information
dbkr committed Dec 19, 2023
1 parent 5e67a17 commit 6d1d047
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
6 changes: 2 additions & 4 deletions spec/unit/matrix-client.spec.ts
Expand Up @@ -2211,8 +2211,7 @@ describe("MatrixClient", function () {
"org.matrix.msc3391": true,
},
};
jest.spyOn(client.http, "request").mockResolvedValue(versionsResponse);
const requestSpy = jest.spyOn(client.http, "authedRequest").mockImplementation(() => Promise.resolve());
const requestSpy = jest.spyOn(client.http, "authedRequest").mockResolvedValue(versionsResponse);
const unstablePrefix = "/_matrix/client/unstable/org.matrix.msc3391";
const path = `/user/${encodeURIComponent(userId)}/account_data/${eventType}`;

Expand Down Expand Up @@ -2250,8 +2249,7 @@ describe("MatrixClient", function () {
"org.matrix.msc3391": false,
},
};
jest.spyOn(client.http, "request").mockResolvedValue(versionsResponse);
const requestSpy = jest.spyOn(client.http, "authedRequest").mockImplementation(() => Promise.resolve());
const requestSpy = jest.spyOn(client.http, "authedRequest").mockResolvedValue(versionsResponse);
const path = `/user/${encodeURIComponent(userId)}/account_data/${eventType}`;

// populate version support
Expand Down
15 changes: 6 additions & 9 deletions src/client.ts
Expand Up @@ -7450,16 +7450,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
return this.serverVersionsPromise;
}

// We send an authenticated request as of MSC4026
this.serverVersionsPromise = this.http
.request<IServerVersions>(
Method.Get,
"/_matrix/client/versions",
undefined, // queryParams
undefined, // data
{
prefix: "",
},
)
.authedRequest<IServerVersions>(Method.Get, "/_matrix/client/versions", undefined, undefined, {
prefix: "",
})
.catch((e) => {
// Need to unset this if it fails, otherwise we'll never retry
this.serverVersionsPromise = undefined;
Expand Down Expand Up @@ -7732,6 +7727,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
*/
public setAccessToken(token: string): void {
this.http.opts.accessToken = token;
// The /versions response can vary for different users so clear the cache
this.serverVersionsPromise = undefined;
}

/**
Expand Down

0 comments on commit 6d1d047

Please sign in to comment.