Bearer-auth JSON-RPC exposes engines, generate, and job tools without duplicating the aigen API path. Co-authored-by: Cursor <cursoragent@cursor.com>
50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import { createHash, timingSafeEqual } from 'node:crypto'
|
|
import type { H3Event } from 'h3'
|
|
import { passwordAuthEnabled, privateInstanceId } from '~/server/utils/localAuth'
|
|
|
|
function configuredApiKey() {
|
|
const config = useRuntimeConfig()
|
|
return String((config as { aigenApiKey?: string }).aigenApiKey || process.env.AIGEN_API_KEY || '').trim()
|
|
}
|
|
|
|
function safeEqual(left: string, right: string) {
|
|
const a = Buffer.from(left)
|
|
const b = Buffer.from(right)
|
|
if (a.length !== b.length) return false
|
|
try {
|
|
return timingSafeEqual(a, b)
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
/** Owner key for bearer calls — same catalog as a no-session / password instance. */
|
|
export function aigenInstanceOwnerKey() {
|
|
if (passwordAuthEnabled()) return privateInstanceId()
|
|
return createHash('sha256').update('local').digest('hex').slice(0, 24)
|
|
}
|
|
|
|
export function aigenApiKeyConfigured() {
|
|
return Boolean(configuredApiKey())
|
|
}
|
|
|
|
export function aigenBearerAuthorized(event: H3Event) {
|
|
const expected = configuredApiKey()
|
|
if (!expected) return false
|
|
const header = String(getHeader(event, 'authorization') || '')
|
|
const match = header.match(/^Bearer\s+(.+)$/i)
|
|
if (!match) return false
|
|
return safeEqual(match[1].trim(), expected)
|
|
}
|
|
|
|
export function aigenApiPathAllowed(path: string, method: string) {
|
|
if (path === '/mcp') return true
|
|
if (path === '/api/aigen' || path.startsWith('/api/aigen/')) return true
|
|
if (method === 'GET' && (path === '/api/library/stills' || path.startsWith('/api/library/stills/'))) return true
|
|
return false
|
|
}
|
|
|
|
export function assertAigenFeature() {
|
|
/* AIGen API is always available on the single studio deployment. */
|
|
}
|