คู่มือการเชื่อมต่อ OpenID Connect กับ PSUSSO (Authentik)

คำแนะนำการใช้งาน OpenID Connect สำหรับการยืนยันตัวตน

ข้อมูลสำหรับทดสอบ (Client ID, Client Secret) ติดต่อทาง Microsoft Teams arafan.h@psu.ac.th

ลิ้งก์แบบฟอร์ม ขอใช้บริการระบบยืนยันตัวตน (สำหรับเชื่อมต่อ OpenID)

ข้อมูลสำคัญ OpenID Connect

รายการค่า/URL
Authorization Endpointhttps://psusso.psu.ac.th/application/o/authorize/
Token Endpointhttps://psusso.psu.ac.th/application/o/token/
UserInfo Endpointhttps://psusso.psu.ac.th/application/o/userinfo/
Logout Endpointhttps://psusso.psu.ac.th/application/o/[APP_NAME]/end-session/
JWKS URIhttps://psusso.psu.ac.th/application/o/[APP_NAME]/jwks/
Issuerhttps://psusso.psu.ac.th/application/o/[APP_NAME]/

การตั้งค่า Client Application

ลงทะเบียนแอปพลิเคชัน

กรอกข้อมูลขอ OpenID และกำหนด Permission ที่ต้องการในลิงก์ด้านบน

ข้อมูลที่จะได้รับหลังจากลงทะเบียน:
รายการค่าที่ได้รับ
Client IDclient_id_1234567890
Client Secretclient_secret_1234567890
OpenID Configuration URLhttps://psusso.psu.ac.th/application/o/[APP_NAME]/.well-known/openid-configuration

OAuth 2.0 Authorization Code Flow

ทุกภาษาสามารถใช้ Library สำเร็จรูปได้ หรือหากต้องการเขียน Authen flow เอง สามารถนำวิธีไปปรับใช้ตามภาษาที่ถนัด

ขั้นตอนการทำงานของ OAuth 2.0 Authorization Code:

Step 1: สร้าง URL สำหรับขออนุญาต
GET https://psusso.psu.ac.th/application/o/authorize/?
  response_type=code&
  client_id=CLIENT_ID&
  redirect_uri=REDIRECT_URI&
  scope=openid profile email psu_profile&
  state=RANDOM_STRING
Step 2: รับ Authorization Code

หลังจากผู้ใช้เข้าสู่ระบบสำเร็จ ระบบจะ redirect กลับมาพร้อม Authorization Code: https://yourapp.com/callback?code=AUTH_CODE&state=RANDOM_STRING

Step 3: แลกเปลี่ยน Authorization Code เป็น Access Token
POST https://psusso.psu.ac.th/application/o/token/
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&
code=AUTH_CODE&
redirect_uri=REDIRECT_URI&
client_id=CLIENT_ID&
client_secret=CLIENT_SECRET
Step 4: รับข้อมูลผู้ใช้
GET https://psusso.psu.ac.th/application/o/userinfo/
Authorization: Bearer ACCESS_TOKEN

Supported Scopes และ Claims

Supported Scopes

Scopes ที่รองรับในระบบ OpenID Connect:

  • psu_profile – ข้อมูลโปรไฟล์ PSU แบบละเอียด
  • email – ข้อมูลอีเมลผู้ใช้
  • offline_access – ขอ Refresh Token
  • openid – สำหรับ OpenID Connect
  • profile – ข้อมูลโปรไฟล์พื้นฐาน
Claims Supported

Claims ทั้งหมดที่รองรับในระบบ:

Claimคำอธิบายตัวอย่าง
subรหัสประจำตัวผู้ใช้1234567890
issIssuer ของ tokenhttps://psusso.psu.ac.th/application/o/[APP_NAME]/
audAudience ของ tokenclient_id
expExpiration time1735689599
iatIssued at time1735685999
auth_timeเวลาที่ผู้ใช้ทำการยืนยันตัวตน1735685999
acrAuthentication Context Class Referenceurn:mace:incommon:iap:silver
amrAuthentication Methods References["pwd"]
nonceค่า nonce จากการขอ tokenn-0S6_WzA2Mj
emailอีเมลsomchai.j@psu.ac.th
nameชื่อเต็มสมชาย ใจดี
preferred_usernameชื่อผู้ใช้ที่ต้องการแสดงsomchai.j
usernameชื่อผู้ใช้งานในระบบsomchai.j
display_nameชื่อแสดงสมชาย ใจดี
display_name_thชื่อแสดงภาษาไทยสมชาย ใจดี
first_nameชื่อSomchai
first_name_thชื่อภาษาไทยสมชาย
last_nameนามสกุลJaidee
last_name_thนามสกุลภาษาไทยใจดี
position_thตำแหน่งภาษาไทยนักวิชาการคอมพิวเตอร์
campus_thวิทยาเขตภาษาไทยวิทยาเขตหาดใหญ่
department_thภาควิชาภาษาไทยฝ่ายระบบโครงสร้างพื้นฐาน
faculty_thคณะภาษาไทยสำนักนวัตกรรมดิจิทัลและระบบอัจฉริยะ
office_name_thชื่อสำนักงานภาษาไทยฝ่ายระบบโครงสร้างพื้นฐาน สำนักนวัตกรรมดิจิทัลและระบบอัจฉริยะ
groupsกลุ่มผู้ใช้["xxx", "yyy"]
faculty_idรหัสคณะ01
department_idรหัสภาควิชา0103
campus_idรหัสวิทยาเขต1
psu_idPSU ID0045637

วิธีการแยก Staff/Student ให้แยกจาก List ของ groups

  • นักศึกษา = student
  • ศิษย์เก่า =alumni
  • บุคลากร = staff
  • อาจารย์ staff in groups และ position_th = “อาจารย์”

ตัวอย่างการเช็คสิทธิ์ (Python)

  • นักศึกษา if “student” in groups:
  • อาจารย์ if “staff” in groups and position_th == “อาจารย์”

ต้องการความช่วยเหลือเพิ่มเติม?

หากคุณมีเอกสาร คู่มือ หรือข้อเสนอแนะที่ต้องการแบ่งปันให้กับนักพัฒนาท่านอื่น สามารถส่งข้อมูลได้ที่: arafan.h@psu.ac.th (9045)

หมายเหตุ CitizenID จะไม่ได้รับเป็นค่าพื้นฐาน หากแอปพลิเคชันใดจำเป็นต้องใช้ ให้แจ้งเพิ่มเติม ในฟอร์มขอใช้บริการ

About The Author

Comments

Leave a Reply