53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { AdminAuthTokenRequestSchema } from '@/lib/agent-os/schemas'
|
|
import { issueAdminToken, isValidAdminKey } from '@/lib/agent-os/admin-auth'
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const body = await request.json()
|
|
const validation = AdminAuthTokenRequestSchema.safeParse(body)
|
|
|
|
if (!validation.success) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: 'Validation error',
|
|
details: validation.error.errors,
|
|
},
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const { adminKey, operator } = validation.data
|
|
|
|
if (!isValidAdminKey(adminKey)) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: 'Unauthorized',
|
|
details: ['Invalid admin key'],
|
|
},
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const token = issueAdminToken(operator || 'admin-console')
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
token: token.token,
|
|
operator: token.operator,
|
|
expiresAt: new Date(token.expiresAt).toISOString(),
|
|
})
|
|
} catch (error) {
|
|
console.error('[AdminAuth] Failed to issue token:', error)
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: 'Internal server error',
|
|
},
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|