โทเค็นทึบ (Opaque token)
ระหว่างกระบวนการการยืนยันตัวตน (Authentication) หากไม่มีการระบุ resource Logto จะออกโทเค็นการเข้าถึงแบบทึบ (opaque access token) แทน JWT โดยโทเค็นทึบจะเป็นสตริงสุ่มและสั้นกว่า JWT มาก:
{
"access_token": "some-random-string", // โทเค็นทึบ (opaque token)
"expires_in": 3600,
"id_token": "eyJhbGc...aBc", // JWT
"scope": "openid profile email",
"token_type": "Bearer"
}
โทเค็นทึบสามารถใช้เรียก userinfo endpoint และเข้าถึงทรัพยากรที่ต้องการการยืนยันตัวตน (Authentication) ได้ เนื่องจากมันไม่ใช่ JWT แล้ว resource server จะตรวจสอบความถูกต้องได้อย่างไร?
Logto มี introspection endpoint สำหรับตรวจสอบความถูกต้องของโทเค็นทึบ โดยค่าเริ่มต้น introspection endpoint คือ /oidc/token/introspection และรับ POST requests โดยต้องมีพารามิเตอร์ดังนี้:
token: โทเค็นทึบที่ต้องการตรวจสอบ
endpoint นี้ต้องการการยืนยันตัวตนของ client ด้วย คุณสามารถใช้วิธีใดวิธีหนึ่งต่อไปนี้:
- HTTP Basic authentication: ใช้ header
Authorizationที่มีค่าBasic <base64-encoded-credentials>โดย credentials คือ client ID และ client secret คั่นด้วย colon (:) และเข้ารหัส base64 - HTTP POST authentication: ใช้พารามิเตอร์
client_idและclient_secret:client_id: client ID ของแอปที่ขอโทเค็นclient_secret: client secret ของแอปที่ขอโทเค็น
client ID (app ID) และ client secret (app secret) สามารถใช้ข้อมูลรับรองของแอปประเภท "traditional web" หรือ "machine-to-machine" ใด ๆ ใน Logto ได้ หาก credentials ไม่ถูกต้อง introspection endpoint จะส่ง error กลับ
introspection endpoint จะคืนค่าเป็นอ็อบเจกต์ JSON ที่มีการอ้างสิทธิ์ (claims) ของโทเค็น:
{
"active": true, // โทเค็นนี้ถูกต้องหรือไม่
"sub": "1234567890" // ผู้ถูกอ้างถึง (subject) ของโทเค็น (user ID)
}
หากโทเค็นไม่ถูกต้อง ฟิลด์ active จะเป็น false และจะไม่มีฟิลด์ sub
ตัวอย่างคำขอ introspection (ตัวอย่างที่ไม่เป็นมาตรฐาน):
curl --location \
--request POST 'https://[tenant-id].logto.app/oidc/token/introspection' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'token=some-random-string' \
--data-urlencode 'client_id=1234567890' \
--data-urlencode 'client_secret=1234567890'
อย่าลืมแทนที่ [tenant-id] ด้วย tenant ID ของคุณ
โทเค็นทึบกับองค์กร
โทเค็นทึบสามารถใช้ดึงข้อมูลสมาชิกองค์กรผ่าน userinfo endpoint ได้ เมื่อคุณร้องขอ scope urn:logto:scope:organizations userinfo endpoint จะคืนค่าการอ้างสิทธิ์ (claims) ที่เกี่ยวข้องกับองค์กรของผู้ใช้ เช่น organizations (organization IDs) และ organization_data
อย่างไรก็ตาม โทเค็นทึบไม่สามารถใช้เป็นโทเค็นองค์กร (organization token) ได้ โทเค็นองค์กรจะออกในรูปแบบ JWT เสมอ เพราะว่า:
- โทเค็นองค์กรมีการอ้างสิทธิ์เฉพาะองค์กร (เช่น
organization_idและสิทธิ์แบบ scoped) ที่ resource server ต้องตรวจสอบ - รูปแบบ JWT ช่วยให้ resource server ตรวจสอบโทเค็นและดึง context ขององค์กรได้โดยไม่ต้องเรียก API เพิ่มเติม
หากต้องการรับโทเค็นองค์กร คุณต้องใช้ refresh token flow หรือ client credentials flow พร้อมพารามิเตอร์ขององค์กร