peppa/apps/web/app/auth/egov/start/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

21 lines
1.2 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 { PeppaError, startEgovFlow, type EgovPurpose } from "@peppa/core";
import { NextResponse, type NextRequest } from "next/server";
import QRCode from "qrcode";
import { currentUser, EGOV_COOKIE, egovCookieOptions } from "@/lib/auth";
import { db } from "@/lib/db";
/** Starts an eGov QR flow (PEP-65): `login` from the login page, `link` from Settings (needs a session). */
export async function POST(req: NextRequest) {
const { purpose } = (await req.json().catch(() => ({}))) as { purpose?: EgovPurpose };
try {
const user = purpose === "link" ? await currentUser() : null;
const flow = await startEgovFlow(db, purpose === "link" ? "link" : "login", user?.id ?? null);
const qr = await QRCode.toDataURL(flow.qrContent, { errorCorrectionLevel: "M", margin: 2, width: 280 });
const res = NextResponse.json({ qr, deeplink: flow.deeplink, expiresAt: flow.expiresAt });
res.cookies.set(EGOV_COOKIE, flow.handle, egovCookieOptions(Math.ceil((flow.expiresAt - Date.now()) / 1000)));
return res;
} catch (e) {
return NextResponse.json({ error: e instanceof PeppaError ? e.message : "Не удалось создать QR eGov" }, { status: 502 });
}
}