Spaces:
Sleeping
Sleeping
File size: 3,515 Bytes
c16e487 | 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 | export interface GoogleCredentials {
/**
* The client email for the Google Cloud service account. Defaults to the
* value of the `GOOGLE_CLIENT_EMAIL` environment variable.
*/
clientEmail: string;
/**
* The private key for the Google Cloud service account. Defaults to the
* value of the `GOOGLE_PRIVATE_KEY` environment variable.
*/
privateKey: string;
/**
* Optional. The private key ID for the Google Cloud service account. Defaults
* to the value of the `GOOGLE_PRIVATE_KEY_ID` environment variable.
*/
privateKeyId?: string;
}
// Convert a string to base64url
const base64url = (str: string) => {
return btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
};
const decodeBase64 = (base64: string) => {
return Buffer.from(base64, "base64").toString("utf-8");
};
const importPrivateKey = async (pemKey: string) => {
const pemHeader = "-----BEGIN PRIVATE KEY-----";
const pemFooter = "-----END PRIVATE KEY-----";
// Remove header, footer, and any whitespace/newlines
const pemContents = pemKey
.replace(pemHeader, "")
.replace(pemFooter, "")
.replace(/\s/g, "");
// Decode base64 to binary
const binaryString = decodeBase64(pemContents);
// Convert binary string to Uint8Array
const binaryData = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
binaryData[i] = binaryString.charCodeAt(i);
}
return await crypto.subtle.importKey(
"pkcs8",
binaryData,
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
true,
["sign"]
);
};
const buildJwt = async (credentials: GoogleCredentials) => {
const now = Math.floor(Date.now() / 1000);
// Only include kid in header if privateKeyId is provided
const header: { alg: string; typ: string; kid?: string } = {
alg: "RS256",
typ: "JWT",
};
if (credentials.privateKeyId) {
header.kid = credentials.privateKeyId;
}
const payload = {
iss: credentials.clientEmail,
scope: "https://www.googleapis.com/auth/cloud-platform",
aud: "https://oauth2.googleapis.com/token",
exp: now + 3600,
iat: now,
};
const privateKey = await importPrivateKey(credentials.privateKey);
const signingInput = `${base64url(JSON.stringify(header))}.${base64url(
JSON.stringify(payload)
)}`;
const encoder = new TextEncoder();
const data = encoder.encode(signingInput);
const signature = await crypto.subtle.sign(
"RSASSA-PKCS1-v1_5",
privateKey,
data
);
const signatureBase64 = base64url(
String.fromCharCode(...new Uint8Array(signature))
);
return `${base64url(JSON.stringify(header))}.${base64url(
JSON.stringify(payload)
)}.${signatureBase64}`;
};
/**
* Generate an authentication token for Google Vertex AI in a manner compatible
* with the Edge runtime.
*/
export async function generateAuthToken(credentials: GoogleCredentials) {
try {
const creds = credentials;
const jwt = await buildJwt(creds);
const response = await fetch("https://oauth2.googleapis.com/token", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
assertion: jwt,
}),
});
if (!response.ok) {
throw new Error(`Token request failed: ${response.statusText}`);
}
const data = await response.json();
return data.access_token;
} catch (error) {
throw error;
}
}
|