This repository was archived by the owner on Jul 1, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgoogleSheetsOAuth.ts
More file actions
502 lines (446 loc) · 14.9 KB
/
Copy pathgoogleSheetsOAuth.ts
File metadata and controls
502 lines (446 loc) · 14.9 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import { googleDrivePickerConfigured } from './googleDrivePicker'
import { exchangeGoogleOAuthCodeViaTowerSmith } from './googleOAuthCodeExchange'
import {
clearGoogleSheetsOAuthState,
createGoogleSheetsOAuthState,
stashGoogleSheetsOAuthState,
verifyGoogleSheetsOAuthState,
} from './googleSheetsOAuthState'
const GIS_SCRIPT_SRC = 'https://accounts.google.com/gsi/client'
/** @deprecated Use {@link DRIVE_FILE_SCOPE} — kept for migration docs only. */
export const LEGACY_SHEETS_SCOPE = 'https://www.googleapis.com/auth/spreadsheets'
export const DRIVE_FILE_SCOPE = 'https://www.googleapis.com/auth/drive.file'
const OAUTH_SCOPE = DRIVE_FILE_SCOPE
const TOKEN_CACHE_KEY = 'towersmith_google_drive_file_token'
const TOKEN_EXPIRY_BUFFER_MS = 60_000
/** Max wait for GIS code/token callback (consent popup dismissed, blocked, or lost). */
const OAUTH_CALLBACK_TIMEOUT_MS = 120_000
/** Silent GIS token auth should fail quickly when no session exists. */
const OAUTH_SILENT_TIMEOUT_MS = 15_000
type CodeClient = {
requestCode: () => void
}
type TokenClient = {
requestAccessToken: (overrideConfig?: { prompt?: string }) => void
}
type CodeResponse = {
code?: string
scope?: string
state?: string
error?: string
error_description?: string
}
type GisTokenResponse = {
access_token?: string
expires_in?: number
error?: string
state?: string
}
type GisOAuthError = {
type?: string
message?: string
}
type GisOAuth2 = {
initCodeClient: (config: {
client_id: string
scope: string
state?: string
ux_mode?: 'popup' | 'redirect'
callback: (response: CodeResponse) => void
error_callback?: (error: GisOAuthError) => void
select_account?: boolean
}) => CodeClient
initTokenClient: (config: {
client_id: string
scope: string
state?: string
callback: (response: GisTokenResponse) => void
error_callback?: (error: GisOAuthError) => void
use_fedcm_for_prompt?: boolean
}) => TokenClient
revoke?: (accessToken: string, done: () => void) => void
}
declare global {
interface Window {
google?: {
accounts?: {
oauth2?: GisOAuth2
}
}
}
}
type CachedSheetsToken = {
accessToken: string
expiresAt: number
}
let gisScriptPromise: Promise<void> | null = null
export function googleSheetsOAuthConfigured(): boolean {
const id = import.meta.env.VITE_GOOGLE_SHEETS_OAUTH_CLIENT_ID
return (
typeof id === 'string' &&
id.trim().length > 0 &&
googleDrivePickerConfigured()
)
}
function readCachedSheetsToken(): string | null {
if (typeof sessionStorage === 'undefined') return null
try {
const raw = sessionStorage.getItem(TOKEN_CACHE_KEY)
if (!raw) return null
const parsed = JSON.parse(raw) as CachedSheetsToken
if (
typeof parsed.accessToken !== 'string' ||
typeof parsed.expiresAt !== 'number' ||
parsed.expiresAt <= Date.now() + TOKEN_EXPIRY_BUFFER_MS
) {
sessionStorage.removeItem(TOKEN_CACHE_KEY)
return null
}
return parsed.accessToken
} catch {
sessionStorage.removeItem(TOKEN_CACHE_KEY)
return null
}
}
export function writeCachedSheetsToken(accessToken: string, expiresInSec?: number): void {
if (typeof sessionStorage === 'undefined') return
const ttlMs = typeof expiresInSec === 'number' && expiresInSec > 0 ? expiresInSec * 1000 : 3_600_000
const payload: CachedSheetsToken = {
accessToken,
expiresAt: Date.now() + ttlMs,
}
sessionStorage.setItem(TOKEN_CACHE_KEY, JSON.stringify(payload))
}
export function clearCachedGoogleSheetsAccessToken(): void {
if (typeof sessionStorage === 'undefined') return
sessionStorage.removeItem(TOKEN_CACHE_KEY)
}
function isRetryableGoogleAuthError(code: string): boolean {
return (
code === 'interaction_required' ||
code === 'login_required' ||
code === 'account_selection_required' ||
code === 'consent_required'
)
}
function gisOAuth2Ready(): boolean {
return Boolean(window.google?.accounts?.oauth2)
}
function loadGisScript(): Promise<void> {
if (typeof window === 'undefined') {
return Promise.reject(new Error('google_oauth_unavailable'))
}
if (gisOAuth2Ready()) {
return Promise.resolve()
}
if (gisScriptPromise) return gisScriptPromise
gisScriptPromise = new Promise<void>((resolve, reject) => {
let settled = false
const finish = (result: 'resolve' | 'reject', err?: Error) => {
if (settled) return
settled = true
if (result === 'resolve') resolve()
else reject(err ?? new Error('google_oauth_unavailable'))
}
const finishIfReady = () => {
if (gisOAuth2Ready()) finish('resolve')
return settled
}
const existing = document.querySelector<HTMLScriptElement>(
`script[src="${GIS_SCRIPT_SRC}"]`,
)
if (existing) {
if (finishIfReady()) return
existing.addEventListener(
'load',
() => {
if (!finishIfReady()) finish('reject', new Error('google_oauth_unavailable'))
},
{ once: true },
)
existing.addEventListener(
'error',
() => finish('reject', new Error('google_oauth_script_failed')),
{ once: true },
)
// Script may have finished loading before listeners were attached.
queueMicrotask(() => {
finishIfReady()
})
return
}
const script = document.createElement('script')
script.src = GIS_SCRIPT_SRC
script.async = true
script.defer = true
script.onload = () => {
if (!finishIfReady()) finish('reject', new Error('google_oauth_unavailable'))
}
script.onerror = () => finish('reject', new Error('google_oauth_script_failed'))
document.head.appendChild(script)
}).catch((err: unknown) => {
gisScriptPromise = null
throw err
}) as Promise<void>
return gisScriptPromise
}
/** GIS popup code flow uses the page origin as redirect_uri (see Google Identity Services docs). */
function gisCodeClientRedirectUri(): string {
return window.location.origin
}
type CodePromptOptions = {
forceConsent?: boolean
}
function oauthErrorFromGis(err: GisOAuthError): Error {
const type = err.type ?? 'unknown'
if (type === 'popup_closed') return new Error('popup_closed_by_user')
if (type === 'popup_failed_to_open') return new Error('popup_blocked')
return new Error(type)
}
async function revokeCachedGoogleAccess(): Promise<void> {
const cached = readCachedSheetsToken()
clearCachedGoogleSheetsAccessToken()
if (!cached) return
await loadGisScript()
const revoke = window.google?.accounts?.oauth2?.revoke
if (!revoke) return
await new Promise<void>((resolve) => {
revoke(cached, () => resolve())
})
}
function isEmbeddedOAuthHost(): boolean {
if (typeof window === 'undefined') return false
const ua = navigator.userAgent
if (/\bElectron\b/i.test(ua)) return true
if (/\bVSCode\b/i.test(ua)) return true
if (window.self !== window.top) return true
return false
}
function shouldUseFedcmForPrompt(): boolean {
if (typeof window === 'undefined') return false
if (isEmbeddedOAuthHost()) return false
if (!('IdentityCredential' in globalThis)) return false
return /Android|iPhone|iPad|iPod/i.test(navigator.userAgent)
}
function interactiveFedcmAttempts(): readonly boolean[] {
return shouldUseFedcmForPrompt() ? [true, false] : [false, true]
}
function isAlternateOAuthUxRetry(code: string): boolean {
return (
code === 'google_oauth_timeout' ||
code === 'popup_blocked' ||
isRetryableGoogleAuthError(code)
)
}
function oauthCallbackTimeoutMs(prompt: '' | 'none' | 'consent'): number {
if (prompt === 'none') return OAUTH_SILENT_TIMEOUT_MS
return OAUTH_CALLBACK_TIMEOUT_MS
}
async function requestTokenWithPrompt(
prompt: '' | 'none' | 'consent',
options: { useFedcm?: boolean } = {},
): Promise<string> {
await loadGisScript()
const oauth2 = window.google?.accounts?.oauth2
if (!oauth2) {
throw new Error('google_oauth_unavailable')
}
const clientId = import.meta.env.VITE_GOOGLE_SHEETS_OAUTH_CLIENT_ID as string
const useFedcm = options.useFedcm ?? shouldUseFedcmForPrompt()
const oauthState = createGoogleSheetsOAuthState()
stashGoogleSheetsOAuthState(oauthState)
return new Promise((resolve, reject) => {
let settled = false
const timeoutId = window.setTimeout(() => {
if (settled) return
settled = true
clearGoogleSheetsOAuthState()
reject(new Error('google_oauth_timeout'))
}, oauthCallbackTimeoutMs(prompt))
const finish = (result: 'resolve' | 'reject', value?: string, err?: Error) => {
if (settled) return
settled = true
window.clearTimeout(timeoutId)
if (result === 'resolve' && value) resolve(value)
else reject(err ?? new Error('google_oauth_no_token'))
}
const client = oauth2.initTokenClient({
client_id: clientId,
scope: OAUTH_SCOPE,
state: oauthState,
use_fedcm_for_prompt: useFedcm,
callback: (response) => {
if (!verifyGoogleSheetsOAuthState(response.state)) {
finish('reject', undefined, new Error('google_oauth_state_mismatch'))
return
}
if (response.error) {
finish('reject', undefined, new Error(response.error))
return
}
if (!response.access_token) {
finish('reject', undefined, new Error('google_oauth_no_token'))
return
}
writeCachedSheetsToken(response.access_token, response.expires_in)
finish('resolve', response.access_token)
},
error_callback: (err) => {
clearGoogleSheetsOAuthState()
finish('reject', undefined, oauthErrorFromGis(err))
},
})
client.requestAccessToken({ prompt })
})
}
async function requestInteractiveToken(prompt: '' | 'consent'): Promise<string> {
const attempts = interactiveFedcmAttempts()
let lastErr: Error | undefined
for (let index = 0; index < attempts.length; index++) {
try {
return await requestTokenWithPrompt(prompt, { useFedcm: attempts[index] })
} catch (err) {
lastErr = err instanceof Error ? err : new Error('google_oauth_failed')
const code = lastErr.message
if (code === 'popup_closed_by_user') throw lastErr
if (index === attempts.length - 1 || !isAlternateOAuthUxRetry(code)) throw lastErr
}
}
throw lastErr ?? new Error('google_oauth_failed')
}
async function requestCodeWithPrompt(options: CodePromptOptions = {}): Promise<string> {
await loadGisScript()
const oauth2 = window.google?.accounts?.oauth2
if (!oauth2) {
throw new Error('google_oauth_unavailable')
}
const clientId = import.meta.env.VITE_GOOGLE_SHEETS_OAUTH_CLIENT_ID as string
const oauthState = createGoogleSheetsOAuthState()
const redirectUri = gisCodeClientRedirectUri()
stashGoogleSheetsOAuthState(oauthState)
return new Promise((resolve, reject) => {
let settled = false
const timeoutId = window.setTimeout(() => {
if (settled) return
settled = true
clearGoogleSheetsOAuthState()
reject(new Error('google_oauth_timeout'))
}, OAUTH_CALLBACK_TIMEOUT_MS)
const finish = (result: 'resolve' | 'reject', value?: string, err?: Error) => {
if (settled) return
settled = true
window.clearTimeout(timeoutId)
if (result === 'resolve' && value) resolve(value)
else reject(err ?? new Error('google_oauth_no_token'))
}
const client = oauth2.initCodeClient({
client_id: clientId,
scope: OAUTH_SCOPE,
state: oauthState,
ux_mode: 'popup',
select_account: options.forceConsent,
callback: (response) => {
void (async () => {
if (!verifyGoogleSheetsOAuthState(response.state)) {
finish('reject', undefined, new Error('google_oauth_state_mismatch'))
return
}
if (response.error) {
finish('reject', undefined, new Error(response.error))
return
}
if (!response.code) {
finish('reject', undefined, new Error('google_oauth_no_token'))
return
}
const tokenResult = await exchangeGoogleOAuthCodeViaTowerSmith(
response.code,
redirectUri,
)
if (tokenResult === 'not_configured') {
finish('reject', undefined, new Error('google_oauth_token_exchange_not_configured'))
return
}
if (!tokenResult) {
finish('reject', undefined, new Error('google_oauth_no_token'))
return
}
writeCachedSheetsToken(tokenResult.accessToken, tokenResult.expiresInSec)
finish('resolve', tokenResult.accessToken)
})().catch((err: unknown) => {
finish(
'reject',
undefined,
err instanceof Error ? err : new Error('google_oauth_failed'),
)
})
},
error_callback: (err) => {
clearGoogleSheetsOAuthState()
finish('reject', undefined, oauthErrorFromGis(err))
},
})
client.requestCode()
})
}
async function requestInteractiveCode(forceConsent = false): Promise<string> {
if (forceConsent) {
await revokeCachedGoogleAccess()
}
try {
return await requestCodeWithPrompt({ forceConsent })
} catch (err) {
const code = err instanceof Error ? err.message : ''
if (code === 'popup_closed_by_user') throw err
if (
code === 'google_oauth_token_exchange_not_configured' ||
code === 'google_oauth_no_token'
) {
return requestInteractiveToken(forceConsent ? 'consent' : '')
}
if (code === 'popup_blocked' || isRetryableGoogleAuthError(code)) {
try {
return await requestCodeWithPrompt({ forceConsent })
} catch (retryErr) {
const retryCode = retryErr instanceof Error ? retryErr.message : ''
if (retryCode === 'google_oauth_token_exchange_not_configured' || retryCode === 'google_oauth_no_token') {
return requestInteractiveToken(forceConsent ? 'consent' : '')
}
throw retryErr
}
}
throw err
}
}
export type GoogleSheetsOAuthOptions = {
/** Force Google consent (re-authorize drive.file scope). */
consent?: boolean
}
/**
* Request a short-lived Google access token with drive.file scope.
* Reuses a cached session token when valid, then runs the GIS authorization-code popup flow.
*/
export async function requestGoogleSheetsAccessToken(
options: GoogleSheetsOAuthOptions = {},
): Promise<string> {
if (!googleSheetsOAuthConfigured()) {
throw new Error('google_oauth_not_configured')
}
if (!options.consent) {
const cached = readCachedSheetsToken()
if (cached) return cached
}
if (!options.consent) {
try {
return await requestTokenWithPrompt('none', { useFedcm: false })
} catch (err) {
const code = err instanceof Error ? err.message : ''
if (!isRetryableGoogleAuthError(code)) throw err
}
}
return requestInteractiveCode(Boolean(options.consent))
}
/** Cached drive.file token for this browser tab session, if still valid. */
export function getCachedGoogleSheetsAccessToken(): string | null {
return readCachedSheetsToken()
}