@trops/dash-core 0.1.77 → 0.1.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5030,6 +5030,101 @@ function interpolate(template, credentials) {
5030
5030
  });
5031
5031
  }
5032
5032
 
5033
+ /**
5034
+ * Refresh a Google OAuth access token before starting the MCP server.
5035
+ * This sidesteps the upstream bug where `new google.auth.OAuth2()` is called
5036
+ * without client_id/client_secret, preventing token refresh.
5037
+ *
5038
+ * Uses only Node built-in `https` — no external dependencies.
5039
+ *
5040
+ * @param {object} tokenRefresh { credentialsPath, oauthKeysPath }
5041
+ */
5042
+ async function refreshGoogleOAuthToken(tokenRefresh) {
5043
+ const home = process.env.HOME || "";
5044
+ const credPath = tokenRefresh.credentialsPath.replace(/^~/, home);
5045
+ const keysPath = tokenRefresh.oauthKeysPath.replace(/^~/, home);
5046
+
5047
+ if (!fs$5.existsSync(credPath) || !fs$5.existsSync(keysPath)) {
5048
+ console.log(
5049
+ "[mcpController] Token refresh skipped: credential files not found",
5050
+ );
5051
+ return;
5052
+ }
5053
+
5054
+ const credentials = JSON.parse(fs$5.readFileSync(credPath, "utf8"));
5055
+ const keysFile = JSON.parse(fs$5.readFileSync(keysPath, "utf8"));
5056
+ const keyData = keysFile.installed || keysFile.web;
5057
+
5058
+ if (
5059
+ !credentials.refresh_token ||
5060
+ !keyData?.client_id ||
5061
+ !keyData?.client_secret
5062
+ ) {
5063
+ console.log(
5064
+ "[mcpController] Token refresh skipped: missing refresh_token or client credentials",
5065
+ );
5066
+ return;
5067
+ }
5068
+
5069
+ // Skip if token is still valid (expiry > 5 minutes from now)
5070
+ if (
5071
+ credentials.expiry_date &&
5072
+ credentials.expiry_date > Date.now() + 5 * 60 * 1000
5073
+ ) {
5074
+ console.log("[mcpController] Token still valid, skipping refresh");
5075
+ return;
5076
+ }
5077
+
5078
+ console.log("[mcpController] Refreshing Google OAuth token...");
5079
+
5080
+ const https = require$$8;
5081
+ const postData = [
5082
+ `client_id=${encodeURIComponent(keyData.client_id)}`,
5083
+ `client_secret=${encodeURIComponent(keyData.client_secret)}`,
5084
+ `refresh_token=${encodeURIComponent(credentials.refresh_token)}`,
5085
+ "grant_type=refresh_token",
5086
+ ].join("&");
5087
+
5088
+ const body = await new Promise((resolve, reject) => {
5089
+ const req = https.request(
5090
+ {
5091
+ hostname: "oauth2.googleapis.com",
5092
+ path: "/token",
5093
+ method: "POST",
5094
+ headers: {
5095
+ "Content-Type": "application/x-www-form-urlencoded",
5096
+ "Content-Length": Buffer.byteLength(postData),
5097
+ },
5098
+ },
5099
+ (res) => {
5100
+ let data = "";
5101
+ res.on("data", (chunk) => (data += chunk));
5102
+ res.on("end", () => {
5103
+ if (res.statusCode === 200) {
5104
+ resolve(JSON.parse(data));
5105
+ } else {
5106
+ reject(
5107
+ new Error(`Token refresh failed (${res.statusCode}): ${data}`),
5108
+ );
5109
+ }
5110
+ });
5111
+ },
5112
+ );
5113
+ req.on("error", reject);
5114
+ req.write(postData);
5115
+ req.end();
5116
+ });
5117
+
5118
+ credentials.access_token = body.access_token;
5119
+ credentials.expiry_date = Date.now() + (body.expires_in || 3600) * 1000;
5120
+ if (body.refresh_token) {
5121
+ credentials.refresh_token = body.refresh_token;
5122
+ }
5123
+
5124
+ fs$5.writeFileSync(credPath, JSON.stringify(credentials, null, 2));
5125
+ console.log("[mcpController] Google OAuth token refreshed successfully");
5126
+ }
5127
+
5033
5128
  const mcpController$2 = {
5034
5129
  /**
5035
5130
  * startServer
@@ -5121,6 +5216,18 @@ const mcpController$2 = {
5121
5216
  });
5122
5217
  }
5123
5218
 
5219
+ // Pre-start token refresh (e.g., Google OAuth)
5220
+ if (mcpConfig.tokenRefresh) {
5221
+ try {
5222
+ await refreshGoogleOAuthToken(mcpConfig.tokenRefresh);
5223
+ } catch (err) {
5224
+ console.warn(
5225
+ "[mcpController] Token refresh failed, continuing:",
5226
+ err.message,
5227
+ );
5228
+ }
5229
+ }
5230
+
5124
5231
  // Build args - start with static args, then append dynamic args from credentials
5125
5232
  const args = [...(mcpConfig.args || [])];
5126
5233
  if (mcpConfig.argsMapping && credentials) {
@@ -5146,7 +5253,10 @@ const mcpController$2 = {
5146
5253
  // Interpolate {{MCP_DIR}} in args to resolve local MCP server scripts
5147
5254
  const mcpDir = path$6.join(__dirname, "..", "mcp");
5148
5255
  for (let i = 0; i < args.length; i++) {
5149
- if (typeof args[i] === "string" && args[i].includes("{{MCP_DIR}}")) {
5256
+ if (
5257
+ typeof args[i] === "string" &&
5258
+ args[i].includes("{{MCP_DIR}}")
5259
+ ) {
5150
5260
  args[i] = args[i].replace(/\{\{MCP_DIR\}\}/g, mcpDir);
5151
5261
  }
5152
5262
  }