Skip to content

ลายเซ็น (Signature) ​

ทุกคำขอไป Gateway และทุก webhook ที่ Gateway ส่งให้ลูกค้า เซ็นแบบเดียวกัน

Headerค่า
X-GW-Key-IDkeyId ของ API key ที่ได้จากผู้ดูแลระบบ (ลูกค้า 1 ราย มีได้หลาย key จึงหมุน key ใหม่ได้โดยไม่ต้องหยุดระบบ)
X-GW-Timestampเวลาปัจจุบันเป็น unix seconds เช่น 1764471600 ต้องห่างจากเวลาของ Gateway ไม่เกิน ±300 วินาที
X-GW-Signaturehex(HMAC-SHA256(secret, rawBody + "." + timestamp))
  • rawBody คือ body ตามไบต์ที่ส่งจริง ห้าม parse แล้ว serialize ใหม่ก่อนเซ็น
  • คำขอที่ไม่มี body (เช่น GET) ใช้ rawBody เป็นสตริงว่าง
  • Gateway ตรวจ IP allowlist ด้วย IP ที่ไม่อยู่ในรายการได้ 403 IP_NOT_ALLOWED
  • ลายเซ็นผิดหรือเวลาเก่าเกินได้ 401 INVALID_SIGNATURE (เวลาห่างเกิน ±300 วินาที)

ตัวอย่าง (Node.js) ​

ts
import { createHmac } from 'node:crypto'

const body = JSON.stringify({ clientRef: 'smsup-0001', route: 'TRUE', from: 'SMSUPPLUS', to: '66812345678', text: 'hello' })
const timestamp = Math.floor(Date.now() / 1000).toString()
const signature = createHmac('sha256', process.env.GW_SECRET!)
  .update(`${body}.${timestamp}`)
  .digest('hex')

await fetch('http://localhost:30080/v1/messages', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-GW-Key-ID': process.env.GW_KEY_ID!,
    'X-GW-Timestamp': timestamp,
    'X-GW-Signature': signature,
  },
  body,
})

ตัวอย่าง (Go) ​

go
mac := hmac.New(sha256.New, []byte(secret))
mac.Write(rawBody)
mac.Write([]byte("." + timestamp))
signature := hex.EncodeToString(mac.Sum(nil))

ตรวจ webhook ฝั่งลูกค้าด้วยสูตรเดียวกัน และเทียบด้วย constant-time compare (crypto.timingSafeEqual, hmac.Equal)