feat(wordaddin): add e-Court pass-through sign-in
All checks were successful
Deploy eCourt Note Web to Server / Validate Database Schema (Drizzle) (push) Successful in 4s
Deploy eCourt Note Web to Server / Build Web Docker Image (push) Successful in 1m8s
Deploy eCourt Note Web to Server / Deploy eCourt Note Web to Server (push) Successful in 6s
Deploy eCourt Note Web to Server / Notify Deployment Status (push) Successful in 0s
All checks were successful
Deploy eCourt Note Web to Server / Validate Database Schema (Drizzle) (push) Successful in 4s
Deploy eCourt Note Web to Server / Build Web Docker Image (push) Successful in 1m8s
Deploy eCourt Note Web to Server / Deploy eCourt Note Web to Server (push) Successful in 6s
Deploy eCourt Note Web to Server / Notify Deployment Status (push) Successful in 0s
ไฟล์ที่ e-Court stamp custom properties token/owner มาใช้งานได้เลย ไม่ต้องกด login — detect() ของ TaskpaneApp อ่านค่าทั้งสองตอนเปิด task pane แล้วเรียก signInFromDocument(): username จาก owner → uid ใน JWT payload → NEXT_PUBLIC_ADDIN_MOCK_USERNAME, token จาก ไฟล์เก็บใน memory ให้ getAccessToken() ใช้ต่อ decodeJwtField ถอด base64url เป็น bytes แล้วผ่าน TextDecoder ก่อน JSON.parse — uid ที่ไม่ใช่ ASCII (เช่น ชื่อไทย) ไม่เป็น mojibake อัปเดตเอกสาร CLAUDE.md + docs/WORDADDIN-DEV.md พร้อมวิธีทดสอบ เองใน Word ผ่าน Advanced Properties Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
parent
dc851fbb73
commit
2b70a50c62
5 changed files with 78 additions and 9 deletions
|
|
@ -14,6 +14,8 @@ export type LoginResult = { ok: true } | { ok: false; error: string };
|
|||
|
||||
/** state เดียวของ mock — null = signed out */
|
||||
let username: string | null = null;
|
||||
/** token จาก custom properties ของไฟล์ (e-Court pass-through) — เก็บใน memory เท่านั้น */
|
||||
let docToken: string | null = null;
|
||||
|
||||
function delay(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
|
@ -31,7 +33,40 @@ export function signInForDemo(): void {
|
|||
username = config.mockUsername;
|
||||
}
|
||||
|
||||
/** ถอด field จาก JWT payload (base64url) โดยไม่ตรวจ signature — ใช้ดู uid ประกอบชื่อผู้ใช้เท่านั้น */
|
||||
function decodeJwtField(token: string, field: string): string | undefined {
|
||||
const payload = token.split(".")[1];
|
||||
if (!payload) return undefined;
|
||||
try {
|
||||
// atob คืน binary string (1 char = 1 byte) — ต้องถอด UTF-8 ด้วย TextDecoder ก่อน JSON.parse
|
||||
// ไม่งั้น uid ที่ไม่ใช่ ASCII (เช่น ชื่อไทย) จะกลายเป็น mojibake
|
||||
const bytes = Uint8Array.from(atob(payload.replace(/-/g, "+").replace(/_/g, "/")), (char) => char.charCodeAt(0));
|
||||
const json: unknown = JSON.parse(new TextDecoder().decode(bytes));
|
||||
const value = typeof json === "object" && json !== null ? (json as Record<string, unknown>)[field] : undefined;
|
||||
return typeof value === "string" && value !== "" ? value : undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* e-Court pass-through (ระยะแรก) — ไฟล์ที่ e-Court stamp `token`/`owner` ไว้ใน custom properties
|
||||
* ใช้งานได้เลยโดยไม่ต้องกด login: username จาก owner → uid ใน token → mockUsername
|
||||
* คืน true = ผ่านเข้าได้ (มี token หรือ owner อย่างน้อยหนึ่งตัว)
|
||||
*/
|
||||
export function signInFromDocument(input: { token?: string; owner?: string }): boolean {
|
||||
const token = input.token?.trim() || undefined;
|
||||
const owner = input.owner?.trim() || (token ? decodeJwtField(token, "uid") : undefined);
|
||||
if (!token && !owner) {
|
||||
return false;
|
||||
}
|
||||
username = owner ?? config.mockUsername;
|
||||
docToken = token ?? null;
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function getAccessToken(): Promise<string | null> {
|
||||
if (docToken) return docToken;
|
||||
return username ? `mock-token-${username}` : null;
|
||||
}
|
||||
|
||||
|
|
@ -58,4 +93,5 @@ export function startAutoRefresh(): void {
|
|||
|
||||
export function signOut(): void {
|
||||
username = null;
|
||||
docToken = null;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue