File size: 10,705 Bytes
f0634fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
 * Device Code OAuth flow β€” pure HTTP wrappers.
 *
 * Three endpoints, all POST form-encoded to the OAuth host:
 *  - `/api/oauth/device_authorization` β†’ DeviceAuthorization
 *  - `/api/oauth/token` (grant_type=device_code) β†’ polling result
 *  - `/api/oauth/token` (grant_type=refresh_token) β†’ refreshed TokenInfo
 *
 * No state is kept here β€” `OAuthManager` drives the flow and decides
 * when to poll / refresh / store.
 */

import { extractApiErrorMessage } from './api-error';
import {
  OAuthConnectionError,
  OAuthError,
  OAuthUnauthorizedError,
  RetryableRefreshError,
} from './errors';
import type { DeviceAuthorization, DeviceHeaders, OAuthFlowConfig, OAuthRequestHeaders, TokenInfo } from './types';
import { isRecord } from './utils';

const RETRYABLE_STATUSES = new Set([429, 500, 502, 503, 504]);

function pickErrorDetail(data: Record<string, unknown>): string {
  return extractApiErrorMessage(data) ?? 'unknown';
}

function tokenFromResponse(payload: Record<string, unknown>): TokenInfo {
  // Required-field validation. Reject responses that are missing
  // any of the three load-bearing fields rather than persisting empty
  // strings that will fail mysteriously later.
  const accessToken = payload['access_token'];
  if (typeof accessToken !== 'string' || accessToken.length === 0) {
    throw new OAuthError('OAuth response missing access_token');
  }
  const refreshToken = payload['refresh_token'];
  if (typeof refreshToken !== 'string' || refreshToken.length === 0) {
    throw new OAuthError('OAuth response missing refresh_token');
  }
  const expiresInRaw = payload['expires_in'];
  const expiresIn = Number(expiresInRaw);
  if (!Number.isFinite(expiresIn) || expiresIn <= 0) {
    throw new OAuthError('OAuth response missing or invalid expires_in');
  }
  return {
    accessToken,
    refreshToken,
    expiresAt: Math.floor(Date.now() / 1000) + expiresIn,
    scope: typeof payload['scope'] === 'string' ? payload['scope'] : '',
    tokenType: typeof payload['token_type'] === 'string' ? payload['token_type'] : 'Bearer',
    expiresIn,
  };
}

/** HTTP client default timeout for OAuth requests. */
const DEFAULT_HTTP_TIMEOUT_MS = 30_000;

async function postForm(
  url: string,
  params: Record<string, string>,
  deviceHeaders?: OAuthRequestHeaders | undefined,
  options?: { timeoutMs?: number; signal?: AbortSignal },
): Promise<{ status: number; data: Record<string, unknown> }> {
  const timeoutMs = options?.timeoutMs ?? DEFAULT_HTTP_TIMEOUT_MS;
  const body = new URLSearchParams(params).toString();
  // Compose a timeout signal with the optional caller signal.
  const signals: AbortSignal[] = [AbortSignal.timeout(timeoutMs)];
  if (options?.signal !== undefined) signals.push(options.signal);
  const signal = AbortSignal.any(signals);
  let response: Response;
  try {
    response = await fetch(url, {
      method: 'POST',
      headers: {
        ...deviceHeaders,
        'Content-Type': 'application/x-www-form-urlencoded',
        Accept: 'application/json',
      },
      body,
      signal,
    });
  } catch (error) {
    throw new OAuthConnectionError(
      `OAuth request to ${url} failed: ${describeFetchFailure(error)}`,
      { cause: error },
    );
  }
  const status = response.status;
  let data: Record<string, unknown> = {};
  try {
    const parsed: unknown = await response.json();
    if (isRecord(parsed)) data = parsed;
  } catch {
    // Non-JSON response β€” leave data empty; caller interprets by status.
  }
  return { status, data };
}

/**
 * Flatten a fetch rejection into a readable message. undici throws a generic
 * `TypeError: fetch failed` and hides the real reason (DNS, refused, TLS,
 * timeout) in a nested `cause` chain β€” walk it so the surfaced message names
 * the actual failure instead of just "fetch failed".
 */
function describeFetchFailure(error: unknown): string {
  if (!(error instanceof Error)) return String(error);
  const messages = new Set<string>();
  let current: Error | undefined = error;
  while (current !== undefined) {
    messages.add(current.message);
    current = current.cause instanceof Error ? current.cause : undefined;
  }
  return [...messages].join(': ');
}

// ── requestDeviceAuthorization ────────────────────────────────────────

export async function requestDeviceAuthorization(
  config: OAuthFlowConfig,
  options: { readonly deviceHeaders?: OAuthRequestHeaders | undefined },
): Promise<DeviceAuthorization> {
  const url = `${config.oauthHost.replace(/\/$/, '')}/api/oauth/device_authorization`;
  const { status, data } = await postForm(
    url,
    { client_id: config.clientId },
    options.deviceHeaders,
  );

  if (status !== 200) {
    throw new OAuthError(
      `Device authorization failed (HTTP ${status}): ${pickErrorDetail(data)}`,
    );
  }

  // Required-field validation for the device authorization response.
  const userCode = data['user_code'];
  const deviceCode = data['device_code'];
  const verificationUriComplete = data['verification_uri_complete'];
  if (typeof userCode !== 'string' || userCode.length === 0) {
    throw new OAuthError('Device authorization response missing user_code');
  }
  if (typeof deviceCode !== 'string' || deviceCode.length === 0) {
    throw new OAuthError('Device authorization response missing device_code');
  }
  if (typeof verificationUriComplete !== 'string' || verificationUriComplete.length === 0) {
    throw new OAuthError('Device authorization response missing verification_uri_complete');
  }

  return {
    userCode,
    deviceCode,
    verificationUri: typeof data['verification_uri'] === 'string' ? data['verification_uri'] : '',
    verificationUriComplete,
    expiresIn: data['expires_in'] !== undefined ? Number(data['expires_in']) : null,
    interval: Number(data['interval'] ?? 5),
  };
}

