Authorization Code Flow
เริ่มต้น OAuth 2.0 Authorization Code Flow เพื่อขอ authorization code สำหรับนำไปแลกเป็น tokens
Authorization code ที่ได้รับมีอายุ 5 นาที และสามารถใช้ได้เพียงครั้งเดียว
Endpoint
GET /oauth2/v1/authorizeRequest Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
client_id | string | ✅ | Client application ID |
redirect_uri | string | ✅ | Callback URL (ต้องตรงกับที่ลงทะเบียนไว้) |
response_type | string | ✅ | ต้องเป็น code |
scope | string | ✅ | Requested scopes (space-separated) เช่น openid profile email |
state | string | ✅ | Random string สำหรับ CSRF protection |
code_challenge | string | ⬜ | PKCE code challenge (แนะนำสำหรับ public clients) |
code_challenge_method | string | ⬜ | plain หรือ S256 (แนะนำ S256) |
source | string | ⬜ | ที่มาของ request เช่น desktop-web (default: desktop-web) |
user_id | string | ⬜ | User ID base64 encoded |
user_type | string | ⬜ | ประเภท user เช่น hrs, admin-acp |
instance_server_code | string | ⬜ | Instance/tenant code |
Available Scopes
| Scope | Description |
|---|---|
openid | Required สำหรับ OIDC (ได้รับ id_token) |
profile | ข้อมูล user profile (first_name, last_name, photograph, employee_code, user_type, instance_server_code) |
email | User email address |
Response Format
Success Response (HTTP 302 Redirect)
เมื่อ authorization สำเร็จ ระบบจะ redirect ไปยัง redirect_uri พร้อม authorization code:
HTTP/1.1 302 Found
Location: https://myapp.com/callback?code=AUTH_CODE_HERE&state=abc123Error Response — Invalid Client
{
"error": "Invalid client_id",
"message": "The provided client_id/redirect_uri does not exist or is not registered."
}Error Response — Invalid Scopes
{
"error": "Invalid scopes",
"message": "The provided scopes are not available for the client_id.",
"non_available_scopes": ["invalid_scope"]
}Code Examples
cURL
curl -X GET "https://auth.humansoft.co.th/oauth2/v1/authorize?\
client_id=my-app&\
redirect_uri=https://myapp.com/callback&\
response_type=code&\
scope=openid%20profile%20email&\
state=abc123&\
code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&\
code_challenge_method=S256"Notes
redirect_uriต้องตรงกับที่ลงทะเบียนไว้กับ client application เท่านั้นstateparameter ใช้สำหรับป้องกัน CSRF attack ควรเป็นค่า random ที่ไม่ซ้ำกัน และต้องตรวจสอบค่าที่ได้รับกลับมาด้วย
Related APIs
- Token Endpoint - แลก authorization code เป็น tokens (Step 2)
- User Info - ดึงข้อมูล user จาก access token
Last updated on