สารบัญหน้านี้ (21)
- ภาพรวม
- Workflow: พนักงานขายคืนเงิน ฿6,890 → ถูกปฏิเสธ → ผู้จัดการสาขาอนุมัติ
- Workflow: เพิ่มนโยบายใหม่อย่างปลอดภัย: จำลอง → dry_run → เปิดใช้ (ชนเวอร์ชัน)
- The Decision object
- The Policy object
- Endpoints (10)
- POSTตรวจสิทธิ์ 1 รายการ
- POSTตรวจสิทธิ์หลายรายการ (batch)
- GETรายการนโยบาย
- POSTสร้างนโยบาย
- GETดูนโยบาย
- PATCHแก้นโยบาย
- DELETEลบนโยบาย
- POSTจำลองนโยบายกับข้อมูลจริง
- GETบันทึกการตัดสินสิทธิ์
- GETอธิบายการตัดสิน 1 รายการ
- Webhooks (6)
- Errors
- Rate limit
- เวอร์ชันและ Changelog
- เครื่องมือ: ตัวจำลองการตรวจสิทธิ์
- KUNJAE ID
- Docs
- Identity API
- Authorization
KUNJAE Identity API · Authorization
Authorization API
ตัดสินสิทธิ์กลางของพนักงานทั้งเครือ MAYARIN — ถามว่า “ใคร ทำอะไร กับอะไร ได้ไหม” แล้วได้คำตอบ allow/deny ที่อธิบายได้ทุกขั้น ด้วยบทบาท (RBAC) + เงื่อนไขจริงทางธุรกิจ (ABAC: สาขา เวลา วงเงิน MFA)
- 10 endpoint
- 2 workflow
- 2 object
- 6 webhook event
- เวอร์ชัน
2026-08-01

