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.

7.0 KB
Raw Blame
import { BRANDING_NAME, BUSINESS_OIDC_CLIENTS } from '@lobechat/business-const';
import {
  ACCEPTANCE_CLIENT_ID,
  ACCEPTANCE_EXTENSION_CLIENT_ID,
  ACCEPTANCE_EXTENSION_IDS,
  ACCEPTANCE_EXTENSION_SCOPE,
  ACCEPTANCE_REVIEW_SCOPE,
  LOBEHUB_DESKTOP_CLIENT_ID,
  LOBOT_DESKTOP_CLIENT_ID,
  LOBOT_MOBILE_CLIENT_ID,
} from '@lobechat/const';
import { type ClientMetadata } from 'oidc-provider';
import urlJoin from 'url-join';

import { appEnv } from '@/envs/app';

const cloudAppOrigins = ['https://app.lobehub.com', 'https://lobehub.com'];
const appUrl = appEnv.APP_URL!;
const desktopAppOrigins = cloudAppOrigins.includes(new URL(appUrl).origin)
  ? cloudAppOrigins
  : [appUrl];
const marketBaseUrl = new URL(appEnv.MARKET_BASE_URL ?? 'https://market.lobehub.com').origin;

/**
 * A desktop app signs in through the shared `/oidc/callback/desktop` handoff:
 * the callback stores the code under the request's `state`, and the app polls
 * it back with its PKCE verifier. Each app still has its own client, so its
 * grants and tokens are told apart from the other desktop apps'.
 */
const desktopClient = (
  client: Pick<ClientMetadata, 'client_id' | 'client_name' | 'logo_uri'>,
): ClientMetadata => ({
  application_type: 'web',
  // Only supports authorization code flow
  grant_types: ['authorization_code', 'refresh_token'],

  post_logout_redirect_uris: [
    // Keep the legacy subdomain working while Cloud moves to the apex domain.
    ...desktopAppOrigins.map((origin) => urlJoin(origin, '/oauth/logout')),
    'http://localhost:3210/oauth/logout',
  ],

  // Desktop authorization callback - changed to web page path
  redirect_uris: [
    ...desktopAppOrigins.map((origin) => urlJoin(origin, '/oidc/callback/desktop')),
    'http://localhost:3210/oidc/callback/desktop',
  ],

  // Supports authorization code for obtaining tokens and refresh tokens
  response_types: ['code'],

  // Marked as public client with no secret
  token_endpoint_auth_method: 'none',

  ...client,
});

/**
 * Clients the open-source distribution registers itself.
 */
