@corsair-dev/cli 0.1.111 → 0.1.113

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.
package/dist/index.js CHANGED
@@ -1882,6 +1882,63 @@ var init_subscribe_google = __esm({
1882
1882
  }
1883
1883
  });
1884
1884
 
1885
+ // src/pkce-nonce.ts
1886
+ import { hkdfSync } from "crypto";
1887
+ import * as fs3 from "fs";
1888
+ import * as os from "os";
1889
+ import * as path3 from "path";
1890
+ function pkceNonceFile(pluginId, tenantId, kek, home = os.homedir()) {
1891
+ const dir = path3.join(home, ".corsair", "pkce");
1892
+ fs3.mkdirSync(dir, { recursive: true, mode: 448 });
1893
+ const info = fs3.lstatSync(dir);
1894
+ if (info.isSymbolicLink() || !info.isDirectory() || typeof process.getuid === "function" && info.uid !== process.getuid()) {
1895
+ throw new Error(
1896
+ `Refusing to keep sign-in data in ${dir}: not a private directory.`
1897
+ );
1898
+ }
1899
+ fs3.chmodSync(dir, 448);
1900
+ const name = Buffer.from(
1901
+ hkdfSync(
1902
+ "sha256",
1903
+ kek,
1904
+ "corsair-pkce-nonce-file",
1905
+ `${pluginId}\0${tenantId}`,
1906
+ 24
1907
+ )
1908
+ ).toString("base64url");
1909
+ return path3.join(dir, `${name}.json`);
1910
+ }
1911
+ function savePkceNonce(file, nonce) {
1912
+ const noFollow = fs3.constants.O_NOFOLLOW ?? 0;
1913
+ const fd = fs3.openSync(
1914
+ file,
1915
+ fs3.constants.O_WRONLY | fs3.constants.O_CREAT | fs3.constants.O_TRUNC | noFollow,
1916
+ 384
1917
+ );
1918
+ try {
1919
+ fs3.writeSync(fd, JSON.stringify({ nonce }));
1920
+ } finally {
1921
+ fs3.closeSync(fd);
1922
+ }
1923
+ }
1924
+ function readPkceNonce(file) {
1925
+ try {
1926
+ if (fs3.lstatSync(file).isSymbolicLink()) return void 0;
1927
+ const { nonce } = JSON.parse(fs3.readFileSync(file, "utf8"));
1928
+ return nonce;
1929
+ } catch {
1930
+ return void 0;
1931
+ }
1932
+ }
1933
+ function removePkceNonce(file, nonce) {
1934
+ if (readPkceNonce(file) === nonce) fs3.rmSync(file, { force: true });
1935
+ }
1936
+ var init_pkce_nonce = __esm({
1937
+ "src/pkce-nonce.ts"() {
1938
+ "use strict";
1939
+ }
1940
+ });
1941
+
1885
1942
  // src/auth.ts
1886
1943
  var auth_exports = {};
