{"id":20215550,"url":"https://github.com/casdoor/casdoor-python-sdk","last_synced_at":"2025-04-04T13:04:01.545Z","repository":{"id":43078771,"uuid":"395005937","full_name":"casdoor/casdoor-python-sdk","owner":"casdoor","description":"Python client SDK for Casdoor","archived":false,"fork":false,"pushed_at":"2025-02-20T19:40:50.000Z","size":175,"stargazers_count":37,"open_issues_count":6,"forks_count":41,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-28T12:01:39.452Z","etag":null,"topics":["auth","authentication","authn","casdoor","oauth","oauth2","oidc","python","sdk","sso"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/casdoor","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/casdoor.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-11T13:51:37.000Z","updated_at":"2025-03-26T07:50:59.000Z","dependencies_parsed_at":"2024-01-13T00:34:59.440Z","dependency_job_id":"9c6ee510-1294-451b-a152-08f29292f836","html_url":"https://github.com/casdoor/casdoor-python-sdk","commit_stats":{"total_commits":89,"total_committers":22,"mean_commits":4.045454545454546,"dds":0.5842696629213483,"last_synced_commit":"275f78ba5c5d335d4dacc7234ecc3f168840747c"},"previous_names":[],"tags_count":44,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casdoor%2Fcasdoor-python-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casdoor%2Fcasdoor-python-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casdoor%2Fcasdoor-python-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casdoor%2Fcasdoor-python-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/casdoor","download_url":"https://codeload.github.com/casdoor/casdoor-python-sdk/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247174418,"owners_count":20896078,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auth","authentication","authn","casdoor","oauth","oauth2","oidc","python","sdk","sso"],"created_at":"2024-11-14T06:23:04.040Z","updated_at":"2025-04-04T13:04:01.500Z","avatar_url":"https://github.com/casdoor.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# casdoor-python-sdk\n\n[![GitHub Action](https://github.com/casdoor/casdoor-python-sdk/workflows/build/badge.svg?branch=master)](https://github.com/casdoor/casdoor-python-sdk/actions)\n[![Version](https://img.shields.io/pypi/v/casdoor.svg)](https://pypi.org/project/casdoor)\n[![PyPI - Wheel](https://img.shields.io/pypi/wheel/casdoor.svg)](https://pypi.org/project/casdoor)\n[![Pyversions](https://img.shields.io/pypi/pyversions/casdoor.svg)](https://pypi.org/project/casdoor)\n[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord\u0026label=discord\u0026color=5865F2)](https://discord.gg/5rPsrAzK7S)\n\nCasdoor's SDK for Python will allow you to easily connect your application to the Casdoor authentication system without having to implement it from scratch.\n\nCasdoor-python-sdk is available on PyPI:\n\n```console\n$ pip install casdoor\n```\n\nCasdoor SDK is simple to use. We will show you the steps below.\n\n## Step1. Init Config\nInitialization requires 5 parameters, which are all str type:\n| Name (in order)  | Must | Description                                         |\n| ---------------- | ---- | --------------------------------------------------- |\n| endpoint         | Yes  | Casdoor Server Url, such as `http://localhost:8000` |\n| client_id        | Yes  | Application.client_id                               |\n| client_secret    | Yes  | Application.client_secret                           |\n| certificate      | Yes  | Same as Casdoor certificate                         |\n| org_name         | Yes  | Organization name                                   |\n| application_name | Yes  | Application name                                    |\n\n```python\nfrom casdoor import CasdoorSDK\n\ncertificate = b'''-----BEGIN CERTIFICATE-----\nMIIE+TCCAuGgAwIBAgIDAeJAMA0GCSqGSIb3DQEBCwUAMDYxHTAbBgNVBAoTFENh\n...\n-----END CERTIFICATE-----'''\n\nsdk = CasdoorSDK(\n    endpoint,\n    client_id,\n    client_secret,\n    certificate,\n    org_name,\n    application_name,\n)\n```\n\nOR use async version\n\n```python\nfrom casdoor import AsyncCasdoorSDK\n\ncertificate = b'''-----BEGIN CERTIFICATE-----\nMIIE+TCCAuGgAwIBAgIDAeJAMA0GCSqGSIb3DQEBCwUAMDYxHTAbBgNVBAoTFENh\n...\n-----END CERTIFICATE-----'''\n\nsdk = AsyncCasdoorSDK(\n    endpoint,\n    client_id,\n    client_secret,\n    certificate,\n    org_name,\n    application_name,\n)\n```\n\n\n## Step2. Authorize with the Casdoor server\nAt this point, we should use some ways to verify with the Casdoor server.  \n\nTo start, we want you understand clearly the verification process of Casdoor.\nThe following paragraphs will mention your app that wants to use Casdoor as a means\nof verification as `APP`, and Casdoor as `Casdoor`.\n\n1. `APP` will send a request to `Casdoor`.  \n   Since `Casdoor` is a UI-based OAuth\n   provider, you cannot use request management service like Postman to send a URL\n   with parameters and get back a JSON file.  \n   \n\n2. 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).\n\n3. Type in the URL in your browser in this format:\n`endpoint/login/oauth/authorize?client_id=xxx\u0026response_type=code\u0026redirect_uri=xxx\u0026scope=read\u0026state=xxx`  \nIn this URL the `endpoint` is your Casdoor's location, as mentioned in Step1; then the `xxx` need to be filled out by yourself.  \n\nHints:  \n1. `redirect_uri` is the URL that your `APP` is configured to\nlisten to the response from `Casdoor`. For example, if your `redirect_uri` is `https://forum.casbin.com/callback`, then Casdoor will send a request to this URL along with two parameters `code` and `state`, which will be used in later steps for authentication.   \n\n2. `state` is usually your Application's name, you can find it under the `Applications` tab in `Casdoor`, and the leftmost `Name` column gives each application's name. \n\n3. 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 to `Casdoor` for verification. For now you are typing it in the browser simply for testing.\n   \n## Step3. Get token and parse\n\nAfter 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\u0026state=yyyy`.\n\nYour web application can get the `code` and call `get_oauth_token(code=code)`, then parse out jwt token.\n\nThe general process is as follows:\n\n```python\ntoken = sdk.get_oauth_token(code=code)\naccess_token = token.get(\"access_token\")\ndecoded_msg = sdk.parse_jwt_token(access_token) # or sdk.parse_jwt_token(access_token, kwargs)\n```\n\n`decoded_msg` is the JSON data decoded from the `access_token`, which contains user info and other useful stuff.\n\n## Step4. Interact with the users\n\ncasdoor-python-sdk support basic user operations, like:\n\n- `get_user(user_id: str)`, get one user by user name.\n- `get_users()`, get all users.\n- `modify_user(method: str, user: User)/add_user(user: User)/update_user(user: User)/delete_user(user: User)`, write user to database.\n- `refresh_token_request(refresh_token: str, scope: str)`, refresh access token\n- `enforce(self, permission_model_name: str, sub: str, obj: str, act: str, v3: Optional[str], v4: Optional[str], v5: Optional[str])`, check permission from model\n- `batch_enforce(self, permission_model_name: str, permission_rules: list[list[str]])`, batch check permission from model\n- `get_user_count(is_online: bool = None)`, get user count.\n\n## Resource Owner Password Credentials Grant\n\nIf your application doesn't have a frontend that redirects users to Casdoor and you have Password Credentials Grant enabled, then you may get access token like this:\n\n```python\ntoken = sdk.get_oauth_token(username=username, password=password)\naccess_token = token.get(\"access_token\")\ndecoded_msg = sdk.parse_jwt_token(access_token) # or sdk.parse_jwt_token(access_token, kwargs)\n```\n\n`decoded_msg` is the JSON data decoded from the `access_token`, which contains user info and other useful stuff.\n\n## Client Credentials Grant\n\nYou can also use Client Credentials Grant when your application does not have a frontend.\nIt is important to note that the AccessToken obtained in this way differs from other in that it corresponds to the application rather than to the user.\n\n```python\ntoken = sdk.get_oauth_token()\naccess_token = token.get(\"access_token\")\ndecoded_msg = sdk.parse_jwt_token(access_token) # or sdk.parse_jwt_token(access_token, kwargs)\n```\n\n`decoded_msg` is the JSON data decoded from the `access_token`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasdoor%2Fcasdoor-python-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcasdoor%2Fcasdoor-python-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasdoor%2Fcasdoor-python-sdk/lists"}