Add Alipay as an OAuth provider
This guide explains how to let users sign in to Casdoor with their Alipay account. The Alipay provider signs its requests with certificates.
Learning outcomes
- Get the APPID and the certificates of an Alipay application.
- Store the certificates in Casdoor.
- Add Alipay as an OAuth provider in Casdoor.
What you need
- A developer account on the Alipay Open Platform. See Preparation before access.
- Administrator access to the Casdoor admin console
Get the APPID and the certificates
- Create an application in the console of the Alipay Open Platform and note its APPID. See Find the APPID.
- Generate an RSA2 key pair as the Alipay documentation describes. You get
appPrivateKey.txtandappPublicKey.txt. - Upload the application certificate in the Alipay application and download three files:
alipayRootCert.crt,appCertPublicKey.crt, andalipayCertPublicKey.crt. - Set the callback URL of the Alipay application to the callback URL of Casdoor,
https://<your-casdoor-host>/callback. See Redirect URL and callback URL.
Store the certificates in Casdoor
In the Casdoor admin console, open the Certs page and add two certificates.
The application certificate:
| Casdoor field | Value |
|---|---|
| Type | x509 |
| Certificate | content of appCertPublicKey.crt |
| Private key | content of appPrivateKey.txt |
The root certificate:
| Casdoor field | Value |
|---|---|
| Type | x509 |
| Certificate | content of alipayCertPublicKey.crt |
| Private key | content of alipayRootCert.crt |
Add the provider in Casdoor
- Go to Identity > Providers and add a provider.
- Set Category to
OAuthand Type toAlipay. - Enter the APPID as the Client ID, and select the application certificate and the root certificate that you created.
- Save the provider.
User ID
Casdoor uses the user_id of Alipay as the ID of the user. Alipay applications that were created after the OpenID migration of Alipay return no user_id, so Casdoor uses the open_id instead. If the response has neither, the sign-in fails with alipay GetUserInfo error: both user_id and open_id are empty.
If you switch an Alipay application from user_id to open_id, the ID of each user changes. Users that signed in before then no longer match their linked Casdoor accounts.
Troubleshooting
If the sign-in fails, for example with asn1: syntax error: sequence truncated, check the following:
- In the application certificate, Certificate is
appCertPublicKey.crtand Private key isappPrivateKey.txt. - In the root certificate, Certificate is
alipayCertPublicKey.crtand Private key isalipayRootCert.crt. - The APPID belongs to the Alipay application.
- The callback URL is set correctly in Alipay and in Casdoor.