eCourt-note/lib/note-token.ts
Your Name 5121970f31
Some checks failed
Deploy eCourt Note Web to Server / Validate Database Schema (Drizzle) (push) Successful in 3s
Deploy eCourt Note Web to Server / Build Web Docker Image (push) Failing after 30s
Deploy eCourt Note Web to Server / Deploy eCourt Note Web to Server (push) Has been skipped
Deploy eCourt Note Web to Server / Notify Deployment Status (push) Failing after 1s
refactor(auth): consolidate auth under /api/auth/* endpoints
- ยุบ route auth ที่เคย copy กันจน drift (flow N11 หายจาก /api/auth/session)
  เหลือโค้ด inline สองเส้นเดียว: POST /api/auth/token (ทางเข้า 3: x-api-key
  docver / x-api-key purpose:"login" mint ลิงก์ note / ticket ของ add-in)
  + POST /api/auth/session (แลก credential + register N8 + N11 fid-only)
- POST /api/documents/token คงไว้เป็น alias re-export (กลุ่ม docver/e-Court
  เดิม — contract เดิมเป๊ะ, มี test toBe กัน copy แยกชุดกลับมา)
- ถอดเส้นฝั่ง note ทั้งสอบ: /api/note/auth/token + /api/note/auth/session
  (e-Court ต้องย้ายมาทางเข้ารวม — แจ้งทีม e-Court ก่อน deploy)
- utils/auth-store.ts: primitives กลางฝั่ง client (jwtExp/isExpired/
  localStorage store + memory fallback) — lib/note-session ใช้ร่วม
  (expMs decode จาก JWT payload = นาฬิกา server) และวางไว้ให้ wordaddin
  ย้ายมาใช้รอบหน้าได้ (รอบนี้ไม่แต้ wordaddin ตามข้อตกลง)
- นโยบาย token หมดอายุกลาง session: PDFViewer reportActionError จับ
  auth-error ของ Server Action ทุกจุด → ล้าง session แล้วพาไป
  /note?sso=expired (จบที่ gate เดียวกับ bootstrap 401)
- openapi: ทางเข้า purpose:"login" + schema LoginTokenRequest + ลบ path
  เส้นที่ถอด + test กันเส้นตายหลุดค้างใน spec
- เอกสาร sync: PLAN-SSO §0.6, CLAUDE.md, PLAN-STAGED, .env.example, /note/dev
- tests: ย้าย/เพิ่มชุด auth (50 เคสกลุ่ม auth ผ่าน, ทั้งชุด 444 ผ่าน) +
  e2e ครบทั้ง API (mint/alias/ticket/404 เส้นเก่า/N11/D4) และ browser

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-09 14:30:29 +07:00

33 lines
2.3 KiB
TypeScript

// session ของแอป note ฝั่ง server (ฉบับ token 2026-10-30 ตามคำขอผู้ใช้ "note ใช้
// แบบ add-in") — แกะ identity จาก access_token (purpose="login" ออกที่
// POST /api/auth/session — เส้นเดิม /api/note/auth/session ถอดแล้ว 2026-11-09
// อายุ 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());
}