mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 07:42:23 +00:00
`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.
55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
/**
|
|
* The WHOOP OAuth client: consent URL, code exchange and refresh, over the
|
|
* shared factory (`integrations/oauth-kit/client.ts`).
|
|
*
|
|
* Token TTL is 3600 s and refresh tokens rotate: each refresh invalidates the
|
|
* prior access and refresh token, so the token lifecycle persists both. The
|
|
* `offline` scope is what returns a refresh token at all, and WHOOP wants it
|
|
* re-sent on the refresh.
|
|
*/
|
|
import {
|
|
defineOAuthClient,
|
|
type OAuthClientCredentials,
|
|
} from "@/lib/integrations/oauth-kit/client";
|
|
import { derivedRedirectUri } from "@/lib/integrations/oauth-kit/redirect-uri";
|
|
|
|
import { WhoopApiError } from "./response-classifier";
|
|
|
|
export type WhoopCredentials = OAuthClientCredentials;
|
|
|
|
/**
|
|
* Space-separated on the wire. `offline` is mandatory for a refresh token;
|
|
* the rest are the read-only collection scopes, all requested so the person
|
|
* grants once.
|
|
*/
|
|
export const WHOOP_OAUTH_SCOPE =
|
|
"offline read:recovery read:sleep read:workout read:cycles read:profile read:body_measurement" as const;
|
|
|
|
export interface WhoopTokenResponse {
|
|
access_token: string;
|
|
refresh_token: string;
|
|
expires_in: number;
|
|
scope?: string;
|
|
token_type?: string;
|
|
}
|
|
|
|
export const whoopOAuth = defineOAuthClient<WhoopTokenResponse>({
|
|
service: "whoop",
|
|
authorizeUrl: "https://api.prod.whoop.com/oauth/oauth2/auth",
|
|
tokenUrl: "https://api.prod.whoop.com/oauth/oauth2/token",
|
|
redirectUri: () =>
|
|
derivedRedirectUri("WHOOP_REDIRECT_URI", "/api/whoop/callback"),
|
|
authorizeParams: () => ({ scope: WHOOP_OAUTH_SCOPE }),
|
|
clientAuth: { kind: "form" },
|
|
extraFields: { refresh: { scope: "offline" } },
|
|
refresh: true,
|
|
error: (failure) =>
|
|
new WhoopApiError({
|
|
verb: failure.verb,
|
|
classification: failure.classification,
|
|
httpStatus: failure.status,
|
|
reason: failure.reason,
|
|
upstreamError: failure.upstreamError,
|
|
}),
|
|
});
|