Files
HealthLog/src/lib/google-health/oauth-state.ts
T
Marc-André Bombeck fb17a4e9c6 feat(google-health): add the Google Health API transport
Add the transport that reads a user's device data from the Google Health
API (health.googleapis.com/v4): Google OAuth with PKCE and offline
access, the paginated dataPoints reader with a daily-rollup fallback for
the types that lack a list method, the metric/activity/sleep/workout
mappers onto the shared measurement shape, and a response classifier
that separates retryable errors, hard rejects, and the re-consent case.

Access tokens live an hour and refresh tokens are reused, not rotated. A
refresh that comes back invalid_grant raises a distinct reauth-required
signal so the app can prompt a fresh consent rather than fail silently —
the periodic re-consent a Testing-mode Google client needs. The four
core readonly scopes ship by default; the Pixel Watch ECG and
irregular-rhythm scopes stay behind an opt-in flag.

Only the transport is new: the mapped-measurement output, the
upsert/dedup/rollup tail, and the source ladders are the shared ones.
2026-07-03 14:13:16 +02:00

39 lines
1.6 KiB
TypeScript

/**
* Shared constants + mint helper for the Google Health OAuth state nonce ledger.
* Mirrors `src/lib/whoop/oauth-state.ts`.
*
* The nonce is random + opaque; a short-lived `GoogleHealthOAuthState` row
* carries the `(nonce → userId)` mapping (plus the PKCE code verifier) so the
* user id and the verifier never leak into request logs or network captures via
* the OAuth `state` param. The connect route, the callback route, and the
* cleanup cron all read these constants so the contract has one source of truth
* and the unit tests can pin it without importing either route file.
*
* The string keys are deliberately distinct from the classic Fitbit provider's
* (`fitbit_state`) so the two coexisting integrations never collide on a cookie.
*/
import { randomBytes } from "node:crypto";
/**
* Cookie name carrying the in-flight state nonce. httpOnly + Secure +
* sameSite:lax at the connect route (mirrors the WHOOP cookie shape). Distinct
* from the classic Fitbit `fitbit_state` cookie.
*/
export const GOOGLE_HEALTH_OAUTH_STATE_COOKIE = "google_health_state" as const;
/**
* 10-minute TTL on the state row. Matches the cookie `maxAge`. Long enough to
* cover a user approving the Google consent prompt; short enough that an
* abandoned handshake doesn't strand a row for hours.
*/
export const GOOGLE_HEALTH_OAUTH_STATE_TTL_MS = 10 * 60 * 1000;
/**
* Mint a fresh state nonce. 16 random bytes → 22 base64url chars without
* padding — 128 bits of entropy, aligned with the OAuth 2.0 §10.10 "at least
* 128 bits" recommendation.
*/
export function mintGoogleHealthOAuthStateNonce(): string {
return randomBytes(16).toString("base64url");
}