Authn authz design

From ksmaster03/claude-canfly-skill · View interactive version

Authn authz design is an AI agent skill from the GitHub repository ksmaster03/claude-canfly-skill. ออกแบบ Authentication (ยืนยันตัวตน: password/MFA/SSO/OAuth2-OIDC, session vs JWT) และ Authorization (กำหนดสิทธิ์: RBAC/ABAC) ให้ปลอดภัยตาม OWASP ASVS — produce a concrete, standards-aligned AuthN/AuthZ design (login flows, session/token strategy, role-permission model). Trigger เมื่อผู้ใช้พิมพ์ /aut

How to use this skill

Skill file

/authn-authz-design — ออกแบบการยืนยันตัวตนและสิทธิ์ (Authentication & Authorization)

สกิลนี้ช่วยออกแบบ "ใครเข้าได้ (AuthN)" และ "เข้ามาแล้วทำอะไรได้ (AuthZ)" ให้ครบและปลอดภัยตามมาตรฐานปัจจุบัน (OWASP ASVS V2/V3/V4, OWASP Top 10 — Broken Authentication & Broken Access Control) โดยส่งมอบ artifact ที่เอาไปสร้างจริงได้ทันที ไม่ใช่หลักการลอย ๆ

ใช้ตอนไหน

ขอบเขต: เฟสนี้ออกแบบ กลไกและโมเดล auth เท่านั้น

- เมทริกซ์บทบาท×สิทธิ์แบบละเอียด + กฎ Segregation of Duties → ไปที่ /sod-matrix

- วิเคราะห์ภัยคุกคาม/attack tree → ไปที่ /threat-model

Input ที่ต้องถามก่อนเริ่ม

  1. ประเภทผู้ใช้ — internal staff / external customer / B2B partner / machine-to-machine (service) ปนกันไหม
  2. จำนวน identity source — self-register, มี IdP องค์กรอยู่แล้ว (Azure AD/Entra, Google Workspace, Keycloak), หรือต้องรองรับ social login
  3. ชนิด client — web SPA, server-rendered web, mobile app, API/B2B, third-party ที่ขอ access แทนผู้ใช้
  4. ความอ่อนไหวของข้อมูล/ข้อบังคับ — ต้อง MFA บังคับไหม, PDPA/PCI-DSS/HIPAA, ต้อง audit log แค่ไหน
  5. ความซับซ้อนของสิทธิ์ — role คงที่ไม่กี่แบบ (→ RBAC) หรือขึ้นกับ attribute/owner/แผนก/เวลา (→ ABAC/ReBAC)
  6. ข้อจำกัด — มี API gateway/reverse proxy ทำ auth ได้ไหม, ต้อง stateless scale หรือมี session store (Redis) ได้, single sign-out จำเป็นไหม

ขั้นตอน (Playbook)

  1. แยกแกน AuthN กับ AuthZ ให้ขาด — AuthN = พิสูจน์ "คุณคือใคร", AuthZ = ตัดสิน "คุณทำสิ่งนี้ได้ไหม" สองอย่างนี้คนละ layer คนละการตัดสินใจ อย่าฝัง role ลงใน login logic
  2. เลือกกลยุทธ์ AuthN
  1. ออกแบบ session strategy — เลือก cookie-session (stateful) หรือ JWT (stateless) ตามตารางตัดสินใจด้านล่าง; กำหนด expiry, refresh, การ revoke, logout, และ cookie flags
  2. เลือก AuthZ model — RBAC / ABAC / ReBAC / PBAC (ดูเกณฑ์ด้านล่าง); ออกแบบ role→permission และ enforcement point (ที่ API/service layer เสมอ ไม่ใช่แค่ที่ UI)
  3. ออกแบบ token contents — กำหนด claims/scopes ให้ access token เล็ก, อายุสั้น, ใส่เฉพาะที่ resource server ต้องใช้; อย่ายัด PII หรือสิทธิ์ทั้งหมดลง token
  4. ครอบ account lifecycle — register, email/phone verify, reset password, lockout/throttle, deactivate/offboard ให้ครบ (ช่องโหว่ broken auth ส่วนใหญ่อยู่ตรง flow รอบ ๆ login ไม่ใช่ตัว login)
  5. ตรวจตาม OWASP ASVS — กากบาทกับ V2 (Authentication), V3 (Session Management), V4 (Access Control) ก่อนปิดงาน
  6. เขียน artifact — ส่งมอบ flow + role table + policy table + JWT example ตามเทมเพลตด้านล่าง

เกณฑ์เลือก session: cookie-session vs JWT

| ประเด็น | Cookie-session (stateful) | JWT access token (stateless) |

|---|---|---|

| Revoke ทันที | ทำได้ (ลบ session ใน store) | ทำยาก — ต้องมี blocklist/short TTL |

| Scale หลาย service | ต้องแชร์ session store (Redis) | ดี — verify ด้วย key อย่างเดียว |

| เหมาะกับ | web app domain เดียว | API, microservices, mobile, M2M |

| ความเสี่ยงหลัก | CSRF (กัน SameSite+token) | token ขโมยแล้วใช้ได้จนหมดอายุ |

| คำแนะนำ default | web ทั่วไป → cookie-session | API/SPA/mobile → access(สั้น)+refresh(rotate) |

