refactor(integrations): move every provider OAuth client onto one factory

`oauth-kit/client.ts` builds a provider's consent URL, code exchange,
refresh and deauthorization from a declaration of what differs: endpoints
and scope, client authentication (form or HTTP Basic, with or without the
client id beside it), the fields a grant adds, how an answer is classified
and the refusal named, the error type, and where the token sits in the
body. The seven hand-written copies of the redirect URI, authorization URL,
token post, Basic header and PKCE pair are gone; each provider keeps an
`oauth.ts` with its declaration.

Shared behaviour is now uniform: a 2xx without a token object (an empty or
non-JSON body) is a transient `empty_token_body` failure for every
provider. WHOOP, Strava and Google Health used to return null, and
Withings threw an unclassified SyntaxError; the OAuth wire golden records
exactly these lines changing and nothing else. Oura's live/legacy endpoint
fallback, Google's invalid_grant lift, Strava's Fault summary and Fitbit's
errorType are declared per provider.
This commit is contained in:
Marc-André Bombeck committed 2026-10-23 12:57:47 +02:00
1 parent 1a84cf27a8
commit b6f45c0d88
89 files changed
+1690 -1764

No files matched your search

+6 -7
View File
@@ -7,12 +7,9 @@ import { apiHandler, requireAuth } from "@/lib/api-handler";
import { annotate, getEvent } from "@/lib/logging/context";
import { auditLog } from "@/lib/auth/audit";
import { encrypt } from "@/lib/crypto";
import {
exchangeCode,
fetchProfile,
resolveFitbitUserId,
FITBIT_OAUTH_SCOPE,
} from "@/lib/fitbit/client";
import { fetchProfile, resolveFitbitUserId } from "@/lib/fitbit/client";
import { fitbitOAuth } from "@/lib/fitbit/oauth";
import { FITBIT_OAUTH_SCOPE } from "@/lib/fitbit/oauth";
import { getUserFitbitCredentials } from "@/lib/fitbit/credentials";
import { FITBIT_OAUTH_STATE_COOKIE } from "@/lib/fitbit/oauth-state";
import { FITBIT_BACKFILL_QUEUE } from "@/lib/fitbit/jobs/queues";
@@ -150,7 +147,9 @@ export const GET = apiHandler(async (request: NextRequest) => {
return ERR("nocreds");
}
const tokens = await exchangeCode(code, consumed.codeVerifier, creds);
const tokens = await fitbitOAuth.exchange(code, creds, {
codeVerifier: consumed.codeVerifier,
});
// The initial code exchange MUST return a refresh token. If Fitbit ever
// omits it, refuse to persist — a stored empty refresh token silently bricks
@@ -27,10 +27,15 @@ vi.mock("@/lib/fitbit/credentials", () => ({
getUserFitbitCredentials: vi.fn(),
}));
vi.mock("@/lib/fitbit/client", () => ({
getAuthorizationUrl: vi.fn(
() => "https://www.fitbit.com/oauth2/authorize?state=nonce-stub",
),
vi.mock("@/lib/fitbit/oauth", () => ({
fitbitOAuth: {
authorizationUrl: vi.fn(
() => "https://www.fitbit.com/oauth2/authorize?state=nonce-stub",
),
},
}));
vi.mock("@/lib/integrations/oauth-kit/pkce", () => ({
generatePkcePair: vi.fn(() => ({
verifier: "verifier-stub",
challenge: "challenge-stub",
+3 -2
View File
@@ -2,7 +2,8 @@ import { apiHandler, requireAuth } from "@/lib/api-handler";
import { prisma } from "@/lib/db";
import { annotate, getEvent } from "@/lib/logging/context";
import { checkRateLimit } from "@/lib/rate-limit";
import { getAuthorizationUrl, generatePkcePair } from "@/lib/fitbit/client";
import { fitbitOAuth } from "@/lib/fitbit/oauth";
import { generatePkcePair } from "@/lib/integrations/oauth-kit/pkce";
import { getUserFitbitCredentials } from "@/lib/fitbit/credentials";
import {
FITBIT_OAUTH_STATE_COOKIE,
@@ -94,7 +95,7 @@ export const GET = apiHandler(async (req: NextRequest) => {
);
}
const url = getAuthorizationUrl(nonce, creds, pkce.challenge);
const url = fitbitOAuth.authorizationUrl(nonce, creds, pkce.challenge);
const response = NextResponse.redirect(url);
response.cookies.set(FITBIT_OAUTH_STATE_COOKIE, nonce, {