ทำอะไร ใช้เมื่อไร
- ระบบหลังบ้านทุกตัว (POS ห้าง ระบบคำสั่งซื้อ ระบบแต้ม ประตูฟิตเนส พอร์ทัลพนักงาน) ใช้จุดตัดสินสิทธิ์เดียวกัน ไม่ต้องเขียน if-else ซ้ำในทุกระบบ
- นโยบายที่เปลี่ยนตามธุรกิจ (วงเงินคืนเงิน เวลาทำการ PDPA) แก้ที่เดียว มีผลทุกระบบภายใน 5 วินาที
- ผลอธิบายได้: เหตุผล นโยบายที่เข้า ลำดับการประเมิน — ตอบพนักงานและผู้ตรวจได้ว่า “ทำไมกดไม่ได้”
- ทดลองนโยบายกับข้อมูลจริงก่อนเปิด (simulate + dry_run) และค้นบันทึกการตัดสินย้อนหลัง 400 วัน
Base path
| path | ใช้ทำอะไร |
|---|---|
/v1/authz | ตรวจสิทธิ์ + บันทึกการตัดสิน |
/v1/policies | จัดการนโยบาย + จำลอง |
Base URL: https://api.kunjae.example · Sandbox https://sandbox.api.kunjae.example · หัวเลือกเวอร์ชัน Kunjae-Version: 2026-08-01
การยืนยันตัวตนและ scope
- check / check-batch: access token แบบ client_credentials ของระบบหลังบ้าน (scope authz:check) — ดูหน้า OAuth
- นโยบายและ audit: management key ของผู้ดูแล (kj_live_mgmt_…) หรือ token พนักงานที่มีสิทธิ์ settings:write / audit:read
| scope | ให้ทำอะไร | ใครใช้ |
|---|---|---|
authz:check | ถามสิทธิ์ (check / check-batch) | ระบบหลังบ้าน (Back-office Services) |
policies:read | ดูนโยบาย + จำลอง | ผู้ดูแล ผู้ตรวจ |
policies:write | สร้าง/แก้/ลบนโยบาย | เจ้าของแอป (ต้อง MFA) |
audit:read | อ่านบันทึกการตัดสิน | ผู้ตรวจ ทีมความปลอดภัย |
Object หลัก
เกี่ยวข้องกับโมดูลอื่น
- Role/Permission สิทธิ์ตั้งต้นมาจากบทบาทที่ผู้ใช้ถือ (รวมสิทธิ์ชั่วคราว) — นโยบายในหน้านี้ปฏิเสธเพิ่มตามเงื่อนไข
- OAuth ระบบหลังบ้านขอ token แบบ client_credentials (scope authz:check) ก่อนเรียก check
- Authentication context.mfa มาจากวิธียืนยันตัวตนของ session (amr) — งานอ่อนไหวต้องมี MFA
- JWT subject มาจาก claim sub ของ token พนักงานที่ตรวจแล้ว — ห้ามรับรหัสผู้ใช้จาก body ของลูกค้า
- ลำดับการประเมิน: ตรวจผู้ใช้ → ตรวจ action → นโยบาย deny (เรียง priority · deny ชนะเสมอ) → บทบาทที่ให้สิทธิ์ → ขอบเขตสาขา → ค่าเริ่มต้นปฏิเสธ
- เงินเป็นสตางค์ (integer) — 500000 = ฿5,000 · เวลา ISO 8601 +07:00
- deny ตอบ HTTP 200 (decision = deny) — HTTP 4xx ใช้กับคำขอที่ผิดรูปแบบเท่านั้น
- list ใช้ cursor (limit + starting_after) · แก้นโยบายต้องส่ง If-Match
- PDPA: บันทึกการตัดสินเก็บรหัสผู้ใช้ ไม่เก็บชื่อ/เบอร์ลูกค้า · สมาชิกอ้างด้วย member id เท่านั้น
Workflow — ลำดับการทำงานจริง (2 เรื่อง)
แต่ละเรื่องมี 2 มุมมอง: ภาพรวม อธิบายทีละขั้นสำหรับทุกคนในทีม และ Sequence กดเล่นทีละขั้นเพื่อดูคำขอ/ผลลัพธ์จริงของ endpoint ในหน้านี้ · ลูกศรประ = ไม่รอผล (webhook/คิว)
พนักงานขายคืนเงิน ฿6,890 → ถูกปฏิเสธ → ผู้จัดการสาขาอนุมัติ
คุณสุนิสา (พนักงานขาย สาขารัชดา) คืนเงินรองเท้าวิ่งให้ลูกค้า ยอดเกินวงเงิน ฿5,000 ระบบจึงปฏิเสธพร้อมเหตุผล แล้วคุณชยพล (ผู้จัดการ) สแกนบัตรอนุมัติ
- คุณสุนิสาพนักงานขาย รัชดา
- เครื่อง POSแคชเชียร์
- ระบบคำสั่งซื้อMAYARIN Mall
- KUNJAE Authz API
- บันทึกตรวจสอบ
- คุณชยพลผู้จัดการสาขา
- 1
คุณสุนิสา → เครื่อง POS
คืนเงินรองเท้าวิ่ง ord_MY2610_0193 ยอด ฿6,890
- 2
เครื่อง POS → ระบบคำสั่งซื้อ
POS ส่งคำขอคืนเงินพร้อม token ของคุณสุนิสา
- 3
ระบบคำสั่งซื้อ → KUNJAE Authz API
ระบบคำสั่งซื้อถามสิทธิ์ก่อนทำรายการ
POST /v1/authz/check - 4
KUNJAE Authz API → บันทึกตรวจสอบ
เก็บผลพร้อมลำดับการประเมิน
ไม่รอผล (async) - 5
KUNJAE Authz API → บันทึกตรวจสอบ
action ความเสี่ยงสูงถูกปฏิเสธ → ส่ง webhook ให้แดชบอร์ดความปลอดภัย
ไม่รอผล (async) - 6
ระบบคำสั่งซื้อ → เครื่อง POS
ตอบกลับพร้อม decision_id และข้อความ “ยอดเกิน ฿5,000 ต้องให้ผู้จัดการอนุมัติ”
- 7
เครื่อง POS → คุณชยพล
POS แสดงปุ่ม “ขออนุมัติ” แทนการปิดงาน
- 8
คุณชยพล → เครื่อง POS
ผู้จัดการยืนยันตัวตนด้วย passkey บนเครื่อง (MFA)
- 9
ระบบคำสั่งซื้อ → KUNJAE Authz API
ถามสิทธิ์ใหม่ในนามผู้จัดการ
POST /v1/authz/check - 10
ระบบคำสั่งซื้อ → ระบบคำสั่งซื้อ
ทำรายการคืนเงินและพิมพ์ใบลดหนี้
- 11
ระบบคำสั่งซื้อ → เครื่อง POS
POS แสดงสำเร็จ พร้อมชื่อผู้อนุมัติบนใบเสร็จ
ตัดสิน 2 ครั้งใช้เวลา 3 + 5 ms · คืนเงินเสร็จใน 2 นาที (10:05 → 10:07) · ทั้ง 2 ครั้งอยู่ใน audit พร้อมเหตุผล
กด เล่น หรือคลิกลูกศรในแผนภาพ เพื่อดูคำขอและผลลัพธ์ของแต่ละขั้น
1 คุณสุนิสา เครื่อง POS
สแกนใบเสร็จ กดคืนเงิน
คืนเงินรองเท้าวิ่ง ord_MY2610_0193 ยอด ฿6,890
2 เครื่อง POS ระบบคำสั่งซื้อ
POST /orders/…/refund
POS ส่งคำขอคืนเงินพร้อม token ของคุณสุนิสา
3 ระบบคำสั่งซื้อ KUNJAE Authz API
POST /v1/authz/check
ระบบคำสั่งซื้อถามสิทธิ์ก่อนทำรายการ
POST /v1/authz/check HTTP/1.1
Authorization: Bearer kj_test_mgmt_7Hd2Qx9LmP4w
{
"subject": {
"id": "usr_6Pb3"
},
"action": "orders:refund",
"resource": {
"type": "order",
"id": "ord_MY2610_0193",
"branch_id": "br_ratchada",
"amount": 689000
},
"context": {
"at": "2026-10-12T10:05:31+07:00",
"mfa": true,
"ip": "10.20.4.17"
},
"explain": true
}HTTP/1.1 200 OK
{
"id": "dec_175ZXKF",
"decision": "deny",
"reason": "policy_denied",
"policy_id": "pol_refund_limit",
"cache_ttl": 0
}4 KUNJAE Authz API บันทึกตรวจสอบasync
บันทึกการตัดสิน
เก็บผลพร้อมลำดับการประเมิน
5 KUNJAE Authz API บันทึกตรวจสอบasync
authz.decision.denied
action ความเสี่ยงสูงถูกปฏิเสธ → ส่ง webhook ให้แดชบอร์ดความปลอดภัย
6 ระบบคำสั่งซื้อ เครื่อง POS
403 access_denied
ตอบกลับพร้อม decision_id และข้อความ “ยอดเกิน ฿5,000 ต้องให้ผู้จัดการอนุมัติ”
HTTP/1.1 403 Forbidden
{
"error": {
"code": "access_denied",
"message": "ยอดคืนเงินเกิน ฿5,000 ต้องให้ผู้จัดการสาขาอนุมัติ",
"decision_id": "dec_175ZXKF",
"policy_id": "pol_refund_limit",
"request_id": "req_5Pq8Tz2Nw",
"doc_url": "https://docs.kunjae.example/errors#access_denied"
}
}7 เครื่อง POS คุณชยพล
เรียกผู้จัดการ
POS แสดงปุ่ม “ขออนุมัติ” แทนการปิดงาน
8 คุณชยพล เครื่อง POS
สแกนบัตร + passkey
ผู้จัดการยืนยันตัวตนด้วย passkey บนเครื่อง (MFA)
9 ระบบคำสั่งซื้อ KUNJAE Authz API
POST /v1/authz/check
ถามสิทธิ์ใหม่ในนามผู้จัดการ
POST /v1/authz/check HTTP/1.1
Authorization: Bearer kj_test_mgmt_7Hd2Qx9LmP4w
{
"subject": {
"id": "usr_8Hc2"
},
"action": "orders:refund",
"resource": {
"type": "order",
"id": "ord_MY2610_0193",
"branch_id": "br_ratchada",
"amount": 689000
},
"context": {
"at": "2026-10-12T10:07:02+07:00",
"mfa": true,
"ip": "10.20.4.17"
},
"explain": true
}HTTP/1.1 200 OK
{
"id": "dec_175ZXLG",
"decision": "allow",
"reason": "role_permission",
"granted_by": [
"branch_manager"
],
"cache_ttl": 60
}10 ระบบคำสั่งซื้อ ระบบคำสั่งซื้อ
คืนเงิน ฿6,890
ทำรายการคืนเงินและพิมพ์ใบลดหนี้
11 ระบบคำสั่งซื้อ เครื่อง POS
200 refunded
POS แสดงสำเร็จ พร้อมชื่อผู้อนุมัติบนใบเสร็จ
HTTP/1.1 200 OK
{
"order_id": "ord_MY2610_0193",
"status": "refunded",
"amount": 689000,
"approved_by": "usr_8Hc2"
}ตัดสิน 2 ครั้งใช้เวลา 3 + 5 ms · คืนเงินเสร็จใน 2 นาที (10:05 → 10:07) · ทั้ง 2 ครั้งอยู่ใน audit พร้อมเหตุผล
เพิ่มนโยบายใหม่อย่างปลอดภัย: จำลอง → dry_run → เปิดใช้ (ชนเวอร์ชัน)
คุณณัฐวุฒิ (เจ้าของแอป) อยากให้ void บิลทำได้เฉพาะผู้จัดการ — จำลองกับ log วันนี้ก่อน พบว่ากระทบหัวแคชเชียร์ชั่วคราว จึงเปิดแบบ dry_run แล้วแก้เงื่อนไขก่อนเปิดจริง
- คุณณัฐวุฒิเจ้าของแอป
- คอนโซลผู้ดูแลStaff Portal
- KUNJAE Authz API
- บันทึกตรวจสอบ
- คิว webhook
- ตัวแคชสิทธิ์ในระบบต่าง ๆPOS / คำสั่งซื้อ
- 1
คุณณัฐวุฒิ → คอนโซลผู้ดูแล
void บิลต้องเป็นผู้จัดการสาขา
- 2
คอนโซลผู้ดูแล → KUNJAE Authz API
จำลองกับการตัดสินจริงตั้งแต่เที่ยงคืน
POST /v1/policies/simulate - 3
KUNJAE Authz API → บันทึกตรวจสอบ
เล่นซ้ำทุกการตัดสินของวันนี้กับร่าง
- 4
KUNJAE Authz API → คอนโซลผู้ดูแล
การ void ของคุณวีรยุทธ (หัวแคชเชียร์ชั่วคราว) จะถูกปฏิเสธ
- 5
คอนโซลผู้ดูแล → KUNJAE Authz API
สร้างแบบทดลอง ไม่บังคับใช้ เก็บผลไว้ดู
POST /v1/policies - 6
KUNJAE Authz API → คิว webhook
แจ้งทุกระบบให้ล้างแคชนโยบาย
ไม่รอผล (async) - 7
คิว webhook → ตัวแคชสิทธิ์ในระบบต่าง ๆ
ตัวแคชในระบบต่าง ๆ โหลดนโยบายใหม่
ไม่รอผล (async) - 8
คอนโซลผู้ดูแล → KUNJAE Authz API
วันรุ่งขึ้นแก้เงื่อนไขให้ยกเว้นหัวแคชเชียร์ + เปิดใช้ แต่ใช้ ETag เก่า
PATCH /v1/policies/pol_void_manager - 9
คอนโซลผู้ดูแล → KUNJAE Authz API
โหลดเวอร์ชันล่าสุดแล้วแก้ซ้ำ
PATCH /v1/policies/pol_void_manager - 10
KUNJAE Authz API → คิว webhook
มีผลทุกระบบภายใน 5 วินาที
ไม่รอผล (async)
เห็นผลกระทบก่อนเปิดใช้ (1 รายการจาก 11 จะเปลี่ยนเป็นปฏิเสธ) · ไม่มีพนักงานถูกบล็อกระหว่างทดลอง · นโยบายมีผลทุกระบบภายใน 5 วินาทีหลังเปิด
กด เล่น หรือคลิกลูกศรในแผนภาพ เพื่อดูคำขอและผลลัพธ์ของแต่ละขั้น
1 คุณณัฐวุฒิ คอนโซลผู้ดูแล
เขียนร่างนโยบาย
void บิลต้องเป็นผู้จัดการสาขา
2 คอนโซลผู้ดูแล KUNJAE Authz API
POST /v1/policies/simulate
จำลองกับการตัดสินจริงตั้งแต่เที่ยงคืน
POST /v1/policies/simulate HTTP/1.1
Authorization: Bearer kj_test_mgmt_7Hd2Qx9LmP4w
{
"draft": {
"name": "void บิลต้องเป็นผู้จัดการสาขา",
"effect": "deny",
"priority": 45,
"actions": [
"orders:void"
],
"subjects": {
"roles": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "!('branch_manager' in subject.roles)"
}
},
"replay": {
"from": "2026-10-12T00:00:00+07:00",
"to": "2026-10-12T10:20:00+07:00"
}
}3 KUNJAE Authz API บันทึกตรวจสอบ
อ่าน 11 รายการ
เล่นซ้ำทุกการตัดสินของวันนี้กับร่าง
4 KUNJAE Authz API คอนโซลผู้ดูแล
200 เปลี่ยนผล 1
การ void ของคุณวีรยุทธ (หัวแคชเชียร์ชั่วคราว) จะถูกปฏิเสธ
HTTP/1.1 200 OK
{
"summary": {
"unchanged": 10,
"allow_to_deny": 1,
"deny_to_allow": 0
},
"changes": [
{
"decision_id": "dec_175ZXRM",
"at": "2026-10-12T10:18:26+07:00",
"subject": "usr_5Tz1",
"subject_name": "วีรยุทธ แสงทอง",
"action": "orders:void",
"resource_id": "ord_MY2610_0201",
"before": "allow",
"after": "deny",
"by_policy": "draft"
}
],
"warnings": [
"จะปฏิเสธ 1 รายการที่วันนี้อนุญาต — ผู้ถือบทบาท cashier_supervisor (ชั่วคราว) จะ void ไม่ได้"
]
}5 คอนโซลผู้ดูแล KUNJAE Authz API
POST /v1/policies (dry_run)
สร้างแบบทดลอง ไม่บังคับใช้ เก็บผลไว้ดู
POST /v1/policies HTTP/1.1
Idempotency-Key: 9b2e4f60-3c1a-4d7e-8f05-6a1b2c3d4e5f
{
"name": "void บิลต้องเป็นผู้จัดการสาขา",
"effect": "deny",
"priority": 45,
"actions": [
"orders:void"
],
"subjects": {
"roles": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "!('branch_manager' in subject.roles)"
},
"status": "dry_run"
}HTTP/1.1 201 Created
{
"id": "pol_void_manager",
"status": "dry_run",
"version": 1
}6 KUNJAE Authz API คิว webhookasync
policy.created
แจ้งทุกระบบให้ล้างแคชนโยบาย
7 คิว webhook ตัวแคชสิทธิ์ในระบบต่าง ๆasync
ล้างแคช
ตัวแคชในระบบต่าง ๆ โหลดนโยบายใหม่
8 คอนโซลผู้ดูแล KUNJAE Authz API
PATCH /v1/policies/pol_void_manager
วันรุ่งขึ้นแก้เงื่อนไขให้ยกเว้นหัวแคชเชียร์ + เปิดใช้ แต่ใช้ ETag เก่า
ผู้ดูแลอีกคนเพิ่งแก้คำอธิบาย — If-Match กันการเขียนทับ
PATCH /v1/policies/pol_void_manager HTTP/1.1
If-Match: W/"v1"
{
"status": "active"
}HTTP/1.1 409 Conflict
{
"error": {
"code": "policy_conflict",
"message": "นโยบายถูกแก้เป็นเวอร์ชัน 2 แล้ว กรุณาโหลดใหม่",
"current_version": 2,
"request_id": "req_5Pq8Tz2Nw",
"doc_url": "https://docs.kunjae.example/errors#policy_conflict"
}
}9 คอนโซลผู้ดูแล KUNJAE Authz API
GET + PATCH (If-Match v2)
โหลดเวอร์ชันล่าสุดแล้วแก้ซ้ำ
PATCH /v1/policies/pol_void_manager HTTP/1.1
If-Match: W/"v2"
{
"status": "active",
"condition": {
"language": "kel",
"expression": "!('branch_manager' in subject.roles) && !('cashier_supervisor' in subject.roles)"
}
}HTTP/1.1 200 OK
{
"id": "pol_void_manager",
"status": "active",
"version": 3
}10 KUNJAE Authz API คิว webhookasync
policy.updated
มีผลทุกระบบภายใน 5 วินาที
เห็นผลกระทบก่อนเปิดใช้ (1 รายการจาก 11 จะเปลี่ยนเป็นปฏิเสธ) · ไม่มีพนักงานถูกบล็อกระหว่างทดลอง · นโยบายมีผลทุกระบบภายใน 5 วินาทีหลังเปิด
Objects
The Decision object
ผลการตัดสิน 1 ครั้ง — ตัวอย่างคือคุณสุนิสาคืนเงิน ฿6,890 แล้วถูกนโยบายวงเงินปฏิเสธ (คำนวณจากตัวประเมินจริงของหน้านี้)
idstringไม่บังคับอ่านอย่างเดียวรหัสการตัดสิน (ใช้อ้างใน log/แจ้งผู้ใช้)
objectstringไม่บังคับอ่านอย่างเดียวชนิด object
decisionenumไม่บังคับอ่านอย่างเดียวผล
reasonenumไม่บังคับอ่านอย่างเดียวเหตุผลแบบเครื่องอ่าน
policy_idstringไม่บังคับอ่านอย่างเดียวนโยบายที่ทำให้ได้ผลนี้ (ถ้ามี)
granted_byarray<string>ไม่บังคับอ่านอย่างเดียวบทบาทที่ให้สิทธิ์ (เมื่อ allow)
subjectobjectไม่บังคับอ่านอย่างเดียวผู้ขอ + บทบาท ณ เวลานั้น
ฟิลด์ย่อยของ
subject(2)idstringไม่บังคับอ่านอย่างเดียวรหัส
rolesarray<string>ไม่บังคับอ่านอย่างเดียวบทบาทที่มีผล (รวมชั่วคราว)
actionstringไม่บังคับอ่านอย่างเดียวaction
resourceobjectไม่บังคับอ่านอย่างเดียวทรัพยากร (ตามที่ส่งมา)
contextobjectไม่บังคับอ่านอย่างเดียวสภาพแวดล้อม
stepsarray<object>ไม่บังคับอ่านอย่างเดียวลำดับการประเมิน (เมื่อ explain = true)
ฟิลด์ย่อยของ
steps(4)nintegerไม่บังคับอ่านอย่างเดียวลำดับ
namestringไม่บังคับอ่านอย่างเดียวขั้น
resultenumไม่บังคับอ่านอย่างเดียวผลขั้นนี้
detailstringไม่บังคับอ่านอย่างเดียวคำอธิบายภาษาไทย
cache_ttlintegerไม่บังคับอ่านอย่างเดียววินาทีที่ฝั่งคุณแคชผลได้ (deny = 0 ไม่แคช)
client_idstringไม่บังคับอ่านอย่างเดียวclient ที่ถาม
latency_msintegerไม่บังคับอ่านอย่างเดียวเวลาที่ใช้ตัดสิน
created_atstring · date-timeไม่บังคับอ่านอย่างเดียวเวลาตัดสิน
{
"id": "dec_175ZXKF",
"object": "authz_decision",
"decision": "deny",
"reason": "policy_denied",
"policy_id": "pol_refund_limit",
"granted_by": [],
"subject": {
"id": "usr_6Pb3",
"roles": [
"sales_staff"
]
},
"action": "orders:refund",
"resource": {
"type": "order",
"id": "ord_MY2610_0193",
"branch_id": "br_ratchada",
"amount": 689000
},
"context": {
"at": "2026-10-12T10:05:31+07:00",
"mfa": true,
"ip": "10.20.4.17"
},
"steps": [
{
"n": 1,
"name": "ตรวจผู้ใช้",
"result": "pass",
"detail": "สุนิสา ใจดี · บทบาท ณ 12 ต.ค. 69 10:05 น.: sales_staff"
},
{
"n": 2,
"name": "ตรวจ action",
"result": "pass",
"detail": "orders:refund (คืนเงินคำสั่งซื้อ · ความเสี่ยง high)"
},
{
"n": 3,
"name": "นโยบายปฏิเสธ pol_auditor_readonly",
"result": "skip",
"detail": "ไม่เข้าเงื่อนไข (ผู้ตรวจอ่านได้อย่างเดียว)"
},
{
"n": 4,
"name": "นโยบายปฏิเสธ pol_pii_sales_deny",
"result": "skip",
"detail": "ไม่เข้าเงื่อนไข (พนักงานขายห้ามดูข้อมูลส่วนบุคคลเต็ม)"
},
{
"n": 5,
"name": "นโยบายปฏิเสธ pol_mfa_sensitive",
"result": "skip",
"detail": "ไม่เข้าเงื่อนไข (งานอ่อนไหวต้องยืนยัน 2 ขั้น)"
},
{
"n": 6,
"name": "นโยบายปฏิเสธ pol_business_hours",
"result": "skip",
"detail": "ไม่เข้าเงื่อนไข (คืนเงิน/void เฉพาะเวลาทำการ 10:00–22:00)"
},
{
"n": 7,
"name": "นโยบายปฏิเสธ pol_refund_limit",
"result": "match",
"detail": "เข้าเงื่อนไข: resource.amount > 500000 && !(subject.roles.hasAny(['branch_manager', 'cashier_supervisor', 'app_owner']))"
}
],
"cache_ttl": 0,
"client_id": "kjc_backoffice_2Mn6",
"latency_ms": 3,
"created_at": "2026-10-12T10:05:31+07:00"
}
The Policy object
กฎเงื่อนไขที่ประเมินเพิ่มจากบทบาท — deny ชนะ allow เสมอ · นิพจน์เขียนด้วย KEL (ไวยากรณ์คล้าย CEL)
idstringไม่บังคับอ่านอย่างเดียวรหัสนโยบาย
objectstringไม่บังคับอ่านอย่างเดียวชนิด object
namestringจำเป็นชื่อ (ไม่ซ้ำ)
descriptionstringไม่บังคับคำอธิบาย/ที่มา (เช่น อ้างระเบียบ)
effectenumจำเป็นผลเมื่อเงื่อนไขเป็นจริง
priorityintegerจำเป็นลำดับประเมิน (น้อย = ก่อน) · deny ชนะ allow เสมอ
statusenumไม่บังคับสถานะ — dry_run = ประเมินและบันทึกผล แต่ไม่บังคับใช้
actionsarray<string>จำเป็นaction ที่นโยบายนี้ครอบคลุม (* = ทั้งหมด)
subjectsobjectไม่บังคับใช้กับใคร
ฟิลด์ย่อยของ
subjects(2)rolesarray<string>ไม่บังคับบทบาท (* = ทุกคน)
usersarray<string>ไม่บังคับผู้ใช้เฉพาะราย
resourcesobjectไม่บังคับทรัพยากรที่ครอบคลุม
ฟิลด์ย่อยของ
resources(2)typesarray<string>ไม่บังคับชนิด
branch_idsarray<string>ไม่บังคับสาขา (* = ทุกสาขา)
conditionobjectจำเป็นเงื่อนไข (KUNJAE Expression Language — คล้าย CEL)
ฟิลด์ย่อยของ
condition(2)languageenumไม่บังคับภาษา
expressionstringจำเป็นนิพจน์ ใช้ subject.* action resource.* context.* ได้
versionintegerไม่บังคับอ่านอย่างเดียวเวอร์ชัน (เพิ่มทุกครั้งที่แก้ · ใช้กับ ETag)
systembooleanไม่บังคับอ่านอย่างเดียวนโยบายระบบ (ลบไม่ได้)
statsobjectไม่บังคับอ่านอย่างเดียวสถิติวันนี้ (คำนวณจาก audit)
ฟิลด์ย่อยของ
stats(2)evaluated_todayintegerไม่บังคับอ่านอย่างเดียวจำนวนการตรวจทั้งหมด
denied_todayintegerไม่บังคับอ่านอย่างเดียวปฏิเสธโดยนโยบายนี้
created_bystringไม่บังคับอ่านอย่างเดียวผู้สร้าง
created_atstring · date-timeไม่บังคับอ่านอย่างเดียวสร้างเมื่อ
updated_atstring · date-timeไม่บังคับอ่านอย่างเดียวแก้ล่าสุด
{
"id": "pol_refund_limit",
"object": "policy",
"name": "คืนเงินเกิน ฿5,000 ต้องเป็นผู้จัดการ/หัวหน้าแคชเชียร์",
"description": "ยอดคืนเงิน (สตางค์) เกิน 500000 ต้องมีบทบาท branch_manager หรือ cashier_supervisor",
"effect": "deny",
"priority": 50,
"status": "active",
"actions": [
"orders:refund"
],
"subjects": {
"roles": [
"*"
],
"users": []
},
"resources": {
"types": [
"*"
],
"branch_ids": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "resource.amount > 500000 && !(subject.roles.hasAny(['branch_manager', 'cashier_supervisor', 'app_owner']))"
},
"version": 5,
"system": false,
"stats": {
"evaluated_today": 11,
"denied_today": 1
},
"created_by": "usr_1Ae5",
"created_at": "2024-05-20T10:00:00+07:00",
"updated_at": "2026-08-18T09:30:00+07:00"
}Endpoints (10)
| Method | Path | ทำอะไร | สิทธิ์ |
|---|---|---|---|
| POST | /v1/authz/check | ตรวจสิทธิ์ 1 รายการ | authz:check |
| POST | /v1/authz/check-batch | ตรวจสิทธิ์หลายรายการ (batch) | authz:check |
| GET | /v1/policies | รายการนโยบาย | policies:read |
| POST | /v1/policies | สร้างนโยบาย | policies:write |
| GET | /v1/policies/{policy_id} | ดูนโยบาย | policies:read |
| PATCH | /v1/policies/{policy_id} | แก้นโยบาย | policies:write |
| DELETE | /v1/policies/{policy_id} | ลบนโยบาย | policies:write |
| POST | /v1/policies/simulate | จำลองนโยบายกับข้อมูลจริง | policies:read |
| GET | /v1/authz/audit | บันทึกการตัดสินสิทธิ์ | audit:read |
| GET | /v1/authz/audit/{decision_id} | อธิบายการตัดสิน 1 รายการใหม่ | audit:read |
ตรวจสิทธิ์ 1 รายการ
ถามว่า “ผู้ใช้คนนี้ ทำ action นี้ กับทรัพยากรนี้ ได้ไหม” — ตอบ allow/deny พร้อมเหตุผล นโยบายที่เข้า และลำดับการประเมิน (explain) · ระบบหลังบ้านของเครือ (POS, ระบบคำสั่งซื้อ, พอร์ทัลพนักงาน) เรียกก่อนทำงานสำคัญทุกครั้ง
Request bodyapplication/json
subjectobjectจำเป็นผู้ขอ (ผู้ใช้หรือ client)
ฟิลด์ย่อยของ
subject(2)idstringจำเป็นรหัสผู้ใช้/รหัส client
typeenumไม่บังคับชนิด
actionstringจำเป็นสิทธิ์รูปแบบ resource:action
resourceobjectจำเป็นทรัพยากรที่จะทำ
ฟิลด์ย่อยของ
resource(4)typestringจำเป็นชนิด
idstringจำเป็นรหัส
branch_idstringไม่บังคับสาขาของทรัพยากร (ใช้ตรวจขอบเขต)
amountintegerไม่บังคับยอดเงิน (สตางค์) — ใช้กับนโยบายวงเงิน
contextobjectไม่บังคับสภาพแวดล้อม (ABAC) — ไม่ส่ง = ใช้เวลาปัจจุบัน + ค่าจาก token
ฟิลด์ย่อยของ
context(3)atstring · date-timeไม่บังคับเวลาที่ทำ
mfabooleanไม่บังคับsession มี MFA หรือไม่ (amr)
ipstring · ipv4ไม่บังคับIP ของเครื่อง POS
explainbooleanไม่บังคับส่งลำดับการประเมิน (steps) กลับมาด้วย
ผลลัพธ์
- 200ปฏิเสธ — ยอดเกิน ฿5,000 (ผลปกติ ไม่ใช่ error)
- 200อนุญาต — ผู้จัดการสาขารัชดาทำรายการเดียวกัน
- 422ไม่รู้จัก action
- 400
invalid_requestคำขอไม่ถูกต้อง - 401
unauthorizedไม่ได้ยืนยันตัวตน - 401
token_expiredtoken หมดอายุ - 403
insufficient_scopescope ไม่พอ - 422
unknown_actionไม่รู้จัก action - 429
rate_limitedเรียกเกินโควตา - 503
service_unavailableปิดปรับปรุงชั่วคราว
- deny ตอบ 200 เสมอ (การตรวจทำงานปกติ) — ระบบของคุณเป็นคนตอบ 403 access_denied ให้ผู้ใช้พร้อม decision_id
- ผล allow แคชได้ตาม cache_ttl (60 วินาที) · ผล deny ห้ามแคช
- ถ้า KUNJAE ไม่ตอบใน 150 ms ให้ถือเป็น deny (fail closed) สำหรับ action ความเสี่ยงสูง
- เวลาเฉลี่ย 2–6 ms ในเครือข่ายภายใน
curl -X POST "https://sandbox.api.kunjae.example/v1/authz/check" \
-H "Authorization: Bearer kj_test_mgmt_7Hd2Qx9LmP4w" \
-H "Content-Type: application/json" \
-H "Kunjae-Version: 2026-08-01" \
-d '{
"subject": {
"id": "usr_6Pb3"
},
"action": "orders:refund",
"resource": {
"type": "order",
"id": "ord_MY2610_0193",
"branch_id": "br_ratchada",
"amount": 689000
},
"context": {
"at": "2026-10-12T10:05:31+07:00",
"mfa": true,
"ip": "10.20.4.17"
},
"explain": true
}'const API_KEY = process.env.API_KEY; // kj_test_mgmt_7Hd2Qx9LmP4w
const res = await fetch('https://sandbox.api.kunjae.example/v1/authz/check', {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
'Kunjae-Version': '2026-08-01',
},
body: JSON.stringify({
"subject": {
"id": "usr_6Pb3"
},
"action": "orders:refund",
"resource": {
"type": "order",
"id": "ord_MY2610_0193",
"branch_id": "br_ratchada",
"amount": 689000
},
"context": {
"at": "2026-10-12T10:05:31+07:00",
"mfa": true,
"ip": "10.20.4.17"
},
"explain": true
}),
});
if (!res.ok) throw new Error((await res.json()).error.code);
const data = await res.json();
console.log(data);import os
import requests
API_KEY = os.environ["API_KEY"] # kj_test_mgmt_7Hd2Qx9LmP4w
res = requests.post(
"https://sandbox.api.kunjae.example/v1/authz/check",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
"Kunjae-Version": "2026-08-01",
},
json={
"subject": {
"id": "usr_6Pb3"
},
"action": "orders:refund",
"resource": {
"type": "order",
"id": "ord_MY2610_0193",
"branch_id": "br_ratchada",
"amount": 689000
},
"context": {
"at": "2026-10-12T10:05:31+07:00",
"mfa": True,
"ip": "10.20.4.17"
},
"explain": True
},
timeout=10,
)
res.raise_for_status()
print(res.json()){
"id": "dec_175ZXKF",
"object": "authz_decision",
"decision": "deny",
"reason": "policy_denied",
"policy_id": "pol_refund_limit",
"granted_by": [],
"subject": {
"id": "usr_6Pb3",
"roles": [
"sales_staff"
]
},
"action": "orders:refund",
"resource": {
"type": "order",
"id": "ord_MY2610_0193",
"branch_id": "br_ratchada",
"amount": 689000
},
"context": {
"at": "2026-10-12T10:05:31+07:00",
"mfa": true,
"ip": "10.20.4.17"
},
"steps": [
{
"n": 1,
"name": "ตรวจผู้ใช้",
"result": "pass",
"detail": "สุนิสา ใจดี · บทบาท ณ 12 ต.ค. 69 10:05 น.: sales_staff"
},
{
"n": 2,
"name": "ตรวจ action",
"result": "pass",
"detail": "orders:refund (คืนเงินคำสั่งซื้อ · ความเสี่ยง high)"
},
{
"n": 3,
"name": "นโยบายปฏิเสธ pol_auditor_readonly",
"result": "skip",
"detail": "ไม่เข้าเงื่อนไข (ผู้ตรวจอ่านได้อย่างเดียว)"
},
{
"n": 4,
"name": "นโยบายปฏิเสธ pol_pii_sales_deny",
"result": "skip",
"detail": "ไม่เข้าเงื่อนไข (พนักงานขายห้ามดูข้อมูลส่วนบุคคลเต็ม)"
},
{
"n": 5,
"name": "นโยบายปฏิเสธ pol_mfa_sensitive",
"result": "skip",
"detail": "ไม่เข้าเงื่อนไข (งานอ่อนไหวต้องยืนยัน 2 ขั้น)"
},
{
"n": 6,
"name": "นโยบายปฏิเสธ pol_business_hours",
"result": "skip",
"detail": "ไม่เข้าเงื่อนไข (คืนเงิน/void เฉพาะเวลาทำการ 10:00–22:00)"
},
{
"n": 7,
"name": "นโยบายปฏิเสธ pol_refund_limit",
"result": "match",
"detail": "เข้าเงื่อนไข: resource.amount > 500000 && !(subject.roles.hasAny(['branch_manager', 'cashier_supervisor', 'app_owner']))"
}
],
"cache_ttl": 0,
"client_id": "kjc_backoffice_2Mn6",
"latency_ms": 3,
"created_at": "2026-10-12T10:05:31+07:00"
}{
"error": {
"code": "unknown_action",
"message": "ไม่รู้จัก action \"orders:refunds\"",
"param": "action",
"suggestion": "orders:refund",
"request_id": "req_5Pq8Tz2Nw",
"doc_url": "https://docs.kunjae.example/errors#unknown_action"
}
}{
"error": {
"code": "invalid_request",
"message": "คำขอไม่ถูกต้อง",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#invalid_request"
}
}{
"error": {
"code": "unauthorized",
"message": "ไม่ได้ยืนยันตัวตน",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#unauthorized"
}
}{
"error": {
"code": "token_expired",
"message": "token หมดอายุ",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#token_expired"
}
}{
"error": {
"code": "insufficient_scope",
"message": "scope ไม่พอ",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#insufficient_scope"
}
}{
"error": {
"code": "unknown_action",
"message": "ไม่รู้จัก action",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#unknown_action"
}
}{
"error": {
"code": "rate_limited",
"message": "เรียกเกินโควตา",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#rate_limited"
}
}{
"error": {
"code": "service_unavailable",
"message": "ปิดปรับปรุงชั่วคราว",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#service_unavailable"
}
}ตรวจสิทธิ์หลายรายการ (batch)
ตรวจหลายรายการในคำขอเดียว — ใช้ตอนวาดหน้าจอ เพื่อรู้ว่าปุ่มไหนควรแสดง/ปิด (เช่น หน้าบิลของ POS) · แต่ละรายการได้ผลแยกกัน ลำดับตรงกับที่ส่ง
Request bodyapplication/json
subjectobjectจำเป็นผู้ขอ (ผู้ใช้หรือ client)
ฟิลด์ย่อยของ
subject(2)idstringจำเป็นรหัสผู้ใช้/รหัส client
typeenumไม่บังคับชนิด
checksarray<object>จำเป็นรายการที่จะตรวจ (สูงสุด 100)
ฟิลด์ย่อยของ
checks(3)keystringไม่บังคับชื่ออ้างอิงของคุณ (เช่น id ปุ่ม)
actionstringจำเป็นaction
resourceobjectจำเป็นทรัพยากร
contextobjectไม่บังคับสภาพแวดล้อม (ABAC) — ไม่ส่ง = ใช้เวลาปัจจุบัน + ค่าจาก token
ฟิลด์ย่อยของ
context(3)atstring · date-timeไม่บังคับเวลาที่ทำ
mfabooleanไม่บังคับsession มี MFA หรือไม่ (amr)
ipstring · ipv4ไม่บังคับIP ของเครื่อง POS
ผลลัพธ์
- 200ผล 5 รายการ (อนุญาต 3)
- 413เกิน 100 รายการ
- 400
invalid_requestคำขอไม่ถูกต้อง - 401
unauthorizedไม่ได้ยืนยันตัวตน - 403
insufficient_scopescope ไม่พอ - 422
unknown_actionไม่รู้จัก action - 413
batch_too_largebatch ใหญ่เกิน - 429
rate_limitedเรียกเกินโควตา
- ใช้ซ่อนปุ่มเพื่อประสบการณ์ที่ดี แต่ต้องตรวจซ้ำด้วย authz/check ตอนกดจริงเสมอ (สิทธิ์อาจเปลี่ยนระหว่างนั้น)
- action ที่ไม่รู้จักในรายการใดรายการหนึ่งได้ reason = unknown_action เฉพาะรายการนั้น ไม่ทำให้ทั้งชุดล้ม
curl -X POST "https://sandbox.api.kunjae.example/v1/authz/check-batch" \
-H "Authorization: Bearer kj_test_mgmt_7Hd2Qx9LmP4w" \
-H "Content-Type: application/json" \
-H "Kunjae-Version: 2026-08-01" \
-d '{
"subject": {
"id": "usr_5Tz1"
},
"checks": [
{
"key": "btn_1",
"action": "orders:read",
"resource": {
"type": "order",
"id": "ord_MY2610_0201",
"branch_id": "br_bangna",
"amount": 845000
}
},
{
"key": "btn_2",
"action": "orders:refund",
"resource": {
"type": "order",
"id": "ord_MY2610_0201",
"branch_id": "br_bangna",
"amount": 845000
}
},
{
"key": "btn_3",
"action": "orders:void",
"resource": {
"type": "order",
"id": "ord_MY2610_0201",
"branch_id": "br_bangna",
"amount": 845000
}
},
{
"key": "btn_4",
"action": "members:pii.read",
"resource": {
"type": "member",
"id": "MBR-0048213"
}
},
{
"key": "btn_5",
"action": "reports:export",
"resource": {
"type": "report",
"id": "rpt_daily_bangna",
"branch_id": "br_bangna"
}
}
],
"context": {
"at": "2026-10-12T10:20:00+07:00",
"mfa": false
}
}'const API_KEY = process.env.API_KEY; // kj_test_mgmt_7Hd2Qx9LmP4w
const res = await fetch('https://sandbox.api.kunjae.example/v1/authz/check-batch', {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
'Kunjae-Version': '2026-08-01',
},
body: JSON.stringify({
"subject": {
"id": "usr_5Tz1"
},
"checks": [
{
"key": "btn_1",
"action": "orders:read",
"resource": {
"type": "order",
"id": "ord_MY2610_0201",
"branch_id": "br_bangna",
"amount": 845000
}
},
{
"key": "btn_2",
"action": "orders:refund",
"resource": {
"type": "order",
"id": "ord_MY2610_0201",
"branch_id": "br_bangna",
"amount": 845000
}
},
{
"key": "btn_3",
"action": "orders:void",
"resource": {
"type": "order",
"id": "ord_MY2610_0201",
"branch_id": "br_bangna",
"amount": 845000
}
},
{
"key": "btn_4",
"action": "members:pii.read",
"resource": {
"type": "member",
"id": "MBR-0048213"
}
},
{
"key": "btn_5",
"action": "reports:export",
"resource": {
"type": "report",
"id": "rpt_daily_bangna",
"branch_id": "br_bangna"
}
}
],
"context": {
"at": "2026-10-12T10:20:00+07:00",
"mfa": false
}
}),
});
if (!res.ok) throw new Error((await res.json()).error.code);
const data = await res.json();
console.log(data);import os
import requests
API_KEY = os.environ["API_KEY"] # kj_test_mgmt_7Hd2Qx9LmP4w
res = requests.post(
"https://sandbox.api.kunjae.example/v1/authz/check-batch",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
"Kunjae-Version": "2026-08-01",
},
json={
"subject": {
"id": "usr_5Tz1"
},
"checks": [
{
"key": "btn_1",
"action": "orders:read",
"resource": {
"type": "order",
"id": "ord_MY2610_0201",
"branch_id": "br_bangna",
"amount": 845000
}
},
{
"key": "btn_2",
"action": "orders:refund",
"resource": {
"type": "order",
"id": "ord_MY2610_0201",
"branch_id": "br_bangna",
"amount": 845000
}
},
{
"key": "btn_3",
"action": "orders:void",
"resource": {
"type": "order",
"id": "ord_MY2610_0201",
"branch_id": "br_bangna",
"amount": 845000
}
},
{
"key": "btn_4",
"action": "members:pii.read",
"resource": {
"type": "member",
"id": "MBR-0048213"
}
},
{
"key": "btn_5",
"action": "reports:export",
"resource": {
"type": "report",
"id": "rpt_daily_bangna",
"branch_id": "br_bangna"
}
}
],
"context": {
"at": "2026-10-12T10:20:00+07:00",
"mfa": False
}
},
timeout=10,
)
res.raise_for_status()
print(res.json()){
"object": "authz_batch",
"subject": {
"id": "usr_5Tz1",
"roles": [
"sales_staff",
"cashier_supervisor"
]
},
"results": [
{
"key": "btn_1",
"action": "orders:read",
"decision": "allow",
"reason": "role_permission",
"policy_id": null
},
{
"key": "btn_2",
"action": "orders:refund",
"decision": "allow",
"reason": "role_permission",
"policy_id": null
},
{
"key": "btn_3",
"action": "orders:void",
"decision": "allow",
"reason": "role_permission",
"policy_id": null
},
{
"key": "btn_4",
"action": "members:pii.read",
"decision": "deny",
"reason": "policy_denied",
"policy_id": "pol_pii_sales_deny"
},
{
"key": "btn_5",
"action": "reports:export",
"decision": "deny",
"reason": "policy_denied",
"policy_id": "pol_mfa_sensitive"
}
],
"summary": {
"total": 5,
"allow": 3,
"deny": 2
},
"evaluated_at": "2026-10-12T10:20:00+07:00"
}{
"error": {
"code": "batch_too_large",
"message": "ส่งได้ไม่เกิน 100 รายการต่อคำขอ",
"max": 100,
"got": 140,
"request_id": "req_5Pq8Tz2Nw",
"doc_url": "https://docs.kunjae.example/errors#batch_too_large"
}
}{
"error": {
"code": "invalid_request",
"message": "คำขอไม่ถูกต้อง",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#invalid_request"
}
}{
"error": {
"code": "unauthorized",
"message": "ไม่ได้ยืนยันตัวตน",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#unauthorized"
}
}{
"error": {
"code": "insufficient_scope",
"message": "scope ไม่พอ",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#insufficient_scope"
}
}{
"error": {
"code": "unknown_action",
"message": "ไม่รู้จัก action",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#unknown_action"
}
}{
"error": {
"code": "batch_too_large",
"message": "batch ใหญ่เกิน",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#batch_too_large"
}
}{
"error": {
"code": "rate_limited",
"message": "เรียกเกินโควตา",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#rate_limited"
}
}รายการนโยบาย
นโยบายทั้งหมดของเครือ เรียงตาม priority — กรองตามผล สถานะ action หรือบทบาท
Query parametersquery
effectenumไม่บังคับกรองผล
statusenumไม่บังคับกรองสถานะ
actionstringไม่บังคับนโยบายที่ครอบคลุม action นี้
rolestringไม่บังคับนโยบายที่ใช้กับบทบาทนี้
limitintegerไม่บังคับจำนวนต่อหน้า
starting_afterstringไม่บังคับcursor — ส่งรหัสของรายการสุดท้ายในหน้าก่อน เพื่อดึงหน้าถัดไป
ending_beforestringไม่บังคับcursor — ส่งรหัสของรายการแรก เพื่อดึงหน้าก่อนหน้า
sortstringไม่บังคับเรียงลำดับ (ใส่ - ข้างหน้า = มากไปน้อย)
ผลลัพธ์
- 200นโยบาย 6 รายการ
- 401
unauthorizedไม่ได้ยืนยันตัวตน - 403
insufficient_scopescope ไม่พอ - 429
rate_limitedเรียกเกินโควตา
curl -X GET "https://sandbox.api.kunjae.example/v1/policies?status=active&action=orders%3Arefund&limit=20" \
-H "Authorization: Bearer kj_test_mgmt_7Hd2Qx9LmP4w" \
-H "Kunjae-Version: 2026-08-01"const API_KEY = process.env.API_KEY; // kj_test_mgmt_7Hd2Qx9LmP4w
const res = await fetch('https://sandbox.api.kunjae.example/v1/policies?status=active&action=orders%3Arefund&limit=20', {
method: 'GET',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Kunjae-Version': '2026-08-01',
},
});
if (!res.ok) throw new Error((await res.json()).error.code);
const data = await res.json();
console.log(data);import os
import requests
API_KEY = os.environ["API_KEY"] # kj_test_mgmt_7Hd2Qx9LmP4w
res = requests.get(
"https://sandbox.api.kunjae.example/v1/policies?status=active&action=orders%3Arefund&limit=20",
headers={
"Authorization": f"Bearer {API_KEY}",
"Kunjae-Version": "2026-08-01",
},
timeout=10,
)
res.raise_for_status()
print(res.json()){
"object": "list",
"url": "/v1/policies",
"has_more": false,
"next_cursor": null,
"data": [
{
"id": "pol_auditor_readonly",
"object": "policy",
"name": "ผู้ตรวจอ่านได้อย่างเดียว",
"description": "ผู้ถือบทบาท auditor ทำได้เฉพาะ action ที่ลงท้าย :read",
"effect": "deny",
"priority": 10,
"status": "active",
"actions": [
"*"
],
"subjects": {
"roles": [
"auditor"
],
"users": []
},
"resources": {
"types": [
"*"
],
"branch_ids": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "'auditor' in subject.roles && !action.endsWith(':read')"
},
"version": 1,
"system": false,
"stats": {
"evaluated_today": 11,
"denied_today": 1
},
"created_by": "usr_1Ae5",
"created_at": "2025-06-01T09:00:00+07:00",
"updated_at": "2025-06-01T09:00:00+07:00"
},
{
"id": "pol_pii_sales_deny",
"object": "policy",
"name": "พนักงานขายห้ามดูข้อมูลส่วนบุคคลเต็ม",
"description": "ตาม PDPA — พนักงานขายเห็นเบอร์/อีเมลแบบปิดบังเท่านั้น",
"effect": "deny",
"priority": 20,
"status": "active",
"actions": [
"members:pii.read",
"members:pii.export"
],
"subjects": {
"roles": [
"sales_staff"
],
"users": []
},
"resources": {
"types": [
"*"
],
"branch_ids": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "'sales_staff' in subject.roles && !('branch_manager' in subject.roles)"
},
"version": 3,
"system": false,
"stats": {
"evaluated_today": 11,
"denied_today": 1
},
"created_by": "usr_1Ae5",
"created_at": "2024-05-20T10:00:00+07:00",
"updated_at": "2026-02-14T11:00:00+07:00"
},
{
"id": "pol_mfa_sensitive",
"object": "policy",
"name": "งานอ่อนไหวต้องยืนยัน 2 ขั้น",
"description": "มอบบทบาท ส่งออกรายงาน/ข้อมูลส่วนบุคคล อนุมัติบัญชี ต้องมี MFA ใน session",
"effect": "deny",
"priority": 30,
"status": "active",
"actions": [
"roles:assign",
"reports:export",
"members:pii.export",
"ledger:approve"
],
"subjects": {
"roles": [
"*"
],
"users": []
},
"resources": {
"types": [
"*"
],
"branch_ids": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "!context.mfa"
},
"version": 4,
"system": false,
"stats": {
"evaluated_today": 11,
"denied_today": 1
},
"created_by": "usr_1Ae5",
"created_at": "2025-03-01T09:00:00+07:00",
"updated_at": "2026-08-01T09:00:00+07:00"
},
{
"id": "pol_business_hours",
"object": "policy",
"name": "คืนเงิน/void เฉพาะเวลาทำการ 10:00–22:00",
"description": "พนักงานหน้าร้าน (ไม่ใช่ผู้จัดการ) คืนเงิน void บิล คืนตั๋ว ได้เฉพาะเวลาห้างเปิด",
"effect": "deny",
"priority": 40,
"status": "active",
"actions": [
"orders:refund",
"orders:void",
"tickets:refund"
],
"subjects": {
"roles": [
"sales_staff",
"cashier_supervisor"
],
"users": []
},
"resources": {
"types": [
"*"
],
"branch_ids": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "!('branch_manager' in subject.roles) && (time(context.at) < '10:00' || time(context.at) >= '22:00')"
},
"version": 2,
"system": false,
"stats": {
"evaluated_today": 11,
"denied_today": 1
},
"created_by": "usr_1Ae5",
"created_at": "2025-11-02T10:00:00+07:00",
"updated_at": "2026-10-12T09:58:00+07:00"
},
{
"id": "pol_refund_limit",
"object": "policy",
"name": "คืนเงินเกิน ฿5,000 ต้องเป็นผู้จัดการ/หัวหน้าแคชเชียร์",
"description": "ยอดคืนเงิน (สตางค์) เกิน 500000 ต้องมีบทบาท branch_manager หรือ cashier_supervisor",
"effect": "deny",
"priority": 50,
"status": "active",
"actions": [
"orders:refund"
],
"subjects": {
"roles": [
"*"
],
"users": []
},
"resources": {
"types": [
"*"
],
"branch_ids": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "resource.amount > 500000 && !(subject.roles.hasAny(['branch_manager', 'cashier_supervisor', 'app_owner']))"
},
"version": 5,
"system": false,
"stats": {
"evaluated_today": 11,
"denied_today": 1
},
"created_by": "usr_1Ae5",
"created_at": "2024-05-20T10:00:00+07:00",
"updated_at": "2026-08-18T09:30:00+07:00"
},
{
"id": "pol_branch_scope",
"object": "policy",
"name": "บทบาทระดับสาขาใช้ได้เฉพาะสาขาตัวเอง",
"description": "บทบาทที่ scope = branch ให้สิทธิ์เฉพาะทรัพยากรที่ branch_id อยู่ในสาขาที่ได้รับมอบ",
"effect": "allow",
"priority": 100,
"status": "active",
"actions": [
"*"
],
"subjects": {
"roles": [
"*"
],
"users": []
},
"resources": {
"types": [
"*"
],
"branch_ids": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "role.scope == 'global' || resource.branch_id == null || resource.branch_id in assignment.branch_ids"
},
"version": 1,
"system": true,
"stats": {
"evaluated_today": 11,
"denied_today": 1
},
"created_by": "usr_system",
"created_at": "2024-02-01T09:00:00+07:00",
"updated_at": "2024-02-01T09:00:00+07:00"
}
]
}{
"error": {
"code": "unauthorized",
"message": "ไม่ได้ยืนยันตัวตน",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#unauthorized"
}
}{
"error": {
"code": "insufficient_scope",
"message": "scope ไม่พอ",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#insufficient_scope"
}
}{
"error": {
"code": "rate_limited",
"message": "เรียกเกินโควตา",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#rate_limited"
}
}สร้างนโยบาย
เพิ่มนโยบายใหม่ — แนะนำสร้างเป็น dry_run ก่อน (ประเมินและบันทึกผลแต่ไม่บังคับ) แล้วดูรายงาน 1–7 วันค่อยเปลี่ยนเป็น active
Headersheader
Idempotency-Keystring · uuidไม่บังคับUUID ต่อคำขอ — ส่งซ้ำด้วยค่าเดิมภายใน 24 ชม. จะได้ผลเดิมโดยไม่สร้างซ้ำ
Request bodyapplication/json
namestringจำเป็นชื่อ (ไม่ซ้ำ)
descriptionstringไม่บังคับคำอธิบาย/ที่มา (เช่น อ้างระเบียบ)
effectenumจำเป็นผลเมื่อเงื่อนไขเป็นจริง
priorityintegerจำเป็นลำดับประเมิน (น้อย = ก่อน) · deny ชนะ allow เสมอ
statusenumไม่บังคับสถานะ — dry_run = ประเมินและบันทึกผล แต่ไม่บังคับใช้
actionsarray<string>จำเป็นaction ที่นโยบายนี้ครอบคลุม (* = ทั้งหมด)
subjectsobjectไม่บังคับใช้กับใคร
ฟิลด์ย่อยของ
subjects(2)rolesarray<string>ไม่บังคับบทบาท (* = ทุกคน)
usersarray<string>ไม่บังคับผู้ใช้เฉพาะราย
resourcesobjectไม่บังคับทรัพยากรที่ครอบคลุม
ฟิลด์ย่อยของ
resources(2)typesarray<string>ไม่บังคับชนิด
branch_idsarray<string>ไม่บังคับสาขา (* = ทุกสาขา)
conditionobjectจำเป็นเงื่อนไข (KUNJAE Expression Language — คล้าย CEL)
ฟิลด์ย่อยของ
condition(2)languageenumไม่บังคับภาษา
expressionstringจำเป็นนิพจน์ ใช้ subject.* action resource.* context.* ได้
ผลลัพธ์
- 201สร้างแล้ว (dry_run)
- 422นิพจน์ผิด
- 400
invalid_requestคำขอไม่ถูกต้อง - 401
unauthorizedไม่ได้ยืนยันตัวตน - 403
insufficient_scopescope ไม่พอ - 422
policy_invalidนโยบายไม่ถูกต้อง - 409
policy_conflictนโยบายชนกัน - 422
unknown_actionไม่รู้จัก action - 409
idempotency_conflictIdempotency-Key ซ้ำแต่ body ต่าง
- priority 1–9 สงวนให้นโยบายระบบ
- นโยบายมีผลภายใน 5 วินาทีหลังบันทึก (ส่ง webhook policy.created ให้ตัวแคชฝั่งคุณล้างค่า)
curl -X POST "https://sandbox.api.kunjae.example/v1/policies" \
-H "Authorization: Bearer kj_test_mgmt_7Hd2Qx9LmP4w" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 7f3c9a2e-1b4d-4c8e-9f60-2a5d8e1c4b7f" \
-H "Kunjae-Version: 2026-08-01" \
-d '{
"name": "void บิลต้องเป็นผู้จัดการสาขา",
"effect": "deny",
"priority": 45,
"actions": [
"orders:void"
],
"subjects": {
"roles": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "!('branch_manager' in subject.roles)"
},
"description": "ช่วงเทศกาล void ต้องเป็นผู้จัดการเท่านั้น (ทดลอง)",
"status": "dry_run",
"resources": {
"types": [
"order"
],
"branch_ids": [
"*"
]
}
}'const API_KEY = process.env.API_KEY; // kj_test_mgmt_7Hd2Qx9LmP4w
const res = await fetch('https://sandbox.api.kunjae.example/v1/policies', {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': '7f3c9a2e-1b4d-4c8e-9f60-2a5d8e1c4b7f',
'Kunjae-Version': '2026-08-01',
},
body: JSON.stringify({
"name": "void บิลต้องเป็นผู้จัดการสาขา",
"effect": "deny",
"priority": 45,
"actions": [
"orders:void"
],
"subjects": {
"roles": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "!('branch_manager' in subject.roles)"
},
"description": "ช่วงเทศกาล void ต้องเป็นผู้จัดการเท่านั้น (ทดลอง)",
"status": "dry_run",
"resources": {
"types": [
"order"
],
"branch_ids": [
"*"
]
}
}),
});
if (!res.ok) throw new Error((await res.json()).error.code);
const data = await res.json();
console.log(data);import os
import requests
API_KEY = os.environ["API_KEY"] # kj_test_mgmt_7Hd2Qx9LmP4w
res = requests.post(
"https://sandbox.api.kunjae.example/v1/policies",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
"Idempotency-Key": "7f3c9a2e-1b4d-4c8e-9f60-2a5d8e1c4b7f",
"Kunjae-Version": "2026-08-01",
},
json={
"name": "void บิลต้องเป็นผู้จัดการสาขา",
"effect": "deny",
"priority": 45,
"actions": [
"orders:void"
],
"subjects": {
"roles": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "!('branch_manager' in subject.roles)"
},
"description": "ช่วงเทศกาล void ต้องเป็นผู้จัดการเท่านั้น (ทดลอง)",
"status": "dry_run",
"resources": {
"types": [
"order"
],
"branch_ids": [
"*"
]
}
},
timeout=10,
)
res.raise_for_status()
print(res.json()){
"id": "pol_void_manager",
"object": "policy",
"name": "void บิลต้องเป็นผู้จัดการสาขา",
"effect": "deny",
"priority": 45,
"actions": [
"orders:void"
],
"subjects": {
"roles": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "!('branch_manager' in subject.roles)"
},
"description": "ช่วงเทศกาล void ต้องเป็นผู้จัดการเท่านั้น (ทดลอง)",
"status": "dry_run",
"resources": {
"types": [
"order"
],
"branch_ids": [
"*"
]
},
"version": 1,
"system": false,
"stats": {
"evaluated_today": 0,
"denied_today": 0
},
"created_by": "usr_1Ae5",
"created_at": "2026-10-12T10:26:02+07:00",
"updated_at": "2026-10-12T10:26:02+07:00"
}{
"error": {
"code": "policy_invalid",
"message": "นิพจน์ผิดที่ตำแหน่ง 18: ไม่รู้จักฟิลด์ subject.role (หมายถึง subject.roles?)",
"details": [
{
"position": 18,
"token": "subject.role"
}
],
"request_id": "req_5Pq8Tz2Nw",
"doc_url": "https://docs.kunjae.example/errors#policy_invalid"
}
}{
"error": {
"code": "invalid_request",
"message": "คำขอไม่ถูกต้อง",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#invalid_request"
}
}{
"error": {
"code": "unauthorized",
"message": "ไม่ได้ยืนยันตัวตน",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#unauthorized"
}
}{
"error": {
"code": "insufficient_scope",
"message": "scope ไม่พอ",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#insufficient_scope"
}
}{
"error": {
"code": "policy_invalid",
"message": "นโยบายไม่ถูกต้อง",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#policy_invalid"
}
}{
"error": {
"code": "policy_conflict",
"message": "นโยบายชนกัน",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#policy_conflict"
}
}{
"error": {
"code": "unknown_action",
"message": "ไม่รู้จัก action",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#unknown_action"
}
}{
"error": {
"code": "idempotency_conflict",
"message": "Idempotency-Key ซ้ำแต่ body ต่าง",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#idempotency_conflict"
}
}ดูนโยบาย
รายละเอียดนโยบาย 1 รายการ + สถิติวันนี้ · หัวตอบกลับมี ETag สำหรับแก้ไขแบบกันชน
Path parameterspath
policy_idstringจำเป็นรหัสนโยบาย
ผลลัพธ์
- 200พบ
- 401
unauthorizedไม่ได้ยืนยันตัวตน - 404
resource_not_foundไม่พบข้อมูล
curl -X GET "https://sandbox.api.kunjae.example/v1/policies/pol_refund_limit" \
-H "Authorization: Bearer kj_test_mgmt_7Hd2Qx9LmP4w" \
-H "Kunjae-Version: 2026-08-01"const API_KEY = process.env.API_KEY; // kj_test_mgmt_7Hd2Qx9LmP4w
const res = await fetch('https://sandbox.api.kunjae.example/v1/policies/pol_refund_limit', {
method: 'GET',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Kunjae-Version': '2026-08-01',
},
});
if (!res.ok) throw new Error((await res.json()).error.code);
const data = await res.json();
console.log(data);import os
import requests
API_KEY = os.environ["API_KEY"] # kj_test_mgmt_7Hd2Qx9LmP4w
res = requests.get(
"https://sandbox.api.kunjae.example/v1/policies/pol_refund_limit",
headers={
"Authorization": f"Bearer {API_KEY}",
"Kunjae-Version": "2026-08-01",
},
timeout=10,
)
res.raise_for_status()
print(res.json()){
"id": "pol_refund_limit",
"object": "policy",
"name": "คืนเงินเกิน ฿5,000 ต้องเป็นผู้จัดการ/หัวหน้าแคชเชียร์",
"description": "ยอดคืนเงิน (สตางค์) เกิน 500000 ต้องมีบทบาท branch_manager หรือ cashier_supervisor",
"effect": "deny",
"priority": 50,
"status": "active",
"actions": [
"orders:refund"
],
"subjects": {
"roles": [
"*"
],
"users": []
},
"resources": {
"types": [
"*"
],
"branch_ids": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "resource.amount > 500000 && !(subject.roles.hasAny(['branch_manager', 'cashier_supervisor', 'app_owner']))"
},
"version": 5,
"system": false,
"stats": {
"evaluated_today": 11,
"denied_today": 1
},
"created_by": "usr_1Ae5",
"created_at": "2024-05-20T10:00:00+07:00",
"updated_at": "2026-08-18T09:30:00+07:00"
}{
"error": {
"code": "unauthorized",
"message": "ไม่ได้ยืนยันตัวตน",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#unauthorized"
}
}{
"error": {
"code": "resource_not_found",
"message": "ไม่พบข้อมูล",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#resource_not_found"
}
}แก้นโยบาย
แก้บางฟิลด์ (เช่น เปลี่ยน dry_run → active หรือแก้นิพจน์) — ต้องส่ง If-Match กันเขียนทับคนอื่น · ทุกการแก้เพิ่ม version และเก็บประวัติ
Path parameterspath
policy_idstringจำเป็นรหัสนโยบาย
Headersheader
If-Matchstringจำเป็นETag ของเวอร์ชันที่อ่านมา — กันเขียนทับการแก้ของคนอื่น (ไม่ตรง = 409)
Request bodyapplication/json
statusenumไม่บังคับสถานะใหม่
priorityintegerไม่บังคับลำดับ
conditionobjectไม่บังคับเงื่อนไขใหม่
descriptionstringไม่บังคับคำอธิบาย
change_notestringไม่บังคับเหตุผลการแก้ (บันทึกในประวัติ)
ผลลัพธ์
- 200แก้แล้ว
- 409มีคนแก้ก่อน (If-Match ไม่ตรง)
- 400
invalid_requestคำขอไม่ถูกต้อง - 401
unauthorizedไม่ได้ยืนยันตัวตน - 404
resource_not_foundไม่พบข้อมูล - 422
policy_invalidนโยบายไม่ถูกต้อง - 409
policy_conflictนโยบายชนกัน
curl -X PATCH "https://sandbox.api.kunjae.example/v1/policies/pol_void_manager" \
-H "Authorization: Bearer kj_test_mgmt_7Hd2Qx9LmP4w" \
-H "Content-Type: application/json" \
-H "Kunjae-Version: 2026-08-01" \
-d '{
"status": "active",
"condition": {
"language": "kel",
"expression": "!('branch_manager' in subject.roles) && !('cashier_supervisor' in subject.roles)"
},
"change_note": "ยกเว้นหัวแคชเชียร์ช่วงเทศกาล"
}'const API_KEY = process.env.API_KEY; // kj_test_mgmt_7Hd2Qx9LmP4w
const res = await fetch('https://sandbox.api.kunjae.example/v1/policies/pol_void_manager', {
method: 'PATCH',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
'Kunjae-Version': '2026-08-01',
},
body: JSON.stringify({
"status": "active",
"condition": {
"language": "kel",
"expression": "!('branch_manager' in subject.roles) && !('cashier_supervisor' in subject.roles)"
},
"change_note": "ยกเว้นหัวแคชเชียร์ช่วงเทศกาล"
}),
});
if (!res.ok) throw new Error((await res.json()).error.code);
const data = await res.json();
console.log(data);import os
import requests
API_KEY = os.environ["API_KEY"] # kj_test_mgmt_7Hd2Qx9LmP4w
res = requests.patch(
"https://sandbox.api.kunjae.example/v1/policies/pol_void_manager",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
"Kunjae-Version": "2026-08-01",
},
json={
"status": "active",
"condition": {
"language": "kel",
"expression": "!('branch_manager' in subject.roles) && !('cashier_supervisor' in subject.roles)"
},
"change_note": "ยกเว้นหัวแคชเชียร์ช่วงเทศกาล"
},
timeout=10,
)
res.raise_for_status()
print(res.json()){
"id": "pol_void_manager",
"object": "policy",
"name": "void บิลต้องเป็นผู้จัดการสาขา",
"effect": "deny",
"priority": 45,
"actions": [
"orders:void"
],
"subjects": {
"roles": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "!('branch_manager' in subject.roles) && !('cashier_supervisor' in subject.roles)"
},
"status": "active",
"version": 3,
"updated_at": "2026-10-13T09:05:40+07:00"
}{
"error": {
"code": "policy_conflict",
"message": "นโยบายถูกแก้เป็นเวอร์ชัน 2 แล้ว กรุณาโหลดใหม่",
"current_version": 2,
"request_id": "req_5Pq8Tz2Nw",
"doc_url": "https://docs.kunjae.example/errors#policy_conflict"
}
}{
"error": {
"code": "invalid_request",
"message": "คำขอไม่ถูกต้อง",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#invalid_request"
}
}{
"error": {
"code": "unauthorized",
"message": "ไม่ได้ยืนยันตัวตน",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#unauthorized"
}
}{
"error": {
"code": "resource_not_found",
"message": "ไม่พบข้อมูล",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#resource_not_found"
}
}{
"error": {
"code": "policy_invalid",
"message": "นโยบายไม่ถูกต้อง",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#policy_invalid"
}
}{
"error": {
"code": "policy_conflict",
"message": "นโยบายชนกัน",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#policy_conflict"
}
}ลบนโยบาย
DELETE/v1/policies/{policy_id}
ลบนโยบาย (ประวัติ/ผล audit ยังเก็บไว้) · นโยบายระบบ (system: true เช่น pol_branch_scope) ลบไม่ได้ — ปิดด้วย status=disabled ก็ไม่ได้
Path parameterspath
policy_idstringจำเป็นรหัสนโยบาย
Headersheader
If-Matchstringไม่บังคับETag ของเวอร์ชันที่อ่านมา — กันเขียนทับการแก้ของคนอื่น (ไม่ตรง = 409)
ผลลัพธ์
- 200ลบแล้ว
- 409นโยบายระบบ
- 401
unauthorizedไม่ได้ยืนยันตัวตน - 403
insufficient_scopescope ไม่พอ - 404
resource_not_foundไม่พบข้อมูล - 409
policy_conflictนโยบายชนกัน
curl -X DELETE "https://sandbox.api.kunjae.example/v1/policies/pol_void_manager" \
-H "Authorization: Bearer kj_test_mgmt_7Hd2Qx9LmP4w" \
-H "Kunjae-Version: 2026-08-01"const API_KEY = process.env.API_KEY; // kj_test_mgmt_7Hd2Qx9LmP4w
const res = await fetch('https://sandbox.api.kunjae.example/v1/policies/pol_void_manager', {
method: 'DELETE',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Kunjae-Version': '2026-08-01',
},
});
if (!res.ok) throw new Error((await res.json()).error.code);
const data = await res.json();
console.log(data);import os
import requests
API_KEY = os.environ["API_KEY"] # kj_test_mgmt_7Hd2Qx9LmP4w
res = requests.delete(
"https://sandbox.api.kunjae.example/v1/policies/pol_void_manager",
headers={
"Authorization": f"Bearer {API_KEY}",
"Kunjae-Version": "2026-08-01",
},
timeout=10,
)
res.raise_for_status()
print(res.json()){
"id": "pol_void_manager",
"object": "policy",
"deleted": true,
"deleted_at": "2026-10-16T09:00:00+07:00"
}{
"error": {
"code": "policy_conflict",
"message": "ลบนโยบายระบบ pol_branch_scope ไม่ได้",
"request_id": "req_5Pq8Tz2Nw",
"doc_url": "https://docs.kunjae.example/errors#policy_conflict"
}
}{
"error": {
"code": "unauthorized",
"message": "ไม่ได้ยืนยันตัวตน",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#unauthorized"
}
}{
"error": {
"code": "insufficient_scope",
"message": "scope ไม่พอ",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#insufficient_scope"
}
}{
"error": {
"code": "resource_not_found",
"message": "ไม่พบข้อมูล",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#resource_not_found"
}
}{
"error": {
"code": "policy_conflict",
"message": "นโยบายชนกัน",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#policy_conflict"
}
}จำลองนโยบายกับข้อมูลจริง
ทดสอบร่างนโยบายก่อนบันทึก — เล่นซ้ำการตัดสินจริงในช่วงเวลาที่เลือก (replay) หรือรายการทดสอบที่ส่งมา แล้วบอกว่ารายการไหนเปลี่ยนผล allow ↔ deny
Request bodyapplication/json
draftobjectจำเป็นร่างนโยบาย (รูปแบบเดียวกับ POST /v1/policies)
replayobjectไม่บังคับเล่นซ้ำ audit ในช่วงเวลา (สูงสุด 7 วัน · 50,000 รายการ)
checksarray<object>ไม่บังคับหรือส่งรายการทดสอบเอง (รูปแบบเดียวกับ check-batch)
ผลลัพธ์
- 200เล่นซ้ำ 11 รายการ เปลี่ยนผล 1
- 400
invalid_requestคำขอไม่ถูกต้อง - 401
unauthorizedไม่ได้ยืนยันตัวตน - 422
policy_invalidนโยบายไม่ถูกต้อง - 422
unknown_actionไม่รู้จัก action - 429
rate_limitedเรียกเกินโควตา
- ไม่เปลี่ยนนโยบายจริงและไม่ยิง webhook
- warnings บอกผลกระทบที่ควรคุยกับเจ้าของงานก่อนเปิดใช้
curl -X POST "https://sandbox.api.kunjae.example/v1/policies/simulate" \
-H "Authorization: Bearer kj_test_mgmt_7Hd2Qx9LmP4w" \
-H "Content-Type: application/json" \
-H "Kunjae-Version: 2026-08-01" \
-d '{
"draft": {
"name": "void บิลต้องเป็นผู้จัดการสาขา",
"effect": "deny",
"priority": 45,
"actions": [
"orders:void"
],
"subjects": {
"roles": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "!('branch_manager' in subject.roles)"
}
},
"replay": {
"from": "2026-10-12T00:00:00+07:00",
"to": "2026-10-12T10:20:00+07:00"
},
"checks": []
}'const API_KEY = process.env.API_KEY; // kj_test_mgmt_7Hd2Qx9LmP4w
const res = await fetch('https://sandbox.api.kunjae.example/v1/policies/simulate', {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
'Kunjae-Version': '2026-08-01',
},
body: JSON.stringify({
"draft": {
"name": "void บิลต้องเป็นผู้จัดการสาขา",
"effect": "deny",
"priority": 45,
"actions": [
"orders:void"
],
"subjects": {
"roles": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "!('branch_manager' in subject.roles)"
}
},
"replay": {
"from": "2026-10-12T00:00:00+07:00",
"to": "2026-10-12T10:20:00+07:00"
},
"checks": []
}),
});
if (!res.ok) throw new Error((await res.json()).error.code);
const data = await res.json();
console.log(data);import os
import requests
API_KEY = os.environ["API_KEY"] # kj_test_mgmt_7Hd2Qx9LmP4w
res = requests.post(
"https://sandbox.api.kunjae.example/v1/policies/simulate",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
"Kunjae-Version": "2026-08-01",
},
json={
"draft": {
"name": "void บิลต้องเป็นผู้จัดการสาขา",
"effect": "deny",
"priority": 45,
"actions": [
"orders:void"
],
"subjects": {
"roles": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "!('branch_manager' in subject.roles)"
}
},
"replay": {
"from": "2026-10-12T00:00:00+07:00",
"to": "2026-10-12T10:20:00+07:00"
},
"checks": []
},
timeout=10,
)
res.raise_for_status()
print(res.json()){
"object": "policy_simulation",
"id": "sim_3Fw8Qa",
"draft": {
"name": "void บิลต้องเป็นผู้จัดการสาขา",
"effect": "deny",
"priority": 45,
"actions": [
"orders:void"
],
"subjects": {
"roles": [
"*"
]
},
"condition": {
"language": "kel",
"expression": "!('branch_manager' in subject.roles)"
}
},
"replay": {
"from": "2026-10-12T00:00:00+07:00",
"to": "2026-10-12T10:20:00+07:00",
"decisions_evaluated": 11
},
"summary": {
"unchanged": 10,
"allow_to_deny": 1,
"deny_to_allow": 0
},
"changes": [
{
"decision_id": "dec_175ZXRM",
"at": "2026-10-12T10:18:26+07:00",
"subject": "usr_5Tz1",
"subject_name": "วีรยุทธ แสงทอง",
"action": "orders:void",
"resource_id": "ord_MY2610_0201",
"before": "allow",
"after": "deny",
"by_policy": "draft"
}
],
"warnings": [
"จะปฏิเสธ 1 รายการที่วันนี้อนุญาต — ผู้ถือบทบาท cashier_supervisor (ชั่วคราว) จะ void ไม่ได้"
],
"created_at": "2026-10-12T10:24:10+07:00"
}{
"error": {
"code": "invalid_request",
"message": "คำขอไม่ถูกต้อง",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#invalid_request"
}
}{
"error": {
"code": "unauthorized",
"message": "ไม่ได้ยืนยันตัวตน",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#unauthorized"
}
}{
"error": {
"code": "policy_invalid",
"message": "นโยบายไม่ถูกต้อง",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#policy_invalid"
}
}{
"error": {
"code": "unknown_action",
"message": "ไม่รู้จัก action",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#unknown_action"
}
}{
"error": {
"code": "rate_limited",
"message": "เรียกเกินโควตา",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#rate_limited"
}
}บันทึกการตัดสินสิทธิ์
ค้นการตัดสินย้อนหลัง (เก็บ 400 วัน ตามข้อกำหนดตรวจสอบภายใน) — กรองตามผู้ใช้ ผล action นโยบาย ช่วงเวลา
Query parametersquery
subjectstringไม่บังคับรหัสผู้ใช้
decisionenumไม่บังคับผล
actionstringไม่บังคับaction
policy_idstringไม่บังคับนโยบาย
fromstring · date-timeไม่บังคับตั้งแต่
tostring · date-timeไม่บังคับถึง
limitintegerไม่บังคับจำนวนต่อหน้า
starting_afterstringไม่บังคับcursor — ส่งรหัสของรายการสุดท้ายในหน้าก่อน เพื่อดึงหน้าถัดไป
ending_beforestringไม่บังคับcursor — ส่งรหัสของรายการแรก เพื่อดึงหน้าก่อนหน้า
sortstringไม่บังคับเรียงลำดับ (ใส่ - ข้างหน้า = มากไปน้อย)
ผลลัพธ์
- 200การตัดสินของ usr_6Pb3 วันนี้ 3 รายการ (ทั้งระบบวันนี้ 11 · ปฏิเสธ 6)
- 401
unauthorizedไม่ได้ยืนยันตัวตน - 403
insufficient_scopescope ไม่พอ - 422
validation_failedข้อมูลไม่ผ่านกฎ - 429
rate_limitedเรียกเกินโควตา
- ผลไม่รวม steps — ดูรายตัวที่ GET /v1/authz/audit/{decision_id}
- ส่งออกเป็น CSV ได้ด้วยหัว Accept: text/csv (สูงสุด 100,000 แถว)
curl -X GET "https://sandbox.api.kunjae.example/v1/authz/audit?subject=usr_6Pb3&decision=deny&from=2026-10-12T00%3A00%3A00%2B07%3A00&limit=20" \
-H "Authorization: Bearer kj_test_mgmt_7Hd2Qx9LmP4w" \
-H "Kunjae-Version: 2026-08-01"const API_KEY = process.env.API_KEY; // kj_test_mgmt_7Hd2Qx9LmP4w
const res = await fetch('https://sandbox.api.kunjae.example/v1/authz/audit?subject=usr_6Pb3&decision=deny&from=2026-10-12T00%3A00%3A00%2B07%3A00&limit=20', {
method: 'GET',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Kunjae-Version': '2026-08-01',
},
});
if (!res.ok) throw new Error((await res.json()).error.code);
const data = await res.json();
console.log(data);import os
import requests
API_KEY = os.environ["API_KEY"] # kj_test_mgmt_7Hd2Qx9LmP4w
res = requests.get(
"https://sandbox.api.kunjae.example/v1/authz/audit?subject=usr_6Pb3&decision=deny&from=2026-10-12T00%3A00%3A00%2B07%3A00&limit=20",
headers={
"Authorization": f"Bearer {API_KEY}",
"Kunjae-Version": "2026-08-01",
},
timeout=10,
)
res.raise_for_status()
print(res.json()){
"object": "list",
"url": "/v1/authz/audit",
"has_more": false,
"next_cursor": null,
"data": [
{
"id": "dec_175ZXHC",
"object": "authz_decision",
"decision": "deny",
"reason": "policy_denied",
"policy_id": "pol_business_hours",
"granted_by": [],
"subject": {
"id": "usr_6Pb3",
"roles": [
"sales_staff"
]
},
"action": "orders:refund",
"resource": {
"type": "order",
"id": "ord_MY2610_0193",
"branch_id": "br_ratchada",
"amount": 689000
},
"context": {
"at": "2026-10-12T09:12:40+07:00",
"mfa": true,
"ip": "10.20.4.17"
},
"cache_ttl": 0,
"client_id": "kjc_backoffice_2Mn6",
"latency_ms": 2,
"created_at": "2026-10-12T09:12:40+07:00"
},
{
"id": "dec_175ZXKF",
"object": "authz_decision",
"decision": "deny",
"reason": "policy_denied",
"policy_id": "pol_refund_limit",
"granted_by": [],
"subject": {
"id": "usr_6Pb3",
"roles": [
"sales_staff"
]
},
"action": "orders:refund",
"resource": {
"type": "order",
"id": "ord_MY2610_0193",
"branch_id": "br_ratchada",
"amount": 689000
},
"context": {
"at": "2026-10-12T10:05:31+07:00",
"mfa": true,
"ip": "10.20.4.17"
},
"cache_ttl": 0,
"client_id": "kjc_backoffice_2Mn6",
"latency_ms": 3,
"created_at": "2026-10-12T10:05:31+07:00"
},
{
"id": "dec_175ZXMH",
"object": "authz_decision",
"decision": "deny",
"reason": "policy_denied",
"policy_id": "pol_pii_sales_deny",
"granted_by": [],
"subject": {
"id": "usr_6Pb3",
"roles": [
"sales_staff"
]
},
"action": "members:pii.read",
"resource": {
"type": "member",
"id": "MBR-0048213"
},
"context": {
"at": "2026-10-12T10:09:44+07:00",
"mfa": true,
"ip": "10.20.4.17"
},
"cache_ttl": 0,
"client_id": "kjc_backoffice_2Mn6",
"latency_ms": 2,
"created_at": "2026-10-12T10:09:44+07:00"
}
]
}{
"error": {
"code": "unauthorized",
"message": "ไม่ได้ยืนยันตัวตน",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#unauthorized"
}
}{
"error": {
"code": "insufficient_scope",
"message": "scope ไม่พอ",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#insufficient_scope"
}
}{
"error": {
"code": "validation_failed",
"message": "ข้อมูลไม่ผ่านกฎ",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#validation_failed"
}
}{
"error": {
"code": "rate_limited",
"message": "เรียกเกินโควตา",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#rate_limited"
}
}อธิบายการตัดสิน 1 รายการ
GET/v1/authz/audit/{decision_id}
ดูการตัดสินพร้อมลำดับการประเมินทุกขั้น — ใช้ตอบพนักงาน/ผู้ตรวจว่า “ทำไมกดไม่ได้” จาก decision_id ที่แสดงบนจอ
Path parameterspath
decision_idstringจำเป็นรหัสการตัดสิน
ผลลัพธ์
- 200พบ — นอกขอบเขตสาขา
- 401
unauthorizedไม่ได้ยืนยันตัวตน - 403
insufficient_scopescope ไม่พอ - 404
resource_not_foundไม่พบข้อมูล
curl -X GET "https://sandbox.api.kunjae.example/v1/authz/audit/dec_175ZXNI" \
-H "Authorization: Bearer kj_test_mgmt_7Hd2Qx9LmP4w" \
-H "Kunjae-Version: 2026-08-01"const API_KEY = process.env.API_KEY; // kj_test_mgmt_7Hd2Qx9LmP4w
const res = await fetch('https://sandbox.api.kunjae.example/v1/authz/audit/dec_175ZXNI', {
method: 'GET',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Kunjae-Version': '2026-08-01',
},
});
if (!res.ok) throw new Error((await res.json()).error.code);
const data = await res.json();
console.log(data);import os
import requests
API_KEY = os.environ["API_KEY"] # kj_test_mgmt_7Hd2Qx9LmP4w
res = requests.get(
"https://sandbox.api.kunjae.example/v1/authz/audit/dec_175ZXNI",
headers={
"Authorization": f"Bearer {API_KEY}",
"Kunjae-Version": "2026-08-01",
},
timeout=10,
)
res.raise_for_status()
print(res.json()){
"id": "dec_175ZXNI",
"object": "authz_decision",
"decision": "deny",
"reason": "out_of_branch_scope",
"policy_id": "pol_branch_scope",
"granted_by": [],
"subject": {
"id": "usr_5Tz1",
"roles": [
"sales_staff",
"cashier_supervisor"
]
},
"action": "orders:read",
"resource": {
"type": "order",
"id": "ord_MY2610_0193",
"branch_id": "br_ratchada",
"amount": 689000
},
"context": {
"at": "2026-10-12T10:11:20+07:00",
"mfa": true,
"ip": "10.20.4.17"
},
"steps": [
{
"n": 1,
"name": "ตรวจผู้ใช้",
"result": "pass",
"detail": "วีรยุทธ แสงทอง · บทบาท ณ 12 ต.ค. 69 10:11 น.: sales_staff, cashier_supervisor"
},
{
"n": 2,
"name": "ตรวจ action",
"result": "pass",
"detail": "orders:read (ดูคำสั่งซื้อ · ความเสี่ยง low)"
},
{
"n": 3,
"name": "นโยบายปฏิเสธ pol_auditor_readonly",
"result": "skip",
"detail": "ไม่เข้าเงื่อนไข (ผู้ตรวจอ่านได้อย่างเดียว)"
},
{
"n": 4,
"name": "นโยบายปฏิเสธ pol_pii_sales_deny",
"result": "skip",
"detail": "ไม่เข้าเงื่อนไข (พนักงานขายห้ามดูข้อมูลส่วนบุคคลเต็ม)"
},
{
"n": 5,
"name": "นโยบายปฏิเสธ pol_mfa_sensitive",
"result": "skip",
"detail": "ไม่เข้าเงื่อนไข (งานอ่อนไหวต้องยืนยัน 2 ขั้น)"
},
{
"n": 6,
"name": "นโยบายปฏิเสธ pol_business_hours",
"result": "skip",
"detail": "ไม่เข้าเงื่อนไข (คืนเงิน/void เฉพาะเวลาทำการ 10:00–22:00)"
},
{
"n": 7,
"name": "นโยบายปฏิเสธ pol_refund_limit",
"result": "skip",
"detail": "ไม่เข้าเงื่อนไข (คืนเงินเกิน ฿5,000 ต้องเป็นผู้จัดการ/หัวหน้าแคชเชียร์)"
},
{
"n": 8,
"name": "หาบทบาทที่ให้สิทธิ์ (RBAC)",
"result": "pass",
"detail": "ได้จาก sales_staff, cashier_supervisor (ชั่วคราว grt_7Yc4)"
},
{
"n": 9,
"name": "ขอบเขตสาขา (pol_branch_scope)",
"result": "fail",
"detail": "ทรัพยากรอยู่ MAYARIN รัชดา แต่สิทธิ์ใช้ได้เฉพาะ MAYARIN บางนา"
}
],
"cache_ttl": 0,
"client_id": "kjc_backoffice_2Mn6",
"latency_ms": 4,
"created_at": "2026-10-12T10:11:20+07:00"
}{
"error": {
"code": "unauthorized",
"message": "ไม่ได้ยืนยันตัวตน",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#unauthorized"
}
}{
"error": {
"code": "insufficient_scope",
"message": "scope ไม่พอ",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#insufficient_scope"
}
}{
"error": {
"code": "resource_not_found",
"message": "ไม่พบข้อมูล",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#resource_not_found"
}
}Webhooks — แจ้งเหตุการณ์ไปที่ระบบของคุณ
ลงทะเบียน URL ปลายทาง แล้วระบบจะส่ง POST พร้อม JSON ทุกครั้งที่เกิดเหตุการณ์ด้านล่าง · ตอบกลับ 2xx ภายใน 10 วินาที ไม่งั้นจะส่งซ้ำตามตาราง · ทุกคำขอมีหัว Kunjae-Signature ให้ตรวจว่ามาจากเราจริง · event อาจมาซ้ำหรือสลับลำดับ ให้ใช้ id กันประมวลผลซ้ำ
authz.decision.deniedปฏิเสธ action ความเสี่ยงสูง
ส่งเมื่อ: deny ของ action ระดับ high/critical (คืนเงิน void ข้อมูลส่วนบุคคล มอบบทบาท)
{
"id": "evt_01JB7Q00",
"type": "authz.decision.denied",
"created": "2026-10-12T10:00:00+07:00",
"livemode": false,
"data": {
"id": "dec_175ZXKF",
"object": "authz_decision",
"decision": "deny",
"reason": "policy_denied",
"policy_id": "pol_refund_limit",
"granted_by": [],
"subject": {
"id": "usr_6Pb3",
"roles": [
"sales_staff"
]
},
"action": "orders:refund",
"resource": {
"type": "order",
"id": "ord_MY2610_0193",
"branch_id": "br_ratchada",
"amount": 689000
},
"context": {
"at": "2026-10-12T10:05:31+07:00",
"mfa": true,
"ip": "10.20.4.17"
},
"cache_ttl": 0,
"client_id": "kjc_backoffice_2Mn6",
"latency_ms": 3,
"created_at": "2026-10-12T10:05:31+07:00"
}
}authz.anomaly.detectedการปฏิเสธพุ่งผิดปกติ
ส่งเมื่อ: ผู้ใช้/สาขาถูกปฏิเสธเกิน 10 ครั้งใน 5 นาที
{
"id": "evt_01JB7Q01",
"type": "authz.anomaly.detected",
"created": "2026-10-12T10:00:00+07:00",
"livemode": false,
"data": {
"subject": "usr_6Pb3",
"branch_id": "br_ratchada",
"denied": 12,
"window_seconds": 300,
"top_reasons": [
{
"reason": "policy_denied",
"policy_id": "pol_refund_limit",
"count": 9
},
{
"reason": "policy_denied",
"policy_id": "pol_pii_sales_deny",
"count": 3
}
],
"detected_at": "2026-10-12T10:31:00+07:00"
}
}policy.createdสร้างนโยบาย
ส่งเมื่อ: POST /v1/policies สำเร็จ
{
"id": "evt_01JB7Q02",
"type": "policy.created",
"created": "2026-10-12T10:00:00+07:00",
"livemode": false,
"data": {
"id": "pol_void_manager",
"name": "void บิลต้องเป็นผู้จัดการสาขา",
"status": "dry_run",
"version": 1,
"created_by": "usr_1Ae5"
}
}policy.updatedแก้นโยบาย
ส่งเมื่อ: PATCH สำเร็จ (รวมเปลี่ยนสถานะ)
{
"id": "evt_01JB7Q03",
"type": "policy.updated",
"created": "2026-10-12T10:00:00+07:00",
"livemode": false,
"data": {
"id": "pol_void_manager",
"changes": [
"status",
"condition"
],
"status": "active",
"version": 3,
"updated_by": "usr_1Ae5",
"change_note": "ยกเว้นหัวแคชเชียร์ช่วงเทศกาล"
}
}policy.deletedลบนโยบาย
ส่งเมื่อ: DELETE สำเร็จ
{
"id": "evt_01JB7Q04",
"type": "policy.deleted",
"created": "2026-10-12T10:00:00+07:00",
"livemode": false,
"data": {
"id": "pol_void_manager",
"deleted_by": "usr_1Ae5",
"deleted_at": "2026-10-16T09:00:00+07:00"
}
}policy.dry_run.reportรายงานนโยบายทดลองรายวัน
ส่งเมื่อ: ทุกวัน 08:00 สำหรับนโยบาย dry_run
{
"id": "evt_01JB7Q05",
"type": "policy.dry_run.report",
"created": "2026-10-12T10:00:00+07:00",
"livemode": false,
"data": {
"id": "pol_void_manager",
"date": "2026-10-12",
"evaluated": 37,
"would_deny": 4,
"subjects_affected": [
"usr_5Tz1"
]
}
}ส่งซ้ำเมื่อปลายทางไม่ตอบ
- 1ทันที
- 21 นาที
- 35 นาที
- 430 นาที
- 52 ชั่วโมง
- 66 ชั่วโมง
- 724 ชั่วโมง (ครั้งสุดท้าย)
ครบทุกครั้งแล้วยังล้มเหลว → ปิด endpoint อัตโนมัติและส่งอีเมลแจ้งผู้ดูแล · ดูประวัติการส่งและกดส่งซ้ำได้ในแดชบอร์ด
ตรวจลายเซ็น
# ลายเซ็นอยู่ในหัว Kunjae-Signature: t=<unix>,v1=<hex>
# คำนวณ HMAC-SHA256(secret, "<t>.<raw body>") แล้วเทียบกับ v1
printf '%s' "$T.$BODY" | openssl dgst -sha256 -hmac "$WEBHOOK_SECRET"import crypto from 'node:crypto';
// ใช้ raw body (ยังไม่ parse JSON) เท่านั้น
export function verify(rawBody, sigHeader, secret) {
const parts = Object.fromEntries(sigHeader.split(',').map((p) => p.split('=')));
const age = Math.abs(Date.now() / 1000 - Number(parts.t));
if (age > 300) return false; // เก่ากว่า 5 นาที = ปฏิเสธ (กัน replay)
const expected = crypto.createHmac('sha256', secret).update(parts.t + '.' + rawBody).digest('hex');
return crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(parts.v1));
}import hmac, hashlib, time
def verify(raw_body: bytes, sig_header: str, secret: str) -> bool:
parts = dict(p.split("=", 1) for p in sig_header.split(","))
if abs(time.time() - int(parts["t"])) > 300:
return False # เก่ากว่า 5 นาที = ปฏิเสธ (กัน replay)
msg = parts["t"].encode() + b"." + raw_body
expected = hmac.new(secret.encode(), msg, hashlib.sha256).hexdigest()
return hmac.compare_digest(expected, parts["v1"])Errors — รหัสข้อผิดพลาด
ทุก error ตอบกลับเป็น JSON รูปแบบเดียวกัน พร้อม request_id สำหรับแจ้งทีมสนับสนุน · รหัส HTTP บอกกลุ่มของปัญหา ส่วน error.code บอกสาเหตุที่แน่นอน (ใช้เขียนเงื่อนไขในโค้ดได้ เพราะไม่เปลี่ยนตามภาษา)
- 4xx คำขอไม่ถูกต้อง — แก้ที่ฝั่งผู้เรียก ส่งซ้ำแบบเดิมจะได้ผลเดิม
- 5xx ปัญหาฝั่งระบบ — ลองใหม่แบบเว้นระยะ (exponential backoff) พร้อม Idempotency-Key
{
"error": {
"code": "invalid_request",
"message": "คำขอไม่ถูกต้อง",
"param": "items[0].quantity",
"request_id": "req_8Fq2LmX0",
"doc_url": "https://docs.kunjae.example/errors#invalid_request"
}
}| HTTP | code | ความหมาย | วิธีแก้ |
|---|---|---|---|
| 400 | invalid_request | คำขอไม่ถูกต้อง ฟิลด์ที่จำเป็นขาด ชนิดข้อมูลผิด หรือ JSON เสีย — ดูฟิลด์ใน error.param | ตรวจ body/query ตาม schema ของ endpoint แล้วส่งใหม่ |
| 401 | unauthorized | ไม่ได้ยืนยันตัวตน ไม่มีหัว Authorization หรือ management key ผิด/ถูกเพิกถอน | ส่ง Authorization: Bearer <key> ที่ถูกต้อง · sandbox ใช้ kj_test_… production ใช้ kj_live_… |
| 401 | token_expired | token หมดอายุ access token เลยเวลา exp แล้ว (เผื่อนาฬิกาคลาดได้ 60 วินาที) | ใช้ refresh_token ขอ token ใหม่ที่ POST /oauth/token แล้วเรียกซ้ำ |
| 403 | insufficient_scope | scope ไม่พอ token/key ไม่มี scope ที่ endpoint ต้องการ (ดูหัว WWW-Authenticate) | ขอ scope เพิ่มตอน authorize หรือสร้าง key ที่มี scope ที่ต้องใช้ |
| 403 | access_denied | ไม่มีสิทธิ์ นโยบายสิทธิ์ปฏิเสธคำขอนี้ (ดู decision_id) | ดูเหตุผลที่ GET /v1/authz/audit/{decision_id} หรือขอบทบาทจากผู้ดูแล |
| 404 | resource_not_found | ไม่พบข้อมูล รหัสที่ระบุไม่มีอยู่ หรืออยู่คนละ environment (sandbox/production) | ตรวจรหัสและ base URL ที่ใช้ |
| 422 | validation_failed | ข้อมูลไม่ผ่านกฎ รูปแบบถูกแต่ค่าผิดกฎธุรกิจ (เช่น เบอร์ไม่ใช่ E.164 วันหมดอายุย้อนหลัง) | อ่าน error.details[] แล้วแก้ทีละฟิลด์ |
| 422 | policy_invalid | นโยบายไม่ถูกต้อง expression ผิดไวยากรณ์ อ้างฟิลด์ที่ไม่มี หรือ priority ชนกับนโยบายระบบ | ทดสอบด้วย POST /v1/policies/simulate ก่อนบันทึก · ดู error.details[].position |
| 409 | policy_conflict | นโยบายชนกัน ชื่อซ้ำ หรือแก้ไขทับเวอร์ชันที่คนอื่นเพิ่งแก้ (If-Match ไม่ตรง) | ดึงเวอร์ชันล่าสุด (ETag) แล้วแก้ซ้ำ |
| 422 | unknown_action | ไม่รู้จัก action action ไม่อยู่ในรายการสิทธิ์ (GET /v1/permissions) | ใช้รูปแบบ resource:action ที่ลงทะเบียนไว้ เช่น orders:refund |
| 413 | batch_too_large | batch ใหญ่เกิน check-batch รับไม่เกิน 100 รายการ/คำขอ | แบ่งเป็นหลายคำขอ |
| 409 | idempotency_conflict | Idempotency-Key ซ้ำแต่ body ต่าง ใช้ Idempotency-Key เดิมกับคำขอที่เนื้อหาไม่เหมือนเดิมภายใน 24 ชม. | สร้าง UUID ใหม่ทุกคำขอที่ต่างกัน · ใช้ key เดิมเฉพาะตอนส่งซ้ำคำขอเดิม |
| 429 | rate_limited | เรียกเกินโควตา เกินโควตาของ key/IP/บัญชี (ดูหัว RateLimit-*) | รอตาม Retry-After แล้วลองใหม่แบบเว้นระยะเพิ่มขึ้น + jitter |
| 500 | internal_error | ระบบขัดข้อง เกิดข้อผิดพลาดฝั่ง KUNJAE ID | ลองใหม่แบบ backoff พร้อม Idempotency-Key เดิม · แจ้ง request_id ถ้ายังเกิด |
| 503 | service_unavailable | ปิดปรับปรุงชั่วคราว ระบบอยู่ระหว่างบำรุงรักษาหรือรับโหลดเกิน | ลองใหม่ตาม Retry-After · ตรวจหน้า status.kunjae.example |
ไม่พบรหัสที่ค้นหา
Rate limit — โควตาการเรียก
| ขอบเขต | โควตา | Burst | หมายเหตุ |
|---|---|---|---|
| POST /v1/authz/check ต่อ client | 3,000 ครั้ง/วินาที | 6,000 | ตอบเฉลี่ย 2–6 ms · แคช allow 60 วินาทีได้ |
| POST /v1/authz/check-batch | 300 คำขอ/วินาที | 600 | ≤ 100 รายการต่อคำขอ |
| เขียนนโยบาย | 30 ครั้ง/นาที | 5 | ต่อ management key |
| POST /v1/policies/simulate | 10 ครั้ง/นาที | 2 | replay ≤ 7 วัน / 50,000 รายการ |
| GET /v1/authz/audit | 60 ครั้ง/นาที | 10 |
RateLimit-Limitโควตาของช่วงเวลาRateLimit-Remainingครั้งที่เหลือRateLimit-Resetวินาทีจนโควตาเริ่มใหม่- เกินโควตา → 429 พร้อม
Retry-After· แนะนำลองใหม่แบบเว้นระยะเพิ่มขึ้น + สุ่มเล็กน้อย (jitter)
HTTP/1.1 429 Too Many Requests
RateLimit-Limit: 100
RateLimit-Remaining: 0
RateLimit-Reset: 23
Retry-After: 23
{
"error": {
"code": "rate_limited",
"message": "เรียกเกินโควตา ลองใหม่ใน 23 วินาที",
"request_id": "req_2Kd9QwZ1"
}
}เวอร์ชันและ Changelog
เลือกเวอร์ชันด้วยหัว Kunjae-Version (ไม่ส่ง = เวอร์ชันที่ผูกกับ API key) · การเปลี่ยนที่ไม่ทำให้โค้ดเดิมพัง (เพิ่มฟิลด์/endpoint) ออกในเวอร์ชันเดิมได้ · การเปลี่ยนที่ทำให้พังออกเป็นเวอร์ชันใหม่เสมอ และแจ้งล่วงหน้าอย่างน้อย 6 เดือนก่อนปิดเวอร์ชันเก่า
| เวอร์ชัน | สถานะ | รองรับถึง |
|---|---|---|
2026-08-01 ล่าสุด | ปัจจุบัน | — |
2025-11-15 | ยังรองรับ | 30 ก.ย. 2570 |
2024-06-01 | เลิกใช้แล้ว | 31 ธ.ค. 2569 |
- เปลี่ยน
2026-08-01pol_business_hours ใช้กับ cashier_supervisor ด้วย (เดิมเฉพาะ sales_staff)
- เพิ่ม
2026-08-01GET /v1/authz/audit/{decision_id} อธิบายการตัดสินพร้อม steps
- เพิ่ม
2026-08-01webhook authz.anomaly.detected (ปฏิเสธพุ่งผิดปกติ)
- เปลี่ยน
2026-08-01reason แยก out_of_branch_scope ออกจาก no_permission
- แก้ไข
2025-11-15check-batch คืนผลไม่ตรงลำดับเมื่อมี action ซ้ำในรายการเดียวกัน
- ความปลอดภัย
2025-11-15บังคับ fail closed: SDK ทางการคืน deny เมื่อหมดเวลา 150 ms
- เพิ่ม
2025-11-15POST /v1/policies/simulate + สถานะ dry_run
- เลิกใช้
2025-11-15ฟิลด์ allowed (boolean) — ใช้ decision แทน · ตอบคู่กันถึง 31 ธ.ค. 2569
- เพิ่ม
2024-06-01นโยบาย pol_mfa_sensitive + context.mfa
- นำออก
2024-06-01นำออก GET /v1/authz/check (ใช้ POST เท่านั้น ป้องกันข้อมูลรั่วใน log ของ URL)
เครื่องมือ: ตัวจำลองการตรวจสิทธิ์
เลือกพนักงาน การกระทำ ทรัพยากร เวลา และ MFA → เห็นผล allow/deny กฎที่เข้า และลำดับการประเมินทีละขั้น (ตัวประเมินเดียวกับที่สร้างตัวอย่างในหน้านี้) · ทำงานในเบราว์เซอร์ทั้งหมด ไม่ส่งข้อมูลออก — ใช้ทำความเข้าใจ spec (ไม่ใช่ปุ่มเรียก API จริง)
ผลที่ POST /v1/authz/check จะตอบ
นโยบายที่ประเมิน (เรียงตาม priority) — แถวที่ไฮไลต์คือกฎที่ตัดสินกรณีนี้
| priority | นโยบาย | ผล | เงื่อนไข (KEL) |
|---|---|---|---|
| 10 | pol_auditor_readonlyผู้ตรวจอ่านได้อย่างเดียว | deny | 'auditor' in subject.roles && !action.endsWith(':read') |
| 20 | pol_pii_sales_denyพนักงานขายห้ามดูข้อมูลส่วนบุคคลเต็ม | deny | 'sales_staff' in subject.roles && !('branch_manager' in subject.roles) |
| 30 | pol_mfa_sensitiveงานอ่อนไหวต้องยืนยัน 2 ขั้น | deny | !context.mfa |
| 40 | pol_business_hoursคืนเงิน/void เฉพาะเวลาทำการ 10:00–22:00 | deny | !('branch_manager' in subject.roles) && (time(context.at) < '10:00' || time(context.at) >= '22:00') |
| 50 | pol_refund_limitคืนเงินเกิน ฿5,000 ต้องเป็นผู้จัดการ/หัวหน้าแคชเชียร์ | deny | resource.amount > 500000 && !(subject.roles.hasAny(['branch_manager', 'cashier_supervisor', 'app_owner'])) |
| 100 | pol_branch_scopeบทบาทระดับสาขาใช้ได้เฉพาะสาขาตัวเอง | allow | role.scope == 'global' || resource.branch_id == null || resource.branch_id in assignment.branch_ids |