diff --git a/deno.json b/deno.json index 20eafda..a425506 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@cloudydeno/bitesized", - "version": "1.0.4", + "version": "1.0.5", "license": "MIT", "exports": { "./crypto/curve25519": "./crypto/curve25519.ts", diff --git a/integrations/google-cloud-auth.ts b/integrations/google-cloud-auth.ts index b5963f0..5d017e7 100644 --- a/integrations/google-cloud-auth.ts +++ b/integrations/google-cloud-auth.ts @@ -1,18 +1,19 @@ import { SubProcess } from "../system/sub-process.ts"; -import { fetchServiceAccountToken } from "./google-metadata-service.ts"; +import { fetchServiceAccountToken, type TokenResponse } from "./google-metadata-service.ts"; /** * Attempts to get a GCP auth token from two possible sources: * 1. A service account token from the GCP Metadata Server. * 2. A user token from the installed `gcloud` CLI. * If neither source is available, throws an Error. + * Returns an object including the token's remaining lifespan. */ -export async function fetchGoogleCloudToken(): Promise { +export async function fetchGoogleCloudAuth(): Promise { const reasons = [`No Google Cloud access token found. Encountered issues:`]; try { const resp = await fetchServiceAccountToken(); - return resp.access_token; + return resp; } catch (thrown) { const err = thrown as Error; const parts = err.message.split(': '); @@ -25,11 +26,15 @@ export async function fetchGoogleCloudToken(): Promise { try { const proc = new SubProcess('gcloud', { - cmd: ['gcloud', 'auth', 'application-default', 'print-access-token'], + cmd: ['gcloud', 'auth', 'print-access-token'], stdin: 'null', errorPrefix: /ERROR:/, }); - return (await proc.captureAllTextOutput()).trimEnd(); + return { + access_token: (await proc.captureAllTextOutput()).trimEnd(), + expires_in: 3600, // gcloud's default + token_type: 'Bearer', + }; } catch (thrown) { const err = thrown as Error; reasons.push(` - gcloud CLI: ${err.message}`); @@ -38,6 +43,17 @@ export async function fetchGoogleCloudToken(): Promise { throw new Error(reasons.join('\n')); } +/** + * Attempts to get a GCP auth token from two possible sources: + * 1. A service account token from the GCP Metadata Server. + * 2. A user token from the installed `gcloud` CLI. + * If neither source is available, throws an Error. + */ +export async function fetchGoogleCloudToken(): Promise { + const auth = await fetchGoogleCloudAuth(); + return auth.access_token; +} + type ServiceAccountCredential = { accessToken: string; expireTime: string;