Skip to main content

Django Authentication Backend for Single Sign-On via Kerberos SPNEGO

Project description

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 this is 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

See here for further excellent information!

Acknowledgements

Project details


Release history Release notifications | RSS feed

This version

5.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_auth_spnego2-5.1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

django_auth_spnego2-5.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file django_auth_spnego2-5.1.tar.gz.

File metadata

  • Download URL: django_auth_spnego2-5.1.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for django_auth_spnego2-5.1.tar.gz
Algorithm Hash digest
SHA256 7a5fe24100c368eb1b7ca73aa344fdab4453f62349006ae69dd4fc6a73263018
MD5 988b3d14053a0161862ac86ca196c3d6
BLAKE2b-256 afbe0c12c6a4e999579bc4605a953da0407f65a0b3c64f7dc60f8c49051ec498

See more details on using hashes here.

File details

Details for the file django_auth_spnego2-5.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_auth_spnego2-5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b3a90f469f1722f7473e99aa4a7f0656a19e907a00b6ed57e96c5455fb406e9e
MD5 03abbfdec0efd86bde5dcf51c8e62c87
BLAKE2b-256 3153c75f9235eb6b548ef59323a364e7c9e9975f0a90a2ef44566b1b5de214c7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page