feat(sso): switch note session from cookies to token

เปลี่ยน session ของแอป note จาก cookie คู่ oacCase/oacCaseSig มาเป็น
access_token แบบ add-in ตามคำขอผู้ใช้ (2026-10-30):

- POST /api/note/auth/session แลก credential ของลิงก์ e-Court
  (JWT ทาง A / ticket ทาง B) เป็น access_token purpose=login อายุ 8 ชม.
  พร้อมลงทะเบียนเอกสาร N8 แบบ trusted ฝั่ง server (fileId จาก fid)
- token เก็บ localStorage "note-auth" (lib/note-session.ts) + Bearer
  ผ่าน noteAuthFetch · Server Actions ทุกตัวรับ token เป็น param แรก
  และแกะ identity เอง (lib/note-token.ts) — ไม่เชื่อ userId จาก client
- หน้า protected เป็น shell + client gate (NoteHome bootstrap ที่ /note,
  AnnotateView + GET /api/note/files/[id]/bootstrap)
- ถอด: middleware.ts, lib/session.ts, utils/user.ts, action logout,
  GET /api/note/auth/e-court, entry /note/annotate?fileUrl=
- ทาง ticket ปฏิเสธ secret default "super_secret" เฉพาะ production
- GET /api/note/files/open: ลิงก์เปิดไฟล์ตัวอย่างแบบ /tmpl (path ใน
  เครื่อง /samples เท่านั้น)
- หน้า /note/dev: ลิงก์ A Link Get(new) ที่ mint credential แล้วยิง
  เข้า flow SSO จริง (JWT/ticket สลับได้) + section Flow + เรียบเรียง
  ภาษาให้อ่านง่าย · หน้า /note เป็นหน้าข้อมูลระบบ
- แก้ตาม code review: redo closure เรียก action ด้วย signature เก่า,
  bootstrap timeout + strip credential ที่ผิดรูป, แยก no-session ออก
  จาก network error, ห้ามอักขระ & = + % ใน identity, ตรวจ purpose
  ก่อน exp ของ login-token, audit log strip อักขระควบคุม
- เอกสาร: docs/PLAN-SSO.md §0.5 (ฉบับ token), CLAUDE.md, .env.example

ทดสอบ: tsc ผ่าน · vitest ผ่านทั้งชุด · ทดสอบจริงผ่าน Playwright
(SSO → viewer → เปลี่ยนสี annotation → undo → redo ครบวงจร)

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
Your Name 2026-10-30 18:05:36 +07:00
parent b1449ab668
commit d37bc2a3f0
49 changed files with 2921 additions and 1307 deletions

View file

