Second deep audit→build→verify pass; all products typecheck clean. - gitgram-web (100): fix feed pagination, reel drafts, 6 gaps - gitgram-backend (99): add admin/moderator role checks to moderation routes; wire backend-e2e CI (Postgres+Redis services); 5 gaps - gitgram-mobile (97): fix broken create-reel flow; 11 gaps - aitm-core (98): dedupe vendored CLI command registrations; real vector search with local fallback; 7 gaps - aitm-api (99): replace hash-based embeddings with real OpenAI/Voyage embeddings (+ checkpoints test_status migration 0008); 8 gaps - aitm-mcp (99): add X-API-Key to workspace tool proxy calls; 8 gaps - aitm-website (94): fix annual billing checkout; 5 gaps Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
76 lines
2.2 KiB
TypeScript
76 lines
2.2 KiB
TypeScript
import { cookies } from 'next/headers'
|
|
import { NextResponse } from 'next/server'
|
|
|
|
/**
|
|
* Server-side AITM credential helpers.
|
|
*
|
|
* The AITM bearer token is stored in an httpOnly cookie so it is never readable
|
|
* by client JavaScript (mitigating XSS exfiltration). All AITM read calls are
|
|
* proxied through Next.js route handlers that attach this token server-side.
|
|
*/
|
|
|
|
export const AITM_COOKIE = 'gitgram_aitm_token'
|
|
export const AITM_BASE_URL =
|
|
process.env.AITM_API_URL ||
|
|
process.env.NEXT_PUBLIC_AITM_API_URL ||
|
|
'https://timemachine-api.gitgram.app/v1'
|
|
|
|
const COOKIE_MAX_AGE = 60 * 60 * 24 * 30 // 30 days
|
|
|
|
/** Read the stored AITM token (server-only). */
|
|
export function readAitmToken(): string | null {
|
|
return cookies().get(AITM_COOKIE)?.value ?? null
|
|
}
|
|
|
|
/** Build the Set-Cookie options for the httpOnly AITM credential. */
|
|
export function aitmCookieOptions(maxAge: number = COOKIE_MAX_AGE) {
|
|
return {
|
|
name: AITM_COOKIE,
|
|
httpOnly: true as const,
|
|
secure: process.env.NODE_ENV === 'production',
|
|
sameSite: 'strict' as const,
|
|
path: '/',
|
|
maxAge,
|
|
}
|
|
}
|
|
|
|
/** Standard JSON error envelope. */
|
|
export function errorResponse(message: string, status: number): NextResponse {
|
|
return NextResponse.json({ error: message }, { status })
|
|
}
|
|
|
|
/**
|
|
* Perform a GET against the AITM API with the stored token.
|
|
* Returns a NextResponse ready to send to the client.
|
|
*/
|
|
export async function proxyAitmGet(path: string): Promise<NextResponse> {
|
|
const token = readAitmToken()
|
|
if (!token) {
|
|
return errorResponse('AITM is not connected', 401)
|
|
}
|
|
|
|
let res: Response
|
|
try {
|
|
res = await fetch(`${AITM_BASE_URL}${path}`, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
// AITM requests are not cached — checkpoint/diff data changes frequently.
|
|
cache: 'no-store',
|
|
})
|
|
} catch {
|
|
return errorResponse('Could not reach the AITM service', 502)
|
|
}
|
|
|
|
const data = await res.json().catch(() => ({}))
|
|
if (!res.ok) {
|
|
const message =
|
|
(data as { message?: string; error?: string }).message ??
|
|
(data as { error?: string }).error ??
|
|
'AITM request failed'
|
|
return NextResponse.json({ error: message }, { status: res.status })
|
|
}
|
|
return NextResponse.json(data)
|
|
}
|