// ── pollDeviceToken ───────────────────────────────────────────────────

export type DevicePollResult =
  | { readonly kind: 'success'; readonly token: TokenInfo }
  | { readonly kind: 'pending'; readonly errorCode: string; readonly description: string }
  | { readonly kind: 'expired' }
  | { readonly kind: 'denied'; readonly description: string };

export async function pollDeviceToken(
  config: OAuthFlowConfig,
  deviceCode: string,
  options: { readonly deviceHeaders?: OAuthRequestHeaders | undefined },
): Promise<DevicePollResult> {
  const url = `${config.oauthHost.replace(/\/$/, '')}/api/oauth/token`;
  const { status, data } = await postForm(
    url,
    {
      client_id: config.clientId,
      device_code: deviceCode,
      grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
    },
    options.deviceHeaders,
  );

  if (status === 200 && typeof data['access_token'] === 'string') {
    return { kind: 'success', token: tokenFromResponse(data) };
  }

  if (status >= 500) {
    throw new OAuthError(
      `Device token polling server error (HTTP ${status}): ${pickErrorDetail(data)}`,
    );
  }

  const errorCode = typeof data['error'] === 'string' ? data['error'] : 'unknown_error';
  const detail = extractApiErrorMessage(data);
  const description =
    typeof data['error_description'] === 'string' ? data['error_description'] : (detail ?? '');
  switch (errorCode) {
    case 'authorization_pending':
    case 'slow_down':
      return { kind: 'pending', errorCode, description };
    case 'expired_token':
      return { kind: 'expired' };
    case 'access_denied':
      return { kind: 'denied', description };
    default:
      throw new OAuthError(
        `Device token polling failed (HTTP ${status}): ${detail ?? `${errorCode} ${description}`}`,
      );
  }
}

// ── refreshAccessToken ────────────────────────────────────────────────

export interface RefreshOptions {
  readonly deviceHeaders?: OAuthRequestHeaders | undefined;
  readonly maxRetries?: number | undefined;
  /**
   * Backoff between retries in ms. Defaults to `2 ** attempt * 1000` (1s, 2s).
   * Accepts an attempt-indexed callable for testability (set to `() => 0`).
   */
  readonly backoffMs?: ((attempt: number) => number) | undefined;
  readonly sleep?: ((ms: number) => Promise<void>) | undefined;
}

export async function refreshAccessToken(
  config: OAuthFlowConfig,
  refreshToken: string,
  options: RefreshOptions,
): Promise<TokenInfo> {
  const maxRetries = options.maxRetries ?? 3;
  const backoff = options.backoffMs ?? ((attempt) => 2 ** attempt * 1000);
  const sleep =
    options.sleep ??
    ((ms: number) =>
      new Promise<void>((resolve) => {
        setTimeout(resolve, ms);
      }));
  const url = `${config.oauthHost.replace(/\/$/, '')}/api/oauth/token`;

  let lastError: Error | undefined;
  for (let attempt = 0; attempt < maxRetries; attempt += 1) {
    let status: number;
    let data: Record<string, unknown>;
    try {
      ({ status, data } = await postForm(
        url,
        {
          client_id: config.clientId,
          grant_type: 'refresh_token',
          refresh_token: refreshToken,
        },
        options.deviceHeaders,
      ));
    } catch (error) {
      // Transport-level failure (DNS, connection refused, timeout). Treat
      // as retryable to match Python's `aiohttp.ClientError` handling.
      lastError = error instanceof Error ? error : new OAuthError(String(error));
      if (attempt < maxRetries - 1) {
        await sleep(backoff(attempt));
        continue;
      }
      throw lastError instanceof Error ? lastError : new OAuthError(String(lastError));
    }

    if (status === 200 && typeof data['access_token'] === 'string') {
      return tokenFromResponse(data);
    }

    const errorCode = typeof data['error'] === 'string' ? data['error'] : '';
    const detail = extractApiErrorMessage(data);
    if (status === 401 || status === 403 || errorCode === 'invalid_grant') {
      throw new OAuthUnauthorizedError(detail ?? 'Token refresh unauthorized.');
    }

    const desc = detail ?? `Token refresh failed (HTTP ${status}).`;
    if (RETRYABLE_STATUSES.has(status)) {
      lastError = new RetryableRefreshError(desc);
      if (attempt < maxRetries - 1) {
        await sleep(backoff(attempt));
        continue;
      }
      // fall through: out of retries, surface the retryable error
    } else {
      throw new OAuthError(desc);
    }
  }

  throw lastError ?? new OAuthError('Token refresh failed after retries.');
}

export type { DeviceHeaders, OAuthRequestHeaders };