SuperTokens Python SDK
repository·dev·Indexed 41 days ago
https://github.com/supertokens/supertokens-pythonA client-side library for Python backends that facilitates communication with the SuperTokens HTTP service to manage authentication and sessions. It provides integration examples for frameworks such as Django, FastAPI, and Flask, including support for the ThirdpartyEmailpassword recipe for social authentication via Google, Github, Apple, and Discord.
What's inside SuperTokens
- The SuperTokens Python SDK is a library designed to interface between a Python API process and the SuperTokens HTTP service. It enables developers to integrate SuperTokens' authentication and session management capabilities into Python-based backend applications.
Setup the Flask SuperTokens example environment
devTo set up the development environment for this Flask example, use the provided script to create a virtual environment and install all necessary dependencies.
source create_env.shRun the Flask SuperTokens web application
devOnce the environment is set up, you can start the Flask application by running
app.pywith Python.python app.pyInstall the FastAPI SuperTokens example
devTo set up the development environment and install all required dependencies for this example, run the provided setup script:
source create_env.shRun the Django SuperTokens web app
devOnce the environment is set up, run the Django migrations and start the development server using
manage.py.python manage.py migrate python manage.py runserverSetup the Django SuperTokens example environment
devTo set up the development environment for this Django example, use the provided script to create a virtual environment and install all necessary dependencies.
source create_env.shStart the FastAPI SuperTokens web app
devRun the application using
uvicorn. The server is configured to listen on all interfaces (0.0.0.0) at port3001with hot-reloading enabled.uvicorn main:app --reload --host=0.0.0.0 --port=3001Configure SuperTokens Core via Docker Compose
devThe
compose.yamlfile defines the deployment for the SuperTokens Core service and an OAuth test service. You can customize the Core service using environment variables for versioning and port mapping.Core Service Configuration
- Image Versioning: Controlled by the
SUPERTOKENS_CORE_VERSIONenvironment variable. If not provided, it defaults tomaster. - Port Mapping: The Core service listens on port
3567inside the container. You can map this to a different host port using theSUPERTOKENS_CORE_PORTenvironment variable (defaults to3567).
Core Service Environment Variables
When running the Core service, the following environment variables are used for configuration:
OAUTH_PROVIDER_PUBLIC_SERVICE_URL: The public service URL for the OAuth provider (e.g.,http://oauth:4444).OAUTH_PROVIDER_ADMIN_SERVICE_URL: The admin service URL for the OAuth provider (e.g.,http://oauth:4445).OAUTH_PROVIDER_CONSENT_LOGIN_BASE_URL: The base URL for OAuth consent login (e.g.,http://localhost:3001/auth).OAUTH_CLIENT_SECRET_ENCRYPTION_KEY: A key used for encrypting OAuth client secrets.INFO_LOG_PATH: Path for info logs (set to"null"to disable in this config).ERROR_LOG_PATH: Path for error logs (set to"null"to disable in this config).LOG_LEVEL: The logging verbosity (e.g.,DEBUG).
services: core: image: supertokens/supertokens-dev-postgresql:${SUPERTOKENS_CORE_VERSION:-master} ports: - ${SUPERTOKENS_CORE_PORT:-3567}:3567 environment: OAUTH_PROVIDER_PUBLIC_SERVICE_URL: http://oauth:4444 OAUTH_PROVIDER_ADMIN_SERVICE_URL: http://oauth:4445 OAUTH_PROVIDER_CONSENT_LOGIN_BASE_URL: http://localhost:3001/auth OAUTH_CLIENT_SECRET_ENCRYPTION_KEY: asdfasdfasdfasdfasdf INFO_LOG_PATH: "null" ERROR_LOG_PATH: "null" LOG_LEVEL: "DEBUG" oauth: image: supertokens/oauth2-test:latest- Image Versioning: Controlled by the
Integrate SuperTokens with Django using ThirdpartyEmailpassword
devThis example demonstrates how to integrate the
supertokens-pythonSDK with the Django framework using theThirdpartyEmailpasswordrecipe. This configuration allows you to implement social authentication for providers such as:- Google Workspaces
- Github
- Apple
- Discord
Use SuperTokens with Flask and ThirdpartyEmailpassword
devThis example demonstrates how to integrate the
supertokens-pythonlibrary with the Flask web framework using theThirdpartyEmailpasswordrecipe. This configuration allows you to implement social authentication for providers such as:- Google Workspaces
- Github
- Apple
- Discord
Use SuperTokens with FastAPI and ThirdpartyEmailpassword
devThis example demonstrates how to integrate the
supertokens-pythonlibrary with the FastAPI framework using theThirdpartyEmailpasswordrecipe. This configuration allows you to implement social authentication for providers such as:- Google Workspaces
- Github
- Apple
- Discord