Casdoor Public API
ส่วนหน้าเว็บ UI ของ Casdoor เป็น SPA (Single-Page Application) ที่พัฒนาด้วย React. ส่วนหน้า React ใช้งาน RESTful API ของ Casdoor ที่เปิดเผยโดยโค้ดด้านหลังที่เขียนด้วย Go. RESTful API นี้เรียกว่า Casdoor Public API
. ในคำอื่น ๆ, ด้วยการเรียก HTTP, คุณสามารถทำทุกอย่างได้เหมือนกับที่ส่วนหน้าเว็บของ Casdoor ทำเอง. ไม่มีข้อจำกัดอื่น ๆ. API นี้สามารถใช้งานได้โดย:
- ส่วนหน้าของ Casdoor
- SDK ของลูกค้า Casdoor (เช่น, casdoor-go-sdk)
- โค้ดที่ปรับแต่งเองจากด้านแอปพลิเคชัน
ข้อมูลอ้างอิงเต็มรูปแบบสำหรับ Casdoor Public API
สามารถดูได้ที่ Swagger: https://door.casdoor.com/swagger. เอกสาร Swagger นี้ถูกสร้างขึ้นโดยอัตโนมัติโดยใช้เครื่องมือ Bee ของ Beego. หากคุณต้องการสร้างเอกสาร Swagger ด้วยตัวเอง, ดูที่: วิธีการสร้างไฟล์ swagger
Machine-to-Machine (M2M) Authentication
Machine-to-machine (M2M) authentication is designed for scenarios where services, applications, or backend systems need to authenticate and communicate with APIs without user interaction. This is particularly useful for:
- Backend services calling Casdoor APIs programmatically
- CLI tools that need to interact with your APIs using access tokens
- B2B enterprise scenarios where organizations need to generate tokens for API access (e.g., admin tokens for management operations, read tokens for data access)
- Automated processes such as scheduled jobs, data synchronization, or system integrations
- Microservices that need to authenticate with each other
Casdoor supports M2M authentication through the following methods:
Client Credentials Grant (OAuth 2.0): The recommended approach for M2M scenarios. This uses the Client Credentials Grant flow where applications authenticate using their
Client ID
andClient Secret
to obtain access tokens. See the OAuth Client Credentials Grant documentation for details on obtaining tokens via this flow.Direct API Authentication with Client ID and Secret: Use the application's credentials directly in API calls (see method #2 below).
Use Cases for M2M Authentication
Organization-level API access: In B2B scenarios, you can create a Casdoor application for each organization. The application's client credentials provide admin-level permissions for that organization, enabling them to manage their users, generate tokens, and access organizational resources independently.
Token generation for downstream services: Generate access tokens programmatically (using Client Credentials Grant) that can be distributed to CLI tools, read-only services, or other applications that need scoped access to your APIs.
Service-to-service authentication: Backend services can authenticate as an "application" rather than as a user, with permissions equivalent to the organization admin.
วิธีการตรวจสอบสิทธิ์กับ Casdoor Public API
1. โดย Access token
เราสามารถใช้ access token ที่ได้รับสำหรับผู้ใช้ที่ผ่านการตรวจสอบสิทธิ์เพื่อเรียก Casdoor Public API
ในฐานะผู้ใช้นั้นเอง.
วิธีการได้รับ access token?
แอปพลิเคชันสามารถได้รับ access token สำหรับผู้ใช้ Casdoor ที่สิ้นสุดกระบวนการเข้าสู่ระบบ OAuth (หรือได้รับโทเค็นโดยรหัสและสถานะ). สิทธิ์สำหรับการเรียก API จะเหมือนกับผู้ใช้.
ตัวอย่างด้านล่างแสดงวิธีการเรียกฟังก์ชัน GetOAuthToken()
ใน Go ผ่าน casdoor-go-sdk.
func (c *ApiController) Signin() {
code := c.Input().Get("code")
state := c.Input().Get("state")
token, err := casdoorsdk.GetOAuthToken(code, state)
if err != nil {
c.ResponseError(err.Error())
return
}
claims, err := casdoorsdk.ParseJwtToken(token.AccessToken)
if err != nil {
c.ResponseError(err.Error())
return
}
if !claims.IsAdmin {
claims.Type = "chat-user"
}
err = c.addInitialChat(&claims.User)
if err != nil {
c.ResponseError(err.Error())
return
}
claims.AccessToken = token.AccessToken
c.SetSessionClaims(claims)
c.ResponseOk(claims)
}
Access token ที่ได้รับอนุญาตทั้งหมดยังสามารถเข้าถึงได้ผ่านส่วนหน้าเว็บ UI โดยผู้ใช้ที่เป็นแอดมินในหน้า Tokens. ตัวอย่างเช่น, เยี่ยมชม: https://door.casdoor.com/tokens สำหรับเว็บไซต์ตัวอย่าง.
วิธีการตรวจสอบสิทธิ์?
พารามิเตอร์ HTTP
GET
, รูปแบบ URL คือ:/page?access_token=<The access token>
ตัวอย่างเว็บไซต์ตัวอย่าง:
https://door.casdoor.com/api/get-global-providers?access_token=eyJhbGciOiJSUzI1NiIs
โทเค็น Bearer ของ HTTP, รูปแบบส่วนหัว HTTP คือ:
Authorization: Bearer <The access token>
2. By Client ID
and Client secret
(Machine-to-Machine)
This method is the primary approach for machine-to-machine (M2M) authentication. It allows applications, services, or backend systems to authenticate with Casdoor APIs without any user interaction.
วิธีการได้รับ client ID และ secret?
หน้าแก้ไขแอปพลิเคชัน (เช่น, https://door.casdoor.com/applications/casbin/app-vue-python-example) จะแสดง client ID และ secret สำหรับแอปพลิเคชัน. This authentication method is useful when you want to call the API as a "machine", "application", or a "service" instead of a user. The permissions for the API calls will be the same as the application (equivalent to the admin of the organization).
Use cases
- Service authentication: Backend services calling Casdoor APIs programmatically
- Organization management: In B2B scenarios, create an application per organization to enable them to manage users and generate tokens independently
- Token generation: Obtain access tokens via the OAuth Client Credentials Grant flow for distribution to CLI tools or other services
วิธีการตรวจสอบสิทธิ์?
พารามิเตอร์ HTTP
GET
, รูปแบบ URL คือ:/page?clientId=<The client ID>&clientSecret=<the client secret>
ตัวอย่างเว็บไซต์ตัวอย่าง:
https://door.casdoor.com/api/get-global-providers?clientId=294b09fbc17f95daf2fe&clientSecret=dd8982f7046ccba1bbd7851d5c1ece4e52bf039d
HTTP Basic Authentication, รูปแบบส่วนหัว HTTP คือ:
Authorization: Basic <The Base64 encoding of client ID and client secret joined by a single colon ":">
หากคุณไม่คุ้นเคยกับการเข้ารหัส Base64, คุณสามารถใช้ไลบรารีเพื่อทำเพราะ HTTP Basic Authentication
เป็นมาตรฐานที่นิยมและได้รับการสนับสนุนจากหลายที่.
Obtaining access tokens with Client Credentials
For machine-to-machine scenarios where you need to obtain an access token (rather than using client credentials directly), use the OAuth 2.0 Client Credentials Grant flow:
Make a POST request to
https://<CASDOOR_HOST>/api/login/oauth/access_token
with:{
"grant_type": "client_credentials",
"client_id": "YOUR_CLIENT_ID",
"client_secret": "YOUR_CLIENT_SECRET"
}You will receive an access token response:
{
"access_token": "eyJhb...",
"token_type": "Bearer",
"expires_in": 10080,
"scope": "openid"
}Use the
access_token
to call Casdoor APIs (see method #1 above).
For more details, see the Client Credentials Grant documentation.
For B2B Enterprises: You can create separate Casdoor applications for each of your customer organizations. Each application has its own client_id
and client_secret
, which your customers can use to:
- Authenticate as their organization (with admin privileges)
- Generate access tokens for their users or services
- Manage their organization's users and permissions independently
- Integrate your APIs into their systems without UI-based login flows
This approach allows you to delegate organization management to your customers while maintaining security and isolation between different organizations.
3. โดย Access key
และ Access secret
เราสามารถใช้ access key และ access secret สำหรับผู้ใช้ Casdoor เพื่อเรียก Casdoor Public API
ในฐานะผู้ใช้นั้นเอง. Access key และ access secret สามารถกำหนดค่าได้ในหน้าการตั้งค่าผู้ใช้โดยแอดมินหรือผู้ใช้เอง. API update-user
ยังสามารถเรียกใช้เพื่ออัปเดตฟิลด์เหล่านี้ได้. สิทธิ์สำหรับการเรียก API จะเหมือนกับผู้ใช้.
วิธีการตรวจสอบสิทธิ์?
Create a pair of accessKey and accessSecret in account setting page.
พารามิเตอร์ HTTP
GET
, รูปแบบ URL คือ:/page?accessKey=<The user's access key>&accessSecret=<the user's access secret>"
ตัวอย่างเว็บไซต์ตัวอย่าง: https://door.casdoor.com/api/get-global-providers?accessKey=b86db9dc-6bd7-4997-935c-af480dd2c796/admin&accessSecret=79911517-fc36-4093-b115-65a9741f6b14
curl --location 'http://door.casdoor.com/api/user?accessKey=b86db9dc-6bd7-4997-935c-af480dd2c796&accessSecret=79911517-fc36-4093-b115-65a9741f6b14'
4. โดย username
และ password
วิธีการตรวจสอบสิทธิ์นี้ไม่ปลอดภัยและเก็บไว้ที่นี่เพื่อเข้ากันได้หรือเพื่อวัตถุประสงค์ในการสาธิตเท่านั้น. เราแนะนำให้ใช้วิธีการตรวจสอบสิทธิ์สามวิธีก่อนหน้านี้แทน.
จะเกิดอะไรขึ้น?
ข้อมูลประจำตัวของผู้ใช้จะถูกเปิดเผยเป็นพารามิเตอร์ GET
ใน URL ของคำขอ. นอกจากนี้, ข้อมูลประจำตัวของผู้ใช้จะถูกดักจับในรูปแบบข้อความธรรมดาโดยเครือข่ายหากคุณใช้ HTTP แทน HTTPS.
เราสามารถใช้ username และ password สำหรับผู้ใช้ Casdoor เพื่อเรียก Casdoor Public API
ในฐานะผู้ใช้นั้นเอง. username มีรูปแบบเป็น <ชื่อองค์กรของผู้ใช้>/<ชื่อผู้ใช้>
. สิทธิ์สำหรับการเรียก API จะเหมือนกับผู้ใช้.
วิธีการตรวจสอบสิทธิ์?
พารามิเตอร์ HTTP
GET
, รูปแบบ URL คือ:/page?username=<The user's organization name>/<The user name>&password=<the user's password>"
ตัวอย่างเว็บไซต์ตัวอย่าง: https://door.casdoor.com/api/get-global-providers?username=built-in/admin&password=123