Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

10.3 KB
Raw Blame
/** The API-origin half of sign-in: `/auth/start`, `/auth/callback/<provider>`, `/auth/jwks.json`, and the handoff to the app origin. */
import { createHash, randomBytes } from 'node:crypto'
import type { Hono } from 'hono'
import { safeEqual, sha256hex } from '../auth'
import type { EnvRow } from '../db'
import { now } from '../db'
import type { Ctx } from '../ctx'
import { readCookie, setCookieValue } from '../http/cookies'
import { escapeHtml } from '../http/html'
import { recordLogs } from '../logsink'
import { OAUTH_PROVIDERS, type OAuthProfile } from '../oauth-providers'
import {
  authCookiePath,
  AUTH_PATH,
  HANDOFF_TTL_S,
  OAUTH_TIMEOUT_MS,
  STATE_TTL_S,
  apiSecure,
  oauthClientFor,
  redirectUri,
  verifierCookieName,
  withTimeout,
} from './config'
import { jwksResponse } from './identity'
import { mirrorUser } from './mirror'
import { htmlPage, noAccessPage, redirect } from './pages'
import { safeNext } from './paths'
import { revokeSessions, upsertUser } from './sessions'
import { verifiedDomainOf } from '../services/domains'
import { signState, verifyState } from './state'

/**
 * End-user sign-in for apps that declare `[auth]` (PRD 3.8, case 1): platform-hosted OAuth, a user
 * pool per app env, server-side sessions, and identity headers the app proxy injects.
 *
 * Every app env has its own origin (origins.ts); the OAuth app's one registered callback lives on
 * the API origin. A sign-in therefore crosses origins:
 *
 *   app origin  /.boxlite/auth/login?next=     → login nonce cookie `bxl_login`, 302 API /auth/start
 *                                                 (provider picked first if several)
 *   API origin  /auth/start                     → PKCE verifier cookie (Path=/auth/), 302 to the provider
 *   API origin  /auth/callback/<provider>       → state + PKCE checked, user upserted and mirrored,
 *                                                 one-time handoff code (60 s, hashed), 302 to the app origin
 *   app origin  /.boxlite/auth/complete?code=   → code + login nonce checked server-side, host-only
 *                                                 `bxl_session`, 302 next
 *   app origin  /.boxlite/auth/logout|me        → as before
 *   both        /auth/jwks.json, /.boxlite/auth/jwks.json
 */

/** Mounted on the API origin (server.ts keeps them off app and console origins). */
export function mountEndUserAuth(root: Hono, ctx: Ctx): void {
  root.get('/auth/jwks.json', () => jwksResponse(ctx))
  root.get('/auth/start', async (c) => await start(ctx, c.req.raw))
  root.get('/auth/callback/:provider', async (c) => await callback(ctx, c.req.param('provider'), c.req.raw))
  // Apple answers with a form POST (response_mode=form_post); the fields are those of the GET.
  root.post('/auth/callback/:provider', async (c) => {
    const url = new URL(c.req.url)
    const form = await c.req.raw.formData().catch(() => null)
    for (const k of ['code', 'state', 'error']) {
      const v = form?.get(k)
      if (typeof v === 'string') url.searchParams.set(k, v)
    }
    return await callback(ctx, c.req.param('provider'), new Request(url, { headers: c.req.raw.headers }))
  })
}

/** Every sign-in outcome lands in the env's `auth` log so `app logs --source auth` explains what users saw. */
export async function authLog(ctx: Ctx, env: EnvRow, level: 'info' | 'warn' | 'error', line: string): Promise<void> {
  await recordLogs(ctx, env.id, [{ ts: new Date().toISOString(), source: 'auth', level, line }])
}

/** API origin: sets the PKCE verifier next to the callback and sends the browser to the provider. */
async function start(ctx: Ctx, req: Request): Promise<Response> {
  const q = new URL(req.url).searchParams
  if (q.get('organization') !== ctx.store.organizationId) return htmlPage('Not found', '<h1>Not found</h1>', 404)
  const app = await ctx.store.appByName(q.get('app') ?? '')
  const env = app ? await ctx.store.envByName(app.id, q.get('env') || 'main') : undefined
  if (!app || !env?.public_url) return htmlPage('Not found', '<h1>There is no site here</h1>', 404)
  const auth = (await ctx.resources.currentSpec(env))?.auth
  const providerName = q.get('provider') ?? ''
  const provider = OAUTH_PROVIDERS[providerName]
  const creds = await oauthClientFor(ctx, app, providerName)
  if (!auth || !provider || !creds || !(auth.providers as string[]).includes(providerName)) {
    return htmlPage('Sign-in unavailable', '<h1>That sign-in method is not available on this site</h1>', 400)
  }
  const next = safeNext(q.get('next'))
  if (next === null)
    return htmlPage(
      'Bad sign-in link',
      '<h1>This sign-in link is not valid</h1><p>Go back to the site and sign in again.</p>',
      400,
    )
  const bh = q.get('bh') ?? ''
  if (!/^[0-9a-f]{64}$/.test(bh)) return htmlPage('Bad sign-in link', '<h1>Start the sign-in from the site</h1>', 400)
  const verifier = randomBytes(32).toString('base64url')
  const asked = q.get('host')
  const host = asked ? await verifiedDomainOf(ctx, env, asked) : null
  if (asked && !host) return htmlPage('Bad sign-in link', '<h1>Start the sign-in from the site</h1>', 400)
  const state = signState(ctx.config.sessionSecret, {
    organization: app.org_id,
    app: app.name,
    env: env.name,
    next,
    provider: providerName,
    vh: sha256hex(verifier),
    bh,
    ...(host ? { host } : {}),
  })
  const location = provider.authorizeUrl({
    creds,
    redirectUri: redirectUri(ctx, providerName),
    state,
    codeChallenge: createHash('sha256').update(verifier).digest('base64url'),
  })
  return redirect(
    location,
    new Headers({
      'set-cookie': setCookieValue(
        apiSecure(ctx),
        verifierCookieName(env.id),
        verifier,
        authCookiePath(ctx),
        STATE_TTL_S,
        provider.formPost,
      ),
    }),
  )
}

