Skip to main content

Python SDK for Civic Auth server-side authentication

Project description

Civic Auth Python SDK

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 Python 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", "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", "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',
    '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
    "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

# Install development dependencies
pip install -e ".[dev]"

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:

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

civic_auth-0.1.0.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

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

civic_auth-0.1.0-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: civic_auth-0.1.0.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for civic_auth-0.1.0.tar.gz
Algorithm Hash digest
SHA256 23e0b2a780339afffa26a030842afe7db0f6cb49b981cddb64eea5797c275742
MD5 ced68e92d1a8129d0a092e4586d9a318
BLAKE2b-256 d073406f0531cb3f1324c536ccec53c774b25d295b937b1115df2e8cfd2b207d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: civic_auth-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for civic_auth-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 55fa26941f6be833e56acc4e004071be216f271460e721ec0aa42d587be2dd9a
MD5 7d303a6782a8523848004ee2c656f8e3
BLAKE2b-256 263e0d81cddefbe6c9054583bb0f3abdcd26aebf420b6b55fcb7b704f0815117

See more details on using hashes here.

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