Auth0 server-side Python SDK
Project description
The Auth0 Server Python SDK is a library for implementing user authentication in Python applications.
📚 Documentation - 🚀 Getting Started - 💬 Feedback
Documentation
- Examples - examples for your different use cases.
- Docs Site - explore our docs site and learn more about Auth0.
Getting Started
1. Install the SDK
pip install auth0-server-python
If you’re using Poetry:
poetry install auth0-server-python
2. Create the Auth0 SDK client
Create an instance of the Auth0 client. This instance will be imported and used in anywhere we need access to the authentication methods.
from auth0_server_python.auth_server.server_client import ServerClient
auth0 = ServerClient(
domain='<AUTH0_DOMAIN>',
client_id='<AUTH0_CLIENT_ID>',
client_secret='<AUTH0_CLIENT_SECRET>',
secret='<AUTH0_SECRET>',
authorization_params= {
redirect_uri: '<AUTH0_REDIRECT_URI>',
}
)
The AUTH0_DOMAIN, AUTH0_CLIENT_ID, and AUTH0_CLIENT_SECRET can be obtained from the Auth0 Dashboard once you've created an application. This application must be a Regular Web Application.
The AUTH0_REDIRECT_URI tells Auth0 what URL to use while redirecting the user back after successful authentication, e.g. http://localhost:3000/auth/callback. Note: your application needs to handle this endpoint and call the SDK's complete_interactive_login(url: string) to finish the authentication process. See below for more information.
The AUTH0_SECRET is the key used to encrypt the session and transaction cookies. You can generate a secret using openssl:
openssl rand -hex 64
3. Add login to your Application (interactive)
Before using redirect-based login, ensure the redirect_uri is configured when initializing the SDK:
auth0 = ServerClient(
# ...
redirect_uri='<AUTH0_REDIRECT_URI>',
# ...
)
[!IMPORTANT]
You will need to register theAUTH0_REDIRECT_URIin your Auth0 Application as an Allowed Callback URLs via the Auth0 Dashboard.
In order to add login to any application, call start_interactive_login(), and redirect the user to the returned URL.
The implementation will vary based on the framework being used, but here is an example of what this would look like in FastAPI:
from fastapi import FastAPI, Request, Response
from starlette.responses import RedirectResponse
app = FastAPI()
@app.get("/auth/login")
async def login(request: Request):
authorization_url = await auth0.start_interactive_login()
return RedirectResponse(url=authorization_url)
Once the user has successfully authenticated, Auth0 will redirect the user back to the provided redirect_uri which needs to be handled in the application.
This implementation will also vary based on the framework used, but what needs to happen is:
- register an endpoint that will handle the configured
redirect_uri. - call the SDK's
complete_interactive_login(url), passing it the full URL, including query parameters.
Here is an example of what this would look like in FastAPI, with redirect_uri configured as http://localhost:3000/auth/callback:
@app.get("/auth/callback")
async def callback(request: Request):
result = await auth0.complete_interactive_login(str(request.url))
# Store session or set cookies as needed
return RedirectResponse(url="/")
4. Login with Custom Token Exchange
If you're migrating from a legacy authentication system or integrating with a custom identity provider, you can exchange external tokens for Auth0 tokens using the OAuth 2.0 Token Exchange specification (RFC 8693):
from auth0_server_python.auth_types import LoginWithCustomTokenExchangeOptions
# Exchange a custom token and establish a session
result = await auth0.login_with_custom_token_exchange(
LoginWithCustomTokenExchangeOptions(
subject_token="your-custom-token",
subject_token_type="urn:acme:mcp-token",
audience="https://api.example.com"
),
store_options={"request": request, "response": response}
)
# Access the user session
user = result.state_data["user"]
For advanced token exchange scenarios (without creating a session), use custom_token_exchange() directly:
from auth0_server_python.auth_types import CustomTokenExchangeOptions
# Exchange a custom token for Auth0 tokens
response = await auth0.custom_token_exchange(
CustomTokenExchangeOptions(
subject_token="your-custom-token",
subject_token_type="urn:acme:mcp-token",
audience="https://api.example.com",
scope="read:data write:data"
)
)
print(response.access_token)
For more details and examples, see examples/CustomTokenExchange.md.
Feedback
Contributing
We appreciate feedback and contribution to this repo! Before you get started, please read the following:
- Auth0's general contribution guidelines
- Auth0's code of conduct guidelines
- This repo's contribution guide
Raise an issue
To provide feedback or report a bug, please raise an issue on our issue tracker.
Vulnerability Reporting
Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.
What is Auth0?
Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout Why Auth0?
This project is licensed under the MIT license. See the LICENSE file for more info.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file auth0_server_python-1.0.0b8.tar.gz.
File metadata
- Download URL: auth0_server_python-1.0.0b8.tar.gz
- Upload date:
- Size: 38.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f426ced511d9d4e637c4700645dba7fa5e8d0f36a3a4e97ce0a71920e12991e3
|
|
| MD5 |
7369f40c416396e951a0044741519a19
|
|
| BLAKE2b-256 |
7686c435ad3165c1fa1556f9782a3a09f7ae772f6cd0ffd6f70c081cbebedcec
|
Provenance
The following attestation bundles were made for auth0_server_python-1.0.0b8.tar.gz:
Publisher:
publish.yml on auth0/auth0-server-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
auth0_server_python-1.0.0b8.tar.gz -
Subject digest:
f426ced511d9d4e637c4700645dba7fa5e8d0f36a3a4e97ce0a71920e12991e3 - Sigstore transparency entry: 924134602
- Sigstore integration time:
-
Permalink:
auth0/auth0-server-python@135f4642173d702a15cab6751d59a91295684df0 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/auth0
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@135f4642173d702a15cab6751d59a91295684df0 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file auth0_server_python-1.0.0b8-py3-none-any.whl.
File metadata
- Download URL: auth0_server_python-1.0.0b8-py3-none-any.whl
- Upload date:
- Size: 42.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a94ae99d441a2f6efa3f9bc4a42c8826ca2ad4ce4f76250fee5c366a077a669c
|
|
| MD5 |
d2667d7397bb2c68ed93a52a311cb731
|
|
| BLAKE2b-256 |
7d78fdf88160e7e07d9fa3a6e924a1e128cccd9e9a6e16c36f77573b90504c29
|
Provenance
The following attestation bundles were made for auth0_server_python-1.0.0b8-py3-none-any.whl:
Publisher:
publish.yml on auth0/auth0-server-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
auth0_server_python-1.0.0b8-py3-none-any.whl -
Subject digest:
a94ae99d441a2f6efa3f9bc4a42c8826ca2ad4ce4f76250fee5c366a077a669c - Sigstore transparency entry: 924134605
- Sigstore integration time:
-
Permalink:
auth0/auth0-server-python@135f4642173d702a15cab6751d59a91295684df0 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/auth0
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@135f4642173d702a15cab6751d59a91295684df0 -
Trigger Event:
workflow_dispatch
-
Statement type: