(client: OAuth2Client)
| 352 | } |
| 353 | |
| 354 | async function loadCachedCredentials(client: OAuth2Client): Promise<boolean> { |
| 355 | const pathsToTry = [ |
| 356 | getCachedCredentialPath(), |
| 357 | process.env['GOOGLE_APPLICATION_CREDENTIALS'], |
| 358 | ].filter((p): p is string => !!p); |
| 359 | |
| 360 | for (const keyFile of pathsToTry) { |
| 361 | try { |
| 362 | const creds = await fs.readFile(keyFile, 'utf-8'); |
| 363 | client.setCredentials(JSON.parse(creds)); |
| 364 | |
| 365 | // This will verify locally that the credentials look good. |
| 366 | const { token } = await client.getAccessToken(); |
| 367 | if (!token) { |
| 368 | continue; |
| 369 | } |
| 370 | |
| 371 | // This will check with the server to see if it hasn't been revoked. |
| 372 | await client.getTokenInfo(token); |
| 373 | |
| 374 | return true; |
| 375 | } catch (_) { |
| 376 | // Ignore and try next path. |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | async function cacheCredentials(credentials: Credentials) { |
| 384 | const filePath = getCachedCredentialPath(); |
no test coverage detected