สารบัญ

ต่อระบบ

OIDC สำหรับทุกภาษา

endpoint, scope, authorization code flow ทีละขั้น และวิธีตรวจ access token

Pi Auth เป็น OpenID Connect provider มาตรฐาน ภาษาใดก็ตามที่มีไลบรารี OIDC — Go, Python, .NET, PHP, แอปมือถือ — ล็อกอินผู้ใช้และอ่าน claim ของ tenant กับสิทธิ์ชุดเดียวกับที่ SDK Next.js ใช้ได้

Endpoint

ทุกอย่างประกาศไว้ใน discovery document; ด้านล่างคือ path ที่ใช้บ่อย เปลี่ยน origin เป็น Pi Auth ของคุณ

EndpointPath
Discovery/.well-known/openid-configuration
Authorization/api/auth/oauth2/authorize
Token/api/auth/oauth2/token
UserInfo/api/auth/oauth2/userinfo
JWKS/api/auth/jwks
สิทธิ์ที่มีผล (เมื่อได้ perms_ref)/api/v1/me/permissions

Scope

Scopeเพิ่มอะไร
openidบังคับ — ออก id_token
profilename, picture
emailemail, email_verified
offline_accessrefresh token เพื่อต่ออายุ access token โดยไม่ต้องล็อกอินใหม่
tenanttid, tslug, mid, trole, org, pos, groups, attrs ใน access token
permissionsperms (หรือ perms_ref) ของแอปที่ระบุใน resource

ทำ flow เองทีละขั้น

  1. Redirect ไป /authorize

    ใช้ response_type=code, PKCE (S256), state และ nonce แบบสุ่ม และ resource indicator เพื่อให้ access token ออกสำหรับแอปของคุณ

    GET
    https://auth.suphark.com/api/auth/oauth2/authorize
      ?response_type=code
      &client_id=AOWrBtppsYLxThsohmXWS7JjCknWiog9
      &redirect_uri=https://pdf.suphark.com/auth/callback
      &scope=openid profile email offline_access tenant permissions
      &state=…&nonce=…
      &code_challenge=…&code_challenge_method=S256
      &resource=urn:pi:app:AOWrBtppsYLxThsohmXWS7JjCknWiog9
  2. แลก code

    confidential client ยืนยันตัวด้วย client_secret; public client อาศัย PKCE อย่างเดียว

    POST /api/auth/oauth2/token
    grant_type=authorization_code
    &code=…
    &redirect_uri=https://pdf.suphark.com/auth/callback
    &client_id=…&client_secret=…
    &code_verifier=…
    &resource=urn:pi:app:AOWrBtppsYLxThsohmXWS7JjCknWiog9
  3. ตรวจ access token

    • ลายเซ็นกับ JWKS (cache ไว้; key หมุนทุก 90 วัน)
    • iss ตรงกับ origin ของ Pi Auth, aud ตรงกับ urn:pi:app:<client_id>, exp ยังไม่หมด (เผื่อ ~60 วินาที)
    • ตรวจว่า nonce ใน id_token ตรงกับที่ส่งไป
  4. ใช้ claim

    ใช้ mid (membership) เป็น key ข้อมูลของคุณ ตรวจสิทธิ์ด้วย perms และแสดง org.path / pos.code ที่ UI ต้องการ รายการเต็มอยู่ใน Pi Auth ทำงานอย่างไร

  5. ต่ออายุก่อนหมด

    POST /api/auth/oauth2/token
    grant_type=refresh_token&refresh_token=…&client_id=…&client_secret=…

การเลือกองค์กร

ผู้ใช้ที่เป็นสมาชิกหลายองค์กรจะเลือกตอนล็อกอิน และโทเค็นผูกกับที่เลือก ถ้าแอปรู้อยู่แล้วว่าให้บริการ tenant ไหน ส่ง tenant_hint=<slug> ไปกับ authorize request (หรือใช้ auth subdomain ของ tenant): สมาชิกที่ active ของ tenant นั้นจะถูกสลับให้ — แม้มี session ของ Pi Auth อยู่แล้ว — และข้ามหน้าเลือก ถ้าไม่ได้เป็นสมาชิก โทเค็นจะเป็นของ tenant ที่ active อยู่ จึงต้องเทียบ tslug กับ tenant ที่ขอเสมอ (SDK Next.js ทำให้และล้มเหลวด้วย TENANT_MISMATCH)

ออกจากระบบ

ล้าง session cookie ของแอปเอง; session ของ Pi Auth อยู่ต่อจนหมดอายุหรือออกจากระบบที่นั่น เมื่อ session ถูกถอนในคอนโซล (โดยสมาชิกหรือผู้ดูแล) Pi Auth ส่ง event session.revoked และ SDK รับ OIDC back-channel logout token มาตรฐานที่ /auth/backchannel-logout ด้วย