Skip to content

Commit

Permalink
Feat remove support for unstable MSC3882
Browse files Browse the repository at this point in the history
  • Loading branch information
hughns committed Sep 27, 2023
1 parent fbb29f4 commit e163e53
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 68 deletions.
31 changes: 1 addition & 30 deletions spec/integ/matrix-client-methods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1204,9 +1204,6 @@ describe("MatrixClient", function () {

describe("requestLoginToken", () => {
it("should hit the expected API endpoint with UIA", async () => {
httpBackend
.when("GET", "/capabilities")
.respond(200, { capabilities: { "m.get_login_token": { enabled: true } } });
const response = {};
const uiaData = {};
const prom = client.requestLoginToken(uiaData);
Expand All @@ -1216,37 +1213,11 @@ describe("MatrixClient", function () {
});

it("should hit the expected API endpoint without UIA", async () => {
httpBackend
.when("GET", "/capabilities")
.respond(200, { capabilities: { "m.get_login_token": { enabled: true } } });
const response = { login_token: "xyz", expires_in_ms: 5000 };
const prom = client.requestLoginToken();
httpBackend.when("POST", "/v1/login/get_token", {}).respond(200, response);
await httpBackend.flush("");
// check that expires_in has been populated for compatibility with r0
expect(await prom).toStrictEqual({ ...response, expires_in: 5 });
});

it("should hit the stable endpoint when capability is disabled", async () => {
httpBackend
.when("GET", "/capabilities")
.respond(200, { capabilities: { "m.get_login_token": { enabled: false } } });
const response = { login_token: "xyz", expires_in_ms: 5000 };
const prom = client.requestLoginToken();
httpBackend.when("POST", "/v1/login/get_token", {}).respond(200, response);
await httpBackend.flush("");
// check that expires_in has been populated for compatibility with r0
expect(await prom).toStrictEqual({ ...response, expires_in: 5 });
});

it("should hit the r0 endpoint for fallback", async () => {
httpBackend.when("GET", "/capabilities").respond(200, {});
const response = { login_token: "xyz", expires_in: 5 };
const prom = client.requestLoginToken();
httpBackend.when("POST", "/unstable/org.matrix.msc3882/login/token", {}).respond(200, response);
await httpBackend.flush("");
// check that expires_in has been populated for compatibility with r1
expect(await prom).toStrictEqual({ ...response, expires_in_ms: 5000 });
expect(await prom).toStrictEqual(response);
});
});

Expand Down
7 changes: 0 additions & 7 deletions src/@types/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,12 @@ export interface LoginResponse {

/**
* The result of a successful `m.login.token` issuance request as per https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv1loginget_token
*
*/
export interface LoginTokenPostResponse {
/**
* The token to use with `m.login.token` to authenticate.
*/
login_token: string;
/**
* Expiration in seconds.
*
* @deprecated this is only provided for compatibility with original revision of [MSC3882](https://github.com/matrix-org/matrix-spec-proposals/pull/3882).
*/
expires_in: number;
/**
* Expiration in milliseconds.
*/
Expand Down
36 changes: 5 additions & 31 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8003,49 +8003,23 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

/**
* Make a request for an `m.login.token` to be issued as per
* [MSC3882](https://github.com/matrix-org/matrix-spec-proposals/pull/3882).
* The server may require User-Interactive auth.
* https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv1loginget_token
*
* Compatibility with unstable implementations of MSC3882 is deprecated and will be removed in a future release.
* The server may require User-Interactive auth.
*
* @param auth - Optional. Auth data to supply for User-Interactive auth.
* @returns Promise which resolves: On success, the token response
* or UIA auth data.
*/
public async requestLoginToken(auth?: AuthDict): Promise<UIAResponse<LoginTokenPostResponse>> {
// use capabilities to determine which revision of the MSC is being used
const capabilities = await this.getCapabilities();

const usesStable = !!capabilities["m.get_login_token"];
const usesUnstableRevision1 = !usesStable && !!capabilities["org.matrix.msc3882.get_login_token"];
// use stable endpoint if capability is exposed otherwise use old unstable r0 endpoint
const endpoint =
usesStable || usesUnstableRevision1
? "/login/get_token" // endpoint for stable + unstable r1
: "/login/token"; // unstable r0 endpoint

// determine whether to use stable or unstable prefix
const prefix = usesStable ? ClientPrefix.V1 : `${ClientPrefix.Unstable}/org.matrix.msc3882`;

const body: UIARequest<{}> = { auth };
const res = await this.http.authedRequest<UIAResponse<LoginTokenPostResponse>>(
return this.http.authedRequest<UIAResponse<LoginTokenPostResponse>>(
Method.Post,
endpoint,
"/login/get_token",
undefined, // no query params
body,
{ prefix },
{ prefix: ClientPrefix.V1 },
);

// the representation of expires_in changed from unstable revision 0 to unstable revision 1 so we cross populate
if ("login_token" in res) {
if (typeof res.expires_in_ms === "number") {
res.expires_in = Math.floor(res.expires_in_ms / 1000);
} else if (typeof res.expires_in === "number") {
res.expires_in_ms = res.expires_in * 1000;
}
}

return res;
}

/**
Expand Down

0 comments on commit e163e53

Please sign in to comment.