Skip to content
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
23 changes: 21 additions & 2 deletions src/client/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ describe("OAuth Authorization", () => {
access_token: "newaccess123",
token_type: "Bearer",
expires_in: 3600,
}
const validTokensWithNewRefreshToken = {
...validTokens,
refresh_token: "newrefresh123",
};

Expand All @@ -356,15 +359,15 @@ describe("OAuth Authorization", () => {
mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => validTokens,
json: async () => validTokensWithNewRefreshToken,
});

const tokens = await refreshAuthorization("https://auth.example.com", {
clientInformation: validClientInfo,
refreshToken: "refresh123",
});

expect(tokens).toEqual(validTokens);
expect(tokens).toEqual(validTokensWithNewRefreshToken);
expect(mockFetch).toHaveBeenCalledWith(
expect.objectContaining({
href: "https://auth.example.com/token",
Expand All @@ -384,6 +387,22 @@ describe("OAuth Authorization", () => {
expect(body.get("client_secret")).toBe("secret123");
});

it("exchanges refresh token for new tokens and keep existing refresh token if none is returned", async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => validTokens,
});

const refreshToken = "refresh123";
const tokens = await refreshAuthorization("https://auth.example.com", {
clientInformation: validClientInfo,
refreshToken,
});

expect(tokens).toEqual({ refresh_token: refreshToken, ...validTokens });
});

it("validates token response schema", async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
Expand Down
2 changes: 1 addition & 1 deletion src/client/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ export async function refreshAuthorization(
throw new Error(`Token refresh failed: HTTP ${response.status}`);
}

return OAuthTokensSchema.parse(await response.json());
return OAuthTokensSchema.parse({ refresh_token: refreshToken, ...(await response.json()) });
}

/**
Expand Down