import { readFile } from 'node:fs/promises'; import { authenticateGoogleDesktop, parseGoogleDesktopClient, refreshGoogleAccessToken, type GoogleDesktopClient, type AuthenticateGoogleDesktopInput, } from '@limrun/play-auth'; import type Limrun from '@limrun/api'; import { GOOGLE_PLAY_OAUTH_SECRET_NAME, GOOGLE_PLAY_OAUTH_TYPE, GOOGLE_PLAY_SERVICE_ACCOUNT_TYPE, LimrunSigningSecretStore, } from './limrun-signing-store'; /** The part of the secret store a Google sign-in needs; tests use an in-memory one. */ export type GoogleSecretStore = Pick; export async function readGoogleDesktopClient(file: string): Promise { const content = await readFile(file, 'utf8'); let config: unknown; try { config = JSON.parse(content); } catch { throw new Error('The Google Desktop OAuth client file is not valid JSON. Download a new copy.'); } return parseGoogleDesktopClient(config); } export async function authenticateGoogle( client: GoogleDesktopClient, openBrowser: (url: string) => Promise, store: GoogleSecretStore, fetcher: typeof fetch = fetch, consoleScope?: AuthenticateGoogleDesktopInput['consoleScope'], secretName = GOOGLE_PLAY_OAUTH_SECRET_NAME, ): Promise { const controller = new AbortController(); const cancel = () => controller.abort(); process.once('SIGINT', cancel); process.once('SIGTERM', cancel); try { const token = await authenticateGoogleDesktop({ ...client, consoleScope, openBrowser, signal: controller.signal, fetch: fetcher, }); await store.put(GOOGLE_PLAY_OAUTH_TYPE, secretName, { ...client, refreshToken: token.refreshToken }); } finally { process.removeListener('SIGINT', cancel); process.removeListener('SIGTERM', cancel); } } /** Mint an access token from the saved sign-in, for Play actions that run on this computer. */ export async function savedGoogleAccessToken( store: GoogleSecretStore, fetcher: typeof fetch = fetch, secretName = GOOGLE_PLAY_OAUTH_SECRET_NAME, ): Promise { const saved = await store.get(GOOGLE_PLAY_OAUTH_TYPE, secretName); if (!saved) return undefined; const { clientId, clientSecret, refreshToken } = saved.data; if (!clientId || !refreshToken) throw new Error('Saved Google credentials are incomplete. Run lim android play auth again.'); let token; try { token = await refreshGoogleAccessToken({ clientId, clientSecret, refreshToken, fetch: fetcher }); } catch (error) { throw new Error( `${ error instanceof Error ? error.message : 'Google token refresh failed.' } Run lim android play auth again.`, ); } if (token.refreshToken && token.refreshToken !== refreshToken) { await store.put(GOOGLE_PLAY_OAUTH_TYPE, secretName, { ...saved.data, refreshToken: token.refreshToken }); } return token.accessToken; } /** * Find the Play credential a build reads itself: the named secret, or the sign-in saved by * lim android play auth. Reads metadata only, so the credential never reaches this computer. */ export async function resolvePlayCredentialSecret( client: Limrun, explicit?: string, store = new LimrunSigningSecretStore(client), ): Promise<{ target: string; permission: string }> { const name = explicit ?? GOOGLE_PLAY_OAUTH_SECRET_NAME; const secret = await store.ready(name); if (!secret) { throw new Error( explicit ? `No secret named ${explicit} was found.` : 'Run lim android play auth first, or provide --playstore-secret, --playstore-service-account or --playstore-access-token for --upload-to-playstore.', ); } if (secret.type !== GOOGLE_PLAY_OAUTH_TYPE && secret.type !== GOOGLE_PLAY_SERVICE_ACCOUNT_TYPE) { throw new Error(`The secret ${name} holds a ${secret.type}, not a Google Play credential.`); } const target = `name/${name}`; return { target, permission: `secret:${target}:read` }; }