Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

17.4 KB
Raw Blame
import { BRANDING_NAME } from '@lobechat/business-const';
import type { AuthorizationServerMetadata } from '@modelcontextprotocol/client';
import { registerClient, RegistrationRejectedError } from '@modelcontextprotocol/client';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import {
  buildConnectorClientMetadata,
  discoverConnectorOAuth,
  exchangeConnectorCode,
  refreshConnectorToken,
  registerDynamicClient,
  registeredAuthMethod,
  resolveConnectorClient,
  resolveConnectorOAuthScopes,
  selectRegistrationAuthMethod,
  toClientInformation,
} from './oauth';

const env = vi.hoisted(() => ({ appEnv: { APP_URL: 'https://app.example.com' } }));
vi.mock('@/envs/app', () => env);
vi.mock('@modelcontextprotocol/client', async (importOriginal) => ({
  ...(await importOriginal<object>()),
  registerClient: vi.fn(),
}));
// These tests stub the global fetch; the SSRF boundary itself is covered by
// oauth.network.test.ts over real sockets.
vi.mock('@lobechat/ssrf-safe-fetch', () => ({
  ssrfSafeFetch: (url: string, init?: RequestInit) => fetch(url, init),
}));

const metadataWith = (
  token_endpoint_auth_methods_supported?: string[],
): AuthorizationServerMetadata =>
  ({
    authorization_endpoint: 'https://auth.example.com/oauth/authorize',
    issuer: 'https://auth.example.com',
    registration_endpoint: 'https://auth.example.com/oauth/register',
    response_types_supported: ['code'],
    token_endpoint: 'https://auth.example.com/oauth/token',
    token_endpoint_auth_methods_supported,
  }) as AuthorizationServerMetadata;

describe('selectRegistrationAuthMethod', () => {
  it('keeps the confidential default when the server does not advertise methods', () => {
    expect(selectRegistrationAuthMethod(metadataWith())).toBe('client_secret_post');
    expect(selectRegistrationAuthMethod(metadataWith([]))).toBe('client_secret_post');
  });

  it('registers a public client when the server only accepts "none"', () => {
    expect(selectRegistrationAuthMethod(metadataWith(['none']))).toBe('none');
  });

  it('prefers client_secret_post, then client_secret_basic, over "none"', () => {
    expect(
      selectRegistrationAuthMethod(
        metadataWith(['none', 'client_secret_basic', 'client_secret_post']),
      ),
    ).toBe('client_secret_post');
    expect(selectRegistrationAuthMethod(metadataWith(['none', 'client_secret_basic']))).toBe(
      'client_secret_basic',
    );
  });

  it('falls back to a public client when no supported method is usable', () => {
    expect(selectRegistrationAuthMethod(metadataWith(['private_key_jwt']))).toBe('none');
  });
});

describe('registerDynamicClient', () => {
  beforeEach(() => {
    vi.mocked(registerClient).mockReset();
    vi.mocked(registerClient).mockResolvedValue({ client_id: 'issued', redirect_uris: [] });
  });

  it('requests the auth method the authorization server supports', async () => {
    // Regression: a hardcoded client_secret_post registration was rejected by
    // public-client-only servers with `invalid_client_metadata`.
    await registerDynamicClient({
      authorizationServerUrl: 'https://auth.example.com',
      metadata: metadataWith(['none']),
      redirectUri: 'https://app.example.com/oauth/connector/callback',
    });

    expect(registerClient).toHaveBeenCalledWith(
      'https://auth.example.com',
      expect.objectContaining({
        clientMetadata: expect.objectContaining({
          redirect_uris: ['https://app.example.com/oauth/connector/callback'],
          token_endpoint_auth_method: 'none',
        }),
      }),
    );
  });
});

/**
 * Token endpoint that, like Linear's, only accepts the auth method the client
 * was registered with (`client_secret_post`). Records how each request
 * authenticated.
 */
const stubPostOnlyTokenEndpoint = (errorStatus = 401) => {
  const attempts: string[] = [];
  vi.stubGlobal(
    'fetch',
    vi.fn(async (_url: string | URL, init?: RequestInit) => {
      const headers = new Headers(init?.headers);
      const body = new URLSearchParams(String(init?.body ?? ''));
      const method = headers.get('authorization')?.startsWith('Basic ')
        ? 'client_secret_basic'
        : body.has('client_secret')
          ? 'client_secret_post'
          : 'none';
      attempts.push(method);
      if (method !== 'client_secret_post') {
        return Response.json(
          { error: 'invalid_client', error_description: 'Client authentication failed' },
          { status: errorStatus },
        );
      }
      return Response.json({ access_token: 'tok', refresh_token: 'rt', token_type: 'Bearer' });
    }),
  );
  return attempts;
};

