mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +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.
65 lines
2.2 KiB
TypeScript
65 lines
2.2 KiB
TypeScript
/**
|
|
* Where a Strava grant lives and how its token endpoint is called, for the
|
|
* shared token lifecycle (`src/lib/integrations/oauth-kit/token.ts`).
|
|
*
|
|
* Strava's tokens sit on `User`, with no expiry column: the sync refreshes
|
|
* after a refused read, and Strava rotates the refresh token on every use.
|
|
*/
|
|
import {
|
|
obtainAccessToken,
|
|
type OAuthTokenDescriptor,
|
|
} from "@/lib/integrations/oauth-kit/token";
|
|
import { type StravaCredentials, stravaOAuth } from "./oauth";
|
|
import { getStravaClientCredentials } from "./credentials";
|
|
|
|
export const stravaTokenDescriptor: OAuthTokenDescriptor<StravaCredentials> = {
|
|
provider: "strava",
|
|
tracksExpiry: false,
|
|
expiryBufferMs: 0,
|
|
async readGrant(db, userId) {
|
|
const row = await db.user.findUnique({
|
|
where: { id: userId },
|
|
select: {
|
|
stravaAccessTokenEncrypted: true,
|
|
stravaRefreshTokenEncrypted: true,
|
|
},
|
|
});
|
|
if (!row?.stravaAccessTokenEncrypted || !row.stravaRefreshTokenEncrypted) {
|
|
return null;
|
|
}
|
|
return {
|
|
accessCiphertext: row.stravaAccessTokenEncrypted,
|
|
refreshCiphertext: row.stravaRefreshTokenEncrypted,
|
|
expiresAt: null,
|
|
};
|
|
},
|
|
async writeGrant(db, userId, spent, next) {
|
|
const { count } = await db.user.updateMany({
|
|
where: { id: userId, stravaRefreshTokenEncrypted: spent },
|
|
data: {
|
|
stravaAccessTokenEncrypted: next.accessCiphertext,
|
|
stravaRefreshTokenEncrypted: next.refreshCiphertext,
|
|
},
|
|
});
|
|
return count;
|
|
},
|
|
loadCredentials: (userId) => getStravaClientCredentials(userId),
|
|
refresh: (refreshToken, credentials) =>
|
|
stravaOAuth.refresh(refreshToken, credentials),
|
|
};
|
|
|
|
/**
|
|
* Refresh after Strava refused the token read with `spentRefreshCiphertext`,
|
|
* once however many syncs ask together. Returns the access token to retry
|
|
* with, or null when there is nothing to retry with. A refused refresh throws.
|
|
*/
|
|
export async function refreshStravaAfterRefusal(
|
|
userId: string,
|
|
spentRefreshCiphertext: string,
|
|
): Promise<string | null> {
|
|
const result = await obtainAccessToken(stravaTokenDescriptor, userId, {
|
|
afterRefusalOf: spentRefreshCiphertext,
|
|
});
|
|
return result.kind === "token" ? result.accessToken : null;
|
|
}
|