/** * Create a PKCE verifier and its S256 challenge. * * Only the challenge travels to Garmin. Because the authorization code comes * back through the athlete's browser, someone able to read it there still * cannot redeem it without the verifier, which never left the server. */ export declare function createPkcePair(): PkcePair; /** * Read activity summaries out of a push payload. * * Garmin posts `{ activities: [...] }`, and has historically also posted a * bare array. Accepting both costs nothing and avoids dropping a batch over a * shape the caller did not anticipate. Entries without a `summaryId` are * dropped, since without it a redelivery cannot be recognised. */ export declare function extractActivitySummaries(body: unknown): GarminActivitySummary[]; /** Read deregistration notices out of a payload. */ export declare function extractDeregistrations(body: unknown): GarminDeregistration[]; /** * Compare a presented webhook secret against the expected one. * * Constant-time: a byte-by-byte comparison leaks how much of a guess was * correct, which is enough to construct the rest. Returns false when no secret * is configured, so an endpoint that forgot to set one rejects everything * rather than accepting everything. */ export declare function isAuthenticWebhook(presented: string | null | undefined, secret: string): boolean; /** * Sign a value so it can be handed to a browser and trusted on return. * * The OAuth callback is a top-level navigation carrying no session, so the * application has to stash who was connecting somewhere the browser holds. * Signing is what stops someone editing that stash to attach their watch to * another person's account. */ export declare function signState(payload: string, secret: string): string; /** Verify and decode a value produced by {@link signState}, or null if it was altered. */ export declare function verifyState(token: string | undefined, secret: string): string | null; /** Is a token past, or nearly past, its expiry? */ export declare function isTokenExpired(tokens: GarminTokens, skewSeconds?: number, now?: number): boolean; /** * Seal an in-flight attempt into a token safe to hand to a browser. * * The callback is a top-level navigation from Garmin carrying no session, so * the app has to stash who was connecting somewhere the browser holds - which * means the value is under the visitor's control and must be signed. Without a * signature someone edits the subject in their own cookie and attaches their * watch to another person's account. * * This is {@link signState} with the shape the flow actually needs: a subject, * the state parameter, the PKCE verifier, and an issue time so a stale attempt * can be refused instead of silently resumed. */ export declare function sealOAuthAttempt(attempt: OAuthAttempt, secret: string): string; /** * Open a sealed attempt, or return null when it was tampered with, malformed, * or has expired. Null always means "start again", never "trust it anyway". */ export declare function openOAuthAttempt(token: string | undefined, secret: string, options?: { now?: number, ttlMs?: number }): OAuthAttempt | null; /** Endpoints from Garmin's OAuth 2.0 PKCE specification. */ export declare const GARMIN_ENDPOINTS: { authorize: 'https://connect.garmin.com/oauth2Confirm'; token: 'https://diauth.garmin.com/di-oauth2-service/oauth/token'; userId: 'https://apis.garmin.com/wellness-api/rest/user/id'; permissions: 'https://apis.garmin.com/wellness-api/rest/user/permissions'; registration: 'https://apis.garmin.com/wellness-api/rest/user/registration'; backfillActivities: 'https://apis.garmin.com/wellness-api/rest/backfill/activities' }; /** How long an in-flight attempt stays valid, in milliseconds. */ export declare const OAUTH_ATTEMPT_TTL_MS: number; export declare interface GarminActivityApiConfig { clientId: string clientSecret: string redirectUri: string scope?: string endpoints?: Partial fetch?: typeof globalThis.fetch } export declare interface GarminTokens { accessToken: string refreshToken?: string expiresAt?: number scope?: string } /** A PKCE verifier and the challenge derived from it. */ export declare interface PkcePair { verifier: string challenge: string } /** * An activity summary as the Activity API pushes it. * * Field names are Garmin's. Everything except `summaryId` is optional, because * a summary reflects what the device recorded: a treadmill run carries no GPS, * a manually entered activity carries almost nothing. */ export declare interface GarminActivitySummary { summaryId: string activityId?: number userId?: string userAccessToken?: string activityType?: string activityName?: string startTimeInSeconds?: number startTimeOffsetInSeconds?: number durationInSeconds?: number distanceInMeters?: number activeKilocalories?: number averageHeartRateInBeatsPerMinute?: number maxHeartRateInBeatsPerMinute?: number averageSpeedInMetersPerSecond?: number maxSpeedInMetersPerSecond?: number averageRunCadenceInStepsPerMinute?: number averageBikeCadenceInRoundsPerMinute?: number averagePaceInMinutesPerKilometer?: number totalElevationGainInMeters?: number totalElevationLossInMeters?: number startingLatitudeInDegree?: number startingLongitudeInDegree?: number steps?: number deviceName?: string manual?: boolean isParent?: boolean parentSummaryId?: string isWebUpload?: boolean } /** Garmin tells an application when an athlete revokes it. */ export declare interface GarminDeregistration { userId: string userAccessToken?: string } /** An in-flight authorization attempt, as it survives the round trip to Garmin. */ export declare interface OAuthAttempt { subject: TSubject state: string verifier: string issuedAt: number } /** * A client for the official Garmin Activity API. * * Holds no state beyond its configuration: tokens are passed in and returned, * so the caller decides where they live. That keeps the client usable from a * request handler, a job, or a test without assuming a database. */ export declare class GarminActivityApiClient { constructor(config: GarminActivityApiConfig); get isConfigured(): boolean; buildAuthorizationUrl(options: { state: string, challenge: string, scope?: string }): string; exchangeCode(code: string, verifier: string): Promise; refreshTokens(refreshToken: string): Promise; getUserId(accessToken: string): Promise; getPermissions(accessToken: string): Promise; deregister(accessToken: string): Promise; requestBackfill(accessToken: string, start: Date, end: Date): Promise; }