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:
parent
b1449ab668
commit
d37bc2a3f0
49 changed files with 2921 additions and 1307 deletions
32
lib/note-token.ts
Normal file
32
lib/note-token.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// session ของแอป note ฝั่ง server (ฉบับ token 2026-10-30 ตามคำขอผู้ใช้ "note ใช้
|
||||
// แบบ add-in") — แกะ identity จาก access_token (purpose="login" ออกที่
|
||||
// POST /api/note/auth/session อายุ NOTE_SESSION_TTL) ให้ Server Actions และ API
|
||||
// ของแอปใช้ร่วม: token ผิด/หมดอายุ/secret ไม่ตั้ง = null เสมอ (fail closed)
|
||||
// · ห้าม log ค่า token ดิบ
|
||||
//
|
||||
// แยกจาก lib/note-auth.ts (pure ใช้ร่วมกับ client ได้) เพราะต้อง import
|
||||
// lib/docver-token ซึ่งใช้ node:crypto — ห้ามให้ client component import ไฟล์นี้
|
||||
import { verifyDocverLoginToken } from "@/lib/docver-token";
|
||||
|
||||
/** อายุ access_token ของ session note (วินาที) — 8 ชม. เท่า session สมัย cookie:
|
||||
* เว็บไม่มี ticket ฝังไฟล์แบบ add-in ให้ renew อัตโนมัติ หมดแล้วผู้ใช้กลับ
|
||||
* e-Court กดลิงก์ใหม่ */
|
||||
export const NOTE_SESSION_TTL = 8 * 60 * 60;
|
||||
|
||||
export type NoteUserIdentity = { userid: string; username: string };
|
||||
|
||||
/** identity จาก token — null = ไม่ผ่านทุกกรณี (ไม่ตั้ง secret รวม) */
|
||||
export function noteUserFromToken(token: string | null | undefined): NoteUserIdentity | null {
|
||||
if (typeof token !== "string" || token === "") return null;
|
||||
const secret = process.env.DOCVER_TOKEN_SECRET?.trim();
|
||||
if (secret === undefined || secret === "") return null;
|
||||
const verified = verifyDocverLoginToken(token, secret);
|
||||
return verified.ok ? { userid: verified.claims.userid, username: verified.claims.username } : null;
|
||||
}
|
||||
|
||||
/** อ่าน `Authorization: Bearer <token>` ของ request → identity (route ใช้) */
|
||||
export function noteUserFromRequest(request: Request): NoteUserIdentity | null {
|
||||
const header = request.headers.get("authorization");
|
||||
if (header === null || !header.startsWith("Bearer ")) return null;
|
||||
return noteUserFromToken(header.slice("Bearer ".length).trim());
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue