peppa/apps/web/app/auth/egov/complete/route.ts
Асель ed49767c31 PEP-65: eGov QR login through the KazEDS relay (sign.aitu.uz)
Login page and Settings → eGov: relay session with a server-side challenge/nonce, QR + eGov Mobile
deep link, polling, one-shot completion; IIN from the signer's certificate (own DER reader),
users.egov_iin. The relay is trusted (PM decision): no own XMLDSig check. CLI peppa user egov,
mock-kazeds-relay, tests. Dev mode: /_next/static is no-store (Cloudflare cached stale chunks).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 01:14:10 +05:00

30 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { completeEgovFlow, isUserAllowed, PeppaError } from "@peppa/core";
import { cookies } from "next/headers";
import { NextResponse, type NextRequest } from "next/server";
import { EGOV_COOKIE, egovCookieOptions, safeNext, sessionCookie } from "@/lib/auth";
import { db } from "@/lib/db";
/** Finishes a signed eGov flow: logs in by the linked IIN, or links the IIN to the current account. */
export async function POST(req: NextRequest) {
const { next } = (await req.json().catch(() => ({}))) as { next?: string };
const handle = (await cookies()).get(EGOV_COOKIE)?.value ?? "";
const done = (body: object, status = 200) => {
const res = NextResponse.json(body, { status });
res.cookies.set(EGOV_COOKIE, "", egovCookieOptions(0));
return res;
};
try {
const r = await completeEgovFlow(db, handle);
// The handle lives only in the starter's httpOnly cookie, so a link flow is finished by the
// browser (and account) that started it.
if (r.purpose === "link") return done({ redirect: "/settings?egov=linked" });
if (!(await isUserAllowed(db, r.user))) return done({ error: "Нет доступа к Пеппе — попросите администратора" }, 403);
const res = done({ redirect: safeNext(next) });
const c = sessionCookie(r.user.id);
res.cookies.set(c.name, c.value, c.options);
return res;
} catch (e) {
return done({ error: e instanceof PeppaError ? e.message : "Не удалось войти через eGov" }, 400);
}
}