Skip to content

Commit dc6a1e1

Browse files
authored
[flex-oidc] Simplify ID token retrieval using gitpod CLI (#20651)
1 parent 67b26eb commit dc6a1e1

File tree

1 file changed

+7
-46
lines changed

1 file changed

+7
-46
lines changed

dev/flex-oidc/oidc.js

+7-46
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,14 @@
1-
const fs = require("fs");
2-
const http2 = require("http2");
1+
const { execSync } = require("child_process");
32

43
const getIDToken = async () => {
54
return new Promise((resolve, reject) => {
65
try {
7-
const configPath = "/usr/local/gitpod/config/initial-spec.json";
8-
const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
9-
10-
const controlPlaneApiEndpoint = config.controlPlaneApiEndpoint;
11-
const environmentToken = config.environmentToken;
12-
13-
const url = new URL(controlPlaneApiEndpoint);
14-
const client = http2.connect(url.origin);
15-
16-
const req = client.request({
17-
":method": "POST",
18-
"content-type": "application/json",
19-
authorization: `Bearer ${environmentToken}`,
20-
":path": `${url.pathname}/gitpod.v1.IdentityService/GetIDToken`,
21-
});
22-
23-
let responseData = "";
24-
25-
req.on("data", (chunk) => {
26-
responseData += chunk;
27-
});
28-
29-
req.on("end", () => {
30-
try {
31-
const result = JSON.parse(responseData);
32-
const token = result.token;
33-
resolve(token);
34-
} catch (error) {
35-
reject(new Error("Error parsing response: " + error.message));
36-
} finally {
37-
client.close();
38-
}
39-
});
40-
41-
req.on("error", (error) => {
42-
reject(new Error(error.message));
43-
client.close();
44-
});
45-
46-
req.end(
47-
JSON.stringify({
48-
audience: ["accounts.google.com"],
49-
}),
50-
);
6+
try {
7+
const token = execSync("gitpod idp token --audience accounts.google.com", { encoding: "utf8" }).trim();
8+
resolve(token);
9+
} catch (error) {
10+
reject(new Error("Error getting token: " + error.message));
11+
}
5112
} catch (e) {
5213
reject(new Error(e.message));
5314
}

0 commit comments

Comments
 (0)