1887
1944
  __export(auth_exports, {
@@ -1889,12 +1946,12 @@ __export(auth_exports, {
1889
1946
  });
1890
1947
  import { spawn } from "child_process";
1891
1948
  import * as crypto4 from "crypto";
1892
- import * as fs3 from "fs";
1949
+ import * as fs4 from "fs";
1893
1950
  import * as http from "http";
1894
1951
  import * as https from "https";
1895
1952
  import * as net from "net";
1896
- import * as os from "os";
1897
- import * as path3 from "path";
1953
+ import * as os2 from "os";
1954
+ import * as path4 from "path";
1898
1955
  import * as querystring from "querystring";
1899
1956
  import {
1900
1957
  CORSAIR_INTERNAL as CORSAIR_INTERNAL3,
@@ -1904,15 +1961,16 @@ import {
1904
1961
  exchangeCodeForTokens,
1905
1962
  generateDEK
1906
1963
  } from "corsair/core";
1964
+ import { localPkceVerifier, newPkceNonce, pkceAuthParams } from "corsair/oauth";
1907
1965
  import { createCorsairOrm } from "corsair/orm";
1908
1966
  function out(data) {
1909
1967
  console.log(JSON.stringify(data));
1910
1968
  }
1911
1969
  function authStateFile(sessionId) {
1912
- return path3.join(os.tmpdir(), `corsair-auth-${sessionId}.json`);
1970
+ return path4.join(os2.tmpdir(), `corsair-auth-${sessionId}.json`);
1913
1971
  }
1914
1972
  function writeAuthState(sessionId, state) {
1915
- fs3.writeFileSync(authStateFile(sessionId), JSON.stringify(state));
1973
+ fs4.writeFileSync(authStateFile(sessionId), JSON.stringify(state));
1916
1974
  }
1917
1975
  function pollAuthState(sessionId, timeoutMs) {
1918
1976
  return new Promise((resolve2, reject) => {
@@ -1920,7 +1978,7 @@ function pollAuthState(sessionId, timeoutMs) {
1920
1978
  const start = Date.now();
1921
1979
  const interval = setInterval(() => {
1922
1980
  try {
1923
- const state = JSON.parse(fs3.readFileSync(file, "utf8"));
1981
+ const state = JSON.parse(fs4.readFileSync(file, "utf8"));
1924
1982
  clearInterval(interval);
1925
1983
  resolve2(state);
1926
1984
  } catch {
@@ -2033,7 +2091,10 @@ async function ensureAccount(database, integrationId, tenantId, kek) {
2033
2091
  dek: encryptedDek
2034
2092
  });
2035
2093
  }
2036
- async function oauthExchangeCode(database, plugin, kek, tenantId, code, redirectUri, clientId, clientSecret, oauthCfg) {
2094
+ function cliPkceParams(oauthCfg, codeVerifier) {
2095
+ return oauthCfg.pkce && codeVerifier ? pkceAuthParams(codeVerifier, oauthCfg.authParams) : {};
2096
+ }
2097
+ async function oauthExchangeCode(database, plugin, kek, tenantId, code, redirectUri, clientId, clientSecret, oauthCfg, codeVerifier) {
2037
2098
  let tokens;
2038
2099
  try {
2039
2100
  tokens = await exchangeCodeForTokens(
@@ -2041,7 +2102,8 @@ async function oauthExchangeCode(database, plugin, kek, tenantId, code, redirect
2041
2102
  clientId,
2042
2103
  clientSecret,
2043
2104
  oauthCfg,
2044
- redirectUri
2105
+ redirectUri,
2106
+ codeVerifier
2045
2107
  );
2046
2108
  } catch (err) {
2047
2109
  out({
@@ -2097,7 +2159,19 @@ async function oauthWithCode(database, plugin, kek, tenantId, code) {
2097
2159
  });
2098
2160
  return;
2099
2161
  }
2100
- await oauthExchangeCode(
2162
+ let codeVerifier;
2163
+ const nonceFile = oauthCfg.pkce ? pkceNonceFile(plugin.id, tenantId, kek) : void 0;
2164
+ const nonce = nonceFile ? readPkceNonce(nonceFile) : void 0;
2165
+ if (nonceFile) {
2166
+ if (!nonce) {
2167
+ out({
2168
+ error: `No sign-in for '${plugin.id}' is waiting for a code. Run \`corsair auth --plugin=${plugin.id}\` again for a new link.`
2169
+ });
2170
+ return;
2171
+ }
2172
+ codeVerifier = localPkceVerifier(plugin.id, tenantId, kek, nonce);
2173
+ }
2174
+ const exchanged = await oauthExchangeCode(
2101
2175
  database,
2102
2176
  plugin,
2103
2177
  kek,
@@ -2106,8 +2180,10 @@ async function oauthWithCode(database, plugin, kek, tenantId, code) {
2106
2180
  redirectUri,
2107
2181
  clientId,
2108
2182
  clientSecret,
2109
- oauthCfg
2183
+ oauthCfg,
2184
+ codeVerifier
2110
2185
  );
2186
+ if (exchanged && nonceFile && nonce) removePkceNonce(nonceFile, nonce);
2111
2187
  }
2112
2188
  async function oauthListen(database, plugin, kek, tenantId, sessionId) {
2113
2189
  const oauthCfg = getOAuthConfigForPlugin(plugin);
@@ -2134,6 +2210,8 @@ async function oauthListen(database, plugin, kek, tenantId, sessionId) {
2134
2210
  });
2135
2211
  return;
2136
2212
  }
2213
+ const pkceNonce = oauthCfg.pkce ? newPkceNonce() : void 0;
2214
+ const codeVerifier = pkceNonce ? localPkceVerifier(plugin.id, tenantId, kek, pkceNonce) : void 0;
2137
2215
  const clientSecret = await integrationKm.get_client_secret();
2138
2216
  if (!clientSecret) {
2139
2217
  writeAuthState(sessionId, {
@@ -2160,9 +2238,12 @@ async function oauthListen(database, plugin, kek, tenantId, sessionId) {
2160
2238
  redirect_uri: stored,
2161
2239
  response_type: "code",
2162
2240
  scope: oauthCfg.scopes.join(" "),
2163
- ...oauthCfg.authParams
2241
+ ...oauthCfg.authParams,
2242
+ ...cliPkceParams(oauthCfg, codeVerifier)
2164
2243
  };
2165
2244
  const authUrl2 = `${oauthCfg.authUrl}?${querystring.stringify(authParams2)}`;
2245
+ if (pkceNonce)
2246
+ savePkceNonce(pkceNonceFile(plugin.id, tenantId, kek), pkceNonce);
2166
2247
  writeAuthState(sessionId, {
2167
2248
  status: "needs_code",
2168
2249
  url: authUrl2,
@@ -2181,7 +2262,8 @@ async function oauthListen(database, plugin, kek, tenantId, sessionId) {
2181
2262
  redirect_uri: redirectUri,
2182
2263
  response_type: "code",
2183
2264
  scope: oauthCfg.scopes.join(" "),
2184
- ...oauthCfg.authParams
2265
+ ...oauthCfg.authParams,
2266
+ ...cliPkceParams(oauthCfg, codeVerifier)
2185
2267
  };
2186
2268
  const authUrl = `${oauthCfg.authUrl}?${querystring.stringify(authParams)}`;
2187
2269
  writeAuthState(sessionId, { status: "listening", url: authUrl });
@@ -2215,7 +2297,8 @@ async function oauthListen(database, plugin, kek, tenantId, sessionId) {
2215
2297
  redirectUri,
2216
2298
  clientId,
2217
2299
  clientSecret,
2218
- oauthCfg
2300
+ oauthCfg,
2301
+ codeVerifier
2219
2302
  );
2220
2303
  writeAuthState(
2221
2304
  sessionId,
@@ -2235,7 +2318,7 @@ async function oauthCollect(database, plugin, kek, tenantId, sessionId) {
2235
2318
  const accessToken = await accountKm.get_access_token();
2236
2319
  if (accessToken) {
2237
2320
  try {
2238
- fs3.unlinkSync(authStateFile(sessionId));
2321
+ fs4.unlinkSync(authStateFile(sessionId));
2239
2322
  } catch {
2240
2323
  }
2241
2324
  out({ status: "success", plugin: plugin.id, tenant: tenantId });
@@ -2243,7 +2326,7 @@ async function oauthCollect(database, plugin, kek, tenantId, sessionId) {
2243
2326
  }
2244
2327
  try {
2245
2328
  const state = JSON.parse(
2246
- fs3.readFileSync(authStateFile(sessionId), "utf8")
2329
+ fs4.readFileSync(authStateFile(sessionId), "utf8")
2247
2330
  );
2248
2331
  if (state.status === "error") {
2249
2332
  out({ status: "error", error: state.error });
@@ -2476,6 +2559,7 @@ Authorize ${plugin.id}
2476
2559
  var init_auth = __esm({
2477
2560
  "src/auth.ts"() {
2478
2561
  "use strict";
2562
+ init_pkce_nonce();
2479
2563
  init_corsair_instance();
2480
2564
  }
2481
2565
  });
@@ -2486,7 +2570,7 @@ import { fileURLToPath } from "url";
2486
2570
  import { Command } from "commander";
2487
2571
 
2488
2572
  // package.json
2489
- var version = "0.1.111";
2573
+ var version = "0.1.113";
2490
2574
 
2491
2575
  // src/utils/subscription.ts
2492
2576
  async function runWebhookSubscription(cwd, pluginId) {
@@ -3308,7 +3392,7 @@ var SharepointSubscribeCommand = class extends SharepointCommand {
3308
3392
  };
3309
3393
 
3310
3394
  // src/commands/studio.command.ts
3311
- import path4 from "path";
3395
+ import path5 from "path";
3312
3396
  function parseStudioPort(raw) {
3313
3397
  if (raw === void 0) {
3314
3398
  return void 0;
@@ -3354,7 +3438,7 @@ var StudioCommand = class extends BaseCommand {
3354
3438
  try {
3355
3439
  const { createRequire } = await import("module");
3356
3440
  const { pathToFileURL } = await import("url");
3357
- const req = createRequire(path4.join(cwd, "package.json"));
3441
+ const req = createRequire(path5.join(cwd, "package.json"));
3358
3442
  const resolvedPath = req.resolve("@corsair-dev/studio/server");
3359
3443
  const mod = await import(pathToFileURL(resolvedPath).href);
3360
3444
  const candidate = mod.start ?? mod.startStudio;
@@ -1 +1 @@
1
- {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/auth.ts"],"names":[],"mappings":"AA4pBA,wBAAsB,OAAO,CAAC,EAC7B,GAAG,EACH,QAAQ,EAAE,WAAW,EACrB,QAAQ,EAAE,WAAW,EACrB,IAAI,EAAE,OAAO,EACb,WAAW,EAAE,eAAuB,EACpC,MAAM,EAAE,UAAkB,EAC1B,OAAO,EAAE,WAAmB,EAC5B,SAAS,GACT,EAAE;IACF,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,uDAAuD;IACvD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,uEAAuE;IACvE,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,iFAAiF;IACjF,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,kFAAkF;IAClF,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,wDAAwD;IACxD,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB,GAAG,OAAO,CAAC,IAAI,CAAC,CAgJhB"}
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/auth.ts"],"names":[],"mappings":"AA8sBA,wBAAsB,OAAO,CAAC,EAC7B,GAAG,EACH,QAAQ,EAAE,WAAW,EACrB,QAAQ,EAAE,WAAW,EACrB,IAAI,EAAE,OAAO,EACb,WAAW,EAAE,eAAuB,EACpC,MAAM,EAAE,UAAkB,EAC1B,OAAO,EAAE,WAAmB,EAC5B,SAAS,GACT,EAAE;IACF,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,uDAAuD;IACvD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,uEAAuE;IACvE,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,iFAAiF;IACjF,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,kFAAkF;IAClF,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,wDAAwD;IACxD,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB,GAAG,OAAO,CAAC,IAAI,CAAC,CAgJhB"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Where a manual sign-in's PKCE nonce waits for its pasted code: a private
3
+ * directory under the home folder, so another local user can't plant a file
4
+ * or symlink there. The name is derived under the KEK from plugin and tenant,
5
+ * so two projects, or tenants like `team.a` and `team_a`, never share a file.
6
+ */
7
+ export declare function pkceNonceFile(pluginId: string, tenantId: string, kek: string, home?: string): string;
8
+ /** Saves the nonce, refusing to follow a symlink at the file's path. */
9
+ export declare function savePkceNonce(file: string, nonce: string): void;
10
+ /** The nonce of the sign-in waiting for a pasted code, left in place until its exchange succeeds. */
11
+ export declare function readPkceNonce(file: string): string | undefined;
12
+ /**
13
+ * Removes the file only while it still holds this exchange's nonce: a newer
14
+ * sign-in for the same plugin and tenant may have saved its own meanwhile, and
15
+ * its pasted code still needs it.
16
+ */
17
+ export declare function removePkceNonce(file: string, nonce: string): void;
18
+ //# sourceMappingURL=pkce-nonce.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pkce-nonce.d.ts","sourceRoot":"","sources":["../../src/pkce-nonce.ts"],"names":[],"mappings":"AAKA;;;;;GAKG;AACH,wBAAgB,aAAa,CAC5B,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,EAChB,GAAG,EAAE,MAAM,EACX,IAAI,SAAe,GACjB,MAAM,CAyBR;AAED,wEAAwE;AACxE,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAe/D;AAED,qGAAqG;AACrG,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAU9D;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAEjE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@corsair-dev/cli",
3
- "version": "0.1.111",
3
+ "version": "0.1.113",
4
4
  "description": "Command-line tool for Corsair, the open-source integration layer. Scaffold a project, add integrations, and run your app.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -24,7 +24,7 @@
24
24
  "c12": "^3.3.3",
25
25
  "commander": "^14.0.3",
26
26
  "zod": "^4.1.13",
27
- "corsair": "0.2.30"
27
+ "corsair": "0.2.32"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/jest": "^29.5.14",