Skip to content

[flex-oidc] Simplify ID token retrieval using gitpod CLI #20651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 7 additions & 46 deletions dev/flex-oidc/oidc.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,14 @@
const fs = require("fs");
const http2 = require("http2");
const { execSync } = require("child_process");

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

const controlPlaneApiEndpoint = config.controlPlaneApiEndpoint;
const environmentToken = config.environmentToken;

const url = new URL(controlPlaneApiEndpoint);
const client = http2.connect(url.origin);

const req = client.request({
":method": "POST",
"content-type": "application/json",
authorization: `Bearer ${environmentToken}`,
":path": `${url.pathname}/gitpod.v1.IdentityService/GetIDToken`,
});

let responseData = "";

req.on("data", (chunk) => {
responseData += chunk;
});

req.on("end", () => {
try {
const result = JSON.parse(responseData);
const token = result.token;
resolve(token);
} catch (error) {
reject(new Error("Error parsing response: " + error.message));
} finally {
client.close();
}
});

req.on("error", (error) => {
reject(new Error(error.message));
client.close();
});

req.end(
JSON.stringify({
audience: ["accounts.google.com"],
}),
);
try {
const token = execSync("gitpod idp token --audience accounts.google.com", { encoding: "utf8" }).trim();
resolve(token);
} catch (error) {
reject(new Error("Error getting token: " + error.message));
}
} catch (e) {
reject(new Error(e.message));
}
Expand Down
Loading