Python SDK built for Casdoor
Project description
casdoor-python-sdk
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 http://localhost:8000 |
client_id | Yes | Application.client_id |
client_secret | Yes | Application.client_secret |
jwt_secret | Yes | Same as Casdoor JWT secret |
org_name | Yes | Organization name |
from casdoor import CasdoorSDK
sdk = CasdoorSDK(
endpoint,
client_id,
client_secret,
jwt_secret,
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 Casdoor
.
-
APP
will send a request toCasdoor
.
SinceCasdoor
is 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. -
The simplest way to try it out is to type the URL in your browser (in which JavaScript can be executed to run the UI).
-
Type in the URL in your browser in this format:
endpoint/login/oauth/authorize?client_id=xxx&response_type=code&redirect_uri=xxx&scope=read&state=xxx
In this URL theendpoint
is your Casdoor's location, as mentioned in Step1; then thexxx
need to be filled out by yourself.
Hints:
-
redirect_uri
is the URL that yourAPP
is configured to listen to the response fromCasdoor
. For example, if yourredirect_uri
ishttps://forum.casbin.com/callback
, then Casdoor will send a request to this URL along with two parameterscode
andstate
, which will be used in later steps for authentication. -
state
is usually your Application's name, you can find it under theApplications
tab inCasdoor
, and the leftmostName
column gives each application's name. -
Of course you want your
APP
to 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 toCasdoor
for 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 https://forum.casbin.com/callback?code=xxx&state=yyyy
.
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file casdoor-1.1.0.tar.gz
.
File metadata
- Download URL: casdoor-1.1.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff6af10a9befe079b08744f0dc9268d89b6b7a6a454574b05bc25d950398dd0d |
|
MD5 | 2c60129deca9c9d3c089a8009c9bfa12 |
|
BLAKE2b-256 | ab59aac66e866a0085c961473a1c4210fed5f94be88a4b56f14697b91b9ae04d |
File details
Details for the file casdoor-1.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: casdoor-1.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3ae334e58c324a144027a5209f833a7ed7528546276afd7c40c21326a015a56 |
|
MD5 | e1eb6fb403a90397fd6a7945fee76239 |
|
BLAKE2b-256 | 07af510a6fd77a77b0e6d398234a21141b7cf6738ccdbc61a4fe070498631843 |