const exchange = (oidc: Parameters<typeof toClientInformation>[0], methods?: string[]) =>
  exchangeConnectorCode({
    authorizationCode: 'code',
    authorizationServerUrl: 'https://auth.example.com',
    clientInformation: toClientInformation(oidc),
    codeVerifier: 'verifier',
    metadata: metadataWith(methods),
    redirectUri: 'https://app.example.com/oauth/connector/callback',
  });

describe('client ID metadata document', () => {
  it('uses its own URL as the client_id and the connector callback as redirect URI', () => {
    expect(buildConnectorClientMetadata()).toEqual({
      client_id: 'https://app.example.com/oauth/connector/client-metadata.json',
      client_name: BRANDING_NAME,
      client_uri: 'https://app.example.com/',
      grant_types: ['authorization_code', 'refresh_token'],
      logo_uri: 'https://app.example.com/app-icons/icon-192x192.png',
      redirect_uris: ['https://app.example.com/oauth/connector/callback'],
      response_types: ['code'],
      token_endpoint_auth_method: 'none',
    });
  });
});

describe('resolveConnectorClient', () => {
  const METADATA_URL = 'https://app.example.com/oauth/connector/client-metadata.json';
  const cimdMetadata = {
    ...metadataWith(['none']),
    client_id_metadata_document_supported: true,
  } as AuthorizationServerMetadata;
  const resolve = (
    existing: Parameters<typeof resolveConnectorClient>[0]['existing'],
    metadata = cimdMetadata,
  ) =>
    resolveConnectorClient({
      authorizationServerUrl: 'https://auth.example.com',
      existing,
      metadata,
      redirectUri: 'https://app.example.com/oauth/connector/callback',
    });

  beforeEach(() => {
    env.appEnv.APP_URL = 'https://app.example.com';
    vi.mocked(registerClient).mockReset();
    vi.mocked(registerClient).mockResolvedValue({ client_id: 'issued', redirect_uris: [] });
  });

  it('reuses a pre-registered client even when the server supports CIMD', async () => {
    await expect(
      resolve({ clientId: 'mine', clientSecret: 's', scheme: 'pre_registration' }),
    ).resolves.toEqual({
      clientId: 'mine',
      clientSecret: 's',
      scheme: 'pre_registration',
      tokenEndpointAuthMethod: undefined,
    });
    expect(registerClient).not.toHaveBeenCalled();
  });

  it('keeps an earlier dynamic registration labelled as DCR', async () => {
    await expect(
      resolve({ clientId: 'issued-before', scheme: 'dcr', tokenEndpointAuthMethod: 'none' }),
    ).resolves.toMatchObject({ clientId: 'issued-before', scheme: 'dcr' });
    expect(registerClient).not.toHaveBeenCalled();
  });

  it('identifies with the metadata document URL as a public client', async () => {
    await expect(resolve({ scheme: 'dcr' })).resolves.toEqual({
      clientId: METADATA_URL,
      scheme: 'client_id_metadata_document',
      tokenEndpointAuthMethod: 'none',
    });
    expect(registerClient).not.toHaveBeenCalled();
  });

  it('re-derives a stored CIMD client_id from the current APP_URL', async () => {
    env.appEnv.APP_URL = 'https://lobe.example.org';
    await expect(
      resolve({ clientId: METADATA_URL, scheme: 'client_id_metadata_document' }),
    ).resolves.toMatchObject({
      clientId: 'https://lobe.example.org/oauth/connector/client-metadata.json',
    });
  });

  it('moves a stored CIMD client to DCR once the server stops supporting CIMD', async () => {
    await expect(
      resolve({ clientId: METADATA_URL, scheme: 'client_id_metadata_document' }, metadataWith()),
    ).resolves.toMatchObject({ clientId: 'issued', scheme: 'dcr' });
  });

  it.each(['http://localhost:3010', 'https://localhost', 'https://10.0.0.8', 'https://lobe.local'])(
    'falls back to DCR when the server cannot reach %s',
    async (appUrl) => {
      env.appEnv.APP_URL = appUrl;
      await expect(resolve({ scheme: 'dcr' })).resolves.toMatchObject({
        clientId: 'issued',
        scheme: 'dcr',
      });
    },
  );

  it('explains a server that supports neither CIMD nor DCR', async () => {
    const metadata = { ...metadataWith(), registration_endpoint: undefined };
    await expect(resolve({ scheme: 'dcr' }, metadata)).rejects.toThrow(
      'This server supports neither Client ID Metadata Documents nor dynamic registration. Provide the OAuth Client ID you registered with this server.',
    );
  });

  it('blames the private APP_URL when the server only supports CIMD', async () => {
    // Regression: a CIMD-only server was reported as supporting "neither"
    // when LobeHub itself could not offer a reachable metadata document.
    env.appEnv.APP_URL = 'http://localhost:3010';
    const metadata = { ...cimdMetadata, registration_endpoint: undefined };
    await expect(resolve({ scheme: 'dcr' }, metadata)).rejects.toThrow(
      'This server only accepts Client ID Metadata Documents, which need LobeHub on a public HTTPS APP_URL. Provide the OAuth Client ID you registered with this server.',
    );
  });

  it('prefixes a rejected dynamic registration', async () => {
    vi.mocked(registerClient).mockRejectedValueOnce(new Error('Use a public client'));
    await expect(resolve({ scheme: 'dcr' }, metadataWith())).rejects.toThrow(
      'Dynamic client registration failed: Use a public client',
    );
  });
});