const builtinClients: ClientMetadata[] = [
  desktopClient({
    client_id: LOBEHUB_DESKTOP_CLIENT_ID,
    client_name: `${BRANDING_NAME} Desktop`,
    logo_uri: 'https://hub-apac-1.lobeobjects.space/lobehub-desktop-icon.png',
  }),
  desktopClient({
    client_id: LOBOT_DESKTOP_CLIENT_ID,
    client_name: 'Lobot',
    logo_uri: 'https://hub-apac-1.lobeobjects.space/lobot-desktop-icon.png',
  }),

  {
    application_type: 'native', // Mobile uses native type
    client_id: 'lobehub-mobile',
    client_name: `${BRANDING_NAME} Mobile`,
    // Supports authorization code flow and refresh token
    grant_types: ['authorization_code', 'refresh_token'],
    logo_uri: 'https://hub-apac-1.lobeobjects.space/docs/73f69adfa1b802a0e250f6ff9d62f70b.png',
    // Mobile does not need post_logout_redirect_uris as logout is typically handled within the app
    post_logout_redirect_uris: [],
    // Mobile uses custom URL Scheme
    redirect_uris: ['com.lobehub.app://auth/callback'],
    response_types: ['code'],
    // Public client with no secret
    token_endpoint_auth_method: 'none',
  },
  {
    application_type: 'native',
    client_id: LOBOT_MOBILE_CLIENT_ID,
    client_name: 'Lobot',
    grant_types: ['authorization_code', 'refresh_token'],
    logo_uri: 'https://hub-apac-1.lobeobjects.space/lobot-desktop-icon.png',
    post_logout_redirect_uris: [],
    // Universal-link callback: iOS only hands it to the app listed in the AASA webcredentials,
    // so another app cannot impersonate Lobot the way it could with a custom scheme.
    redirect_uris: desktopAppOrigins.map((origin) =>
      urlJoin(origin, `/oidc/callback/${LOBOT_MOBILE_CLIENT_ID}`),
    ),
    response_types: ['code'],
    token_endpoint_auth_method: 'none',
  },
  {
    application_type: 'native',
    client_id: 'lobehub-cli',
    client_name: 'LobeHub CLI',
    grant_types: ['urn:ietf:params:oauth:grant-type:device_code', 'refresh_token'],
    logo_uri: 'https://hub-apac-1.lobeobjects.space/lobehub-desktop-icon.png',
    response_types: [],
    token_endpoint_auth_method: 'none',
  },
  {
    application_type: 'web',
    client_id: ACCEPTANCE_CLIENT_ID,
    client_name: 'LobeHub Acceptance',
    grant_types: ['urn:ietf:params:oauth:grant-type:device_code', 'authorization_code'],
    scope: ACCEPTANCE_REVIEW_SCOPE,
    logo_uri: 'https://hub-apac-1.lobeobjects.space/lobehub-desktop-icon.png',
    redirect_uris: [urlJoin(appUrl, '/oidc/acceptance/callback')],
    response_types: ['code'],
    token_endpoint_auth_method: 'none',
  },
  {
    application_type: 'web',
    client_id: ACCEPTANCE_EXTENSION_CLIENT_ID,
    client_name: 'LobeHub Acceptance Extension',
    grant_types: ['authorization_code', 'refresh_token'],
    logo_uri: 'https://hub-apac-1.lobeobjects.space/lobehub-desktop-icon.png',
    post_logout_redirect_uris: [],
    redirect_uris: ACCEPTANCE_EXTENSION_IDS.map((id) => `https://${id}.chromiumapp.org/oidc`),
    response_types: ['code'],
    scope: `openid profile offline_access ${ACCEPTANCE_EXTENSION_SCOPE}`,
    token_endpoint_auth_method: 'none',
  },
  {
    application_type: 'web',
    client_id: 'lobehub-market',
    client_name: 'LobeHub Marketplace',
    grant_types: ['authorization_code', 'refresh_token'],
    logo_uri: 'https://hub-apac-1.lobeobjects.space/lobehub-desktop-icon.png',
    post_logout_redirect_uris: [
      urlJoin(marketBaseUrl!, '/lobehub-oidc/logout'),
      'http://localhost:8787/lobehub-oidc/logout',
    ],
    redirect_uris: [
      urlJoin(marketBaseUrl!, '/lobehub-oidc/consent/callback'),
      'http://localhost:8787/lobehub-oidc/consent/callback',
    ],
    response_types: ['code'],
    token_endpoint_auth_method: 'none',
  },
];

/**
 * Appends a distribution's statically registered clients to the built-in ones.
 *
 * A distribution client may not reuse a built-in `client_id`: oidc-provider
 * would silently keep one of the two, and a slot that replaced the desktop
 * client's redirect URIs or grant types would change who can sign in as it.
 */
export const mergeBusinessClients = (
  builtin: ClientMetadata[],
  business: readonly ClientMetadata[],
): ClientMetadata[] => {
  const seen = new Set(builtin.map((client) => client.client_id));

  for (const client of business) {
    if (seen.has(client.client_id)) {
      throw new Error(`OIDC client "${client.client_id}" is registered more than once`);
    }
    seen.add(client.client_id);
  }

  return [...builtin, ...business];
};

/**
 * Default OIDC client configuration
 */
export const defaultClients: ClientMetadata[] = mergeBusinessClients(
  builtinClients,
  BUSINESS_OIDC_CLIENTS as ClientMetadata[],
);

/**
 * OIDC Scopes definition
 */
export const defaultScopes = [
  'openid',
  'profile',
  'email',
  'offline_access', // Allows obtaining refresh_token
  ACCEPTANCE_REVIEW_SCOPE,
  ACCEPTANCE_EXTENSION_SCOPE,
];

/**
 * OIDC Claims definition
 */
export const defaultClaims = {
  email: ['email', 'email_verified'],
  openid: ['sub'],
  // subject (unique user identifier)
  profile: ['name', 'picture'],
};