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>
85 lines
4 KiB
TypeScript
85 lines
4 KiB
TypeScript
// @vitest-environment jsdom
|
|
// utils/auth-store — primitives กลางของ session store ฝั่ง client (2026-11-09:
|
|
// ฝั่ง note ใช้ผ่าน lib/note-session.ts · ฝั่ง add-in วางไว้ย้ายมารอบหน้า)
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { createAuthStore, isExpired, jwtExp } from "@/utils/auth-store";
|
|
|
|
function b64url(obj: unknown): string {
|
|
return btoa(JSON.stringify(obj)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
}
|
|
|
|
describe("jwtExp", () => {
|
|
it("decode exp จาก JWT payload 3 segment (ไม่สน signature)", () => {
|
|
const token = `${b64url({ alg: "HS256", typ: "JWT" })}.${b64url({ exp: 1790000000 })}.sig`;
|
|
expect(jwtExp(token)).toBe(1_790_000_000);
|
|
});
|
|
|
|
it("payload ไม่มี exp / ไม่ใช่ตัวเลข / token ไม่มี 3 segment / base64 พัง → undefined", () => {
|
|
expect(jwtExp(`${b64url({ a: 1 })}.sig`)).toBeUndefined();
|
|
expect(jwtExp(`h.${b64url({ exp: "soon" })}.sig`)).toBeUndefined();
|
|
expect(jwtExp("not-a-jwt")).toBeUndefined();
|
|
expect(jwtExp("a.!!!.c")).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("isExpired", () => {
|
|
it("exp อนาคต = false · อดีต = true", () => {
|
|
const now = Math.floor(Date.now() / 1000);
|
|
expect(isExpired(now + 600)).toBe(false);
|
|
expect(isExpired(now - 600)).toBe(true);
|
|
});
|
|
|
|
it("margin — หมดอายุไปแล้วแต่ยังไม่เกิน margin = false (ช่วงผ่อนกัน clock skew) · เลิน margin = true", () => {
|
|
const now = Math.floor(Date.now() / 1000);
|
|
expect(isExpired(now - 20, 30_000)).toBe(false); // หมดแล้ว 20 วิ ยังในช่วงผ่อน 30 วิ
|
|
expect(isExpired(now - 40, 30_000)).toBe(true); // หมดเกิน 40 วิ เลย margin
|
|
expect(isExpired(now - 20, 0)).toBe(true); // ไม่มี margin = ตายทันทีที่ถึง exp
|
|
});
|
|
});
|
|
|
|
describe("createAuthStore", () => {
|
|
it("เขียน/อ่าน/ลบค่าดิบผ่าน localStorage ตาม key", () => {
|
|
const store = createAuthStore("test-auth-key");
|
|
expect(store.readRaw()).toBeNull();
|
|
store.writeRaw('{"a":1}');
|
|
expect(store.readRaw()).toBe('{"a":1}');
|
|
store.remove();
|
|
expect(store.readRaw()).toBeNull();
|
|
});
|
|
|
|
it("คนละ key ไม่ปนกัน", () => {
|
|
const a = createAuthStore("test-key-a");
|
|
const b = createAuthStore("test-key-b");
|
|
a.writeRaw("A");
|
|
b.writeRaw("B");
|
|
expect(a.readRaw()).toBe("A");
|
|
expect(b.readRaw()).toBe("B");
|
|
a.remove();
|
|
expect(b.readRaw()).toBe("B");
|
|
});
|
|
|
|
it("localStorage.setItem โยน (quota/private mode) — ยังเขียน/อ่านได้ผ่าน memory fallback", () => {
|
|
const spy = vi
|
|
.spyOn(Storage.prototype, "setItem")
|
|
.mockImplementation(() => { throw new DOMException("quota exceeded", "QuotaExceededError"); });
|
|
const store = createAuthStore("test-quota");
|
|
store.writeRaw('{"q":1}');
|
|
expect(store.readRaw()).toBe('{"q":1}');
|
|
spy.mockRestore();
|
|
});
|
|
|
|
it("ค่าใหม่ใน memory ชนะค่าเก่าที่ค้างใน localStorage (code review 2026-11-09)", () => {
|
|
// เขียนครั้งแรกสำเร็จทั้งคู่ → localStorage = "old" / memory = "old"
|
|
const store = createAuthStore("test-quota-stale");
|
|
store.writeRaw("old");
|
|
// ครั้งถัดมา localStorage ล้ม → memory = "new" แต่ localStorage ยังค้าง "old"
|
|
const spy = vi
|
|
.spyOn(Storage.prototype, "setItem")
|
|
.mockImplementation(() => { throw new DOMException("quota exceeded", "QuotaExceededError"); });
|
|
store.writeRaw("new");
|
|
spy.mockRestore();
|
|
// ต้องได้ค่าใหม่จาก memory ไม่ใช่ค่าเก่าตาย ๆ จาก localStorage
|
|
expect(store.readRaw()).toBe("new");
|
|
});
|
|
});
|