describe('token endpoint client authentication', () => {
  afterEach(() => {
    vi.unstubAllGlobals();
  });

  it('uses the method recorded at registration instead of the SDK preference', async () => {
    // Regression: a DCR client registered as client_secret_post was sent with
    // HTTP Basic and rejected with `invalid_client: Client authentication failed`.
    const attempts = stubPostOnlyTokenEndpoint();

    const result = await exchange(
      { clientId: 'cid', clientSecret: 'secret', tokenEndpointAuthMethod: 'client_secret_post' },
      ['client_secret_basic', 'client_secret_post', 'none'],
    );

    expect(attempts).toEqual(['client_secret_post']);
    expect(result).toEqual({
      authMethod: undefined,
      tokens: expect.objectContaining({ access_token: 'tok' }),
    });
  });

  it('retries with the other secret method when none is recorded, and reports it', async () => {
    const attempts = stubPostOnlyTokenEndpoint();

    const result = await exchange({ clientId: 'cid', clientSecret: 'secret' }, [
      'client_secret_basic',
      'client_secret_post',
    ]);

    expect(attempts).toEqual(['client_secret_basic', 'client_secret_post']);
    expect(result.authMethod).toBe('client_secret_post');
  });

  it('does not retry when the recorded method is rejected', async () => {
    const attempts = stubPostOnlyTokenEndpoint();

    await expect(
      exchange(
        { clientId: 'cid', clientSecret: 'secret', tokenEndpointAuthMethod: 'client_secret_basic' },
        ['client_secret_basic', 'client_secret_post'],
      ),
    ).rejects.toThrow('Client authentication failed');
    expect(attempts).toEqual(['client_secret_basic']);
  });

  it('does not retry a public client', async () => {
    const attempts = stubPostOnlyTokenEndpoint();

    await expect(exchange({ clientId: 'cid' }, ['none'])).rejects.toThrow();
    expect(attempts).toEqual(['none']);
  });

  it('applies the same fallback to token refresh', async () => {
    const attempts = stubPostOnlyTokenEndpoint();

    const result = await refreshConnectorToken({
      authorizationServerUrl: 'https://auth.example.com',
      clientInformation: toClientInformation({ clientId: 'cid', clientSecret: 'secret' }),
      metadata: metadataWith(['client_secret_basic', 'client_secret_post']),
      refreshToken: 'rt',
    });

    expect(attempts).toEqual(['client_secret_basic', 'client_secret_post']);
    expect(result.authMethod).toBe('client_secret_post');
  });
});

describe('registeredAuthMethod', () => {
  it('prefers the method the server echoed back', () => {
    expect(
      registeredAuthMethod(
        { client_id: 'c', redirect_uris: [], token_endpoint_auth_method: 'client_secret_basic' },
        metadataWith(['client_secret_post', 'client_secret_basic']),
      ),
    ).toBe('client_secret_basic');
  });

  it('falls back to the requested method when the response omits it', () => {
    expect(
      registeredAuthMethod({ client_id: 'c', redirect_uris: [] }, metadataWith(['none'])),
    ).toBe('none');
  });
});

