|
| 1 | +import { |
| 2 | + ArcticFetchError, |
| 3 | + createOAuth2Request, |
| 4 | + createOAuth2RequestError, |
| 5 | + UnexpectedErrorResponseBodyError, |
| 6 | + UnexpectedResponseError |
| 7 | +} from "../request.js"; |
| 8 | +import { OAuth2Tokens } from "../oauth2.js"; |
| 9 | + |
| 10 | +const authorizationEndpoint = "https://account.withings.com/oauth2_user/authorize2"; |
| 11 | +const tokenEndpoint = "https://wbsapi.withings.net/v2/oauth2"; |
| 12 | + |
| 13 | +export class Withings { |
| 14 | + private clientId: string; |
| 15 | + private clientSecret: string; |
| 16 | + private redirectURI: string; |
| 17 | + |
| 18 | + constructor(clientId: string, clientSecret: string, redirectURI: string) { |
| 19 | + this.clientId = clientId; |
| 20 | + this.clientSecret = clientSecret; |
| 21 | + this.redirectURI = redirectURI; |
| 22 | + } |
| 23 | + |
| 24 | + public createAuthorizationURL(state: string, scopes: string[]): URL { |
| 25 | + const url = new URL(authorizationEndpoint); |
| 26 | + url.searchParams.set("response_type", "code"); |
| 27 | + url.searchParams.set("client_id", this.clientId); |
| 28 | + url.searchParams.set("state", state); |
| 29 | + // Withings deviates from the RFC and uses a comma-delimitated string instead of spaces. |
| 30 | + if (scopes.length > 0) { |
| 31 | + url.searchParams.set("scope", scopes.join(",")); |
| 32 | + } |
| 33 | + url.searchParams.set("redirect_uri", this.redirectURI); |
| 34 | + return url; |
| 35 | + } |
| 36 | + |
| 37 | + public async validateAuthorizationCode(code: string): Promise<OAuth2Tokens> { |
| 38 | + const body = new URLSearchParams(); |
| 39 | + // Withings requires an `action` parameter. |
| 40 | + body.set("action", "requesttoken"); |
| 41 | + body.set("grant_type", "authorization_code"); |
| 42 | + body.set("code", code); |
| 43 | + body.set("redirect_uri", this.redirectURI); |
| 44 | + body.set("client_id", this.clientId); |
| 45 | + body.set("client_secret", this.clientSecret); |
| 46 | + const request = createOAuth2Request(tokenEndpoint, body); |
| 47 | + const tokens = await sendTokenRequest(request); |
| 48 | + return tokens; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +async function sendTokenRequest(request: Request): Promise<OAuth2Tokens> { |
| 53 | + let response: Response; |
| 54 | + try { |
| 55 | + response = await fetch(request); |
| 56 | + } catch (e) { |
| 57 | + throw new ArcticFetchError(e); |
| 58 | + } |
| 59 | + |
| 60 | + // Withings returns a 200 even for error responses. |
| 61 | + if (response.status !== 200) { |
| 62 | + if (response.body !== null) { |
| 63 | + await response.body.cancel(); |
| 64 | + } |
| 65 | + throw new UnexpectedResponseError(response.status); |
| 66 | + } |
| 67 | + |
| 68 | + let data: unknown; |
| 69 | + try { |
| 70 | + data = await response.json(); |
| 71 | + } catch { |
| 72 | + throw new UnexpectedResponseError(response.status); |
| 73 | + } |
| 74 | + if (typeof data !== "object" || data === null) { |
| 75 | + throw new UnexpectedErrorResponseBodyError(response.status, data); |
| 76 | + } |
| 77 | + |
| 78 | + // Withings returns an `error` field but the value deviates from the RFC. |
| 79 | + // Probably better to throw `UnexpectedErrorResponseBodyError` |
| 80 | + // but we're keeping `OAuth2RequestError` for now to be consistent with the other providers. |
| 81 | + if ("error" in data && typeof data.error === "string") { |
| 82 | + let error: Error; |
| 83 | + try { |
| 84 | + error = createOAuth2RequestError(data); |
| 85 | + } catch { |
| 86 | + throw new UnexpectedErrorResponseBodyError(response.status, data); |
| 87 | + } |
| 88 | + throw error; |
| 89 | + } |
| 90 | + |
| 91 | + // Withings returns `{"status": 0, "body": {...}}`. |
| 92 | + if (!("body" in data) || typeof data.body !== "object" || data.body === null) { |
| 93 | + throw new Error("Missing or invalid 'body' field"); |
| 94 | + } |
| 95 | + const tokens = new OAuth2Tokens(data.body); |
| 96 | + return tokens; |
| 97 | +} |
0 commit comments