Skip to main content

Integrate Clerk with Django

Project description

Django Clerk Users

Integrate Clerk authentication with Django.

Note: This package is in early development (v0.0.2). APIs may change.

Features

  • Custom user model (ClerkUser) with Clerk integration
  • JWT token validation via Clerk SDK
  • Session-based authentication middleware (validates once, caches in session)
  • Webhook handling with Svix signature verification
  • Optional organizations support (separate sub-app)
  • Django REST Framework authentication (optional)

Installation

pip install django-clerk-users

For Django REST Framework support:

pip install django-clerk-users[drf]

Quick Start

1. Add to installed apps

INSTALLED_APPS = [
    # ...
    "django_clerk_users",
    # Optional: for organization support
    # "django_clerk_users.organizations",
]

2. Configure settings

# Required
CLERK_SECRET_KEY = "sk_live_..."  # From Clerk Dashboard
CLERK_WEBHOOK_SIGNING_KEY = "whsec_..."  # From Clerk Webhooks
CLERK_FRONTEND_HOSTS = ["https://your-app.com"]  # Your frontend URLs

# Optional
CLERK_SESSION_REVALIDATION_SECONDS = 300  # Re-validate JWT every 5 minutes
CLERK_CACHE_TIMEOUT = 300  # Cache timeout for user lookups

3. Set the user model

AUTH_USER_MODEL = "django_clerk_users.ClerkUser"

Or extend the abstract model for custom fields:

# myapp/models.py
from django_clerk_users.models import AbstractClerkUser

class CustomUser(AbstractClerkUser):
    company = models.CharField(max_length=255, blank=True)

    class Meta(AbstractClerkUser.Meta):
        swappable = "AUTH_USER_MODEL"

# settings.py
AUTH_USER_MODEL = "myapp.CustomUser"

4. Add middleware

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django_clerk_users.middleware.ClerkAuthMiddleware",  # Add after AuthenticationMiddleware
    # ...
]

5. Add authentication backend

AUTHENTICATION_BACKENDS = [
    "django_clerk_users.authentication.ClerkBackend",
]

6. Run migrations

python manage.py migrate

7. Configure webhooks

Add the webhook URL to your urls.py:

from django_clerk_users.webhooks import clerk_webhook_view

urlpatterns = [
    # ...
    path("webhooks/clerk/", clerk_webhook_view, name="clerk_webhook"),
]

Then configure your Clerk Dashboard to send webhooks to https://your-app.com/webhooks/clerk/.

Usage

Accessing the user in views

def my_view(request):
    if request.user.is_authenticated:
        # Access Clerk user attributes
        print(request.user.clerk_id)
        print(request.user.email)
        print(request.user.full_name)

        # Access organization (if using organizations)
        print(request.org)  # Organization ID from JWT

Decorators

from django_clerk_users.decorators import clerk_user_required

@clerk_user_required
def protected_view(request):
    # Only authenticated Clerk users can access
    return HttpResponse(f"Hello, {request.user.email}")

Django REST Framework

# settings.py
REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": [
        "django_clerk_users.authentication.ClerkAuthentication",
    ],
}

Organizations (Optional)

For Clerk organization support:

# settings.py
INSTALLED_APPS = [
    # ...
    "django_clerk_users",
    "django_clerk_users.organizations",
]

MIDDLEWARE = [
    # ...
    "django_clerk_users.middleware.ClerkAuthMiddleware",
    "django_clerk_users.organizations.middleware.ClerkOrganizationMiddleware",
]

Management Commands

# Sync users from Clerk
python manage.py sync_clerk_users

# Sync organizations from Clerk
python manage.py sync_clerk_organizations

Configuration Reference

Setting Required Default Description
CLERK_SECRET_KEY Yes - Your Clerk secret key
CLERK_WEBHOOK_SIGNING_KEY Yes* - Webhook signing secret (*required for webhooks)
CLERK_FRONTEND_HOSTS Yes [] Authorized frontend URLs
CLERK_SESSION_REVALIDATION_SECONDS No 300 JWT revalidation interval
CLERK_CACHE_TIMEOUT No 300 User cache timeout
CLERK_ORG_CACHE_TIMEOUT No 900 Organization cache timeout

License

MIT

Contributing

Contributions are welcome! Please open an issue or PR on GitHub.

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

django_clerk_users-0.0.2.tar.gz (48.2 kB view details)

Uploaded Source

Built Distribution

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

django_clerk_users-0.0.2-py3-none-any.whl (46.5 kB view details)

Uploaded Python 3

File details

Details for the file django_clerk_users-0.0.2.tar.gz.

File metadata

  • Download URL: django_clerk_users-0.0.2.tar.gz
  • Upload date:
  • Size: 48.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_clerk_users-0.0.2.tar.gz
Algorithm Hash digest
SHA256 3cbb4079512c6ec7435af199739474ef2a917e4948143f93cf8ba74dc891b811
MD5 826b95ca19498eeaa7ce8808a58d0000
BLAKE2b-256 8a5be38bc8578dbb9f8b7dfe7cef45a81d6c8f9cf48b1c611a111d9396ef30e9

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_clerk_users-0.0.2.tar.gz:

Publisher: main.yaml on jmitchel3/django-clerk-users

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

File details

Details for the file django_clerk_users-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_clerk_users-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0084728db398e9039f8106a9b13b844b97c539d65f9dd96993cd3f35b2dbd7a4
MD5 90ceb2836696662bcd18602b589e0166
BLAKE2b-256 35edecb5890feb114526906d4f1db084d848a33abafcce11f23fe3daaa1b06cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_clerk_users-0.0.2-py3-none-any.whl:

Publisher: main.yaml on jmitchel3/django-clerk-users

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