Drop empty authorization_servers well-known ads, 404 discovery probes as JSON, and keep bearer-only /mcp auth. Co-authored-by: Cursor <cursoragent@cursor.com>
39 lines
1.4 KiB
TypeScript
39 lines
1.4 KiB
TypeScript
import { aigenApiPathAllowed, aigenBearerAuthorized } from '~/server/utils/aigenApiAuth'
|
|
import { applyMcpCors } from '~/server/utils/aigenMcp'
|
|
|
|
export default defineEventHandler((event) => {
|
|
if (!authEnabled()) return
|
|
const path = getRequestURL(event).pathname
|
|
if (
|
|
path.startsWith('/api/auth/login') ||
|
|
path.startsWith('/api/auth/callback') ||
|
|
path.startsWith('/api/auth/logout') ||
|
|
path.startsWith('/api/auth/me') ||
|
|
path.startsWith('/api/health') ||
|
|
path.startsWith('/api/monitor') ||
|
|
path.startsWith('/_nuxt') ||
|
|
path.startsWith('/favicon') ||
|
|
path === '/login' ||
|
|
// Claude MCP discovery — never 302 to /login (JSON 404 handlers serve these).
|
|
path === '/.well-known/oauth-authorization-server' ||
|
|
path === '/.well-known/openid-configuration'
|
|
) {
|
|
return
|
|
}
|
|
const method = String(event.method || getMethod(event) || 'GET').toUpperCase()
|
|
// /mcp auth + 401 JSON-RPC bodies are handled in the route — do not 401 here.
|
|
if (path === '/mcp') {
|
|
applyMcpCors(event)
|
|
return
|
|
}
|
|
if (aigenApiPathAllowed(path, method) && aigenBearerAuthorized(event)) {
|
|
return
|
|
}
|
|
const user = getSessionUser(event)
|
|
if (user) return
|
|
if (path.startsWith('/api/')) {
|
|
throw createError({ statusCode: 401, statusMessage: 'Authentication required' })
|
|
}
|
|
return sendRedirect(event, path === '/studio-2' ? '/login?next=/studio-2' : '/login', 302)
|
|
})
|