Skip to main content

Logto Python SDK.

Project description

Logto Python SDK

Logto Stable Version PyPI - Python Version PyPI - License Discord

Prerequisites

  • Python 3.8 or higher
  • A Logto Cloud 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 guide to create one.

Installation

pip install logto # or `poetry add logto` or whatever you use

Tutorial

See tutorial for a quick start.

API reference

See API reference for more details.

Run the sample

There's a Flask sample in the samples directory. The sample has been tested with Python 3.8.17.

Install dependencies

This repo uses 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):

pdm install

Configure environment variables

To run the sample, you need to set the following environment variables:

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:

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.

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.

For instance, an API may throw a 401 error if the user is not authenticated:

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:

from flask import g, jsonify

@app.route("/protected")
@authenticated(shouldRedirect=True)
def protected():
    return "This is a protected route"

See the flask.py file for more details.

Resources

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logto-0.2.1.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

logto-0.2.1-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file logto-0.2.1.tar.gz.

File metadata

  • Download URL: logto-0.2.1.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.14.0 CPython/3.12.3 Darwin/23.4.0

File hashes

Hashes for logto-0.2.1.tar.gz
Algorithm Hash digest
SHA256 33b2cc69fa254b5a01f31c1bc5f7bcf953f96d7259fa7b597f7dfc1f213c6f04
MD5 defc2c4461a52357fdbb2a8c7fed9ab5
BLAKE2b-256 90ac03b8ee35bea23c54f8ab09b17288ea94d7ad830905ee94e95c6f8bafe49a

See more details on using hashes here.

File details

Details for the file logto-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: logto-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 20.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.14.0 CPython/3.12.3 Darwin/23.4.0

File hashes

Hashes for logto-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 04288b9c9444464e32f838226b9220fda9e212de96589d5eb2d27d969cb6be7f
MD5 c8bb2f02a3547e5b6da75e9d71854cec
BLAKE2b-256 c54143460f2622b998ed89c076dcee506bbb6fb0420cf81c199d8ad97ff5ed10

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page