@corsair-dev/cli 0.1.11 → 0.1.13
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/auth.d.ts +7 -3
- package/dist/auth.d.ts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +391 -166
- package/package.json +2 -2
package/dist/auth.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare function runAuth({ cwd, pluginId: pluginIdArg, tenantId: tenantIdArg, code: codeArg, credentials: showCredentials,
|
|
1
|
+
export declare function runAuth({ cwd, pluginId: pluginIdArg, tenantId: tenantIdArg, code: codeArg, credentials: showCredentials, listen: listenMode, collect: collectMode, sessionId, }: {
|
|
2
2
|
cwd: string;
|
|
3
3
|
pluginId?: string;
|
|
4
4
|
tenantId?: string;
|
|
@@ -6,7 +6,11 @@ export declare function runAuth({ cwd, pluginId: pluginIdArg, tenantId: tenantId
|
|
|
6
6
|
code?: string;
|
|
7
7
|
/** Output current credential status instead of starting OAuth flow. */
|
|
8
8
|
credentials?: boolean;
|
|
9
|
-
/**
|
|
10
|
-
|
|
9
|
+
/** Internal: run as the background OAuth callback server for a given session. */
|
|
10
|
+
listen?: boolean;
|
|
11
|
+
/** Check whether the background OAuth session completed and tokens are stored. */
|
|
12
|
+
collect?: boolean;
|
|
13
|
+
/** Session ID tying --listen and --collect together. */
|
|
14
|
+
sessionId?: string;
|
|
11
15
|
}): Promise<void>;
|
|
12
16
|
//# sourceMappingURL=auth.d.ts.map
|
package/dist/auth.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../auth.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../auth.ts"],"names":[],"mappings":"AAguBA,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"}
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAyLA;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAQhE;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,kBAAkB,CAAC,EACxC,GAAG,EACH,UAAU,EACV,kBAA0B,GAC1B,EAAE;IACF,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC7B,eAqLA"}
|
package/dist/index.js
CHANGED
|
@@ -1577,9 +1577,14 @@ var auth_exports = {};
|
|
|
1577
1577
|
__export(auth_exports, {
|
|
1578
1578
|
runAuth: () => runAuth
|
|
1579
1579
|
});
|
|
1580
|
+
import { spawn } from "child_process";
|
|
1581
|
+
import * as crypto4 from "crypto";
|
|
1582
|
+
import * as fs2 from "fs";
|
|
1580
1583
|
import * as http from "http";
|
|
1581
1584
|
import * as https from "https";
|
|
1582
1585
|
import * as net from "net";
|
|
1586
|
+
import * as os from "os";
|
|
1587
|
+
import * as path2 from "path";
|
|
1583
1588
|
import * as querystring from "querystring";
|
|
1584
1589
|
import {
|
|
1585
1590
|
CORSAIR_INTERNAL as CORSAIR_INTERNAL3,
|
|
@@ -1592,6 +1597,30 @@ import { createCorsairOrm } from "corsair/orm";
|
|
|
1592
1597
|
function out(data) {
|
|
1593
1598
|
console.log(JSON.stringify(data));
|
|
1594
1599
|
}
|
|
1600
|
+
function authStateFile(sessionId) {
|
|
1601
|
+
return path2.join(os.tmpdir(), `corsair-auth-${sessionId}.json`);
|
|
1602
|
+
}
|
|
1603
|
+
function writeAuthState(sessionId, state) {
|
|
1604
|
+
fs2.writeFileSync(authStateFile(sessionId), JSON.stringify(state));
|
|
1605
|
+
}
|
|
1606
|
+
function pollAuthState(sessionId, timeoutMs) {
|
|
1607
|
+
return new Promise((resolve, reject) => {
|
|
1608
|
+
const file = authStateFile(sessionId);
|
|
1609
|
+
const start = Date.now();
|
|
1610
|
+
const interval = setInterval(() => {
|
|
1611
|
+
try {
|
|
1612
|
+
const state = JSON.parse(fs2.readFileSync(file, "utf8"));
|
|
1613
|
+
clearInterval(interval);
|
|
1614
|
+
resolve(state);
|
|
1615
|
+
} catch {
|
|
1616
|
+
if (Date.now() - start > timeoutMs) {
|
|
1617
|
+
clearInterval(interval);
|
|
1618
|
+
reject(new Error("Timed out waiting for OAuth server to start"));
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
}, 100);
|
|
1622
|
+
});
|
|
1623
|
+
}
|
|
1595
1624
|
function getOAuthConfigForPlugin(plugin) {
|
|
1596
1625
|
return plugin.oauthConfig ?? null;
|
|
1597
1626
|
}
|
|
@@ -1743,100 +1772,6 @@ async function ensureAccount(database, integrationId, tenantId, kek) {
|
|
|
1743
1772
|
dek: encryptedDek
|
|
1744
1773
|
});
|
|
1745
1774
|
}
|
|
1746
|
-
async function oauthGetUrl(database, plugin, kek, tenantId) {
|
|
1747
|
-
const oauthCfg = getOAuthConfigForPlugin(plugin);
|
|
1748
|
-
if (!oauthCfg) {
|
|
1749
|
-
out({ error: `No oauthConfig defined on plugin '${plugin.id}'.` });
|
|
1750
|
-
return;
|
|
1751
|
-
}
|
|
1752
|
-
const extraFields = getCustomIntegrationFields(plugin, "oauth_2");
|
|
1753
|
-
const integrationKm = createIntegrationKeyManager3({
|
|
1754
|
-
authType: "oauth_2",
|
|
1755
|
-
integrationName: plugin.id,
|
|
1756
|
-
kek,
|
|
1757
|
-
database,
|
|
1758
|
-
extraIntegrationFields: extraFields
|
|
1759
|
-
});
|
|
1760
|
-
const clientId = await integrationKm.get_client_id();
|
|
1761
|
-
if (!clientId) {
|
|
1762
|
-
out({
|
|
1763
|
-
error: `client_id not set for '${plugin.id}'. Run: corsair setup --${plugin.id} client_id=YOUR_CLIENT_ID`
|
|
1764
|
-
});
|
|
1765
|
-
return;
|
|
1766
|
-
}
|
|
1767
|
-
const clientSecret = await integrationKm.get_client_secret();
|
|
1768
|
-
if (!clientSecret) {
|
|
1769
|
-
out({
|
|
1770
|
-
error: `client_secret not set for '${plugin.id}'. Run: corsair setup --${plugin.id} client_secret=YOUR_CLIENT_SECRET`
|
|
1771
|
-
});
|
|
1772
|
-
return;
|
|
1773
|
-
}
|
|
1774
|
-
let redirectUri;
|
|
1775
|
-
if (oauthCfg.requiresRegisteredRedirect) {
|
|
1776
|
-
const stored = await integrationKm.get_redirect_url();
|
|
1777
|
-
if (!stored) {
|
|
1778
|
-
out({
|
|
1779
|
-
error: `redirect_url required for '${plugin.id}'. Run: corsair setup --${plugin.id} redirect_url=YOUR_REDIRECT_URI`
|
|
1780
|
-
});
|
|
1781
|
-
return;
|
|
1782
|
-
}
|
|
1783
|
-
redirectUri = stored;
|
|
1784
|
-
} else {
|
|
1785
|
-
const port = await findFreePort();
|
|
1786
|
-
redirectUri = `http://localhost:${port}`;
|
|
1787
|
-
}
|
|
1788
|
-
const authParams = {
|
|
1789
|
-
client_id: clientId,
|
|
1790
|
-
redirect_uri: redirectUri,
|
|
1791
|
-
response_type: "code",
|
|
1792
|
-
scope: oauthCfg.scopes.join(" "),
|
|
1793
|
-
...oauthCfg.authParams
|
|
1794
|
-
};
|
|
1795
|
-
const authUrl = `${oauthCfg.authUrl}?${querystring.stringify(authParams)}`;
|
|
1796
|
-
const localhostPortMatch = redirectUri.match(
|
|
1797
|
-
/^https?:\/\/(?:localhost|127\.0\.0\.1):(\d+)/
|
|
1798
|
-
);
|
|
1799
|
-
const localhostPort = localhostPortMatch?.[1] ? parseInt(localhostPortMatch[1], 10) : null;
|
|
1800
|
-
if (localhostPort) {
|
|
1801
|
-
out({
|
|
1802
|
-
status: "pending_oauth",
|
|
1803
|
-
authUrl,
|
|
1804
|
-
redirectUri,
|
|
1805
|
-
plugin: plugin.id,
|
|
1806
|
-
tenant: tenantId,
|
|
1807
|
-
note: "Open authUrl in a browser. Tokens will be saved automatically once authorized."
|
|
1808
|
-
});
|
|
1809
|
-
let code;
|
|
1810
|
-
try {
|
|
1811
|
-
code = await waitForOAuthCode(localhostPort);
|
|
1812
|
-
} catch (err) {
|
|
1813
|
-
out({
|
|
1814
|
-
error: `Authorization failed: ${err instanceof Error ? err.message : String(err)}`
|
|
1815
|
-
});
|
|
1816
|
-
return;
|
|
1817
|
-
}
|
|
1818
|
-
await oauthExchangeCode(
|
|
1819
|
-
database,
|
|
1820
|
-
plugin,
|
|
1821
|
-
kek,
|
|
1822
|
-
tenantId,
|
|
1823
|
-
code,
|
|
1824
|
-
redirectUri,
|
|
1825
|
-
clientId,
|
|
1826
|
-
clientSecret,
|
|
1827
|
-
oauthCfg
|
|
1828
|
-
);
|
|
1829
|
-
return;
|
|
1830
|
-
}
|
|
1831
|
-
out({
|
|
1832
|
-
status: "needs_code",
|
|
1833
|
-
authUrl,
|
|
1834
|
-
redirectUri,
|
|
1835
|
-
plugin: plugin.id,
|
|
1836
|
-
tenant: tenantId,
|
|
1837
|
-
note: "Open authUrl, complete auth, then run: corsair auth --plugin=<id> --code=CODE"
|
|
1838
|
-
});
|
|
1839
|
-
}
|
|
1840
1775
|
async function oauthExchangeCode(database, plugin, kek, tenantId, code, redirectUri, clientId, clientSecret, oauthCfg) {
|
|
1841
1776
|
let tokens;
|
|
1842
1777
|
try {
|
|
@@ -1851,13 +1786,13 @@ async function oauthExchangeCode(database, plugin, kek, tenantId, code, redirect
|
|
|
1851
1786
|
out({
|
|
1852
1787
|
error: `Token exchange failed: ${err instanceof Error ? err.message : String(err)}`
|
|
1853
1788
|
});
|
|
1854
|
-
return;
|
|
1789
|
+
return false;
|
|
1855
1790
|
}
|
|
1856
1791
|
if (!tokens.access_token) {
|
|
1857
1792
|
out({
|
|
1858
1793
|
error: `No access_token in response from ${oauthCfg.providerName}.`
|
|
1859
1794
|
});
|
|
1860
|
-
return;
|
|
1795
|
+
return false;
|
|
1861
1796
|
}
|
|
1862
1797
|
const extraAccountFields = getCustomAccountFields(plugin, "oauth_2");
|
|
1863
1798
|
const accountKm = createAccountKeyManager3({
|
|
@@ -1876,6 +1811,7 @@ async function oauthExchangeCode(database, plugin, kek, tenantId, code, redirect
|
|
|
1876
1811
|
(Math.floor(Date.now() / 1e3) + tokens.expires_in).toString()
|
|
1877
1812
|
);
|
|
1878
1813
|
out({ status: "success", plugin: plugin.id, tenant: tenantId });
|
|
1814
|
+
return true;
|
|
1879
1815
|
}
|
|
1880
1816
|
async function oauthWithCode(database, plugin, kek, tenantId, code) {
|
|
1881
1817
|
const oauthCfg = getOAuthConfigForPlugin(plugin);
|
|
@@ -1912,6 +1848,153 @@ async function oauthWithCode(database, plugin, kek, tenantId, code) {
|
|
|
1912
1848
|
oauthCfg
|
|
1913
1849
|
);
|
|
1914
1850
|
}
|
|
1851
|
+
async function oauthListen(database, plugin, kek, tenantId, sessionId) {
|
|
1852
|
+
const oauthCfg = getOAuthConfigForPlugin(plugin);
|
|
1853
|
+
if (!oauthCfg) {
|
|
1854
|
+
writeAuthState(sessionId, {
|
|
1855
|
+
status: "error",
|
|
1856
|
+
error: `No oauthConfig defined on plugin '${plugin.id}'.`
|
|
1857
|
+
});
|
|
1858
|
+
return;
|
|
1859
|
+
}
|
|
1860
|
+
const extraFields = getCustomIntegrationFields(plugin, "oauth_2");
|
|
1861
|
+
const integrationKm = createIntegrationKeyManager3({
|
|
1862
|
+
authType: "oauth_2",
|
|
1863
|
+
integrationName: plugin.id,
|
|
1864
|
+
kek,
|
|
1865
|
+
database,
|
|
1866
|
+
extraIntegrationFields: extraFields
|
|
1867
|
+
});
|
|
1868
|
+
const clientId = await integrationKm.get_client_id();
|
|
1869
|
+
if (!clientId) {
|
|
1870
|
+
writeAuthState(sessionId, {
|
|
1871
|
+
status: "error",
|
|
1872
|
+
error: `client_id not set for '${plugin.id}'.`
|
|
1873
|
+
});
|
|
1874
|
+
return;
|
|
1875
|
+
}
|
|
1876
|
+
const clientSecret = await integrationKm.get_client_secret();
|
|
1877
|
+
if (!clientSecret) {
|
|
1878
|
+
writeAuthState(sessionId, {
|
|
1879
|
+
status: "error",
|
|
1880
|
+
error: `client_secret not set for '${plugin.id}'.`
|
|
1881
|
+
});
|
|
1882
|
+
return;
|
|
1883
|
+
}
|
|
1884
|
+
let redirectUri;
|
|
1885
|
+
let localhostPort;
|
|
1886
|
+
if (oauthCfg.requiresRegisteredRedirect) {
|
|
1887
|
+
const stored = await integrationKm.get_redirect_url();
|
|
1888
|
+
if (!stored) {
|
|
1889
|
+
writeAuthState(sessionId, {
|
|
1890
|
+
status: "error",
|
|
1891
|
+
error: `redirect_url required for '${plugin.id}'.`
|
|
1892
|
+
});
|
|
1893
|
+
return;
|
|
1894
|
+
}
|
|
1895
|
+
const match = stored.match(/^https?:\/\/(?:localhost|127\.0\.0\.1):(\d+)/);
|
|
1896
|
+
if (!match) {
|
|
1897
|
+
const authParams2 = {
|
|
1898
|
+
client_id: clientId,
|
|
1899
|
+
redirect_uri: stored,
|
|
1900
|
+
response_type: "code",
|
|
1901
|
+
scope: oauthCfg.scopes.join(" "),
|
|
1902
|
+
...oauthCfg.authParams
|
|
1903
|
+
};
|
|
1904
|
+
const authUrl2 = `${oauthCfg.authUrl}?${querystring.stringify(authParams2)}`;
|
|
1905
|
+
writeAuthState(sessionId, {
|
|
1906
|
+
status: "needs_code",
|
|
1907
|
+
url: authUrl2,
|
|
1908
|
+
redirectUri: stored
|
|
1909
|
+
});
|
|
1910
|
+
return;
|
|
1911
|
+
}
|
|
1912
|
+
redirectUri = stored;
|
|
1913
|
+
localhostPort = parseInt(match[1], 10);
|
|
1914
|
+
} else {
|
|
1915
|
+
localhostPort = await findFreePort();
|
|
1916
|
+
redirectUri = `http://localhost:${localhostPort}`;
|
|
1917
|
+
}
|
|
1918
|
+
const authParams = {
|
|
1919
|
+
client_id: clientId,
|
|
1920
|
+
redirect_uri: redirectUri,
|
|
1921
|
+
response_type: "code",
|
|
1922
|
+
scope: oauthCfg.scopes.join(" "),
|
|
1923
|
+
...oauthCfg.authParams
|
|
1924
|
+
};
|
|
1925
|
+
const authUrl = `${oauthCfg.authUrl}?${querystring.stringify(authParams)}`;
|
|
1926
|
+
writeAuthState(sessionId, { status: "listening", url: authUrl });
|
|
1927
|
+
const timeout = setTimeout(
|
|
1928
|
+
() => {
|
|
1929
|
+
writeAuthState(sessionId, {
|
|
1930
|
+
status: "error",
|
|
1931
|
+
error: "OAuth session timed out after 10 minutes."
|
|
1932
|
+
});
|
|
1933
|
+
process.exit(0);
|
|
1934
|
+
},
|
|
1935
|
+
10 * 60 * 1e3
|
|
1936
|
+
);
|
|
1937
|
+
timeout.unref();
|
|
1938
|
+
let code;
|
|
1939
|
+
try {
|
|
1940
|
+
code = await waitForOAuthCode(localhostPort);
|
|
1941
|
+
} catch (err) {
|
|
1942
|
+
writeAuthState(sessionId, {
|
|
1943
|
+
status: "error",
|
|
1944
|
+
error: `Authorization failed: ${err instanceof Error ? err.message : String(err)}`
|
|
1945
|
+
});
|
|
1946
|
+
return;
|
|
1947
|
+
}
|
|
1948
|
+
const success = await oauthExchangeCode(
|
|
1949
|
+
database,
|
|
1950
|
+
plugin,
|
|
1951
|
+
kek,
|
|
1952
|
+
tenantId,
|
|
1953
|
+
code,
|
|
1954
|
+
redirectUri,
|
|
1955
|
+
clientId,
|
|
1956
|
+
clientSecret,
|
|
1957
|
+
oauthCfg
|
|
1958
|
+
);
|
|
1959
|
+
writeAuthState(
|
|
1960
|
+
sessionId,
|
|
1961
|
+
success ? { status: "complete" } : { status: "error", error: "Token exchange failed." }
|
|
1962
|
+
);
|
|
1963
|
+
}
|
|
1964
|
+
async function oauthCollect(database, plugin, kek, tenantId, sessionId) {
|
|
1965
|
+
const extraAccountFields = getCustomAccountFields(plugin, "oauth_2");
|
|
1966
|
+
const accountKm = createAccountKeyManager3({
|
|
1967
|
+
authType: "oauth_2",
|
|
1968
|
+
integrationName: plugin.id,
|
|
1969
|
+
tenantId,
|
|
1970
|
+
kek,
|
|
1971
|
+
database,
|
|
1972
|
+
extraAccountFields
|
|
1973
|
+
});
|
|
1974
|
+
const accessToken = await accountKm.get_access_token();
|
|
1975
|
+
if (accessToken) {
|
|
1976
|
+
try {
|
|
1977
|
+
fs2.unlinkSync(authStateFile(sessionId));
|
|
1978
|
+
} catch {
|
|
1979
|
+
}
|
|
1980
|
+
out({ status: "success", plugin: plugin.id, tenant: tenantId });
|
|
1981
|
+
return;
|
|
1982
|
+
}
|
|
1983
|
+
try {
|
|
1984
|
+
const state = JSON.parse(
|
|
1985
|
+
fs2.readFileSync(authStateFile(sessionId), "utf8")
|
|
1986
|
+
);
|
|
1987
|
+
if (state.status === "error") {
|
|
1988
|
+
out({ status: "error", error: state.error });
|
|
1989
|
+
return;
|
|
1990
|
+
}
|
|
1991
|
+
} catch {
|
|
1992
|
+
}
|
|
1993
|
+
out({
|
|
1994
|
+
status: "pending",
|
|
1995
|
+
message: "User has not completed sign-in yet. Try again after the user visits the auth URL."
|
|
1996
|
+
});
|
|
1997
|
+
}
|
|
1915
1998
|
async function getCredentials(database, plugin, kek, tenantId) {
|
|
1916
1999
|
const authType = getAuthType(plugin);
|
|
1917
2000
|
const result = {};
|
|
@@ -1991,7 +2074,9 @@ async function runAuth({
|
|
|
1991
2074
|
tenantId: tenantIdArg,
|
|
1992
2075
|
code: codeArg,
|
|
1993
2076
|
credentials: showCredentials = false,
|
|
1994
|
-
|
|
2077
|
+
listen: listenMode = false,
|
|
2078
|
+
collect: collectMode = false,
|
|
2079
|
+
sessionId
|
|
1995
2080
|
}) {
|
|
1996
2081
|
let internal;
|
|
1997
2082
|
try {
|
|
@@ -2037,29 +2122,95 @@ async function runAuth({
|
|
|
2037
2122
|
});
|
|
2038
2123
|
return;
|
|
2039
2124
|
}
|
|
2040
|
-
if (
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
This command opens a browser and waits for an OAuth callback \u2014 it is a long-running interactive task. Either ask the user to run it in a separate terminal, or run it as a background process. Do NOT run it inline in the current session.`
|
|
2055
|
-
});
|
|
2125
|
+
if (listenMode) {
|
|
2126
|
+
if (!sessionId) {
|
|
2127
|
+
out({ error: "--listen requires --session=<id>." });
|
|
2128
|
+
process.exit(1);
|
|
2129
|
+
}
|
|
2130
|
+
await oauthListen(database, plugin, kek, tenantId, sessionId);
|
|
2131
|
+
return;
|
|
2132
|
+
}
|
|
2133
|
+
if (collectMode) {
|
|
2134
|
+
if (!sessionId) {
|
|
2135
|
+
out({ error: "--collect requires --session=<id>." });
|
|
2136
|
+
process.exit(1);
|
|
2137
|
+
}
|
|
2138
|
+
await oauthCollect(database, plugin, kek, tenantId, sessionId);
|
|
2056
2139
|
return;
|
|
2057
2140
|
}
|
|
2058
2141
|
if (codeArg) {
|
|
2059
2142
|
await oauthWithCode(database, plugin, kek, tenantId, codeArg);
|
|
2060
|
-
|
|
2061
|
-
await oauthGetUrl(database, plugin, kek, tenantId);
|
|
2143
|
+
return;
|
|
2062
2144
|
}
|
|
2145
|
+
const session = crypto4.randomUUID().slice(0, 8);
|
|
2146
|
+
const needsQuoting = tenantIdArg && /[^a-zA-Z0-9_\-.]/.test(tenantIdArg);
|
|
2147
|
+
const tenantFlag = tenantIdArg && tenantIdArg !== "default" ? `--tenant=${needsQuoting ? `"${tenantIdArg}"` : tenantIdArg}` : "";
|
|
2148
|
+
const spawnOpts = {
|
|
2149
|
+
detached: true,
|
|
2150
|
+
stdio: "ignore",
|
|
2151
|
+
cwd: process.cwd(),
|
|
2152
|
+
env: process.env
|
|
2153
|
+
};
|
|
2154
|
+
const child = spawn(
|
|
2155
|
+
process.execPath,
|
|
2156
|
+
[
|
|
2157
|
+
...process.execArgv,
|
|
2158
|
+
process.argv[1] ?? "",
|
|
2159
|
+
"auth",
|
|
2160
|
+
`--plugin=${plugin.id}`,
|
|
2161
|
+
"--listen",
|
|
2162
|
+
`--session=${session}`,
|
|
2163
|
+
...tenantFlag ? [tenantFlag] : []
|
|
2164
|
+
],
|
|
2165
|
+
spawnOpts
|
|
2166
|
+
);
|
|
2167
|
+
child.unref();
|
|
2168
|
+
let state;
|
|
2169
|
+
try {
|
|
2170
|
+
state = await pollAuthState(session, 5e3);
|
|
2171
|
+
} catch (err) {
|
|
2172
|
+
out({
|
|
2173
|
+
error: `Failed to start OAuth server: ${err instanceof Error ? err.message : String(err)}`
|
|
2174
|
+
});
|
|
2175
|
+
return;
|
|
2176
|
+
}
|
|
2177
|
+
if (state.status === "needs_code") {
|
|
2178
|
+
console.log(`
|
|
2179
|
+
Authorize ${plugin.id}
|
|
2180
|
+
`);
|
|
2181
|
+
console.log(` Open this URL in your browser:
|
|
2182
|
+
`);
|
|
2183
|
+
console.log(` ${state.url}
|
|
2184
|
+
`);
|
|
2185
|
+
console.log(
|
|
2186
|
+
` Once you've signed in, copy the code from the redirect URL and run:
|
|
2187
|
+
`
|
|
2188
|
+
);
|
|
2189
|
+
console.log(` pnpm corsair auth --plugin=${plugin.id} --code=CODE
|
|
2190
|
+
`);
|
|
2191
|
+
return;
|
|
2192
|
+
}
|
|
2193
|
+
if (state.status === "error") {
|
|
2194
|
+
out({ error: state.error });
|
|
2195
|
+
return;
|
|
2196
|
+
}
|
|
2197
|
+
const collectCmd = [
|
|
2198
|
+
`pnpm corsair auth --plugin=${plugin.id}`,
|
|
2199
|
+
"--collect",
|
|
2200
|
+
`--session=${session}`,
|
|
2201
|
+
...tenantFlag ? [tenantFlag] : []
|
|
2202
|
+
].join(" ");
|
|
2203
|
+
console.log(`
|
|
2204
|
+
Authorize ${plugin.id}
|
|
2205
|
+
`);
|
|
2206
|
+
console.log(` Open this URL in your browser:
|
|
2207
|
+
`);
|
|
2208
|
+
console.log(` ${state.url}
|
|
2209
|
+
`);
|
|
2210
|
+
console.log(` Once you've signed in, run:
|
|
2211
|
+
`);
|
|
2212
|
+
console.log(` ${collectCmd}
|
|
2213
|
+
`);
|
|
2063
2214
|
}
|
|
2064
2215
|
var init_auth = __esm({
|
|
2065
2216
|
"auth.ts"() {
|
|
@@ -2069,33 +2220,35 @@ var init_auth = __esm({
|
|
|
2069
2220
|
});
|
|
2070
2221
|
|
|
2071
2222
|
// index.ts
|
|
2072
|
-
import
|
|
2073
|
-
import
|
|
2223
|
+
import fs3, { existsSync, realpathSync } from "fs";
|
|
2224
|
+
import path3 from "path";
|
|
2225
|
+
import { fileURLToPath } from "url";
|
|
2074
2226
|
import babelPresetReact from "@babel/preset-react";
|
|
2075
2227
|
import babelPresetTypeScript from "@babel/preset-typescript";
|
|
2076
2228
|
import { loadConfig } from "c12";
|
|
2229
|
+
import { getSchema, listOperations } from "corsair";
|
|
2077
2230
|
function resolveReferencePath(configDir, refPath) {
|
|
2078
|
-
const resolvedPath =
|
|
2231
|
+
const resolvedPath = path3.resolve(configDir, refPath);
|
|
2079
2232
|
if (refPath.endsWith(".json")) {
|
|
2080
2233
|
return resolvedPath;
|
|
2081
2234
|
}
|
|
2082
|
-
if (
|
|
2235
|
+
if (fs3.existsSync(resolvedPath)) {
|
|
2083
2236
|
try {
|
|
2084
|
-
const stats =
|
|
2237
|
+
const stats = fs3.statSync(resolvedPath);
|
|
2085
2238
|
if (stats.isFile()) {
|
|
2086
2239
|
return resolvedPath;
|
|
2087
2240
|
}
|
|
2088
2241
|
} catch {
|
|
2089
2242
|
}
|
|
2090
2243
|
}
|
|
2091
|
-
return
|
|
2244
|
+
return path3.resolve(configDir, refPath, "tsconfig.json");
|
|
2092
2245
|
}
|
|
2093
2246
|
function getPathAliasesRecursive(tsconfigPath, visited = /* @__PURE__ */ new Set()) {
|
|
2094
2247
|
if (visited.has(tsconfigPath)) {
|
|
2095
2248
|
return {};
|
|
2096
2249
|
}
|
|
2097
2250
|
visited.add(tsconfigPath);
|
|
2098
|
-
if (!
|
|
2251
|
+
if (!fs3.existsSync(tsconfigPath)) {
|
|
2099
2252
|
console.warn(`Referenced tsconfig not found: ${tsconfigPath}`);
|
|
2100
2253
|
return {};
|
|
2101
2254
|
}
|
|
@@ -2103,14 +2256,14 @@ function getPathAliasesRecursive(tsconfigPath, visited = /* @__PURE__ */ new Set
|
|
|
2103
2256
|
const tsConfig = getTsconfigInfo(void 0, tsconfigPath);
|
|
2104
2257
|
const { paths = {}, baseUrl = "." } = tsConfig.compilerOptions || {};
|
|
2105
2258
|
const result = {};
|
|
2106
|
-
const configDir =
|
|
2259
|
+
const configDir = path3.dirname(tsconfigPath);
|
|
2107
2260
|
const obj = Object.entries(paths);
|
|
2108
2261
|
for (const [alias, aliasPaths] of obj) {
|
|
2109
2262
|
for (const aliasedPath of aliasPaths) {
|
|
2110
|
-
const resolvedBaseUrl =
|
|
2263
|
+
const resolvedBaseUrl = path3.resolve(configDir, baseUrl);
|
|
2111
2264
|
const finalAlias = alias.slice(-1) === "*" ? alias.slice(0, -1) : alias;
|
|
2112
2265
|
const finalAliasedPath = aliasedPath.slice(-1) === "*" ? aliasedPath.slice(0, -1) : aliasedPath;
|
|
2113
|
-
result[finalAlias || ""] =
|
|
2266
|
+
result[finalAlias || ""] = path3.join(resolvedBaseUrl, finalAliasedPath);
|
|
2114
2267
|
}
|
|
2115
2268
|
}
|
|
2116
2269
|
if (tsConfig.references) {
|
|
@@ -2131,8 +2284,8 @@ function getPathAliasesRecursive(tsconfigPath, visited = /* @__PURE__ */ new Set
|
|
|
2131
2284
|
}
|
|
2132
2285
|
}
|
|
2133
2286
|
function getPathAliases(cwd) {
|
|
2134
|
-
const tsConfigPath =
|
|
2135
|
-
if (!
|
|
2287
|
+
const tsConfigPath = path3.join(cwd, "tsconfig.json");
|
|
2288
|
+
if (!fs3.existsSync(tsConfigPath)) {
|
|
2136
2289
|
return null;
|
|
2137
2290
|
}
|
|
2138
2291
|
try {
|
|
@@ -2145,7 +2298,7 @@ function getPathAliases(cwd) {
|
|
|
2145
2298
|
}
|
|
2146
2299
|
function findCorsairConfigPath(cwd) {
|
|
2147
2300
|
for (const possiblePath of possiblePaths) {
|
|
2148
|
-
const fullPath =
|
|
2301
|
+
const fullPath = path3.join(cwd, possiblePath);
|
|
2149
2302
|
if (existsSync(fullPath)) {
|
|
2150
2303
|
return fullPath;
|
|
2151
2304
|
}
|
|
@@ -2160,7 +2313,7 @@ async function getCorsairInstance({
|
|
|
2160
2313
|
try {
|
|
2161
2314
|
let corsairInstance = null;
|
|
2162
2315
|
if (configPath) {
|
|
2163
|
-
let resolvedPath =
|
|
2316
|
+
let resolvedPath = path3.join(cwd, configPath);
|
|
2164
2317
|
if (existsSync(configPath)) {
|
|
2165
2318
|
resolvedPath = configPath;
|
|
2166
2319
|
}
|
|
@@ -2311,8 +2464,8 @@ function resolveClient(instance, tenant) {
|
|
|
2311
2464
|
}
|
|
2312
2465
|
return obj;
|
|
2313
2466
|
}
|
|
2314
|
-
function navigateToEndpoint(client,
|
|
2315
|
-
const parts =
|
|
2467
|
+
function navigateToEndpoint(client, path4) {
|
|
2468
|
+
const parts = path4.split(".");
|
|
2316
2469
|
let current = client;
|
|
2317
2470
|
for (const part of parts) {
|
|
2318
2471
|
if (current === null || typeof current !== "object") return void 0;
|
|
@@ -2346,20 +2499,26 @@ function parseAuthArgs(args) {
|
|
|
2346
2499
|
let code;
|
|
2347
2500
|
let credentials = false;
|
|
2348
2501
|
let webhook = false;
|
|
2349
|
-
let
|
|
2502
|
+
let listen = false;
|
|
2503
|
+
let collect = false;
|
|
2504
|
+
let sessionId;
|
|
2350
2505
|
for (const arg of args) {
|
|
2351
2506
|
if (arg === "--credentials") {
|
|
2352
2507
|
credentials = true;
|
|
2353
2508
|
continue;
|
|
2354
2509
|
}
|
|
2355
|
-
if (arg === "--agent") {
|
|
2356
|
-
agent = true;
|
|
2357
|
-
continue;
|
|
2358
|
-
}
|
|
2359
2510
|
if (arg === "--webhook") {
|
|
2360
2511
|
webhook = true;
|
|
2361
2512
|
continue;
|
|
2362
2513
|
}
|
|
2514
|
+
if (arg === "--listen") {
|
|
2515
|
+
listen = true;
|
|
2516
|
+
continue;
|
|
2517
|
+
}
|
|
2518
|
+
if (arg === "--collect") {
|
|
2519
|
+
collect = true;
|
|
2520
|
+
continue;
|
|
2521
|
+
}
|
|
2363
2522
|
const eqIdx = arg.indexOf("=");
|
|
2364
2523
|
if (arg.startsWith("--") && eqIdx !== -1) {
|
|
2365
2524
|
const key = arg.slice(2, eqIdx);
|
|
@@ -2376,9 +2535,22 @@ function parseAuthArgs(args) {
|
|
|
2376
2535
|
code = value;
|
|
2377
2536
|
continue;
|
|
2378
2537
|
}
|
|
2538
|
+
if (key === "session") {
|
|
2539
|
+
sessionId = value;
|
|
2540
|
+
continue;
|
|
2541
|
+
}
|
|
2379
2542
|
}
|
|
2380
2543
|
}
|
|
2381
|
-
return {
|
|
2544
|
+
return {
|
|
2545
|
+
pluginId,
|
|
2546
|
+
tenantId,
|
|
2547
|
+
code,
|
|
2548
|
+
credentials,
|
|
2549
|
+
webhook,
|
|
2550
|
+
listen,
|
|
2551
|
+
collect,
|
|
2552
|
+
sessionId
|
|
2553
|
+
};
|
|
2382
2554
|
}
|
|
2383
2555
|
function parseSetupArgs(args) {
|
|
2384
2556
|
let backfill = false;
|
|
@@ -2428,6 +2600,34 @@ function parseRunArgs(args) {
|
|
|
2428
2600
|
}
|
|
2429
2601
|
return { path: endpointPath, input, tenant };
|
|
2430
2602
|
}
|
|
2603
|
+
function parseUiArgs(args) {
|
|
2604
|
+
let port;
|
|
2605
|
+
let open;
|
|
2606
|
+
for (let i = 0; i < args.length; i++) {
|
|
2607
|
+
const arg = args[i];
|
|
2608
|
+
if (arg === "--no-open") {
|
|
2609
|
+
open = false;
|
|
2610
|
+
continue;
|
|
2611
|
+
}
|
|
2612
|
+
if (arg === "--open") {
|
|
2613
|
+
open = true;
|
|
2614
|
+
continue;
|
|
2615
|
+
}
|
|
2616
|
+
if (arg === "--port" && args[i + 1]) {
|
|
2617
|
+
const next = args[++i];
|
|
2618
|
+
if (next) {
|
|
2619
|
+
const parsed = Number.parseInt(next, 10);
|
|
2620
|
+
if (Number.isFinite(parsed)) port = parsed;
|
|
2621
|
+
}
|
|
2622
|
+
continue;
|
|
2623
|
+
}
|
|
2624
|
+
if (arg.startsWith("--port=")) {
|
|
2625
|
+
const parsed = Number.parseInt(arg.slice("--port=".length), 10);
|
|
2626
|
+
if (Number.isFinite(parsed)) port = parsed;
|
|
2627
|
+
}
|
|
2628
|
+
}
|
|
2629
|
+
return { port, open };
|
|
2630
|
+
}
|
|
2431
2631
|
function parseScriptArgs(args) {
|
|
2432
2632
|
let code;
|
|
2433
2633
|
let tenant;
|
|
@@ -2453,12 +2653,12 @@ function printHelp() {
|
|
|
2453
2653
|
"pnpm corsair setup --plugin=<id> <field>=VALUE Set plugin credentials",
|
|
2454
2654
|
"pnpm corsair auth --plugin=<id> Start OAuth flow",
|
|
2455
2655
|
"pnpm corsair auth --plugin=<id> --code=<code> Exchange OAuth code for tokens",
|
|
2456
|
-
"pnpm corsair auth --plugin=<id> --agent Run this if you are an agent (specific instructions to handle pending process)",
|
|
2457
2656
|
"pnpm corsair auth --plugin=<id> --credentials Show credential status",
|
|
2458
2657
|
"pnpm corsair auth --plugin=<id> --webhook Set up webhook subscription",
|
|
2459
2658
|
" `pnpm corsair list --type=webhooks` to see webhook plugins",
|
|
2460
2659
|
"pnpm corsair list [--plugin=<id>] [--type=api|webhooks|db] List endpoint paths (tip: pipe to grep to filter)",
|
|
2461
2660
|
"pnpm corsair schema <path> Show schema for an endpoint/webhook/DB entity",
|
|
2661
|
+
"pnpm corsair ui [--port=4317] [--no-open] Open the Corsair Studio dashboard (requires @corsair-dev/studio)",
|
|
2462
2662
|
'pnpm corsair script --code "<js>" [--tenant=<id>]',
|
|
2463
2663
|
" corsair is injected; use return to output a value.",
|
|
2464
2664
|
" IMPORTANT: Always filter results inline \u2014 you are the consumer of the return value, so returning full list responses wastes tokens.",
|
|
@@ -2554,29 +2754,17 @@ async function main() {
|
|
|
2554
2754
|
if (command === "list") {
|
|
2555
2755
|
const { plugin, type } = parseListArgs(args.slice(1));
|
|
2556
2756
|
const instance = await getCorsairInstance({ cwd });
|
|
2557
|
-
const
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
);
|
|
2562
|
-
process.exit(1);
|
|
2563
|
-
}
|
|
2564
|
-
const result = corsair.list_operations({ plugin, type });
|
|
2757
|
+
const result = listOperations(instance, {
|
|
2758
|
+
plugin,
|
|
2759
|
+
type
|
|
2760
|
+
});
|
|
2565
2761
|
if (type === "db") {
|
|
2566
2762
|
console.log(
|
|
2567
2763
|
"[#corsair]: NOTE: Every DB query listed here has both .search() and .list() methods available."
|
|
2568
2764
|
);
|
|
2569
2765
|
console.log("");
|
|
2570
2766
|
}
|
|
2571
|
-
|
|
2572
|
-
console.log(result);
|
|
2573
|
-
} else if (Array.isArray(result)) {
|
|
2574
|
-
console.log(result.join("\n"));
|
|
2575
|
-
} else if (result && typeof result === "object") {
|
|
2576
|
-
const grouped = result;
|
|
2577
|
-
const all = Object.values(grouped).flat();
|
|
2578
|
-
console.log(all.join("\n"));
|
|
2579
|
-
}
|
|
2767
|
+
console.log(result);
|
|
2580
2768
|
return;
|
|
2581
2769
|
}
|
|
2582
2770
|
if (SHOW_RUN && command === "run") {
|
|
@@ -2645,6 +2833,40 @@ async function main() {
|
|
|
2645
2833
|
}
|
|
2646
2834
|
return;
|
|
2647
2835
|
}
|
|
2836
|
+
if (command === "ui" || command === "studio") {
|
|
2837
|
+
const { port, open } = parseUiArgs(args.slice(1));
|
|
2838
|
+
let startStudio;
|
|
2839
|
+
try {
|
|
2840
|
+
const { createRequire } = await import("module");
|
|
2841
|
+
const { pathToFileURL } = await import("url");
|
|
2842
|
+
const req = createRequire(path3.join(cwd, "package.json"));
|
|
2843
|
+
const resolvedPath = req.resolve("@corsair-dev/studio/server");
|
|
2844
|
+
const mod = await import(pathToFileURL(resolvedPath).href);
|
|
2845
|
+
const candidate = mod.start ?? mod.startStudio;
|
|
2846
|
+
if (!candidate) {
|
|
2847
|
+
throw new Error("@corsair-dev/studio/server did not export `start`.");
|
|
2848
|
+
}
|
|
2849
|
+
startStudio = candidate;
|
|
2850
|
+
} catch (err) {
|
|
2851
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
2852
|
+
if (msg.includes("Cannot find package") || msg.includes("Cannot find module") || msg.includes("ERR_MODULE_NOT_FOUND") || msg.includes("MODULE_NOT_FOUND")) {
|
|
2853
|
+
console.error("[#corsair]: Corsair Studio is not installed.");
|
|
2854
|
+
console.error("[#corsair]: Install it with:");
|
|
2855
|
+
console.error("");
|
|
2856
|
+
console.error(" pnpm add -D @corsair-dev/studio");
|
|
2857
|
+
console.error("");
|
|
2858
|
+
process.exit(1);
|
|
2859
|
+
}
|
|
2860
|
+
throw err;
|
|
2861
|
+
}
|
|
2862
|
+
await startStudio({ cwd, port, open });
|
|
2863
|
+
await new Promise((resolve) => {
|
|
2864
|
+
const shutdown = () => resolve();
|
|
2865
|
+
process.once("SIGINT", shutdown);
|
|
2866
|
+
process.once("SIGTERM", shutdown);
|
|
2867
|
+
});
|
|
2868
|
+
return;
|
|
2869
|
+
}
|
|
2648
2870
|
if (command === "schema") {
|
|
2649
2871
|
const schemaPath = args[1];
|
|
2650
2872
|
if (!schemaPath) {
|
|
@@ -2655,20 +2877,24 @@ async function main() {
|
|
|
2655
2877
|
process.exit(1);
|
|
2656
2878
|
}
|
|
2657
2879
|
const instance = await getCorsairInstance({ cwd });
|
|
2658
|
-
const
|
|
2659
|
-
if (typeof corsair.get_schema !== "function") {
|
|
2660
|
-
console.error(
|
|
2661
|
-
"[#corsair]: get_schema not available on this Corsair instance."
|
|
2662
|
-
);
|
|
2663
|
-
process.exit(1);
|
|
2664
|
-
}
|
|
2665
|
-
const result = corsair.get_schema(schemaPath);
|
|
2880
|
+
const result = getSchema(instance, schemaPath);
|
|
2666
2881
|
console.log(result);
|
|
2667
2882
|
return;
|
|
2668
2883
|
}
|
|
2669
2884
|
printHelp();
|
|
2670
2885
|
}
|
|
2671
|
-
|
|
2886
|
+
function detectIsMainModule() {
|
|
2887
|
+
const argv1 = process.argv[1];
|
|
2888
|
+
if (!argv1) return false;
|
|
2889
|
+
try {
|
|
2890
|
+
const argvResolved = realpathSync(argv1);
|
|
2891
|
+
const selfResolved = realpathSync(fileURLToPath(import.meta.url));
|
|
2892
|
+
return argvResolved === selfResolved;
|
|
2893
|
+
} catch {
|
|
2894
|
+
return import.meta.url === `file://${argv1.replace(/\\/g, "/")}`;
|
|
2895
|
+
}
|
|
2896
|
+
}
|
|
2897
|
+
var possiblePaths, jitiOptions, isCorsairInstance, SHOW_RUN;
|
|
2672
2898
|
var init_index = __esm({
|
|
2673
2899
|
"index.ts"() {
|
|
2674
2900
|
init_get_tsconfig_info();
|
|
@@ -2733,8 +2959,7 @@ var init_index = __esm({
|
|
|
2733
2959
|
return Object.keys(object).length > 0;
|
|
2734
2960
|
};
|
|
2735
2961
|
SHOW_RUN = false;
|
|
2736
|
-
|
|
2737
|
-
if (isMainModule) {
|
|
2962
|
+
if (detectIsMainModule()) {
|
|
2738
2963
|
main().then(() => process.exit(0)).catch((e) => {
|
|
2739
2964
|
console.error(e);
|
|
2740
2965
|
process.exit(1);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@corsair-dev/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.13",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@clack/prompts": "^1.0.0",
|
|
24
24
|
"c12": "^3.3.3",
|
|
25
25
|
"zod": "^3.25.76",
|
|
26
|
-
"corsair": "0.1.
|
|
26
|
+
"corsair": "0.1.60"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/jest": "^29.5.14",
|