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

Use correct /v3 prefix for /refresh #3016

Merged
merged 6 commits into from
Jun 3, 2023
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
90 changes: 90 additions & 0 deletions spec/unit/login.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import fetchMock from "fetch-mock-jest";

import { ClientPrefix, MatrixClient } from "../../src";
import { SSOAction } from "../../src/@types/auth";
import { TestClient } from "../TestClient";

function createExampleMatrixClient(): MatrixClient {
return new MatrixClient({
baseUrl: "https://example.com",
});
}

describe("Login request", function () {
let client: TestClient;

Expand Down Expand Up @@ -57,3 +66,84 @@ describe("SSO login URL", function () {
});
});
});

describe("refreshToken", () => {
davidisaaclee marked this conversation as resolved.
Show resolved Hide resolved
afterEach(() => {
fetchMock.mockReset();
});

it("requests the correctly-prefixed /refresh endpoint when server correctly accepts /v3", async () => {
const client = createExampleMatrixClient();

const response = {
access_token: "access_token",
refresh_token: "refresh_token",
expires_in_ms: 30000,
};

fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V3).toString(), response);
fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V1).toString(), () => {
throw new Error("/v1/refresh unexpectedly called");
});

const refreshResult = await client.refreshToken("initial_refresh_token");
expect(refreshResult).toEqual(response);
});

it("falls back to /v1 when server does not recognized /v3 refresh", async () => {
const client = createExampleMatrixClient();

const response = {
access_token: "access_token",
refresh_token: "refresh_token",
expires_in_ms: 30000,
};

fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V3).toString(), {
status: 400,
body: { errcode: "M_UNRECOGNIZED" },
});
fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V1).toString(), response);

const refreshResult = await client.refreshToken("initial_refresh_token");
expect(refreshResult).toEqual(response);
});

it("re-raises M_UNRECOGNIZED exceptions from /v1", async () => {
const client = createExampleMatrixClient();

fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V3).toString(), {
status: 400,
body: { errcode: "M_UNRECOGNIZED" },
});
fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V1).toString(), {
status: 400,
body: { errcode: "M_UNRECOGNIZED" },
});

expect(client.refreshToken("initial_refresh_token")).rejects.toMatchObject({ errcode: "M_UNRECOGNIZED" });
});

it("re-raises non-M_UNRECOGNIZED exceptions from /v3", async () => {
const client = createExampleMatrixClient();

fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V3).toString(), 429);
fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V1).toString(), () => {
throw new Error("/v1/refresh unexpectedly called");
});

expect(client.refreshToken("initial_refresh_token")).rejects.toMatchObject({ httpStatus: 429 });
});

it("re-raises non-M_UNRECOGNIZED exceptions from /v1", async () => {
const client = createExampleMatrixClient();

fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V3).toString(), {
status: 400,
body: { errcode: "M_UNRECOGNIZED" },
});
fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V1).toString(), 429);

expect(client.refreshToken("initial_refresh_token")).rejects.toMatchObject({ httpStatus: 429 });
});
});
31 changes: 21 additions & 10 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7689,16 +7689,27 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* @returns Rejects with an error response.
*/
public refreshToken(refreshToken: string): Promise<IRefreshTokenResponse> {
return this.http.authedRequest(
Method.Post,
"/refresh",
undefined,
{ refresh_token: refreshToken },
{
prefix: ClientPrefix.V1,
inhibitLogoutEmit: true, // we don't want to cause logout loops
},
);
const performRefreshRequestWithPrefix = (prefix: ClientPrefix): Promise<IRefreshTokenResponse> =>
this.http.authedRequest(
Method.Post,
"/refresh",
undefined,
{ refresh_token: refreshToken },
{
prefix,
inhibitLogoutEmit: true, // we don't want to cause logout loops
},
);

// First try with the (specced) /v3/ prefix.
// However, before Synapse 1.72.0, Synapse incorrectly required a /v1/ prefix, so we fall
// back to that if the request fails, for backwards compatibility.
return performRefreshRequestWithPrefix(ClientPrefix.V3).catch((e) => {
if (e.errcode === "M_UNRECOGNIZED") {
return performRefreshRequestWithPrefix(ClientPrefix.V1);
}
throw e;
});
}

/**
Expand Down