Skip to main content

Civic Auth Python SDK

PyPI version Python Versions License: MIT

Python SDK for Civic Auth server-side authentication. This library provides a Python implementation that matches the API of the Node.js Civic Auth library.

What is Civic Auth?

Civic Auth is a flexible authentication and user management solution that provides seamless sign-in experiences for your applications. It supports multiple authentication methods including email, Google, passkeys, and wallet-based authentication, making it suitable for both traditional web applications and Web3 projects.

Key features:

  • Multiple sign-in options - Email, social providers, passkeys, and Web3 wallets
  • Privacy-preserving - Built with user privacy at its core
  • Flexible integration - Works with any web framework
  • Embedded wallets - Optional Web3 capabilities for blockchain applications
  • User verification - Support for identity proofs and credentials

Installation

pip install civic-auth

For specific framework support:

# Flask
pip install "civic-auth[flask]"

# FastAPI
pip install "civic-auth[fastapi]"

# Django
pip install "civic-auth[django]"

Quick Start

FastAPI Integration

Full example →

from fastapi import FastAPI, Depends
from civic_auth.integrations.fastapi import create_auth_router, create_auth_dependencies

app = FastAPI()

# Configure
config = {
    "client_id": "your-client-id",  # Get this from auth.civic.com
    "redirect_url": "..."
}

# Add auth routes  
app.include_router(create_auth_router(config))

# Create dependencies
civic_auth_dep, get_current_user, require_auth = create_auth_dependencies(config)

# Protected route
@app.get("/protected", dependencies=[Depends(require_auth)])
async def protected(user = Depends(get_current_user)):
    return f"Hello {user.name}!"

Flask Integration

Full example →

from flask import Flask
from civic_auth.integrations.flask import init_civic_auth, create_auth_blueprint, civic_auth_required

app = Flask(__name__)

# Configure and initialize
config = {
    "client_id": "your-client-id",  # Get this from auth.civic.com
    "redirect_url": "..."
}
init_civic_auth(app, config)
app.register_blueprint(create_auth_blueprint(config))

# Protected route
@app.route("/protected")
@civic_auth_required
async def protected():
    # user available via get_civic_user()
    ...

Django Integration

Full example →

# settings.py
MIDDLEWARE = [
    # ...
    'civic_auth.integrations.django.CivicAuthMiddleware',
]

CIVIC_AUTH = {
    'client_id': 'your-client-id',  # Get this from auth.civic.com
    'redirect_url': '...',
}

# urls.py
from civic_auth.integrations.django import get_auth_urls
urlpatterns = [
    path('', include(get_auth_urls())),  # Adds /auth/* routes
    # ...
]

# views.py
from civic_auth.integrations.django import civic_auth_required

@civic_auth_required
def protected(request):
    # user available via request.civic_user
    ...

Other Frameworks

from civic_auth import CivicAuth
from civic_auth.storage import AuthStorage

class MyStorage(AuthStorage):
    # Implement get/set/delete for your framework's session/cookies
    ...

storage = MyStorage()
civic_auth = CivicAuth(storage, config)

# Use the API
login_url = await civic_auth.build_login_url()
await civic_auth.resolve_oauth_access_code(code, state)
user = await civic_auth.get_user()

API Reference

CivicAuth Class

Methods

  • get_user() - Get the authenticated user information
  • get_tokens() - Get the stored OAuth tokens
  • is_logged_in() - Check if user is authenticated
  • build_login_url(scopes=None) - Build OAuth authorization URL
  • resolve_oauth_access_code(code, state) - Exchange auth code for tokens
  • refresh_tokens() - Refresh access tokens
  • build_logout_redirect_url() - Build logout URL
  • clear_tokens() - Clear all stored tokens

Types

from civic_auth.types import BaseUser, AuthConfig, Tokens

# BaseUser
{
    "id": str,
    "email": Optional[str],
    "username": Optional[str],
    "name": Optional[str],
    "given_name": Optional[str],
    "family_name": Optional[str],
    "picture": Optional[str],
    "updated_at": Optional[datetime]
}

# AuthConfig
{
    "client_id": str,  # Required - Get this from auth.civic.com
    "redirect_url": str,  # Required
    "oauth_server": Optional[str],  # Default: "https://auth.civic.com/oauth"
    "post_logout_redirect_url": Optional[str],
    "scopes": Optional[List[str]]  # Default: ["openid", "email", "profile"]
}

# Tokens
{
    "access_token": str,
    "id_token": str,
    "refresh_token": Optional[str],
    "token_type": str,
    "expires_in": Optional[int],
    "scope": Optional[str]
}

Examples

See the examples directory for complete working examples:

Development

Setup

# Clone the repository
git clone https://github.com/civicteam/civic-auth-py.git
cd civic-auth-py

# Create a virtual environment (recommended)
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install in development mode with all dependencies
pip install -e ".[dev,all]"

Testing

# Run tests
pytest

# Run with coverage
pytest --cov=civic_auth

Linting and Formatting

# Format code
black civic_auth tests

# Lint
ruff civic_auth tests

# Type checking
mypy civic_auth

License

MIT License - see LICENSE file for details.

Support

For issues and questions:

Metadata

Release files for civic-auth 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for civic-auth 0.1.2
File Size Uploaded Provenance Actions
civic_auth-0.1.2.tar.gz 22.9 kB Without provenance Details for civic_auth-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for civic-auth 0.1.2
File Interpreter ABI Platform Actions
civic_auth-0.1.2-py3-none-any.whl Python 3 none any Details for civic_auth-0.1.2-py3-none-any.whl

Total release size: 39.6 kB

Release files / civic_auth-0.1.2.tar.gz

Download URL civic_auth-0.1.2.tar.gz
Size 22.9 kB
Tags Source
SHA-256 checksum
How to use checksums
620fd788037579b74307beac94bb83923bb6ac63c50e22ed3a1d307d3d8a1b2c
BLAKE2b-256 checksum
How to use checksums
7f4e63cfd8ef102f05fd14f1e4e86df66751ce4a1f57c42cae229417d6cc1ae9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.13

Release files / civic_auth-0.1.2-py3-none-any.whl

Download URL civic_auth-0.1.2-py3-none-any.whl
Size 16.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0bf1bff7e90d75e0d9468b69852ff9c04c90566b73ef58274173008cf740382c
BLAKE2b-256 checksum
How to use checksums
c1781cca689de4de0aaa391da004f3720e1503186d0d15f71bb4b011fa850694
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.13

Security for civic-auth 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page