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

@ -81,6 +81,13 @@ describe("parseDocverTicket", () => {
expect(parseDocverTicket(JSON.stringify({ exp: 1, fid: "f".repeat(201), uid: "u", uname: "n" })).ok).toBe(false);
expect(parseDocverTicket(JSON.stringify({ exp: 1, fid: "f", uid: "u", uname: "n", tid: "t".repeat(101) })).ok).toBe(false);
});
it("uid/uname มี & = + % = ไม่ผ่าน (round-trip ผ่าน cookie oacCase ไม่ได้ — 2026-10-30)", () => {
for (const bad of ["a&b", "a=b", "a+b", "a%41"]) {
expect(parseDocverTicket(JSON.stringify({ exp: 1, fid: "f", uid: bad, uname: "n" })).ok, bad).toBe(false);
expect(parseDocverTicket(JSON.stringify({ exp: 1, fid: "f", uid: "u", uname: bad })).ok, bad).toBe(false);
}
});
});
describe("isTicketExpired", () => {

View file

@ -1,28 +0,0 @@
import { describe, expect, it } from "vitest";
import { sanitizeNextPath } from "@/utils/external-file";
// N8 D5 — path วนกลับหลัง login ต้องเป็น path ใน app เท่านั้น (กัน open-redirect)
describe("sanitizeNextPath", () => {
it("path ใน app (มี query ก็ได้) ผ่านตรง ๆ", () => {
expect(sanitizeNextPath("/")).toBe("/");
expect(sanitizeNextPath("/annotate")).toBe("/annotate");
expect(sanitizeNextPath("/annotate/abc-123")).toBe("/annotate/abc-123");
expect(sanitizeNextPath("/?fileUrl=https://x&fileId=1")).toBe(
"/?fileUrl=https://x&fileId=1",
);
});
it("absolute URL / protocol-relative / backslash → null กัน open-redirect", () => {
expect(sanitizeNextPath("//evil.com")).toBeNull();
expect(sanitizeNextPath("/\\evil.com")).toBeNull(); // browser ตีความ \ เป็น /
expect(sanitizeNextPath("https://evil.com")).toBeNull();
expect(sanitizeNextPath("http://evil.com/path")).toBeNull();
expect(sanitizeNextPath("/\\//evil.com")).toBeNull();
});
it("ค่าว่าง/ไม่ขึ้นต้น / ขาด ๆ เกิน ๆ → null", () => {
expect(sanitizeNextPath("")).toBeNull();
expect(sanitizeNextPath("annotate")).toBeNull(); // ไม่มี / นำหน้า
expect(sanitizeNextPath(" /annotate")).toBeNull(); // space นำหน้าไม่ผ่าน startsWith
});
});

View file

@ -1,58 +0,0 @@
import { describe, expect, it } from "vitest";
import {
buildOacCaseCookie,
OAC_CASE_COOKIE,
parseOacCase,
} from "@/utils/user";
/** ตัวอย่าง cookie จริงจากระบบ e-Court (เก็บใน test เพื่อล็อก format ของจริง) */
const REAL_OAC_CASE =
"u_userid=235821&u_userlogin=tu_2&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=";
describe("parseOacCase", () => {
it("cookie e-Court จริง → identity จาก u_userid/u_userlogin", () => {
expect(parseOacCase(REAL_OAC_CASE)).toEqual({
userId: "235821",
userName: "tu_2",
});
});
it("ค่าจาก buildOacCaseCookie อ่านกลับได้ (round-trip ค่า mock login จริง user1)", () => {
expect(parseOacCase(buildOacCaseCookie("user1", "user1"))).toEqual({
userId: "user1",
userName: "user1",
});
});
it("ค่าเสีย/ว่าง/ขาด field → null (ถือว่ายังไม่เข้าสู่ระบบ)", () => {
expect(parseOacCase(null)).toBeNull();
expect(parseOacCase(undefined)).toBeNull();
expect(parseOacCase("")).toBeNull();
expect(parseOacCase("garbage")).toBeNull();
expect(parseOacCase("u_userlogin=user_x")).toBeNull(); // ขาด u_userid
expect(parseOacCase("u_userid=123&u_userlogin=")).toBeNull(); // u_userlogin ว่าง
});
it("ค่ารอบด้วย space ถูก trim", () => {
expect(parseOacCase("u_userid= 123 &u_userlogin= tu_2 ")).toEqual({
userId: "123",
userName: "tu_2",
});
});
it("ชื่อ cookie ตรงกับระบบ e-Court", () => {
expect(OAC_CASE_COOKIE).toBe("oacCase");
});
});
describe("buildOacCaseCookie", () => {
it("แทนเฉพาะ u_userid/u_userlogin — ฟิลด์อื่นคงค่าตัวอย่างจริงครบทุก field", () => {
const fields = Object.fromEntries(new URLSearchParams(buildOacCaseCookie("xxxxxx", "user_x")));
expect(fields["u_userid"]).toBe("xxxxxx");
expect(fields["u_userlogin"]).toBe("user_x");
expect(fields["u_courtid"]).toBe("4");
expect(fields["u_perCardNo"]).toBe("5000000000006");
expect(fields["u_AuditID"]).toBe("1995227");
expect(Object.keys(fields)).toHaveLength(16);
});
});