{"id":18612980,"url":"https://github.com/logto-io/python","last_synced_at":"2025-04-10T23:31:41.859Z","repository":{"id":186523443,"uuid":"675278576","full_name":"logto-io/python","owner":"logto-io","description":"🐍 Build authentication and authorization with Logto Python SDK.","archived":false,"fork":false,"pushed_at":"2024-11-28T08:45:08.000Z","size":289,"stargazers_count":7,"open_issues_count":3,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-08T21:45:57.226Z","etag":null,"topics":["authentication","authorization","jwt","logto","mfa","multi-tenant","oauth2","oidc","python","python3","saml","sso"],"latest_commit_sha":null,"homepage":"https://docs.logto.io/quick-starts/python","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/logto-io.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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},"funding":{"github":"logto-io"}},"created_at":"2023-08-06T11:49:27.000Z","updated_at":"2025-03-25T14:53:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"e2608b99-a61c-4883-b611-8e5f5e2962b8","html_url":"https://github.com/logto-io/python","commit_stats":null,"previous_names":["logto-io/python"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logto-io%2Fpython","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logto-io%2Fpython/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logto-io%2Fpython/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logto-io%2Fpython/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/logto-io","download_url":"https://codeload.github.com/logto-io/python/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248316205,"owners_count":21083403,"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":["authentication","authorization","jwt","logto","mfa","multi-tenant","oauth2","oidc","python","python3","saml","sso"],"created_at":"2024-11-07T03:19:21.787Z","updated_at":"2025-04-10T23:31:41.551Z","avatar_url":"https://github.com/logto-io.png","language":"Python","funding_links":["https://github.com/sponsors/logto-io"],"categories":[],"sub_categories":[],"readme":"# Logto Python SDK\n\n[![Logto](https://img.shields.io/badge/for-logto-7958ff)][Website]\n[![Stable Version](https://img.shields.io/pypi/v/logto?label=stable)][PyPI Releases]\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/logto)][PyPI]\n[![PyPI - License](https://img.shields.io/pypi/l/logto)](https://github.com/logto-io/python)\n[![Discord](https://img.shields.io/discord/965845662535147551?color=5865f2\u0026logo=discord\u0026label=discord)][Discord]\n\n## Prerequisites\n\n- Python 3.8 or higher\n- A [Logto Cloud][Website] account or a self-hosted Logto\n- A Logto traditional web application created\n\nIf you don't have the Logto application created, please follow the [⚡ Get started](https://docs.logto.io/docs/tutorials/get-started/) guide to create one.\n\n## Installation\n```bash\npip install logto # or `poetry add logto` or whatever you use\n```\n\n## Tutorial\n\nSee [tutorial](./docs/tutorial.md) for a quick start.\n\n## API reference\n\nSee [API reference](./docs/api.md) for more details.\n\n## Run the sample\n\nThere's a Flask sample in the [samples](./samples) directory. The sample has been tested with Python 3.8.17.\n\n### Install dependencies\n\nThis repo uses [PDM](https://github.com/pdm-project/pdm) as the package manager. To install the dependencies, run the following command in the root directory of the repo (not in the `samples` directory):\n\n```bash\npdm install\n```\n\n### Configure environment variables\n\nTo run the sample, you need to set the following environment variables:\n\n```bash\nAPP_SECRET_KEY=your-secret-key # This is for Flask\nLOGTO_ENDPOINT=http://your-logto-endpoint.com\nLOGTO_APP_ID=your-logto-app-id\nLOGTO_APP_SECRET=your-logto-app-secret\nLOGTO_REDIRECT_URI=http://127.0.0.1:5000/sign-in-callback\nLOGTO_POST_LOGOUT_REDIRECT_URI=http://127.0.0.1:5000/\n```\n\nReplace the values with your own.\n\nFor `LOGTO_REDIRECT_URI` and `LOGTO_POST_LOGOUT_REDIRECT_URI`, you should:\n\n1. Go to your Logto Console and add the URIs to the application's settings accordingly.\n2. Update the domain and port to match your local environment if necessary.\n\n\u003e [!Note]\n\u003e The sample project also support dotenv. You can create a `.env` file in the root directory of the sample project and add the environment variables there.\n\n### Run the sample\n\nIn the root directory of the repo, run the following command:\n\n```bash\npdm run flask\n```\n\nThe script can be found in the `pyproject.toml` file.\n\n### Fetch user information\n\nCall `client.getIdTokenClaims()` to get the basic user info. For a more detailed user info, you can call `client.fetchUserInfo()`.\n\nFor details on fetching user info, see the [Get user information](https://docs.logto.io/sdk/python/#get-user-information).\n\n### Route protection\n\nYou have many ways to accomplish this.\n\n**Directly check the user's authentication status**\n\nYou can call `client.isAuthenticated()` to check if the user is authenticated and can proceed with the request.\n\n**Use a decorator**\n\nYou can create a decorator like `@authenticated()` to protect your routes. A sample decorator can be found at [samples/authenticated.py](./samples/authenticated.py).\n\nFor instance, an API may throw a 401 error if the user is not authenticated:\n\n```python\nfrom flask import g, jsonify\n\n@app.route(\"/api/protected\")\n@authenticated()\ndef protected():\n    print(g.user) # The `@authenticated()` decorator sets the user object in the `g` object\n    return jsonify({\"message\": \"This is a protected route\"})\n```\n\nOr, you can redirect the user to the sign-in page:\n\n```python\nfrom flask import g, jsonify\n\n@app.route(\"/protected\")\n@authenticated(shouldRedirect=True)\ndef protected():\n    return \"This is a protected route\"\n```\n\nSee the [flask.py](./samples/flask.py) file for more details.\n\n## Resources\n\n- [Logto website][Website]\n- [Logto documentation](https://docs.logto.io/)\n- [Join Discord][Discord]\n\n[Website]: https://logto.io/\n[PyPI]: https://pypi.org/project/logto/\n[PyPI Releases]: https://pypi.org/project/logto/#history\n[Discord]: https://discord.gg/vRvwuwgpVX\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flogto-io%2Fpython","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flogto-io%2Fpython","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flogto-io%2Fpython/lists"}