async function callback(ctx: Ctx, providerName: string, req: Request): Promise<Response> {
  const url = new URL(req.url)
  const provider = OAUTH_PROVIDERS[providerName]
  const st = verifyState(ctx.config.sessionSecret, url.searchParams.get('state'))
  if (!provider || !st || st.provider !== providerName) {
    return htmlPage(
      'Sign-in expired',
      '<h1>This sign-in link has expired</h1><p>Go back to the site and sign in again.</p>',
      400,
    )
  }
  if (st.organization !== ctx.store.organizationId) return htmlPage('Not found', '<h1>Not found</h1>', 404)
  const app = await ctx.store.appByName(st.app)
  const env = app ? await ctx.store.envByName(app.id, st.env) : undefined
  if (!app || !env?.public_url) return htmlPage('Not found', '<h1>This site is not available</h1>', 404)
  // Back to the custom domain the sign-in began on, if it is still a verified domain of this env.
  const custom = st.host ? await verifiedDomainOf(ctx, env, st.host) : null
  const appUrl = custom ? `https://${custom}` : env.public_url
  const retry = `<p><a href="${escapeHtml(`${appUrl}/.boxlite/auth/login?next=${encodeURIComponent(st.next)}`)}">Try again</a></p>`
  const headers = new Headers({ 'cache-control': 'no-store' })
  const verifierName = verifierCookieName(env.id)
  const verifier = readCookie(req.headers, verifierName)
  headers.append(
    'set-cookie',
    setCookieValue(apiSecure(ctx), verifierName, '', authCookiePath(ctx), 0, provider?.formPost),
  )

  if (url.searchParams.get('error'))
    await authLog(ctx, env, 'warn', `${providerName} sign-in cancelled by the user (${url.searchParams.get('error')})`)
  if (url.searchParams.get('error'))
    return htmlPage('Sign-in cancelled', `<h1>Sign-in was cancelled</h1>${retry}`, 400, headers)
  // The verifier cookie binds the callback to the browser that started the sign-in.
  if (!verifier || !safeEqual(sha256hex(verifier), st.vh))
    await authLog(
      ctx,
      env,
      'warn',
      `${providerName} sign-in rejected: callback came from a browser that did not start it, or after 10 minutes`,
    )
  if (!verifier || !safeEqual(sha256hex(verifier), st.vh))
    return htmlPage(
      'Sign-in expired',
      `<h1>Start the sign-in again</h1><p>This browser did not start this sign-in, or it took longer than 10 minutes.</p>${retry}`,
      400,
      headers,
    )
  const creds = await oauthClientFor(ctx, app, providerName)
  const auth = (await ctx.resources.currentSpec(env))?.auth
  if (!creds || !(auth?.providers as string[] | undefined)?.includes(providerName))
    return htmlPage(
      'Sign-in unavailable',
      `<h1>${escapeHtml(provider.label)} sign-in is not available on this site</h1>`,
      400,
      headers,
    )
  const code = url.searchParams.get('code')
  if (!code)
    return htmlPage(
      'Sign-in failed',
      `<h1>Sign-in failed</h1><p>${escapeHtml(provider.label)} did not finish the sign-in.</p>${retry}`,
      400,
      headers,
    )

  let profile: OAuthProfile
  try {
    profile = await provider.signIn({
      creds,
      redirectUri: redirectUri(ctx, providerName),
      code,
      codeVerifier: verifier,
      fetch: withTimeout(ctx.oauthFetch ?? fetch, OAUTH_TIMEOUT_MS),
    })
  } catch (err) {
    console.error(`[auth] ${providerName} sign-in for ${app.name}/${env.name}: ${(err as Error).message}`)
    await authLog(ctx, env, 'error', `${providerName} sign-in failed: ${(err as Error).message}`)
    return htmlPage(
      'Sign-in failed',
      `<h1>${escapeHtml(provider.label)} sign-in failed</h1><p>Please try again. If it keeps failing, tell the site owner.</p>${retry}`,
      502,
      headers,
    )
  }
  const user = await upsertUser(ctx, app, env, providerName, profile)
  if (user.blocked) {
    await authLog(ctx, env, 'warn', `blocked user ${user.email} tried to sign in with ${providerName}`)
    await revokeSessions(ctx, user.id)
    return noAccessPage(headers)
  }
  await mirrorUser(ctx, env.id, user)
  await authLog(ctx, env, 'info', `${user.email} signed in with ${providerName} → ${st.next}`)
  // The session cookie must be set by the app origin itself (host-only), so hand over a one-time code.
  const handoff = randomBytes(32).toString('base64url')
  await ctx.store.run('DELETE FROM auth_handoffs WHERE expires_at < ?', now())
  await ctx.store.run(
    'INSERT INTO auth_handoffs (hash, env_id, user_id, next, browser_hash, created_at, expires_at) VALUES (?, ?, ?, ?, ?, ?, ?)',
    sha256hex(handoff),
    env.id,
    user.id,
    st.next,
    st.bh ?? '',
    now(),
    new Date(Date.now() + HANDOFF_TTL_S * 1000).toISOString(),
  )
  headers.set('referrer-policy', 'no-referrer')
  return redirect(`${appUrl}/.boxlite/auth/complete?code=${handoff}`, headers)
}