@corsair-dev/cli 0.1.12 → 0.1.14
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 +316 -214
- 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":"AA6pBA,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,21 +1577,51 @@ 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,
|
|
1586
1591
|
createAccountKeyManager as createAccountKeyManager3,
|
|
1587
1592
|
createIntegrationKeyManager as createIntegrationKeyManager3,
|
|
1588
1593
|
encryptDEK,
|
|
1594
|
+
exchangeCodeForTokens,
|
|
1589
1595
|
generateDEK
|
|
1590
1596
|
} from "corsair/core";
|
|
1591
1597
|
import { createCorsairOrm } from "corsair/orm";
|
|
1592
1598
|
function out(data) {
|
|
1593
1599
|
console.log(JSON.stringify(data));
|
|
1594
1600
|
}
|
|
1601
|
+
function authStateFile(sessionId) {
|
|
1602
|
+
return path2.join(os.tmpdir(), `corsair-auth-${sessionId}.json`);
|
|
1603
|
+
}
|
|
1604
|
+
function writeAuthState(sessionId, state) {
|
|
1605
|
+
fs2.writeFileSync(authStateFile(sessionId), JSON.stringify(state));
|
|
1606
|
+
}
|
|
1607
|
+
function pollAuthState(sessionId, timeoutMs) {
|
|
1608
|
+
return new Promise((resolve, reject) => {
|
|
1609
|
+
const file = authStateFile(sessionId);
|
|
1610
|
+
const start = Date.now();
|
|
1611
|
+
const interval = setInterval(() => {
|
|
1612
|
+
try {
|
|
1613
|
+
const state = JSON.parse(fs2.readFileSync(file, "utf8"));
|
|
1614
|
+
clearInterval(interval);
|
|
1615
|
+
resolve(state);
|
|
1616
|
+
} catch {
|
|
1617
|
+
if (Date.now() - start > timeoutMs) {
|
|
1618
|
+
clearInterval(interval);
|
|
1619
|
+
reject(new Error("Timed out waiting for OAuth server to start"));
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
}, 100);
|
|
1623
|
+
});
|
|
1624
|
+
}
|
|
1595
1625
|
function getOAuthConfigForPlugin(plugin) {
|
|
1596
1626
|
return plugin.oauthConfig ?? null;
|
|
1597
1627
|
}
|
|
@@ -1654,56 +1684,6 @@ function waitForOAuthCode(port) {
|
|
|
1654
1684
|
server.on("error", reject);
|
|
1655
1685
|
});
|
|
1656
1686
|
}
|
|
1657
|
-
function exchangeCodeForTokens(code, clientId, clientSecret, oauthConfig, redirectUri) {
|
|
1658
|
-
const tokenUrl = new URL(oauthConfig.tokenUrl);
|
|
1659
|
-
const useBasicAuth = oauthConfig.tokenAuthMethod === "basic";
|
|
1660
|
-
return new Promise((resolve, reject) => {
|
|
1661
|
-
const postDataParams = {
|
|
1662
|
-
code: code.trim(),
|
|
1663
|
-
redirect_uri: redirectUri,
|
|
1664
|
-
grant_type: "authorization_code"
|
|
1665
|
-
};
|
|
1666
|
-
if (!useBasicAuth) {
|
|
1667
|
-
postDataParams.client_id = clientId;
|
|
1668
|
-
postDataParams.client_secret = clientSecret;
|
|
1669
|
-
}
|
|
1670
|
-
const postData = querystring.stringify(postDataParams);
|
|
1671
|
-
const headers = {
|
|
1672
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
1673
|
-
"Content-Length": Buffer.byteLength(postData).toString()
|
|
1674
|
-
};
|
|
1675
|
-
if (useBasicAuth) {
|
|
1676
|
-
headers.Authorization = `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString("base64")}`;
|
|
1677
|
-
}
|
|
1678
|
-
const req = https.request(
|
|
1679
|
-
{
|
|
1680
|
-
hostname: tokenUrl.hostname,
|
|
1681
|
-
path: tokenUrl.pathname,
|
|
1682
|
-
method: "POST",
|
|
1683
|
-
headers
|
|
1684
|
-
},
|
|
1685
|
-
(res) => {
|
|
1686
|
-
let data = "";
|
|
1687
|
-
res.on("data", (chunk) => {
|
|
1688
|
-
data += chunk;
|
|
1689
|
-
});
|
|
1690
|
-
res.on("end", () => {
|
|
1691
|
-
if (res.statusCode !== 200) {
|
|
1692
|
-
reject(new Error(`Token exchange failed: ${data}`));
|
|
1693
|
-
return;
|
|
1694
|
-
}
|
|
1695
|
-
resolve(JSON.parse(data));
|
|
1696
|
-
});
|
|
1697
|
-
}
|
|
1698
|
-
);
|
|
1699
|
-
req.on(
|
|
1700
|
-
"error",
|
|
1701
|
-
(error) => reject(new Error(`Request failed: ${error.message}`))
|
|
1702
|
-
);
|
|
1703
|
-
req.write(postData);
|
|
1704
|
-
req.end();
|
|
1705
|
-
});
|
|
1706
|
-
}
|
|
1707
1687
|
async function extractInternalConfig3(cwd) {
|
|
1708
1688
|
const instance = await getCorsairInstance({ cwd, shouldThrowOnError: true });
|
|
1709
1689
|
const internal = instance[CORSAIR_INTERNAL3];
|
|
@@ -1743,100 +1723,6 @@ async function ensureAccount(database, integrationId, tenantId, kek) {
|
|
|
1743
1723
|
dek: encryptedDek
|
|
1744
1724
|
});
|
|
1745
1725
|
}
|
|
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
1726
|
async function oauthExchangeCode(database, plugin, kek, tenantId, code, redirectUri, clientId, clientSecret, oauthCfg) {
|
|
1841
1727
|
let tokens;
|
|
1842
1728
|
try {
|
|
@@ -1851,13 +1737,13 @@ async function oauthExchangeCode(database, plugin, kek, tenantId, code, redirect
|
|
|
1851
1737
|
out({
|
|
1852
1738
|
error: `Token exchange failed: ${err instanceof Error ? err.message : String(err)}`
|
|
1853
1739
|
});
|
|
1854
|
-
return;
|
|
1740
|
+
return false;
|
|
1855
1741
|
}
|
|
1856
1742
|
if (!tokens.access_token) {
|
|
1857
1743
|
out({
|
|
1858
1744
|
error: `No access_token in response from ${oauthCfg.providerName}.`
|
|
1859
1745
|
});
|
|
1860
|
-
return;
|
|
1746
|
+
return false;
|
|
1861
1747
|
}
|
|
1862
1748
|
const extraAccountFields = getCustomAccountFields(plugin, "oauth_2");
|
|
1863
1749
|
const accountKm = createAccountKeyManager3({
|
|
@@ -1876,6 +1762,7 @@ async function oauthExchangeCode(database, plugin, kek, tenantId, code, redirect
|
|
|
1876
1762
|
(Math.floor(Date.now() / 1e3) + tokens.expires_in).toString()
|
|
1877
1763
|
);
|
|
1878
1764
|
out({ status: "success", plugin: plugin.id, tenant: tenantId });
|
|
1765
|
+
return true;
|
|
1879
1766
|
}
|
|
1880
1767
|
async function oauthWithCode(database, plugin, kek, tenantId, code) {
|
|
1881
1768
|
const oauthCfg = getOAuthConfigForPlugin(plugin);
|
|
@@ -1912,6 +1799,153 @@ async function oauthWithCode(database, plugin, kek, tenantId, code) {
|
|
|
1912
1799
|
oauthCfg
|
|
1913
1800
|
);
|
|
1914
1801
|
}
|
|
1802
|
+
async function oauthListen(database, plugin, kek, tenantId, sessionId) {
|
|
1803
|
+
const oauthCfg = getOAuthConfigForPlugin(plugin);
|
|
1804
|
+
if (!oauthCfg) {
|
|
1805
|
+
writeAuthState(sessionId, {
|
|
1806
|
+
status: "error",
|
|
1807
|
+
error: `No oauthConfig defined on plugin '${plugin.id}'.`
|
|
1808
|
+
});
|
|
1809
|
+
return;
|
|
1810
|
+
}
|
|
1811
|
+
const extraFields = getCustomIntegrationFields(plugin, "oauth_2");
|
|
1812
|
+
const integrationKm = createIntegrationKeyManager3({
|
|
1813
|
+
authType: "oauth_2",
|
|
1814
|
+
integrationName: plugin.id,
|
|
1815
|
+
kek,
|
|
1816
|
+
database,
|
|
1817
|
+
extraIntegrationFields: extraFields
|
|
1818
|
+
});
|
|
1819
|
+
const clientId = await integrationKm.get_client_id();
|
|
1820
|
+
if (!clientId) {
|
|
1821
|
+
writeAuthState(sessionId, {
|
|
1822
|
+
status: "error",
|
|
1823
|
+
error: `client_id not set for '${plugin.id}'.`
|
|
1824
|
+
});
|
|
1825
|
+
return;
|
|
1826
|
+
}
|
|
1827
|
+
const clientSecret = await integrationKm.get_client_secret();
|
|
1828
|
+
if (!clientSecret) {
|
|
1829
|
+
writeAuthState(sessionId, {
|
|
1830
|
+
status: "error",
|
|
1831
|
+
error: `client_secret not set for '${plugin.id}'.`
|
|
1832
|
+
});
|
|
1833
|
+
return;
|
|
1834
|
+
}
|
|
1835
|
+
let redirectUri;
|
|
1836
|
+
let localhostPort;
|
|
1837
|
+
if (oauthCfg.requiresRegisteredRedirect) {
|
|
1838
|
+
const stored = await integrationKm.get_redirect_url();
|
|
1839
|
+
if (!stored) {
|
|
1840
|
+
writeAuthState(sessionId, {
|
|
1841
|
+
status: "error",
|
|
1842
|
+
error: `redirect_url required for '${plugin.id}'.`
|
|
1843
|
+
});
|
|
1844
|
+
return;
|
|
1845
|
+
}
|
|
1846
|
+
const match = stored.match(/^https?:\/\/(?:localhost|127\.0\.0\.1):(\d+)/);
|
|
1847
|
+
if (!match) {
|
|
1848
|
+
const authParams2 = {
|
|
1849
|
+
client_id: clientId,
|
|
1850
|
+
redirect_uri: stored,
|
|
1851
|
+
response_type: "code",
|
|
1852
|
+
scope: oauthCfg.scopes.join(" "),
|
|
1853
|
+
...oauthCfg.authParams
|
|
1854
|
+
};
|
|
1855
|
+
const authUrl2 = `${oauthCfg.authUrl}?${querystring.stringify(authParams2)}`;
|
|
1856
|
+
writeAuthState(sessionId, {
|
|
1857
|
+
status: "needs_code",
|
|
1858
|
+
url: authUrl2,
|
|
1859
|
+
redirectUri: stored
|
|
1860
|
+
});
|
|
1861
|
+
return;
|
|
1862
|
+
}
|
|
1863
|
+
redirectUri = stored;
|
|
1864
|
+
localhostPort = parseInt(match[1], 10);
|
|
1865
|
+
} else {
|
|
1866
|
+
localhostPort = await findFreePort();
|
|
1867
|
+
redirectUri = `http://localhost:${localhostPort}`;
|
|
1868
|
+
}
|
|
1869
|
+
const authParams = {
|
|
1870
|
+
client_id: clientId,
|
|
1871
|
+
redirect_uri: redirectUri,
|
|
1872
|
+
response_type: "code",
|
|
1873
|
+
scope: oauthCfg.scopes.join(" "),
|
|
1874
|
+
...oauthCfg.authParams
|
|
1875
|
+
};
|
|
1876
|
+
const authUrl = `${oauthCfg.authUrl}?${querystring.stringify(authParams)}`;
|
|
1877
|
+
writeAuthState(sessionId, { status: "listening", url: authUrl });
|
|
1878
|
+
const timeout = setTimeout(
|
|
1879
|
+
() => {
|
|
1880
|
+
writeAuthState(sessionId, {
|
|
1881
|
+
status: "error",
|
|
1882
|
+
error: "OAuth session timed out after 10 minutes."
|
|
1883
|
+
});
|
|
1884
|
+
process.exit(0);
|
|
1885
|
+
},
|
|
1886
|
+
10 * 60 * 1e3
|
|
1887
|
+
);
|
|
1888
|
+
timeout.unref();
|
|
1889
|
+
let code;
|
|
1890
|
+
try {
|
|
1891
|
+
code = await waitForOAuthCode(localhostPort);
|
|
1892
|
+
} catch (err) {
|
|
1893
|
+
writeAuthState(sessionId, {
|
|
1894
|
+
status: "error",
|
|
1895
|
+
error: `Authorization failed: ${err instanceof Error ? err.message : String(err)}`
|
|
1896
|
+
});
|
|
1897
|
+
return;
|
|
1898
|
+
}
|
|
1899
|
+
const success = await oauthExchangeCode(
|
|
1900
|
+
database,
|
|
1901
|
+
plugin,
|
|
1902
|
+
kek,
|
|
1903
|
+
tenantId,
|
|
1904
|
+
code,
|
|
1905
|
+
redirectUri,
|
|
1906
|
+
clientId,
|
|
1907
|
+
clientSecret,
|
|
1908
|
+
oauthCfg
|
|
1909
|
+
);
|
|
1910
|
+
writeAuthState(
|
|
1911
|
+
sessionId,
|
|
1912
|
+
success ? { status: "complete" } : { status: "error", error: "Token exchange failed." }
|
|
1913
|
+
);
|
|
1914
|
+
}
|
|
1915
|
+
async function oauthCollect(database, plugin, kek, tenantId, sessionId) {
|
|
1916
|
+
const extraAccountFields = getCustomAccountFields(plugin, "oauth_2");
|
|
1917
|
+
const accountKm = createAccountKeyManager3({
|
|
1918
|
+
authType: "oauth_2",
|
|
1919
|
+
integrationName: plugin.id,
|
|
1920
|
+
tenantId,
|
|
1921
|
+
kek,
|
|
1922
|
+
database,
|
|
1923
|
+
extraAccountFields
|
|
1924
|
+
});
|
|
1925
|
+
const accessToken = await accountKm.get_access_token();
|
|
1926
|
+
if (accessToken) {
|
|
1927
|
+
try {
|
|
1928
|
+
fs2.unlinkSync(authStateFile(sessionId));
|
|
1929
|
+
} catch {
|
|
1930
|
+
}
|
|
1931
|
+
out({ status: "success", plugin: plugin.id, tenant: tenantId });
|
|
1932
|
+
return;
|
|
1933
|
+
}
|
|
1934
|
+
try {
|
|
1935
|
+
const state = JSON.parse(
|
|
1936
|
+
fs2.readFileSync(authStateFile(sessionId), "utf8")
|
|
1937
|
+
);
|
|
1938
|
+
if (state.status === "error") {
|
|
1939
|
+
out({ status: "error", error: state.error });
|
|
1940
|
+
return;
|
|
1941
|
+
}
|
|
1942
|
+
} catch {
|
|
1943
|
+
}
|
|
1944
|
+
out({
|
|
1945
|
+
status: "pending",
|
|
1946
|
+
message: "User has not completed sign-in yet. Try again after the user visits the auth URL."
|
|
1947
|
+
});
|
|
1948
|
+
}
|
|
1915
1949
|
async function getCredentials(database, plugin, kek, tenantId) {
|
|
1916
1950
|
const authType = getAuthType(plugin);
|
|
1917
1951
|
const result = {};
|
|
@@ -1991,7 +2025,9 @@ async function runAuth({
|
|
|
1991
2025
|
tenantId: tenantIdArg,
|
|
1992
2026
|
code: codeArg,
|
|
1993
2027
|
credentials: showCredentials = false,
|
|
1994
|
-
|
|
2028
|
+
listen: listenMode = false,
|
|
2029
|
+
collect: collectMode = false,
|
|
2030
|
+
sessionId
|
|
1995
2031
|
}) {
|
|
1996
2032
|
let internal;
|
|
1997
2033
|
try {
|
|
@@ -2037,29 +2073,95 @@ async function runAuth({
|
|
|
2037
2073
|
});
|
|
2038
2074
|
return;
|
|
2039
2075
|
}
|
|
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
|
-
});
|
|
2076
|
+
if (listenMode) {
|
|
2077
|
+
if (!sessionId) {
|
|
2078
|
+
out({ error: "--listen requires --session=<id>." });
|
|
2079
|
+
process.exit(1);
|
|
2080
|
+
}
|
|
2081
|
+
await oauthListen(database, plugin, kek, tenantId, sessionId);
|
|
2082
|
+
return;
|
|
2083
|
+
}
|
|
2084
|
+
if (collectMode) {
|
|
2085
|
+
if (!sessionId) {
|
|
2086
|
+
out({ error: "--collect requires --session=<id>." });
|
|
2087
|
+
process.exit(1);
|
|
2088
|
+
}
|
|
2089
|
+
await oauthCollect(database, plugin, kek, tenantId, sessionId);
|
|
2056
2090
|
return;
|
|
2057
2091
|
}
|
|
2058
2092
|
if (codeArg) {
|
|
2059
2093
|
await oauthWithCode(database, plugin, kek, tenantId, codeArg);
|
|
2060
|
-
|
|
2061
|
-
|
|
2094
|
+
return;
|
|
2095
|
+
}
|
|
2096
|
+
const session = crypto4.randomUUID().slice(0, 8);
|
|
2097
|
+
const needsQuoting = tenantIdArg && /[^a-zA-Z0-9_\-.]/.test(tenantIdArg);
|
|
2098
|
+
const tenantFlag = tenantIdArg && tenantIdArg !== "default" ? `--tenant=${needsQuoting ? `"${tenantIdArg}"` : tenantIdArg}` : "";
|
|
2099
|
+
const spawnOpts = {
|
|
2100
|
+
detached: true,
|
|
2101
|
+
stdio: "ignore",
|
|
2102
|
+
cwd: process.cwd(),
|
|
2103
|
+
env: process.env
|
|
2104
|
+
};
|
|
2105
|
+
const child = spawn(
|
|
2106
|
+
process.execPath,
|
|
2107
|
+
[
|
|
2108
|
+
...process.execArgv,
|
|
2109
|
+
process.argv[1] ?? "",
|
|
2110
|
+
"auth",
|
|
2111
|
+
`--plugin=${plugin.id}`,
|
|
2112
|
+
"--listen",
|
|
2113
|
+
`--session=${session}`,
|
|
2114
|
+
...tenantFlag ? [tenantFlag] : []
|
|
2115
|
+
],
|
|
2116
|
+
spawnOpts
|
|
2117
|
+
);
|
|
2118
|
+
child.unref();
|
|
2119
|
+
let state;
|
|
2120
|
+
try {
|
|
2121
|
+
state = await pollAuthState(session, 5e3);
|
|
2122
|
+
} catch (err) {
|
|
2123
|
+
out({
|
|
2124
|
+
error: `Failed to start OAuth server: ${err instanceof Error ? err.message : String(err)}`
|
|
2125
|
+
});
|
|
2126
|
+
return;
|
|
2062
2127
|
}
|
|
2128
|
+
if (state.status === "needs_code") {
|
|
2129
|
+
console.log(`
|
|
2130
|
+
Authorize ${plugin.id}
|
|
2131
|
+
`);
|
|
2132
|
+
console.log(` Open this URL in your browser:
|
|
2133
|
+
`);
|
|
2134
|
+
console.log(` ${state.url}
|
|
2135
|
+
`);
|
|
2136
|
+
console.log(
|
|
2137
|
+
` Once you've signed in, copy the code from the redirect URL and run:
|
|
2138
|
+
`
|
|
2139
|
+
);
|
|
2140
|
+
console.log(` pnpm corsair auth --plugin=${plugin.id} --code=CODE
|
|
2141
|
+
`);
|
|
2142
|
+
return;
|
|
2143
|
+
}
|
|
2144
|
+
if (state.status === "error") {
|
|
2145
|
+
out({ error: state.error });
|
|
2146
|
+
return;
|
|
2147
|
+
}
|
|
2148
|
+
const collectCmd = [
|
|
2149
|
+
`pnpm corsair auth --plugin=${plugin.id}`,
|
|
2150
|
+
"--collect",
|
|
2151
|
+
`--session=${session}`,
|
|
2152
|
+
...tenantFlag ? [tenantFlag] : []
|
|
2153
|
+
].join(" ");
|
|
2154
|
+
console.log(`
|
|
2155
|
+
Authorize ${plugin.id}
|
|
2156
|
+
`);
|
|
2157
|
+
console.log(` Open this URL in your browser:
|
|
2158
|
+
`);
|
|
2159
|
+
console.log(` ${state.url}
|
|
2160
|
+
`);
|
|
2161
|
+
console.log(` Once you've signed in, run:
|
|
2162
|
+
`);
|
|
2163
|
+
console.log(` ${collectCmd}
|
|
2164
|
+
`);
|
|
2063
2165
|
}
|
|
2064
2166
|
var init_auth = __esm({
|
|
2065
2167
|
"auth.ts"() {
|
|
@@ -2069,34 +2171,35 @@ var init_auth = __esm({
|
|
|
2069
2171
|
});
|
|
2070
2172
|
|
|
2071
2173
|
// index.ts
|
|
2072
|
-
import
|
|
2073
|
-
import
|
|
2174
|
+
import fs3, { existsSync, realpathSync } from "fs";
|
|
2175
|
+
import path3 from "path";
|
|
2074
2176
|
import { fileURLToPath } from "url";
|
|
2075
2177
|
import babelPresetReact from "@babel/preset-react";
|
|
2076
2178
|
import babelPresetTypeScript from "@babel/preset-typescript";
|
|
2077
2179
|
import { loadConfig } from "c12";
|
|
2180
|
+
import { getSchema, listOperations } from "corsair";
|
|
2078
2181
|
function resolveReferencePath(configDir, refPath) {
|
|
2079
|
-
const resolvedPath =
|
|
2182
|
+
const resolvedPath = path3.resolve(configDir, refPath);
|
|
2080
2183
|
if (refPath.endsWith(".json")) {
|
|
2081
2184
|
return resolvedPath;
|
|
2082
2185
|
}
|
|
2083
|
-
if (
|
|
2186
|
+
if (fs3.existsSync(resolvedPath)) {
|
|
2084
2187
|
try {
|
|
2085
|
-
const stats =
|
|
2188
|
+
const stats = fs3.statSync(resolvedPath);
|
|
2086
2189
|
if (stats.isFile()) {
|
|
2087
2190
|
return resolvedPath;
|
|
2088
2191
|
}
|
|
2089
2192
|
} catch {
|
|
2090
2193
|
}
|
|
2091
2194
|
}
|
|
2092
|
-
return
|
|
2195
|
+
return path3.resolve(configDir, refPath, "tsconfig.json");
|
|
2093
2196
|
}
|
|
2094
2197
|
function getPathAliasesRecursive(tsconfigPath, visited = /* @__PURE__ */ new Set()) {
|
|
2095
2198
|
if (visited.has(tsconfigPath)) {
|
|
2096
2199
|
return {};
|
|
2097
2200
|
}
|
|
2098
2201
|
visited.add(tsconfigPath);
|
|
2099
|
-
if (!
|
|
2202
|
+
if (!fs3.existsSync(tsconfigPath)) {
|
|
2100
2203
|
console.warn(`Referenced tsconfig not found: ${tsconfigPath}`);
|
|
2101
2204
|
return {};
|
|
2102
2205
|
}
|
|
@@ -2104,14 +2207,14 @@ function getPathAliasesRecursive(tsconfigPath, visited = /* @__PURE__ */ new Set
|
|
|
2104
2207
|
const tsConfig = getTsconfigInfo(void 0, tsconfigPath);
|
|
2105
2208
|
const { paths = {}, baseUrl = "." } = tsConfig.compilerOptions || {};
|
|
2106
2209
|
const result = {};
|
|
2107
|
-
const configDir =
|
|
2210
|
+
const configDir = path3.dirname(tsconfigPath);
|
|
2108
2211
|
const obj = Object.entries(paths);
|
|
2109
2212
|
for (const [alias, aliasPaths] of obj) {
|
|
2110
2213
|
for (const aliasedPath of aliasPaths) {
|
|
2111
|
-
const resolvedBaseUrl =
|
|
2214
|
+
const resolvedBaseUrl = path3.resolve(configDir, baseUrl);
|
|
2112
2215
|
const finalAlias = alias.slice(-1) === "*" ? alias.slice(0, -1) : alias;
|
|
2113
2216
|
const finalAliasedPath = aliasedPath.slice(-1) === "*" ? aliasedPath.slice(0, -1) : aliasedPath;
|
|
2114
|
-
result[finalAlias || ""] =
|
|
2217
|
+
result[finalAlias || ""] = path3.join(resolvedBaseUrl, finalAliasedPath);
|
|
2115
2218
|
}
|
|
2116
2219
|
}
|
|
2117
2220
|
if (tsConfig.references) {
|
|
@@ -2132,8 +2235,8 @@ function getPathAliasesRecursive(tsconfigPath, visited = /* @__PURE__ */ new Set
|
|
|
2132
2235
|
}
|
|
2133
2236
|
}
|
|
2134
2237
|
function getPathAliases(cwd) {
|
|
2135
|
-
const tsConfigPath =
|
|
2136
|
-
if (!
|
|
2238
|
+
const tsConfigPath = path3.join(cwd, "tsconfig.json");
|
|
2239
|
+
if (!fs3.existsSync(tsConfigPath)) {
|
|
2137
2240
|
return null;
|
|
2138
2241
|
}
|
|
2139
2242
|
try {
|
|
@@ -2146,7 +2249,7 @@ function getPathAliases(cwd) {
|
|
|
2146
2249
|
}
|
|
2147
2250
|
function findCorsairConfigPath(cwd) {
|
|
2148
2251
|
for (const possiblePath of possiblePaths) {
|
|
2149
|
-
const fullPath =
|
|
2252
|
+
const fullPath = path3.join(cwd, possiblePath);
|
|
2150
2253
|
if (existsSync(fullPath)) {
|
|
2151
2254
|
return fullPath;
|
|
2152
2255
|
}
|
|
@@ -2161,7 +2264,7 @@ async function getCorsairInstance({
|
|
|
2161
2264
|
try {
|
|
2162
2265
|
let corsairInstance = null;
|
|
2163
2266
|
if (configPath) {
|
|
2164
|
-
let resolvedPath =
|
|
2267
|
+
let resolvedPath = path3.join(cwd, configPath);
|
|
2165
2268
|
if (existsSync(configPath)) {
|
|
2166
2269
|
resolvedPath = configPath;
|
|
2167
2270
|
}
|
|
@@ -2312,8 +2415,8 @@ function resolveClient(instance, tenant) {
|
|
|
2312
2415
|
}
|
|
2313
2416
|
return obj;
|
|
2314
2417
|
}
|
|
2315
|
-
function navigateToEndpoint(client,
|
|
2316
|
-
const parts =
|
|
2418
|
+
function navigateToEndpoint(client, path4) {
|
|
2419
|
+
const parts = path4.split(".");
|
|
2317
2420
|
let current = client;
|
|
2318
2421
|
for (const part of parts) {
|
|
2319
2422
|
if (current === null || typeof current !== "object") return void 0;
|
|
@@ -2347,20 +2450,26 @@ function parseAuthArgs(args) {
|
|
|
2347
2450
|
let code;
|
|
2348
2451
|
let credentials = false;
|
|
2349
2452
|
let webhook = false;
|
|
2350
|
-
let
|
|
2453
|
+
let listen = false;
|
|
2454
|
+
let collect = false;
|
|
2455
|
+
let sessionId;
|
|
2351
2456
|
for (const arg of args) {
|
|
2352
2457
|
if (arg === "--credentials") {
|
|
2353
2458
|
credentials = true;
|
|
2354
2459
|
continue;
|
|
2355
2460
|
}
|
|
2356
|
-
if (arg === "--agent") {
|
|
2357
|
-
agent = true;
|
|
2358
|
-
continue;
|
|
2359
|
-
}
|
|
2360
2461
|
if (arg === "--webhook") {
|
|
2361
2462
|
webhook = true;
|
|
2362
2463
|
continue;
|
|
2363
2464
|
}
|
|
2465
|
+
if (arg === "--listen") {
|
|
2466
|
+
listen = true;
|
|
2467
|
+
continue;
|
|
2468
|
+
}
|
|
2469
|
+
if (arg === "--collect") {
|
|
2470
|
+
collect = true;
|
|
2471
|
+
continue;
|
|
2472
|
+
}
|
|
2364
2473
|
const eqIdx = arg.indexOf("=");
|
|
2365
2474
|
if (arg.startsWith("--") && eqIdx !== -1) {
|
|
2366
2475
|
const key = arg.slice(2, eqIdx);
|
|
@@ -2377,9 +2486,22 @@ function parseAuthArgs(args) {
|
|
|
2377
2486
|
code = value;
|
|
2378
2487
|
continue;
|
|
2379
2488
|
}
|
|
2489
|
+
if (key === "session") {
|
|
2490
|
+
sessionId = value;
|
|
2491
|
+
continue;
|
|
2492
|
+
}
|
|
2380
2493
|
}
|
|
2381
2494
|
}
|
|
2382
|
-
return {
|
|
2495
|
+
return {
|
|
2496
|
+
pluginId,
|
|
2497
|
+
tenantId,
|
|
2498
|
+
code,
|
|
2499
|
+
credentials,
|
|
2500
|
+
webhook,
|
|
2501
|
+
listen,
|
|
2502
|
+
collect,
|
|
2503
|
+
sessionId
|
|
2504
|
+
};
|
|
2383
2505
|
}
|
|
2384
2506
|
function parseSetupArgs(args) {
|
|
2385
2507
|
let backfill = false;
|
|
@@ -2482,7 +2604,6 @@ function printHelp() {
|
|
|
2482
2604
|
"pnpm corsair setup --plugin=<id> <field>=VALUE Set plugin credentials",
|
|
2483
2605
|
"pnpm corsair auth --plugin=<id> Start OAuth flow",
|
|
2484
2606
|
"pnpm corsair auth --plugin=<id> --code=<code> Exchange OAuth code for tokens",
|
|
2485
|
-
"pnpm corsair auth --plugin=<id> --agent Run this if you are an agent (specific instructions to handle pending process)",
|
|
2486
2607
|
"pnpm corsair auth --plugin=<id> --credentials Show credential status",
|
|
2487
2608
|
"pnpm corsair auth --plugin=<id> --webhook Set up webhook subscription",
|
|
2488
2609
|
" `pnpm corsair list --type=webhooks` to see webhook plugins",
|
|
@@ -2584,29 +2705,17 @@ async function main() {
|
|
|
2584
2705
|
if (command === "list") {
|
|
2585
2706
|
const { plugin, type } = parseListArgs(args.slice(1));
|
|
2586
2707
|
const instance = await getCorsairInstance({ cwd });
|
|
2587
|
-
const
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
);
|
|
2592
|
-
process.exit(1);
|
|
2593
|
-
}
|
|
2594
|
-
const result = corsair.list_operations({ plugin, type });
|
|
2708
|
+
const result = listOperations(instance, {
|
|
2709
|
+
plugin,
|
|
2710
|
+
type
|
|
2711
|
+
});
|
|
2595
2712
|
if (type === "db") {
|
|
2596
2713
|
console.log(
|
|
2597
2714
|
"[#corsair]: NOTE: Every DB query listed here has both .search() and .list() methods available."
|
|
2598
2715
|
);
|
|
2599
2716
|
console.log("");
|
|
2600
2717
|
}
|
|
2601
|
-
|
|
2602
|
-
console.log(result);
|
|
2603
|
-
} else if (Array.isArray(result)) {
|
|
2604
|
-
console.log(result.join("\n"));
|
|
2605
|
-
} else if (result && typeof result === "object") {
|
|
2606
|
-
const grouped = result;
|
|
2607
|
-
const all = Object.values(grouped).flat();
|
|
2608
|
-
console.log(all.join("\n"));
|
|
2609
|
-
}
|
|
2718
|
+
console.log(result);
|
|
2610
2719
|
return;
|
|
2611
2720
|
}
|
|
2612
2721
|
if (SHOW_RUN && command === "run") {
|
|
@@ -2681,7 +2790,7 @@ async function main() {
|
|
|
2681
2790
|
try {
|
|
2682
2791
|
const { createRequire } = await import("module");
|
|
2683
2792
|
const { pathToFileURL } = await import("url");
|
|
2684
|
-
const req = createRequire(
|
|
2793
|
+
const req = createRequire(path3.join(cwd, "package.json"));
|
|
2685
2794
|
const resolvedPath = req.resolve("@corsair-dev/studio/server");
|
|
2686
2795
|
const mod = await import(pathToFileURL(resolvedPath).href);
|
|
2687
2796
|
const candidate = mod.start ?? mod.startStudio;
|
|
@@ -2719,14 +2828,7 @@ async function main() {
|
|
|
2719
2828
|
process.exit(1);
|
|
2720
2829
|
}
|
|
2721
2830
|
const instance = await getCorsairInstance({ cwd });
|
|
2722
|
-
const
|
|
2723
|
-
if (typeof corsair.get_schema !== "function") {
|
|
2724
|
-
console.error(
|
|
2725
|
-
"[#corsair]: get_schema not available on this Corsair instance."
|
|
2726
|
-
);
|
|
2727
|
-
process.exit(1);
|
|
2728
|
-
}
|
|
2729
|
-
const result = corsair.get_schema(schemaPath);
|
|
2831
|
+
const result = getSchema(instance, schemaPath);
|
|
2730
2832
|
console.log(result);
|
|
2731
2833
|
return;
|
|
2732
2834
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@corsair-dev/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.14",
|
|
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.65"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/jest": "^29.5.14",
|