File size: 949 Bytes
94e1b2f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import type { Request } from 'express'
import { AuthenticationError } from './errors'
import { getAllowedManimcatApiKeys, hasManimcatApiKey } from './manimcat-auth'
export function extractBearerToken(authHeader: string | string[] | undefined): string {
if (!authHeader) return ''
if (typeof authHeader === 'string') {
return authHeader.replace(/^Bearer\s+/i, '')
}
if (Array.isArray(authHeader)) {
return authHeader[0]?.replace(/^Bearer\s+/i, '') || ''
}
return ''
}
export function requirePromptOverrideAuth(req: Pick<Request, 'headers'>): void {
const keys = getAllowedManimcatApiKeys()
if (keys.length === 0) {
throw new AuthenticationError('Prompt overrides require MANIMCAT_ROUTE_KEYS to be set.')
}
const token = extractBearerToken(req.headers?.authorization)
if (!token || !hasManimcatApiKey(token)) {
throw new AuthenticationError('Prompt overrides require a valid ManimCat API key token.')
}
}
|