|
1 |
| -const fs = require("fs"); |
2 |
| -const http2 = require("http2"); |
| 1 | +const { execSync } = require("child_process"); |
3 | 2 |
|
4 | 3 | const getIDToken = async () => {
|
5 | 4 | return new Promise((resolve, reject) => {
|
6 | 5 | 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 | + } |
51 | 12 | } catch (e) {
|
52 | 13 | reject(new Error(e.message));
|
53 | 14 | }
|
|
0 commit comments