|
| 1 | +const fs = require("fs"); |
| 2 | +const http2 = require("http2"); |
| 3 | + |
| 4 | +const getIDToken = async () => { |
| 5 | + return new Promise((resolve, reject) => { |
| 6 | + 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 workspaceToken = config.workspaceToken; |
| 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 ${workspaceToken}`, |
| 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 | + ); |
| 51 | + } catch (e) { |
| 52 | + reject(new Error(e.message)); |
| 53 | + } |
| 54 | + }); |
| 55 | +}; |
| 56 | + |
| 57 | +(async () => { |
| 58 | + try { |
| 59 | + const token = await getIDToken(); |
| 60 | + console.log( |
| 61 | + JSON.stringify({ |
| 62 | + version: 1, |
| 63 | + success: true, |
| 64 | + token_type: "urn:ietf:params:oauth:token-type:id_token", |
| 65 | + id_token: token, |
| 66 | + }), |
| 67 | + ); |
| 68 | + } catch (error) { |
| 69 | + console.log( |
| 70 | + JSON.stringify({ |
| 71 | + version: 1, |
| 72 | + success: false, |
| 73 | + code: "401", |
| 74 | + message: error.message, |
| 75 | + }), |
| 76 | + ); |
| 77 | + } |
| 78 | +})(); |
0 commit comments