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>
2026-10-30 18:05:36 +07:00
|
|
|
// GET /api/note/files/open?fileUrl=/samples/… — ทางเปิดเอกสารตัวอย่างของหน้า /note/dev
|
|
|
|
|
// แบบ "A Link Get(new)" ของ /tmpl (คำขอผู้ใช้ 2026-10-30): ลิงก์เปล่า <Link> ยิง GET ได้เลย
|
|
|
|
|
// ไม่มี Bearer (browser navigation ใส่ header ไม่ได้) จึง**รับเฉพาะ path ในเครื่องที่ขึ้นต้น
|
|
|
|
|
// "/" เท่านั้น** — registerExternalFile ตรวจว่ามีไฟล์จริงใน public/ ผ่าน path guard เดิม
|
|
|
|
|
// (สร้างแถว url:… เท่านั้น — แถว ecourt: เกิดไม่ได้เพราะไม่รับ fileId และ path รีโมตโดนเด็ดขาด)
|
|
|
|
|
// แล้ว 302 เข้า viewer · ไฟล์ไม่ผ่าน = 302 /note?fileError=… ให้หน้าแรกแสดงสาเหตุ
|
|
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
|
|
|
|
|
|
import { registerExternalFile } from "@/lib/external-import";
|
|
|
|
|
import { withBasePath } from "@/utils/base-path";
|
|
|
|
|
|
|
|
|
|
export const runtime = "nodejs";
|
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
|
|
|
|
|
|
export async function GET(request: Request): Promise<Response> {
|
|
|
|
|
const url = new URL(request.url);
|
|
|
|
|
// withBasePath("") = หน้าแรก /docedit/note · withBasePath("/annotate/x") = viewer —
|
|
|
|
|
// อย่าส่ง "/note" เข้า withBasePath (มันเติม app path ให้อีกชั้นเป็น /note/note — D11)
|
2026-11-02 11:12:11 +07:00
|
|
|
// Location เป็น relative โดยเจตนา: browser resolve เทียบ origin ที่ตัวเองยิงมาเอง
|
|
|
|
|
// ส่วน NextResponse.redirect(new URL(path, request.url)) จะประกอบ absolute URL จาก
|
|
|
|
|
// Host ที่ reverse proxy ส่งมา (nginx default เขียนทับเป็น upstream เช่น 0.0.0.0:3000)
|
|
|
|
|
// แล้วพา browser หลุดโฮสต์จริง (prod 2026-11-02)
|
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>
2026-10-30 18:05:36 +07:00
|
|
|
const redirectTo = (path: string, query = ""): NextResponse => {
|
2026-11-02 11:12:11 +07:00
|
|
|
return new NextResponse(null, {
|
|
|
|
|
status: 302,
|
|
|
|
|
headers: { location: `${withBasePath(path)}${query}`, "cache-control": "no-store" },
|
|
|
|
|
});
|
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>
2026-10-30 18:05:36 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const fileUrl = url.searchParams.get("fileUrl") ?? "";
|
|
|
|
|
if (!fileUrl.startsWith("/") || fileUrl.includes("..") || fileUrl.length > 2048) {
|
|
|
|
|
return redirectTo(
|
|
|
|
|
"",
|
|
|
|
|
`?fileError=${encodeURIComponent("ลิงก์เปิดเอกสารตัวอย่างต้องเป็น path ในเครื่อง เช่น /samples/…")}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await registerExternalFile(fileUrl, undefined, undefined, { trusted: false });
|
|
|
|
|
if (!result.ok) {
|
|
|
|
|
return redirectTo("", `?fileError=${encodeURIComponent(result.error)}`);
|
|
|
|
|
}
|
|
|
|
|
return redirectTo(`/annotate/${result.fileId}`);
|
|
|
|
|
}
|