blob: 3fe63628b79287bb144c12c3c8fcb173f80c77e3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import got from "got/dist/source";
export const fetchIdToken = async function (aud: string): Promise<string> {
const metadataServerTokenURL = `http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=${aud}`;
let resp;
try {
resp = await got(metadataServerTokenURL, {
headers: {
"Metadata-Flavor": "Google"
}
});
} catch (err) {
console.error(err);
throw new Error("Failed to fetch ID token from Google metadata endpoint");
}
const token = resp.body;
if (!token) {
throw new Error("ID token from Google metadata endpoint is empty");
}
return token;
};
|