refactor(auth): consolidate auth under /api/auth/* endpoints
Some checks failed
Deploy eCourt Note Web to Server / Validate Database Schema (Drizzle) (push) Successful in 3s
Deploy eCourt Note Web to Server / Build Web Docker Image (push) Failing after 30s
Deploy eCourt Note Web to Server / Deploy eCourt Note Web to Server (push) Has been skipped
Deploy eCourt Note Web to Server / Notify Deployment Status (push) Failing after 1s
Some checks failed
Deploy eCourt Note Web to Server / Validate Database Schema (Drizzle) (push) Successful in 3s
Deploy eCourt Note Web to Server / Build Web Docker Image (push) Failing after 30s
Deploy eCourt Note Web to Server / Deploy eCourt Note Web to Server (push) Has been skipped
Deploy eCourt Note Web to Server / Notify Deployment Status (push) Failing after 1s
- ยุบ route auth ที่เคย copy กันจน drift (flow N11 หายจาก /api/auth/session) เหลือโค้ด inline สองเส้นเดียว: POST /api/auth/token (ทางเข้า 3: x-api-key docver / x-api-key purpose:"login" mint ลิงก์ note / ticket ของ add-in) + POST /api/auth/session (แลก credential + register N8 + N11 fid-only) - POST /api/documents/token คงไว้เป็น alias re-export (กลุ่ม docver/e-Court เดิม — contract เดิมเป๊ะ, มี test toBe กัน copy แยกชุดกลับมา) - ถอดเส้นฝั่ง note ทั้งสอบ: /api/note/auth/token + /api/note/auth/session (e-Court ต้องย้ายมาทางเข้ารวม — แจ้งทีม e-Court ก่อน deploy) - utils/auth-store.ts: primitives กลางฝั่ง client (jwtExp/isExpired/ localStorage store + memory fallback) — lib/note-session ใช้ร่วม (expMs decode จาก JWT payload = นาฬิกา server) และวางไว้ให้ wordaddin ย้ายมาใช้รอบหน้าได้ (รอบนี้ไม่แต้ wordaddin ตามข้อตกลง) - นโยบาย token หมดอายุกลาง session: PDFViewer reportActionError จับ auth-error ของ Server Action ทุกจุด → ล้าง session แล้วพาไป /note?sso=expired (จบที่ gate เดียวกับ bootstrap 401) - openapi: ทางเข้า purpose:"login" + schema LoginTokenRequest + ลบ path เส้นที่ถอด + test กันเส้นตายหลุดค้างใน spec - เอกสาร sync: PLAN-SSO §0.6, CLAUDE.md, PLAN-STAGED, .env.example, /note/dev - tests: ย้าย/เพิ่มชุด auth (50 เคสกลุ่ม auth ผ่าน, ทั้งชุด 444 ผ่าน) + e2e ครบทั้ง API (mint/alias/ticket/404 เส้นเก่า/N11/D4) และ browser Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
parent
c57a75e36a
commit
5121970f31
26 changed files with 625 additions and 523 deletions
|
|
@ -1,19 +1,24 @@
|
|||
// POST /api/auth/session (path สั้นของ POST /api/note/auth/session — โค้ดก้อนเดียวกัน
|
||||
// copy มา 2026-11-05) — แลก credential ของลิงก์ e-Court เป็น access_token
|
||||
// (ฉบับ token 2026-10-30 ตามคำขอผู้ใช้ "note ใช้แบบ add-in" — แทน GET e-court ที่
|
||||
// set cookie คู่ oacCase ซึ่งถอดแล้ว): body {t | ticket_data+HmacSha256, fileUrl?,
|
||||
// typeId?} → verify → ลงทะเบียนเอกสาร N8 แบบ trusted ฝั่ง server เลย (fileId จาก
|
||||
// fid ที่เซ็นมา — ไม่ต้องมี link proof อีก) → ออก login-token อายุ NOTE_SESSION_TTL
|
||||
// (8 ชม.) ให้ client เก็บ localStorage "note-auth"
|
||||
// POST /api/auth/session — **ทางเข้าหลัก (และเดียว) ของแอป note** (2026-11-09
|
||||
// รอบสอง: ถอด /api/note/auth/session เส้นเก่าออก — ผู้เรียกต้องย้ายมาเส้นนี้) —
|
||||
// แลก credential ของลิงก์ e-Court เป็น access_token (ฉบับ token 2026-10-30 ตามคำขอ
|
||||
// ผู้ใช้ "note ใช้แบบ add-in" — แทน GET e-court ที่ set cookie ซึ่งถอดแล้ว):
|
||||
// body {t | ticket_data+HmacSha256, fileUrl?, typeId?} → verify → ลงทะเบียนเอกสาร
|
||||
// N8 แบบ trusted ฝั่ง server เลย (fileId จาก fid ที่เซ็นมา) → ออก login-token
|
||||
// อายุ NOTE_SESSION_TTL (8 ชม.) ให้ client เก็บ localStorage "note-auth"
|
||||
// · 400 body ผิด · 401 {error, reason: expired|invalid} · 500 secret ไม่ตั้ง
|
||||
// · audit log เฉพาะ userid (strip อักขระควบคุม) — ห้าม log token/query ดิบ
|
||||
import { NextResponse } from "next/server";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
import { db } from "@/db";
|
||||
import { files } from "@/db/schema";
|
||||
import { addinTicketSecret, verifyAddinTicket } from "@/lib/addin-ticket";
|
||||
import { issueDocverToken, verifyDocverLoginToken } from "@/lib/docver-token";
|
||||
import { ecourtExternalId } from "@/lib/ecourt-fid";
|
||||
import { registerExternalFile } from "@/lib/external-import";
|
||||
import { parseSessionExchangeBody } from "@/lib/note-auth";
|
||||
import { NOTE_SESSION_TTL } from "@/lib/note-token";
|
||||
import { hasStagedDoc, isStagedSource } from "@/lib/staged-docs";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
|
@ -82,6 +87,30 @@ export async function POST(request: Request): Promise<Response> {
|
|||
});
|
||||
if (result.ok) fileId = result.fileId;
|
||||
else fileError = result.error;
|
||||
} else {
|
||||
// N11 (PLAN-STAGED.md D5): ลิงก์ fid-only (เอกสาร staging — เนื้อส่งมาทาง POST
|
||||
// /api/note/files/stage แล้ว ลิงก์มีแค่ t=) — เช็คตั้งแต่ตอนแลก token เลย ไม่ปล่อย
|
||||
// เข้า viewer ไปเจอ 410 เอง · ใช้ hasStagedDoc (ไม่ยืด TTL ไม่ถือ Buffer — การอ่าน
|
||||
// จริงเกิดที่ viewer) · เอกสาร N8 ที่รู้จักแล้ว (pdf_url เป็น URL/path ปกติ) เข้าได้
|
||||
// ทางเดียวกัน · ผู้ใช้ได้ token ตามปกติทุกกรณี
|
||||
try {
|
||||
const [row] = await db
|
||||
.select()
|
||||
.from(files)
|
||||
.where(eq(files.externalId, ecourtExternalId(identity.fid)))
|
||||
.limit(1);
|
||||
if (!row) {
|
||||
fileError = "เอกสารยังไม่ถูกส่งเข้าระบบ — กลับ e-Court กดลิงก์ใหม่";
|
||||
} else if (isStagedSource(row.pdfUrl)) {
|
||||
if (hasStagedDoc(row.pdfUrl)) fileId = row.id;
|
||||
else fileError = "เอกสารพ้นช่วงจัดเตรียม — กลับ e-Court กดลิงก์ใหม่";
|
||||
} else {
|
||||
fileId = row.id;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[sso] ค้นหาเอกสารของ fid ล้ม", err);
|
||||
fileError = "ค้นเอกสารไม่สำเร็จ — ลองอีกครั้ง";
|
||||
}
|
||||
}
|
||||
|
||||
const access_token = issueDocverToken(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue