# Logto Python SDK
[![Logto](https://img.shields.io/badge/for-logto-7958ff)][Website]
[![Stable Version](https://img.shields.io/pypi/v/logto?label=stable)][PyPI Releases]
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/logto)][PyPI]
[![PyPI - License](https://img.shields.io/pypi/l/logto)](https://github.com/logto-io/python)
[![Discord](https://img.shields.io/discord/965845662535147551?color=5865f2&logo=discord&label=discord)][Discord]
## Prerequisites
- Python 3.8 or higher
- A [Logto Cloud][Website] account or a self-hosted Logto
- A Logto traditional web application created
If 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.
## Installation
```bash
pip install logto # or `poetry add logto` or whatever you use
```
## Tutorial
See [tutorial](./docs/tutorial.md) for a quick start.
## API reference
See [API reference](./docs/api.md) for more details.
## Run the sample
There's a Flask sample in the [samples](./samples) directory. The sample has been tested with Python 3.8.17.
### Install dependencies
This 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):
```bash
pdm install
```
### Configure environment variables
To run the sample, you need to set the following environment variables:
```bash
APP_SECRET_KEY=your-secret-key # This is for Flask
LOGTO_ENDPOINT=http://your-logto-endpoint.com
LOGTO_APP_ID=your-logto-app-id
LOGTO_APP_SECRET=your-logto-app-secret
LOGTO_REDIRECT_URI=http://127.0.0.1:5000/sign-in-callback
LOGTO_POST_LOGOUT_REDIRECT_URI=http://127.0.0.1:5000/
```
Replace the values with your own.
For `LOGTO_REDIRECT_URI` and `LOGTO_POST_LOGOUT_REDIRECT_URI`, you should:
1. Go to your Logto Console and add the URIs to the application's settings accordingly.
2. Update the domain and port to match your local environment if necessary.
> [!Note]
> 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.
### Run the sample
In the root directory of the repo, run the following command:
```bash
pdm run flask
```
The script can be found in the `pyproject.toml` file.
### Fetch user information
Call `client.getIdTokenClaims()` to get the basic user info. For a more detailed user info, you can call `client.fetchUserInfo()`.
For details on fetching user info, see the [Get user information](https://docs.logto.io/sdk/python/#get-user-information).
### Route protection
You have many ways to accomplish this.
**Directly check the user's authentication status**
You can call `client.isAuthenticated()` to check if the user is authenticated and can proceed with the request.
**Use a decorator**
You can create a decorator like `@authenticated()` to protect your routes. A sample decorator can be found at [samples/authenticated.py](./samples/authenticated.py).
For instance, an API may throw a 401 error if the user is not authenticated:
```python
from flask import g, jsonify
@app.route("/api/protected")
@authenticated()
def protected():
print(g.user) # The `@authenticated()` decorator sets the user object in the `g` object
return jsonify({"message": "This is a protected route"})
```
Or, you can redirect the user to the sign-in page:
```python
from flask import g, jsonify
@app.route("/protected")
@authenticated(shouldRedirect=True)
def protected():
return "This is a protected route"
```
See the [flask.py](./samples/flask.py) file for more details.
## Resources
- [Logto website][Website]
- [Logto documentation](https://docs.logto.io/)
- [Join Discord][Discord]
[Website]: https://logto.io/
[PyPI]: https://pypi.org/project/logto/
[PyPI Releases]: https://pypi.org/project/logto/#history
[Discord]: https://discord.gg/vRvwuwgpVX
Raw data
{
"_id": null,
"home_page": "https://logto.io/",
"name": "logto",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.8",
"maintainer_email": null,
"keywords": "logto, auth, user, authentication, authorization",
"author": null,
"author_email": "\"Silverhand Inc.\" <contact@silverhand.io>",
"download_url": "https://files.pythonhosted.org/packages/90/ac/03b8ee35bea23c54f8ab09b17288ea94d7ad830905ee94e95c6f8bafe49a/logto-0.2.1.tar.gz",
"platform": null,
"description": "# 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&logo=discord&label=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 [\u26a1 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> [!Note]\n> 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",
"bugtrack_url": null,
"license": "MIT",
"summary": "Logto Python SDK.",
"version": "0.2.1",
"project_urls": {
"Documentation": "https://github.com/logto-io/python/tree/master/docs",
"Homepage": "https://logto.io/",
"Repository": "https://github.com/logto-io/python"
},
"split_keywords": [
"logto",
" auth",
" user",
" authentication",
" authorization"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c54143460f2622b998ed89c076dcee506bbb6fb0420cf81c199d8ad97ff5ed10",
"md5": "c8bb2f02a3547e5b6da75e9d71854cec",
"sha256": "04288b9c9444464e32f838226b9220fda9e212de96589d5eb2d27d969cb6be7f"
},
"downloads": -1,
"filename": "logto-0.2.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c8bb2f02a3547e5b6da75e9d71854cec",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 20333,
"upload_time": "2024-04-19T14:31:59",
"upload_time_iso_8601": "2024-04-19T14:31:59.938439Z",
"url": "https://files.pythonhosted.org/packages/c5/41/43460f2622b998ed89c076dcee506bbb6fb0420cf81c199d8ad97ff5ed10/logto-0.2.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "90ac03b8ee35bea23c54f8ab09b17288ea94d7ad830905ee94e95c6f8bafe49a",
"md5": "defc2c4461a52357fdbb2a8c7fed9ab5",
"sha256": "33b2cc69fa254b5a01f31c1bc5f7bcf953f96d7259fa7b597f7dfc1f213c6f04"
},
"downloads": -1,
"filename": "logto-0.2.1.tar.gz",
"has_sig": false,
"md5_digest": "defc2c4461a52357fdbb2a8c7fed9ab5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 17489,
"upload_time": "2024-04-19T14:32:02",
"upload_time_iso_8601": "2024-04-19T14:32:02.635862Z",
"url": "https://files.pythonhosted.org/packages/90/ac/03b8ee35bea23c54f8ab09b17288ea94d7ad830905ee94e95c6f8bafe49a/logto-0.2.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-19 14:32:02",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "logto-io",
"github_project": "python",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "logto"
}