เปลี่ยน 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>
48 lines
2.3 KiB
TypeScript
48 lines
2.3 KiB
TypeScript
// PLAN-SSO.md §3.1 — POST /api/note/auth/token: เส้น mint login-token ของแอป note
|
|
// (ทางเข้า x-api-key ตัวเดียวกับ docver แต่แยกเส้นจาก /api/documents/token — D1
|
|
// contract docver ไม่ถูกแต้) — สำหรับ backend e-Court ประกอบลิงก์
|
|
// `/note?…&t=<access_token>` ให้ผู้ใช้คลิก (ฉบับ token 2026-10-30: หน้าแรกเป็น
|
|
// client bootstrap แลก credential เป็น session token ที่ /api/note/auth/session)
|
|
// token ที่ได้เป็น purpose="login" ใช้ได้กับการเข้าแอป note เท่านั้น (D4)
|
|
// ไม่ import @/db · ห้าม log ค่า key/body ดิบ
|
|
import { docverError } from "@/lib/document-versioning";
|
|
import { issueDocverToken, requireDocverApiKey } from "@/lib/docver-token";
|
|
import { parseLoginTokenBody } from "@/lib/note-auth";
|
|
|
|
export const runtime = "nodejs";
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export async function POST(request: Request): Promise<Response> {
|
|
const apiKeyFail = requireDocverApiKey(request);
|
|
if (apiKeyFail !== null) return apiKeyFail;
|
|
|
|
let raw: unknown;
|
|
try {
|
|
raw = await request.json();
|
|
} catch {
|
|
return docverError("body ต้องเป็น JSON", 400);
|
|
}
|
|
const parsed = parseLoginTokenBody(raw);
|
|
if (!parsed.ok) return docverError(parsed.error, 400);
|
|
|
|
// ความผิด config ฝั่ง server ไม่ใช่ความผิดของผู้เรียก — ไม่ตั้ง = 500 (ห้าม log ค่า secret)
|
|
const secret = process.env.DOCVER_TOKEN_SECRET?.trim();
|
|
if (secret === undefined || secret === "") {
|
|
return docverError("DOCVER_TOKEN_SECRET ยังไม่ถูกตั้งค่า", 500);
|
|
}
|
|
|
|
const accessToken = issueDocverToken(
|
|
{ userid: parsed.claims.userid, username: parsed.claims.username, fid: parsed.claims.fid, template: null },
|
|
secret,
|
|
parsed.ttlSeconds,
|
|
"login",
|
|
);
|
|
return new Response(
|
|
JSON.stringify({
|
|
access_token: accessToken,
|
|
token_type: "Bearer",
|
|
expires_in: parsed.ttlSeconds,
|
|
}),
|
|
{ headers: { "content-type": "application/json; charset=utf-8" } },
|
|
);
|
|
}
|