djangosaml2

1.12.0 · active · verified Tue Apr 14

djangosaml2 is a Python library that integrates `pysaml2` into Django applications, enabling SAML 2.0 based Single Sign-On (SSO). The current stable version is 1.12.0. It receives regular updates to support new Django and Python versions, often aligning with Django's release cycle.

Warnings

Install

Imports

Quickstart

This quickstart outlines the essential `settings.py` and `urls.py` configurations for `djangosaml2`. It involves adding the app and authentication backend, defining `LOGIN_URL`, and setting up the `SAML_CONFIG` dictionary which holds all PySAML2 related configurations, including SP entity ID, endpoints, attribute mappings, and IdP metadata. Ensure `xmlsec1` is installed at the OS level and paths to certificates are correct.

# settings.py
import os

INSTALLED_APPS = [
    # ... other Django apps
    'djangosaml2',
]

AUTHENTICATION_BACKENDS = [
    'django.contrib.auth.backends.ModelBackend', # Keep default for admin
    'djangosaml2.backends.Saml2Backend',
]

LOGIN_URL = '/saml2/login/'
SESSION_EXPIRE_AT_BROWSER_CLOSE = True # Recommended for SAML

BASEDIR = os.path.dirname(os.path.abspath(__file__))
SAML_CONFIG = {
    'xmlsec_binary': '/usr/bin/xmlsec1', # Adjust path if necessary
    'entityid': 'http://localhost:8000/saml2/metadata/', # Your SP Entity ID
    'service': {
        'sp': {
            'endpoints': {
                'assertion_consumer_service': [
                    ('http://localhost:8000/saml2/acs/', saml2.BINDING_HTTP_POST),
                ],
                'single_logout_service': [
                    ('http://localhost:8000/saml2/ls/', saml2.BINDING_HTTP_REDIRECT),
                    ('http://localhost:8000/saml2/ls/post', saml2.BINDING_HTTP_POST)
                ],
            },
            'allow_unsolicited': True, # Set to True for IdP-initiated SSO without prior SP request
            'name_id_format': saml2.NAMEID_FORMAT_UNSPECIFIED,
            'attribute_mapping': {
                'uid': ('username', ),
                'mail': ('email', ),
                'cn': ('first_name', ),
                'sn': ('last_name', ),
            },
            'metadata': {
                'remote': [{
                    'url': 'https://idp.example.com/saml/metadata/', # Your IdP's metadata URL
                }],
            },
            'key_file': os.path.join(BASEDIR, 'certs/private.key'), # Path to SP private key
            'cert_file': os.path.join(BASEDIR, 'certs/public.cert'),   # Path to SP public certificate
            'encryption_keypairs': [{
                'key_file': os.path.join(BASEDIR, 'certs/private.key'),
                'cert_file': os.path.join(BASEDIR, 'certs/public.cert'),
            }],
        },
    },
}


# urls.py
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('saml2/', include('djangosaml2.urls')),
    # Your other app URLs
]

view raw JSON →