refactor
: Move Google Cloud specific code into its own namespace [#2]
Retrieving an access token for GCP
import { getAuthToken } from "web-auth-library/gcp";
// Get an access token for interacting with Google Cloud Platform APIs.
const token = await getAuthToken({
credentials: env.GOOGLE_CLOUD_CREDENTIALS,
scope: "https://www.googleapis.com/auth/cloud-platform",
});
// => {
// accessToken: "ya29.c.b0AXv0zTOQVv0...",
// type: "Bearer",
// expires: 1653855236,
// }
Retrieving an ID token for the arbitrary resource
import { getAuthToken } from "web-auth-library/gcp";
// Get an ID token for the target resource (audience)
const token = await getAuthToken({
credentials: env.GOOGLE_CLOUD_CREDENTIALS,
audience: "https://example.com",
});
// => {
// idToken: "eyJhbGciOiJSUzI1NiIsImtpZ...",
// audience: "https://example.com",
// expires: 1653855236,
// }
Generating a digital signature
import { importKey, sign, getCredentials } from "web-auth-library/gcp";
// Convert GCP service account key into `CryptoKey` object
const credentials = getCredentials(env.GOOGLE_CLOUD_CREDENTIALS);
const signingKey = await importKey(credentials.private_key, ["sign"]);
// Generate a digital signature
const signature = await sign(signingKey, "xxx");