describe('SDK v2 OAuth regressions', () => {
  afterEach(() => vi.unstubAllGlobals());

  it('retries invalid_client in an HTTP 200 OAuth error response', async () => {
    const attempts = stubPostOnlyTokenEndpoint(200);
    const result = await exchange({ clientId: 'cid', clientSecret: 'secret' }, [
      'client_secret_basic',
      'client_secret_post',
    ]);
    expect(attempts).toEqual(['client_secret_basic', 'client_secret_post']);
    expect(result.tokens.access_token).toBe('tok');
  });

  it('passes the authorization response issuer and rejects its absence when required', async () => {
    stubPostOnlyTokenEndpoint();
    const params = {
      authorizationCode: 'code',
      authorizationServerUrl: 'https://auth.example.com',
      clientInformation: toClientInformation({
        clientId: 'cid',
        clientSecret: 'secret',
        tokenEndpointAuthMethod: 'client_secret_post',
      }),
      codeVerifier: 'verifier',
      metadata: { ...metadataWith(), authorization_response_iss_parameter_supported: true },
      redirectUri: 'https://app.example.com/oauth/connector/callback',
    };
    await expect(
      exchangeConnectorCode({ ...params, iss: 'https://auth.example.com' }),
    ).resolves.toMatchObject({ tokens: { access_token: 'tok' } });
    await expect(exchangeConnectorCode(params)).rejects.toThrow('Issuer mismatch');
  });

  it('discovers a root issuer with a trailing slash when PRM is absent', async () => {
    vi.stubGlobal(
      'fetch',
      vi.fn(async (url: string | URL) => {
        if (String(url).includes('oauth-authorization-server'))
          return Response.json({ ...metadataWith(), issuer: 'https://auth.example.com/' });
        return new Response(null, { status: 404 });
      }),
    );
    await expect(discoverConnectorOAuth('https://auth.example.com/mcp')).resolves.toMatchObject({
      authorizationServerUrl: 'https://auth.example.com/',
      metadata: { issuer: 'https://auth.example.com/' },
    });
  });

  it('keeps the authorization server registration error_description', async () => {
    vi.mocked(registerClient).mockRejectedValueOnce(
      new RegistrationRejectedError({
        status: 400,
        body: '{"error":"invalid_client_metadata","error_description":"Use a public client"}',
        submittedMetadata: { redirect_uris: [] },
      }),
    );
    await expect(
      registerDynamicClient({
        authorizationServerUrl: 'https://auth.example.com',
        metadata: metadataWith(),
        redirectUri: 'https://app.example.com/callback',
      }),
    ).rejects.toThrow('Use a public client');
  });
});

describe('OAuth metadata issuer validation', () => {
  afterEach(() => vi.unstubAllGlobals());
  it('explains a mismatched discovery issuer without exposing the received value', async () => {
    vi.stubGlobal(
      'fetch',
      vi.fn(async (url: string | URL) => {
        if (String(url).includes('oauth-authorization-server'))
          return Response.json({ ...metadataWith(), issuer: 'https://untrusted.example/' });
        return new Response(null, { status: 404 });
      }),
    );
    await expect(discoverConnectorOAuth('https://auth.example.com/mcp')).rejects.toThrow(
      'Check the connector authorization server configuration.',
    );
  });
});

describe('identity scopes are opt-in on initial automatic client registration', () => {
  const discovery = {
    authorizationServerUrl: 'https://as',
    metadata: { ...metadataWith(), scopes_supported: ['tools', 'openid', 'profile', 'email'] },
    scopes: ['tools'],
  };
  it('adds only advertised identity scopes to nonempty initial scopes', () => {
    expect(resolveConnectorOAuthScopes({ discovery })).toEqual([
      'tools',
      'openid',
      'profile',
      'email',
    ]);
    expect(
      resolveConnectorOAuthScopes({
        discovery: {
          ...discovery,
          metadata: { ...discovery.metadata, scopes_supported: ['tools', 'openid'] },
        },
      }),
    ).toEqual(['tools', 'openid']);
  });
  it('preserves nonempty frozen scopes, pre-registered clients and empty resources', () => {
    expect(resolveConnectorOAuthScopes({ discovery, existingScopes: ['tools'] })).toEqual([
      'tools',
    ]);

    expect(resolveConnectorOAuthScopes({ discovery, preRegistered: true })).toEqual(['tools']);
    expect(
      resolveConnectorOAuthScopes({ discovery: { ...discovery, scopes: [] } }),
    ).toBeUndefined();
  });
  it('falls back to discovery when Marketplace supplies an empty stored scope array', () => {
    expect(resolveConnectorOAuthScopes({ discovery, existingScopes: [] })).toEqual([
      'tools',
      'openid',
      'profile',
      'email',
    ]);
    expect(resolveConnectorOAuthScopes({ discovery, existingScopes: undefined })).toEqual([
      'tools',
      'openid',
      'profile',
      'email',
    ]);
  });
});