Skip to main content

django-auth-spngeo 🪄

Django authentication backend for adding Kerberos/GSS auth to your Django application for single sign-on.

Provides authentication backends and views that are ready-to-use without further modification or can be used as mixins and part of already-existing logic.

Requirements

  • Generated keytab file that's either at /etc/krb5.keytab or set via environment variable KRB5_KTNAME See here for excellent information on how to create one
  • A working Kerberos KDC (MIT, Microsoft AD DS, Heimdall, ...)
  • SPN (Service Principal Name) for your application server(s)
  • A method for mapping Kerberos Principals to User objects in your backend

Installation 👾

Install the package with pip:

pip install django-auth-spnego2

To use the auth backend in a Django project, add 'django_auth_spnego.backends.SpnegoModelBackend' to AUTHENTICATION_BACKENDS:

AUTHENTICATION_BACKENDS = [
    'django_auth_spnego.backends.SpnegoModelBackend',
]

If you want to use the pre-configured views to authenticate users, add django_auth_spnego to INSTALLED_APPS to be able to use the views:

INSTALLED_APPS = [
    ...
    'django_auth_spnego',
]

Then simply add the authentication view to your urls.py (alternatively use SpnegoLoginView for redirects):

from django_auth_spnego.views import SpnegoView

urls.append(r"^auth/spnego$", SpnegoView.as_view(), name="spnego")

Configuration 🛠️

# Optional setting to define which SPN to use in your keytab file. 
# If this is empty, all keytab entries will be used.
#   For example: `HTTP/sso.contoso.loc`
AUTH_KERBEROS_SPN: str = ''

# Split the Kerberos ticket UPN (User Principal Name) at the rightmost `@` sign. 
# This can be useful if you want to match the left part to Django's default 
# username or don't have your UPN's set up to match the e-mail address.
#       `Administrator@CONTOSO.LOC ==> Administrator`
#   This is only relevant when using the default authentication backend.
AUTH_KERBEROS_UPN_SPLIT: bool = True

# Which Django user field should be used for lookup (e.g. `username`, `email`). 
# If empty, the `USERNAME_FIELD` configured in the user model will be used instead.
#   This is only relevant when using the default authentication backend.
AUTH_KERBEROS_USERNAME_LOOKUP: str = ''

# Automatically create users attempting to authenticate that do not exist yet. 
#   This is only relevant when using the default authentication backend.
AUTH_KERBEROS_CREATE_UNKNOWN_USERS: bool = True

Advanced Usage Information & Client Auth

Optionally, combine Kerberos authentication with LDAP via django-auth-ldap to aggregate further user information from your Domain Controller after successful authentication – like display name, email address and group memberships.

from django_auth_ldap.backend import LDAPBackend
from django_auth_spnego.backends import SpnegoBackendMixin


class SpnegoLdapBackend(SpnegoBackendMixin, LDAPBackend):
    def get_user_from_username(self, username):
        return self.populate_user(username)

To test Kerberos authentication, acquire a ticket, and point your favorite supported client at the endpoint.

import requests
from requests_kerberos import HTTPKerberosAuth

r = requests.get('http://sso.contoso.loc/auth/spnego', auth=HTTPKerberosAuth())
r.status_code

To streamline authentication for function-based views, a decorator is available to automatically authenticate users when necessary. This is particularly useful for scripts accessing protected resources, as it eliminates the need to manually call an authentication endpoint in advance.

from django_auth_spnego.decorators import login_required_spnego

@login_required_spnego
def view(request):
    ...

See here for further excellent information!

Acknowledgements

Metadata

Release files for django-auth-spnego2 5.3

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-auth-spnego2 5.3
File Size Uploaded Provenance
django_auth_spnego2-5.3.tar.gz 11.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-auth-spnego2 5.3
File Interpreter ABI Platform
django_auth_spnego2-5.3-py3-none-any.whl Python 3 none any Details

Total release size: 23.2 kB

Release files / django_auth_spnego2-5.3.tar.gz

Download URL django_auth_spnego2-5.3.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
2510396cfdddb7f9e710b5f032682a5a010cbba6e398eac3560fe7d27a131073
BLAKE2b-256 checksum
How to use checksums
aa93fa25b5d6b51b5f7de35a5989c780980800cab58a5494e0eb2fe9620d8128
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release files / django_auth_spnego2-5.3-py3-none-any.whl

Download URL django_auth_spnego2-5.3-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
59787a04a3d778b461f41ec5023bbd1777a4a632143210bef80572d3a2685688
BLAKE2b-256 checksum
How to use checksums
1ac30b9c8f2654ac73a4c81a57126a8dbce710d7eddb30a67646b34d0ac4a64c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Security for django-auth-spnego2 5.3

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

5.3 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