แนวทางที่นิยม: SPA/mobile ใช้ access token อายุสั้น (5–15 นาที) + refresh token หมุน (rotation + reuse detection) เก็บใน httpOnly cookie หรือ secure storage; web แบบ classic ใช้ cookie-session ตรง ๆ ปลอดภัยและง่ายกว่า

เกณฑ์เลือก AuthZ model

Output / Artifact (เทมเพลตพร้อมใช้)

1) AuthN flow — OIDC Authorization Code + PKCE (สเต็ป)

1. Client สร้าง code_verifier (สุ่ม) → คำนวณ code_challenge = BASE64URL(SHA256(verifier))
2. Browser → /authorize?response_type=code&client_id&redirect_uri
      &scope=openid profile email&state=<csrf>&code_challenge&code_challenge_method=S256
3. IdP ยืนยันตัวตนผู้ใช้ (password + MFA) → redirect กลับ redirect_uri?code=<auth_code>&state
4. Client ตรวจ state ตรงกับที่ส่งไป (กัน CSRF)
5. Client → POST /token  (code + code_verifier + client_id [+ secret ถ้าเป็น confidential client])
6. IdP ตรวจ verifier ↔ challenge → คืน id_token (JWT) + access_token + refresh_token
7. Client ตรวจ id_token: signature(JWKS), iss, aud, exp, nonce → สร้าง session ฝั่ง app
8. เรียก resource API ด้วย access_token (Authorization: Bearer ...); API ตรวจ sig+exp+aud+scope
9. access หมดอายุ → ใช้ refresh_token ขอใหม่ (rotation: คืน refresh ใหม่, เพิกถอนตัวเก่า)

SPA/mobile = public client → ต้องใช้ PKCE และไม่มี client_secret

2) RBAC: role → permission matrix

| Permission \ Role | Admin | Manager | Staff | Viewer |

|---|:---:|:---:|:---:|:---:|

| user.create / user.delete | ✅ | — | — | — |

| user.read | ✅ | ✅ | ✅ | ✅ |

| order.create | ✅ | ✅ | ✅ | — |

| order.approve | ✅ | ✅ | — | — |

| order.read.all | ✅ | ✅ | own only | own only |

| report.export | ✅ | ✅ | — | — |

| settings.manage | ✅ | — | — | — |

permission ตั้งชื่อแบบ resource.action[.scope]; "own only" คือจุดที่เสริม ABAC (record.ownerId == subject.id)

3) Policy table — password / MFA / session (ค่าที่แนะนำ อิง ASVS/NIST 800-63B)

| ด้าน | พารามิเตอร์ | ค่าที่แนะนำ |

|---|---|---|

| Password | ความยาวขั้นต่ำ | ≥ 12 อักขระ (ไม่บังคับ composition rule) |

| Password | ตรวจ breach list | บล็อกรหัสที่หลุดแล้ว (เช่น HaveIBeenPwned k-anon) |

| Password | hashing | Argon2id (หรือ bcrypt cost ≥ 12 / scrypt) ห้าม MD5/SHA1/plain |

| Password | บังคับเปลี่ยนตามรอบ | ไม่บังคับ เปลี่ยนเฉพาะเมื่อมีสัญญาณรั่ว |

| MFA | TOTP / WebAuthn(passkey) | บังคับสำหรับ admin/role สิทธิ์สูง; แนะนำกับทุกคน |

| MFA | SMS OTP | ใช้ได้แต่เป็น factor ที่อ่อนสุด หลีกเลี่ยงถ้ามีทางเลือก |

| Lockout | throttle/lock | exponential backoff + lock ชั่วคราวหลังพลาด ~5–10 ครั้ง + กัน enumeration |

| Session | idle timeout | 15–30 นาที (ระบบความอ่อนไหวสูง) |

| Session | absolute timeout | 8–12 ชม. แล้วบังคับ re-auth |

| Token | access token TTL | 5–15 นาที |

| Token | refresh token | rotation + reuse detection; absolute lifetime จำกัด (เช่น 7–30 วัน) |

| Cookie | flags | Secure + HttpOnly + SameSite=Lax (หรือ Strict); ตั้ง __Host- prefix |

| Logout | revoke | ลบ server session / เพิกถอน refresh; รองรับ single logout ถ้าใช้ SSO |

4) ตัวอย่าง JWT (access token) claims

{
  "iss": "https://auth.example.com",
  "sub": "u_8f3a91c2",
  "aud": "https://api.example.com",
  "exp": 1718900000,
  "iat": 1718899100,
  "jti": "tok_4b1e...",
  "scope": "order.read order.create",
  "roles": ["staff"],
  "tenant": "acme",
  "amr": ["pwd", "otp"]
}

หลักการ: ใส่เฉพาะ claim ที่ resource server ต้องใช้ตัดสิน, ไม่ใส่ PII/secret, ตั้ง aud ให้ตรง API, ตรวจ exp+signature(JWKS) ทุกครั้ง, ใช้ jti ทำ revoke/replay-protection; ห้าม ยอมรับ alg: none

Checklist / Definition of Done

เคล็ดลับ & ข้อควรระวัง

เชื่อมกับเฟสอื่น

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.