OAuth2 authentication plugin for Litestar.
- 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
uv add litestar-oauthOr with pip:
pip install litestar-oauthfrom 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 callbackUse 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}!")| Provider | Class | Default Scopes |
|---|---|---|
| GitHub | GitHubOAuthProvider |
read:user, user:email |
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.
# Apple Sign In (requires JWT signing)
uv add litestar-oauth[apple]
# All provider extras
uv add litestar-oauth[all]MIT License - see LICENSE for details.