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>
30 lines
1.5 KiB
TypeScript
30 lines
1.5 KiB
TypeScript
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);
|
||
}
|
||
}
|