Skip to main content

Django Nexus Auth

Published on Django Packages PyPI version

Django Nexus Auth is a Django package that provides OAuth authentication support following the Authentication Code Grant Flow with PKCE. It is designed to work seamlessly for Single-Page Applications that use Django REST Framework and simplejwt for authentication.

Features

  • Support for Microsoft Entra ID and Google
  • Provides API endpoints for facilitating OAuth 2.0 + OIDC authentication flow
  • Uses Proof Key for Code Exchange (PKCE) as defined in RFC 7636
  • Returns JWT tokens to the frontend client

Installation

pip install django-nexus-auth

Configuration

Define the configuration in your settings.py file:

NEXUS_AUTH = {
    "CONFIG": {
        "microsoft_tenant": {
            "client_id": "your-client-id",
            "client_secret": "your-client-secret",
            "tenant_id": "your-tenant-id",
        },
        "google": {
            "client_id": "your-client-id",
            "client_secret": "your-client-secret",
        },
    },
}

Add nexus_auth to your INSTALLED_APPS setting:

INSTALLED_APPS = [
    ...
    'nexus_auth',
]

Include the URLs in your project's URL configuration:

from django.urls import include, re_path

urlpatterns = [
    ...
    re_path(r"", include("nexus_auth.urls")),
]

API Endpoints

  • GET /oauth/providers: Get the active provider types and the corresponding authorization URLs.
  • POST /oauth/<str:provider_type>/exchange: Exchange the authorization code retrieved from the authorization URL for JWT tokens for your Django application.

Multi-Tenant Example

In a multi-tenant configuration, you may need to define different provider configurations for each tenant. In that case, you can use the PROVIDERS_HANDLER to dynamically define the provider configs from a request object, such as:

def your_handler_function(request):
    # Get the tenant from the request headers
    tenant = request.headers.get("X-Tenant")

    if tenant == "companyA":
        return { "microsoft_tenant": {
            "client_id": "... ",
            "client_secret": "... ",
            "tenant_id": " ... ",
        },
        "google": {
            "client_id": "...",
            "client_secret": "...",
        }}
    elif tenant == "companyB":
        return { "microsoft_tenant": {
            "client_id": "... ",
            "client_secret": "... ",
            "tenant_id": " ... ",
        }}

    return None

In this case, you would set the PROVIDERS_HANDLER to the path of your handler function:

NEXUS_AUTH = {
    "PROVIDERS_HANDLER": "path.to.your_handler_function",
}

Adding a new provider

Define the provider object and builder class for your new provider.

from nexus_auth.providers.base import ProviderBuilder, OAuth2IdentityProvider

# Extend OAuth2IdentityProvider class
class CustomProvider(OAuth2IdentityProvider):
    def get_authorization_url(self):
        return "https://your-provider.com/o/oauth2/authorize"

    def get_token_url(self):
        return "https://your-provider.com/o/oauth2/token"


# Define the builder class
class CustomProviderBuilder(ProviderBuilder):
    def __init__(self):
        self._instance = None

    def __call__(self, client_id, client_secret, **_ignored):
        if self._instance is None:
            self._instance = CustomProvider(client_id, client_secret)
        return self._instance

Register additional providers in the PROVIDER_BUILDERS setting:

NEXUS_AUTH = {
    "PROVIDER_BUILDERS": {
        "custom_provider_key": "path.to.CustomProviderBuilder",
    },
}

This will effectively add the new provider on top of the existing default providers.

Release files for django-nexus-auth 0.1.7

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

Source distribution (sdist)

Source distribution for django-nexus-auth 0.1.7
File Size Uploaded
django_nexus_auth-0.1.7.tar.gz 14.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-nexus-auth 0.1.7
File Interpreter ABI Platform
django_nexus_auth-0.1.7-py3-none-any.whl Python 3 none any Details

Total release size: 28.0 kB

Release files / django_nexus_auth-0.1.7.tar.gz

Download URL django_nexus_auth-0.1.7.tar.gz
Size 14.3 kB
Tags Source
SHA-256 checksum
How to use checksums
170abe388855ec003459018b43ca242e229fa4a793e216756fd5ea7f5ee20967
BLAKE2b-256 checksum
How to use checksums
55d28b2346ba5fd37e5421a61724355bfa1b4101251af2c5ba0681b9833ff1bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.11

Release files / django_nexus_auth-0.1.7-py3-none-any.whl

Download URL django_nexus_auth-0.1.7-py3-none-any.whl
Size 13.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
513a33b34b973813864f05cde1617989459e64ad9273b1e901c610757b5f495d
BLAKE2b-256 checksum
How to use checksums
ab3a97faf735eeba8ce03977b88564da4042d07ea056ef95c1c1c05870bfbf6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.11

Release history Release notifications | RSS feed

This release

0.1.7 This release

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

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