@ -38,6 +38,14 @@ const TICKET_LIMITS = {
/** allowlist ของ ticket_data — field อื่นปนมา = reject (กัน typo เงียบ ๆ ตาม convention ของ repo) */
const TICKET_FIELDS = ["exp", "fid", "tid", "uid", "uname"] as const;
/** อักขระที่ uid/uname ใช้ไม่ได้ — & = + % เป็นชุดที่ตีความต่างกันตาม context
* ง่าย (query string/URL decode/ฟอร์ม) และค่า identity นี้ไหลลง DB เป็น
* user_id/user_name ของ annotation/bookmark (แสดง UI + เงื่อนไขค้นหา) —
* เคยทำ identity เพี้ยนในรอบ cookie (payload ถูก parse ผ่าน URLSearchParams —
* ถอดแล้ว 2026-10-30) จึงกันไว้ตั้งแต่แกะ ticket (เกณฑ์เดียวกับ
* parseLoginTokenBody ของ lib/note-auth.ts) */
const UNSAFE_IDENTITY_RE = /[&=+%]/;
export type DocverTicketParseResult =
| { ok: true; ticket: DocverTicket }
| { ok: false; error: string };
@ -82,8 +90,14 @@ export function parseDocverTicket(raw: string): DocverTicketParseResult {
const uid = field("uid");
if (uid === null) return { ok: false, error: "ticket_data ต้องมี uid (string 1–100)" };
if (UNSAFE_IDENTITY_RE.test(uid)) {
return { ok: false, error: "ticket_data ของ uid มีอักขระ & = + % ที่รองรับไม่ได้" };
}
const uname = field("uname");
if (uname === null) return { ok: false, error: "ticket_data ต้องมี uname (string 1–200)" };
if (UNSAFE_IDENTITY_RE.test(uname)) {
return { ok: false, error: "ticket_data ของ uname มีอักขระ & = + % ที่รองรับไม่ได้" };
}
const fid = field("fid");
if (fid === null) return { ok: false, error: "ticket_data ต้องมี fid (string 1–200)" };

View file

@ -82,21 +82,12 @@ export function externalIdFor(fileId: string | undefined, url: URL): string {
return id ? `ecourt:${id}` : `url:${url.href}`;
}
/** path วนกลับหลัง login (N8 D5) — ต้องขึ้นต้น "/" และไม่ใช่ "//" หรือ "/\"
* (กัน open-redirect: browser ตีความ "//evil.com" และ "/\evil.com" เป็น absolute
* URL ไป host อื่น) — ค่า next มาจาก query ที่ใครก็แก้ได้ ต้อง sanitize ก่อนใช้เสมอ;
* ไม่ผ่านคืน null ให้ caller ใช้ "/" */
export function sanitizeNextPath(s: string): string | null {
if (!s.startsWith("/") || s.startsWith("//") || s.startsWith("/\\")) return null;
return s;
}
/** รหัสกลุ่มเอกสาร (N9 `type_id`) normalize — single source ใช้ร่วมทั้ง N8
* `registerExternalFile` (ลิงก์ e-Court) และ N4 `uploadFile` (ช่องกลุ่มของฟอร์ม
* อัปโหลด): trim → ว่าง = ไม่มีกลุ่ม (null), cap 200 ตัวอักษร (เทียบ title)
* ค่าที่ไม่ใช่ string (เช่น File จาก FormData) = null (responsive audit 2026-09-24:
* เดิมเป็นสองสำเนา mirroring กัน — normGroup ของ external-import + normTypeId
* ของ files.ts — เสี่ยง drift) */
/** รหัสกลุ่มเอกสาร (N9 `type_id`) normalize — ใช้ที่เดียวคือ N8
* `registerExternalFile` (ลิงก์ e-Court — ทั้ง insert ครั้งแรกและ update เมื่อลิงก์
* รอบใหม่ส่ง typeId มา): trim → ว่าง = ไม่มีกลุ่ม (null), cap 200 ตัวอักษร
* (เทียบ title) ค่าที่ไม่ใช่ string (เช่น File จาก FormData) = null
* (responsive audit 2026-09-24: เดิมเป็นสองสำเนา mirroring กัน — normGroup ของ
* external-import + normTypeId ของ files.ts — เสี่ยง drift) */
export function normDocumentGroup(raw: unknown): string | null {
if (typeof raw !== "string") return null;
const v = raw.trim().slice(0, 200);

View file

@ -1,53 +0,0 @@
// identity ของแอป: mock login e-Court หน้า /login — อ่านจาก cookie "oacCase"
// (userId = u_userid, userName = u_userlogin) แทน localStorage เดิม
// ไฟล์นี้เป็น pure module — middleware (edge) และ client import ร่วมได้ ห้ามใส่ next/headers
export type OacUser = { userId: string; userName: string };
export const OAC_CASE_COOKIE = "oacCase";
/** ผู้ใช้ mock หน้า /login (dropdown) — LoginForm (client) + action login (server)
* ใช้ร่วมกันผ่าน module นี้กันสองที่ drift; รหัสผ่านกลาง Demo1234 อยู่ใน action */
export const MOCK_USERS: readonly string[] = [
"user1",
"user2",
"user3",
"user4",
"user5",
];
/** แยกค่า query string ของ cookie oacCase → identity
* ขาด/ว่าง u_userid หรือ u_userlogin = ถือว่ายังไม่เข้าสู่ระบบ (null)
* หมายเหตุ: Next (cookies().get / req.cookies.get) decode ค่า cookie กลับมาให้แล้ว
* จึงส่งให้ URLSearchParams ตรง ๆ — ห้าม decodeURIComponent ซ้ำ */
export function parseOacCase(raw: string | null | undefined): OacUser | null {
if (!raw) return null;
const params = new URLSearchParams(raw);
const userId = params.get("u_userid")?.trim();
const userName = params.get("u_userlogin")?.trim();
if (!userId || !userName) return null;
return { userId, userName };
}
/** ค่า cookie oacCase ตาม format จริงของ e-Court (ฟิลด์อื่นคงค่าจากตัวอย่างจริง)
* mock login แทนเฉพาะ u_userid/u_userlogin */
export function buildOacCaseCookie(userId: string, userLogin: string): string {
return [
`u_userid=${userId}`,
`u_userlogin=${userLogin}`,
"u_courtid=4",
"u_password=",
"u_AdminStatus=",
"u_perCardNo=5000000000006",
"u_perCardNoEncript=1800650010005002003",
"u_userType=4",
"u_CidAuthen=NNNENNNNNNNNNNNNNNNN",
"u_hSecId=NNNNNNNNNNNNNNNNNNNN",
"u_username=",
"u_courtName=",
"u_AuditID=1995227",
"u_PsGroupNum=1",
"u_PsGroup=11",
"u_PsGroupName=",
].join("&");
}