Skip to content

Commit ac76d09

Browse files
committed
[flex-oidc] Simplify ID token retrieval using gitpod CLI
1 parent 61ecd26 commit ac76d09

File tree

1 file changed

+7
-41
lines changed

1 file changed

+7
-41
lines changed

dev/flex-oidc/oidc.js

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,19 @@
11
const fs = require("fs");
22
const http2 = require("http2");
3+
const { execSync } = require("child_process");
34

45
const getIDToken = async () => {
56
return new Promise((resolve, reject) => {
67
try {
78
const configPath = "/usr/local/gitpod/config/initial-spec.json";
89
const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
910

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-
);
11+
try {
12+
const token = execSync("gitpod idp token --audience accounts.google.com", { encoding: "utf8" }).trim();
13+
resolve(token);
14+
} catch (error) {
15+
reject(new Error("Error getting token: " + error.message));
16+
}
5117
} catch (e) {
5218
reject(new Error(e.message));
5319
}

0 commit comments

Comments
 (0)