Send authenticated /versions request (#3968)

* Send authenticated /versions request

Implements [MSC4026](https://github.com/matrix-org/matrix-spec-proposals/pull/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
This commit is contained in:
David Baker
2023-12-19 17:27:08 +00:00
committed by GitHub
parent 5e67a173c8
commit 6d1d04782a
2 changed files with 8 additions and 13 deletions
+2 -4
View File
@@ -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}`;
@@ -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
+6 -9
View File
@@ -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;
@@ -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;
}
/**