Skip to content

armgit / Orkas

Public Imported from GitHub
0

Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.

fix(connectors): continue OAuth discovery past incomplete metadata

CloudTianTian77858e953f6b
diff --git a/src/main/features/connectors/oauth-dcr.ts b/src/main/features/connectors/oauth-dcr.ts
index a7118a47..9cc432f6 100644
--- a/src/main/features/connectors/oauth-dcr.ts
+++ b/src/main/features/connectors/oauth-dcr.ts
@@ -224,9 +224,28 @@ function _fetchDcr(label: string, url: string, init: RequestInit = {}): Promise<
);
}
+/** Some hosts answer unknown discovery paths with HTTP 200 and an error body (Zapier MCP returns a
+ * JSON-RPC "not found" object), so a successful status alone does not make a candidate usable. */
+function _hasAuthorizationServers(body: unknown): boolean {
+ const servers = (body as ProtectedResourceMetadata | null)?.authorization_servers;
+ return Array.isArray(servers) && servers.length > 0 && servers.every(server => typeof server === 'string' && !!server);
+}
+
+function _hasAuthorizationEndpoints(body: unknown): boolean {
+ const meta = body as Partial<AuthServerMetadata> | null;
+ return typeof meta?.authorization_endpoint === 'string' && !!meta.authorization_endpoint
+ && typeof meta.token_endpoint === 'string' && !!meta.token_endpoint;
+}
+
/** Fetch `<base>/.well-known/<name>`, falling back to root-level discovery if path-suffixed
- * fails (per MCP authorization spec — providers can serve discovery at either location). */
-async function _fetchWellKnown<T>(mcpUrl: string, wellKnownName: string, strict = false): Promise<T> {
+ * fails (per MCP authorization spec — providers can serve discovery at either location).
+ * A 200 whose body fails `accept` counts as a miss, so the next candidate is still tried. */
+async function _fetchWellKnown<T>(
+ mcpUrl: string,
+ wellKnownName: string,
+ strict = false,
+ accept: (body: unknown) => boolean = () => true,
+): Promise<T> {
const url = new URL(mcpUrl);
// Path-suffixed first (e.g. https://mcp.notion.com/mcp/.well-known/oauth-protected-resource),
// then root well-known with the MCP path appended (e.g. Atlassian's
@@ -240,7 +259,12 @@ async function _fetchWellKnown<T>(mcpUrl: string, wellKnownName: string, strict
for (const cand of Array.from(new Set(candidates))) {
try {
const r = await _fetchDcr(`DCR ${wellKnownName}`, cand, strict ? { redirect: 'error' } : {});
- if (r.ok) return await r.json() as T;
+ if (r.ok) {
+ const body: unknown = await r.json();
+ if (accept(body)) return body as T;
+ lastErr = new Error(`DCR ${wellKnownName} metadata missing required fields`);
+ continue;
+ }
lastErr = new Error(`DCR ${wellKnownName} metadata HTTP ${r.status}`);
} catch (err) {
lastErr = _fmtFetchErr(`DCR ${wellKnownName} metadata fetch failed`, err);
@@ -270,7 +294,16 @@ async function _fetchAuthServerMetadata(authServer: string, mcpUrl: string, stri
lastErr = _fmtFetchErr('DCR auth server metadata fetch failed', err);
continue;
}
- if (r.ok) return await r.json() as AuthServerMetadata;
+ if (r.ok) {
+ let body: unknown;
+ try { body = await r.json(); } catch (err) {
+ lastErr = _fmtFetchErr('DCR auth server metadata parse failed', err);
+ continue;
+ }
+ if (_hasAuthorizationEndpoints(body)) return body as AuthServerMetadata;
+ lastErr = new Error('auth server metadata missing authorization or token endpoint');
+ continue;
+ }
lastErr = new Error(`auth server metadata HTTP ${r.status}`);
}
throw lastErr || new Error('failed to fetch auth server metadata');
@@ -304,7 +337,9 @@ async function _fetchChallengedResourceMetadata(mcpUrl: string): Promise<Protect
async function _discoverAuthServer(mcpUrl: string, custom = false): Promise<{ meta: AuthServerMetadata; resource: string }> {
let prm: ProtectedResourceMetadata;
try {
- prm = await _fetchWellKnown<ProtectedResourceMetadata>(mcpUrl, 'oauth-protected-resource', custom);
+ prm = await _fetchWellKnown<ProtectedResourceMetadata>(
+ mcpUrl, 'oauth-protected-resource', custom, _hasAuthorizationServers,
+ );
} catch (error) {
if (!custom) throw error;
prm = await _fetchChallengedResourceMetadata(mcpUrl);
diff --git a/test/main/features/connectors/oauth-dcr.test.ts b/test/main/features/connectors/oauth-dcr.test.ts
index 1dd6a535..3eed0684 100644
--- a/test/main/features/connectors/oauth-dcr.test.ts
+++ b/test/main/features/connectors/oauth-dcr.test.ts
@@ -222,6 +222,64 @@ describe('features/connectors/oauth-dcr', () => {
.toBe(false);
});
+ it('skips a 200 discovery response that is not resource metadata and tries the next well-known path', async () => {
+ // Zapier MCP answers the path-suffixed probe with HTTP 200 and a JSON-RPC "not found" body.
+ const fetchMock = vi.fn(async (raw: string) => {
+ const url = String(raw);
+ if (url === 'https://mcp.example.com/api/mcp/mcp/.well-known/oauth-protected-resource') return jsonResponse({
+ jsonrpc: '2.0', id: null, error: { code: -32603, message: 'not found' },
+ });
+ if (url === 'https://mcp.example.com/.well-known/oauth-protected-resource/api/mcp/mcp') return jsonResponse({
+ resource: 'https://mcp.example.com/api/mcp/mcp', authorization_servers: ['https://mcp.example.com'],
+ });
+ if (url === 'https://mcp.example.com/.well-known/oauth-authorization-server') return jsonResponse({
+ issuer: 'https://mcp.example.com',
+ authorization_endpoint: 'https://mcp.example.com/oauth/authorize',
+ token_endpoint: 'https://mcp.example.com/oauth/token',
+ registration_endpoint: 'https://mcp.example.com/oauth/register',
+ });
+ if (url === 'https://mcp.example.com/oauth/register') return jsonResponse({ client_id: 'client-1' });
+ throw new Error(`unexpected request ${url}`);
+ });
+ vi.stubGlobal('fetch', fetchMock);
+ const oauth = await import('../../../../src/main/features/connectors/oauth-dcr');
+ const pending = oauth.startCustomMcpOAuth('custom-example', 'https://mcp.example.com/api/mcp/mcp');
+ const outcome = expect(pending).rejects.toThrow();
+ await vi.waitFor(() => expect(electronMock.openExternal).toHaveBeenCalledTimes(1));
+ expect(new URL(String(electronMock.openExternal.mock.calls[0][0])).searchParams.get('resource'))
+ .toBe('https://mcp.example.com/api/mcp/mcp');
+ await oauth.handleDcrCallbackUrl('orkas://connectors/oauth/dcr-callback?status=cancelled');
+ await outcome;
+ });
+
+ it('skips a 200 authorization-server document without endpoints and tries the next discovery URL', async () => {
+ const fetchMock = vi.fn(async (raw: string) => {
+ const url = String(raw);
+ if (url.includes('oauth-protected-resource')) return jsonResponse({
+ resource: 'https://mcp.example.com/mcp', authorization_servers: ['https://login.example.com'],
+ });
+ if (url === 'https://login.example.com/.well-known/oauth-authorization-server') return jsonResponse({
+ error: 'not_found',
+ });
+ if (url === 'https://login.example.com/.well-known/openid-configuration') return jsonResponse({
+ issuer: 'https://login.example.com',
+ authorization_endpoint: 'https://login.example.com/authorize',
+ token_endpoint: 'https://login.example.com/token',
+ registration_endpoint: 'https://login.example.com/register',
+ });
+ if (url === 'https://login.example.com/register') return jsonResponse({ client_id: 'client-1' });
+ throw new Error(`unexpected request ${url}`);
+ });
+ vi.stubGlobal('fetch', fetchMock);
+ const oauth = await import('../../../../src/main/features/connectors/oauth-dcr');
+ const pending = oauth.startCustomMcpOAuth('custom-example', 'https://mcp.example.com/mcp');
+ const outcome = expect(pending).rejects.toThrow();
+ await vi.waitFor(() => expect(electronMock.openExternal).toHaveBeenCalledTimes(1));
+ expect(new URL(String(electronMock.openExternal.mock.calls[0][0])).origin).toBe('https://login.example.com');
+ await oauth.handleDcrCallbackUrl('orkas://connectors/oauth/dcr-callback?status=cancelled');
+ await outcome;
+ });
+
it('rejects root metadata for a different tenant when custom issuer discovery falls back', async () => {
const fetchMock = vi.fn(async (raw: string) => {
const url = String(raw);