Skip to main content

OAuth2 authentication plugin for Litestar

Project description

litestar-oauth

PyPI - Version PyPI - Python Version CI Coverage License - MIT

OAuth2 authentication plugin for Litestar.

Features

  • Async-First Design: Built on httpx for async HTTP operations
  • Pre-built Providers: GitHub, Google, Discord, and a generic provider for any OAuth2/OIDC service
  • Type-Safe: Full typing with Protocol-based interfaces
  • CSRF Protection: Built-in state management to prevent cross-site request forgery
  • Automatic Routes: Plugin registers login and callback routes automatically
  • Normalized User Data: Consistent user info format across all providers
  • Extensible: Easy to add custom providers for any OAuth2-compliant identity provider

Installation

uv add litestar-oauth

Or with pip:

pip install litestar-oauth

Quick Start

from litestar import Litestar
from litestar_oauth.contrib.litestar import OAuthPlugin, OAuthConfig

app = Litestar(
    plugins=[
        OAuthPlugin(
            config=OAuthConfig(
                redirect_base_url="https://example.com",
                github_client_id="your-client-id",
                github_client_secret="your-client-secret",
            )
        )
    ],
)

# Routes automatically registered:
# GET /auth/github/login    - Redirects to GitHub OAuth
# GET /auth/github/callback - Handles OAuth callback

Standalone Usage

Use the OAuth providers without the Litestar plugin:

from litestar_oauth.providers import GitHubOAuthProvider

provider = GitHubOAuthProvider(
    client_id="your-client-id",
    client_secret="your-client-secret",
)

# Generate authorization URL
auth_url = provider.get_authorization_url(
    redirect_uri="https://example.com/callback",
    state="random-state-token",
)

# After callback, exchange code for token
token = await provider.exchange_code(
    code="authorization-code",
    redirect_uri="https://example.com/callback",
)

# Get user info
user_info = await provider.get_user_info(token.access_token)
print(f"Hello, {user_info.username}!")

Supported Providers

Provider Class Default Scopes
GitHub GitHubOAuthProvider read:user, user:email
Google GoogleOAuthProvider openid, email, profile
Discord DiscordOAuthProvider identify, email
Generic GenericOAuthProvider Configurable

Use GenericOAuthProvider for any OAuth2/OIDC provider like Keycloak, Auth0, Okta, or Azure AD.

Optional Extras

# Apple Sign In (requires JWT signing)
uv add litestar-oauth[apple]

# All provider extras
uv add litestar-oauth[all]

Links

License

MIT License - see LICENSE for details.

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

litestar_oauth-0.1.0.tar.gz (33.8 kB view details)

Uploaded Source

Built Distribution

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

litestar_oauth-0.1.0-py3-none-any.whl (50.9 kB view details)

Uploaded Python 3

File details

Details for the file litestar_oauth-0.1.0.tar.gz.

File metadata

  • Download URL: litestar_oauth-0.1.0.tar.gz
  • Upload date:
  • Size: 33.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for litestar_oauth-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e4667d9644e6fffa782afe19b28877f553bbff9b942700acfa0484994ae4464e
MD5 cffda60ad5c06f9b447472094c813814
BLAKE2b-256 a7c26c08ba4d87efedcaf8e5239990f9d3d39d3c8a1706dbb2adba52112f9fbe

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_oauth-0.1.0.tar.gz:

Publisher: publish.yml on JacobCoffee/litestar-oauth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file litestar_oauth-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: litestar_oauth-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 50.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for litestar_oauth-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fea10d74da2a1c1f7fce947a72dbb822b2fdc8d67b5f5ac4652bc62604c57e65
MD5 256e61923de7907b83d6c5f1b32cc499
BLAKE2b-256 9c4162deacb5bfbe87531e5735b84e179f9952032a82bd7c24e310383041a430

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_oauth-0.1.0-py3-none-any.whl:

Publisher: publish.yml on JacobCoffee/litestar-oauth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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