Python SDK built for Casdoor
Casdoor's SDK for Python will allow you to easily connect your application to the Casdoor authentication system without having to implement it from scratch.
Casdoor-python-sdk is available on PyPI:
$ pip install casdoor
Casdoor SDK is simple to use. We will show you the steps below.
Step1. Init Config
Initialization requires 5 parameters, which are all str type:
|Name (in order)||Must||Description|
|endpoint||Yes||Casdoor Server Url, such as
|certificate||Yes||Same as Casdoor certificate|
from casdoor import CasdoorSDK certificate = b'''-----BEGIN CERTIFICATE----- MIIE+TCCAuGgAwIBAgIDAeJAMA0GCSqGSIb3DQEBCwUAMDYxHTAbBgNVBAoTFENh ... -----END CERTIFICATE-----''' sdk = CasdoorSDK( endpoint, client_id, client_secret, certificate, org_name, )
Step2. Authorize with the Casdoor server
At this point, we should use some ways to verify with the Casdoor server.
To start, we want you understand clearly the verification process of Casdoor.
The following paragraphs will mention your app that wants to use Casdoor as a means
of verification as
APP, and Casdoor as
APPwill send a request to
Casdooris a UI-based OAuth provider, you cannot use request management service like Postman to send a URL with parameters and get back a JSON file.
Type in the URL in your browser in this format:
In this URL the
endpointis your Casdoor's location, as mentioned in Step1; then the
xxxneed to be filled out by yourself.
redirect_uriis the URL that your
APPis configured to listen to the response from
Casdoor. For example, if your
https://forum.casbin.com/callback, then Casdoor will send a request to this URL along with two parameters
state, which will be used in later steps for authentication.
stateis usually your Application's name, you can find it under the
Casdoor, and the leftmost
Namecolumn gives each application's name.
Of course you want your
APPto be able to send the URL. For example you should have something like a button, and it carries this URL. So when you click the button, you should be redirected to
Casdoorfor verification. For now you are typing it in the browser simply for testing.
Step3. Get token and parse
After Casdoor verification passed, it will be redirected to your application with code and state as said in Step2, like
Your web application can get the
code and call
get_oauth_token(code), then parse out jwt token.
The general process is as follows:
access_token = sdk.get_oauth_token(code) decoded_msg = sdk.parse_jwt_token(access_token)
decoded_msg is the JSON data decoded from the
access_token, which contains user info and other useful stuff.
Step4. Interact with the users
casdoor-python-sdk support basic user operations, like:
get_user(user_id: str), get one user by user name.
get_users(), get all users.
modify_user(method: str, user: User)/add_user(user: User)/update_user(user: User)/delete_user(user: User), write user to database.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Hashes for casdoor-1.1.1-py2.py3-none-any.whl