mdedit-cli 0.4.10 → 0.4.11

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
@@ -1670,13 +1670,14 @@ async function discoverOAuthConfiguration() {
1670
1670
  const authorizationEndpoint = process.env.MDEDIT_OAUTH_AUTHORIZATION_ENDPOINT;
1671
1671
  const tokenEndpoint = process.env.MDEDIT_OAUTH_TOKEN_ENDPOINT;
1672
1672
  const revocationEndpoint = process.env.MDEDIT_OAUTH_REVOCATION_ENDPOINT;
1673
- if (clientId && authorizationEndpoint && tokenEndpoint && revocationEndpoint) {
1673
+ if (clientId && authorizationEndpoint && tokenEndpoint) {
1674
1674
  return {
1675
1675
  authorizationEndpoint,
1676
1676
  clientId,
1677
1677
  defaultScopes: (process.env.MDEDIT_OAUTH_SCOPES || "").split(",").map((scope) => scope.trim()).filter(Boolean),
1678
1678
  resource: process.env.MDEDIT_OAUTH_RESOURCE || "https://apiv2.mdedit.ai/api",
1679
1679
  revocationEndpoint,
1680
+ scopeFormat: process.env.MDEDIT_OAUTH_SCOPE_FORMAT === "bare" ? "bare" : "resource",
1680
1681
  tokenEndpoint
1681
1682
  };
1682
1683
  }
@@ -1685,22 +1686,34 @@ async function discoverOAuthConfiguration() {
1685
1686
  throw new Error(`OAuth discovery failed (${response.status})`);
1686
1687
  }
1687
1688
  const document = await response.json();
1688
- if (!document.clientId || !document.authorizationEndpoint || !document.tokenEndpoint || !document.revocationEndpoint || !document.resource || !Array.isArray(document.defaultScopes)) {
1689
+ if (!document.clientId || !document.authorizationEndpoint || !document.tokenEndpoint || !document.resource || !Array.isArray(document.defaultScopes)) {
1689
1690
  throw new Error("OAuth discovery response is incomplete");
1690
1691
  }
1691
1692
  return document;
1692
1693
  }
1693
- function credentialStore() {
1694
+ function credentialStore(discovery) {
1694
1695
  return new OsCredentialStore({
1695
1696
  loadProfileIndex: async () => profileIndex(),
1696
- saveProfileIndex: async (profiles) => setProfileIndex(profiles)
1697
+ saveProfileIndex: async (profiles) => setProfileIndex(profiles),
1698
+ // Keep existing Cognito credentials for rollback, while preventing a
1699
+ // previously valid Cognito token from being sent to Clerk or the API after
1700
+ // discovery changes providers.
1701
+ service: discovery.scopeFormat === "bare" ? "ai.mdedit.oauth.clerk" : "ai.mdedit.oauth"
1697
1702
  });
1698
1703
  }
1699
- function qualifyOAuthScopes(scopes, resource) {
1704
+ function qualifyOAuthScopes(scopes, resource, format = "resource") {
1700
1705
  const prefix = `${resource.replace(/\/+$/, "")}/`;
1706
+ const standardScopes = /* @__PURE__ */ new Set(["openid", "email", "profile", "offline_access"]);
1701
1707
  return [...new Set(scopes.map((scope) => {
1702
1708
  const normalized = scope.trim();
1703
1709
  if (!normalized) return "";
1710
+ if (standardScopes.has(normalized)) return normalized;
1711
+ if (format === "bare") {
1712
+ if (!/^[a-z][a-z0-9_/-]*:[a-z][a-z0-9_-]*$/.test(normalized)) {
1713
+ throw new Error(`Invalid OAuth scope: ${normalized}`);
1714
+ }
1715
+ return normalized;
1716
+ }
1704
1717
  if (normalized.startsWith(prefix)) return normalized;
1705
1718
  if (normalized.includes("://") || normalized.includes("/")) {
1706
1719
  throw new Error(`OAuth scope is for a different resource: ${normalized}`);
@@ -1717,7 +1730,7 @@ async function oauthClient(scopes) {
1717
1730
  redirectUri: FIXED_REDIRECT_URI,
1718
1731
  resource: discovery.resource,
1719
1732
  revocationEndpoint: discovery.revocationEndpoint,
1720
- scopes: qualifyOAuthScopes(selectedScopes, discovery.resource),
1733
+ scopes: qualifyOAuthScopes(selectedScopes, discovery.resource, discovery.scopeFormat),
1721
1734
  tokenEndpoint: discovery.tokenEndpoint
1722
1735
  };
1723
1736
  const cacheKey = JSON.stringify(configuration);
@@ -1725,7 +1738,7 @@ async function oauthClient(scopes) {
1725
1738
  if (existing) return existing;
1726
1739
  const client = new NodeOAuthClient({
1727
1740
  configuration,
1728
- credentialStore: credentialStore()
1741
+ credentialStore: credentialStore(discovery)
1729
1742
  });
1730
1743
  oauthClientsByConfiguration.set(cacheKey, client);
1731
1744
  return client;
@@ -1800,7 +1813,14 @@ async function logoutOAuth({
1800
1813
  async function storedOAuthAccessToken(profile, forceRefresh = false) {
1801
1814
  const normalizedProfile = activeProfile(profile);
1802
1815
  if (!profileIndex().includes(normalizedProfile)) return null;
1803
- return (await oauthClient()).getAccessToken(normalizedProfile, { forceRefresh });
1816
+ try {
1817
+ return await (await oauthClient()).getAccessToken(normalizedProfile, { forceRefresh });
1818
+ } catch (error) {
1819
+ if (error instanceof OAuthClientError && error.code === "OAUTH_LOGIN_REQUIRED") {
1820
+ return null;
1821
+ }
1822
+ throw error;
1823
+ }
1804
1824
  }
1805
1825
  var DEFAULT_PROFILE, FIXED_REDIRECT_URI, oauthClientsByConfiguration;
1806
1826
  var init_oauth = __esm({