refactor(auth): consolidate auth under /api/auth/* endpoints
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
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
- ยุบ 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>
This commit is contained in:
parent
c57a75e36a
commit
5121970f31
26 changed files with 625 additions and 523 deletions
79
utils/auth-store.ts
Normal file
79
utils/auth-store.ts
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
// auth-store — primitives กลางของ session store ฝั่ง client ใช้ร่วมกันหลายแอป
|
||||
// (สร้าง 2026-11-09 รอบ "รวม auth ทางเข้าเดียว /api/auth/*": ฝั่ง note ใช้ผ่าน
|
||||
// lib/note-session.ts — ฝั่ง add-in วางไว้ย้ายมาใช้รอบหน้า · วางที่ utils/
|
||||
// เพราะเป็นโซนเดียวที่ wordaddin นำเข้าได้ตามกติกา client-only zone)
|
||||
//
|
||||
// pure client — ไม่ import อะไรเลย · ไม่มี secret/การตรวจ signature ใด ๆ
|
||||
// (jwtExp decode payload อย่างเดียวเพื่ออ่าน exp — การตรวจ signature เป็นของ server)
|
||||
|
||||
/** ถอด exp (unix วินาที) จาก JWT payload โดยไม่ตรวจ signature — undefined = decode ไม่ได้ */
|
||||
export function jwtExp(token: string): number | undefined {
|
||||
const payload = token.split(".")[1];
|
||||
if (!payload) return undefined;
|
||||
try {
|
||||
const bytes = Uint8Array.from(
|
||||
atob(payload.replace(/-/g, "+").replace(/_/g, "/")),
|
||||
(char) => char.charCodeAt(0),
|
||||
);
|
||||
const json: unknown = JSON.parse(new TextDecoder().decode(bytes));
|
||||
const exp =
|
||||
typeof json === "object" && json !== null
|
||||
? (json as Record<string, unknown>).exp
|
||||
: undefined;
|
||||
return typeof exp === "number" && Number.isFinite(exp) ? exp : undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/** exp (unix วินาที) หมดอายุแล้วหรือยัง — margin กันขอบเขตตกหล่นระหว่างนาฬิกาสองฝั่ง */
|
||||
export function isExpired(expSeconds: number, marginMs = 30_000): boolean {
|
||||
return expSeconds * 1000 <= Date.now() - marginMs;
|
||||
}
|
||||
|
||||
/** raw store ต่อ key — ผู้ใช้แต่ละแอป parse/validate shape เอง */
|
||||
export type AuthRawStore = {
|
||||
/** ค่าดิบจาก localStorage — fallback ที่ memory ของ session นี้เมื่อ storage โดน block · null = ไม่มีค่า */
|
||||
readRaw(): string | null;
|
||||
writeRaw(value: string): void;
|
||||
remove(): void;
|
||||
};
|
||||
|
||||
/** memory fallback ต่อ key — localStorage โยน (private mode/quota/webview บางตัว) ก็ยังใช้ได้ใน session เดียว · ⚠️ module นี้ถูก evaluate ตอน SSR ด้วย (Map แชร์ข้าม request บน server process) — ห้ามเรียก write/remove ระหว่าง render ฝั่ง server ให้เรียกจาก effect/handler ฝั่ง browser เท่านั้น ไม่งั้น session ปนกันข้ามผู้ใช้เงียบ ๆ */
|
||||
const memoryFallback = new Map<string, string>();
|
||||
|
||||
/** store ของหนึ่ง key — ทุกทาง try/catch ไม่โยนออกมาเด็ดขาด */
|
||||
export function createAuthStore(key: string): AuthRawStore {
|
||||
return {
|
||||
readRaw(): string | null {
|
||||
// memory ก่อนเสมอ — writeRaw เขียน memory ก่อน localStorage ค่าใน memory จึง
|
||||
// ใหม่กว่าหรือเท่ากับ localStorage เสมอ: quota ล้มกลาง session แล้ว localStorage
|
||||
// ค้างค่าเก่า ต้องไม่อ่านค่าเก่าตาย ๆ กลับมาแทนค่าใหม่ที่ยังอยู่ใน memory
|
||||
// (code review 2026-11-09) · tab ใหม่ memory ว่าง = อ่าน localStorage ตามปกติ
|
||||
if (typeof window === "undefined") return memoryFallback.get(key) ?? null;
|
||||
try {
|
||||
return memoryFallback.get(key) ?? window.localStorage.getItem(key);
|
||||
} catch {
|
||||
return memoryFallback.get(key) ?? null;
|
||||
}
|
||||
},
|
||||
writeRaw(value: string): void {
|
||||
memoryFallback.set(key, value);
|
||||
if (typeof window === "undefined") return;
|
||||
try {
|
||||
window.localStorage.setItem(key, value);
|
||||
} catch {
|
||||
// private mode/quota — memory ของ session นี้ยังถืออยู่
|
||||
}
|
||||
},
|
||||
remove(): void {
|
||||
memoryFallback.delete(key);
|
||||
if (typeof window === "undefined") return;
|
||||
try {
|
||||
window.localStorage.removeItem(key);
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue