Package: django-saml-sp
Installationpip install django-saml-spAddspto yourINSTALLED_APPSsettingLocal Test ApplicationStart the local SimpleSAML IdPdocker run -it --rm -p 8080:8080 -p 8443:8443 \
-e SIMPLESAMLPHP_SP_ENTITY_ID=http://localhost:8000/sso/local/ \
-e SIMPLESAMLPHP_SP_ASSERTION_CONSUMER_SERVICE=http://localhost:8000/sso/local/acs/ \
-e SIMPLESAMLPHP_SP_SINGLE_LOGOUT_SERVICE=http://localhost:8000/sso/local/slo/ \
kristophjunge/test-saml-idpBootstrap and run the local SP test apppython manage.py migrate
python manage.py bootstrap
python manage.py runserverThe test SAML IdP defines the following user accounts you can use for testing:UIDUsernamePasswordGroupEmail1user1user1passgroup1user1@example.com2user2user2passgroup2user2@example.comSustainsys Stub IdPThe bootstrap command also creates astubIdP which authenticates viahttps://stubidp.sustainsys.com. This is a good option if you don't want to run your own
local identity provider for testing.Integration GuideDjango SettingsAUTHENTICATION_BACKENDS- By default the Django authentication system is used to authenticate and log in users. Addsp.backends.SAMLAuthenticationBackendto yourAUTHENTICATION_BACKENDSsetting to authenticate using Django'sUsermodel. The user is looked up usingUser.USERNAME_FIELDmatching the SAMLnameid, and optionally created if it doesn't already exist. See theField Mappingsection below for how to map SAML attributes toUserattributes.LOGIN_REDIRECT_URL- This is the URL users will be redirected to by default after a successful login (or verification). Optional if you setIdP.login_redirector specify anextparameter in your login URL.LOGOUT_REDIRECT_URL- This is the URL users will be redirected to by default after a successful logout. Optional if you setIdP.logout_redirector specify anextparameter in your logout URL.SESSION_SERIALIZER- By default, Django usesdjango.contrib.sessions.serializers.JSONSerializer, which does not allow for setting specific expiration dates on sessions. If you want to use theIdP.respect_expirationflag to let the IdP dictate when the Django session should expire, you should change this todjango.contrib.sessions.serializers.PickleSerializer. But if you do not plan on using that feature, leave the default.Note: Django 4.1 forward now supports datetime session exipry using the default JSONSerializer.SP SettingsSP_IDP_LOADER- Allow you to specify a custom method for the SP views to retrieve anIdPinstance given a request and the URL path parameters.SP_AUTHENTICATE- A custom authentication method to use forIdPinstances that do not specify one. By default,sp.utils.authenticateis used (delegating to the auth backend).SP_LOGIN- A custom login method to use forIdPinstances that do not specify one. By default,sp.utils.loginis used (again, delegating to the auth backend).SP_LOGOUT- A custom logout method to use forIdPinstances that do not specify one. By default,sp.utils.logoutis used, which simply delegates to Django'sauth.logout.SP_PREPARE_REQUEST- A custom prepare_request method to use forIdPinstances that do not specify one. By default,sp.utils.prepare_requestis used.SP_UPDATE_USER- A custom update_user method to use forIdPinstances that do not specify one. By default,sp.utils.update_useris used, which updates user fields based on mapped SAML attributes when users are created, or when the attributes are set to always update.SP_UNIQUE_USERNAMES- WhenTrue(the default),SAMLAuthenticationBackendwill generate usernames unique to theIdPthat authenticated them, both when associating existing users and creating new users. This prevents user accounts from being linked to multiple IDPs (and prevents spoofing if untrusted IDPs can be configured).URLsThe application comes with a URLconf that can be included, using any path parameters you want. TheIdPis fetched by matching any URL parameters to theurl_paramsfield (or by some custom means viaSP_IDP_LOADERabove). For example:path("<prefix>/sso/<idp_slug>/",include("sp.urls"))Assuming the URL configuration above, and anIdPconfigured withurl_params={"prefix": "my", "idp_slug": "local"}, the following URLs would be available:URLDescription/my/sso/local/The entity ID, and metadata URL. Visiting this will produce metadata XML you can give to the IdP administrator./my/sso/local/acs/The Assertion Consumer Service (ACS). This is what the IdP will POST to upon a successful login./my/sso/local/slo/The Single Logout Service (SLO). The IdP will redirect to this URL when logging out of all SSO services./my/sso/local/login/URL to trigger the login sequence for this IdP. Available programmatically asidp.get_login_url(). Takes anextparameter to redirect to after login. Also takes areauthparameter to force the IdP to ask for credentials again (also see the verify URL below)./my/sso/local/test/URL to trigger an IdP login and display a test page containing all the SAML attributes passed back. Available programmatically asidp.get_test_url(). Does not actually perform a Django user login./my/sso/local/verify/URL to trigger a verification sequence for this IdP. Available programmatically asidp.get_verify_url(). Does not perform a Django user login, but does check that the user authenticated by the IdP matches the currentrequest.user./my/sso/local/logout/URL to trigger the logout sequence for this IdP. Available programmatically asidp.get_logout_url(). Takes anextparameter to redirect to after logout.You can also includesp.urlswithout any URL parameters (e.g.path("sso/", include("sp.urls"))) if only a singleIdPis needed (it should haveurl_params={}).Configuring an identity provider (IdP)Create anIdPmodel object, either via the Django admin or programmatically. If you have metadata from your IdP, you can enter the URL or XML now, but it is not required yet.Generate a certificate to use for SAML requests between your SP and this IdP. You may use the built-in admin action for this by going to the Django admin page for Identity Providers, checking the row(s) you want, and selecting "Generate certificates" from the Action dropdown. If you already have a certificate you want to use, you can paste it into the appropriate fields.Give your IdP administrator the Entity ID/Metadata URL and ACS URL, if they need to explicitly allow access or provide you attributes.At this point, if you didn't in step 1, you'll need to enter either the IdP metadata URL, or metadata XML directly. Saving will automatically trigger an import of the IdP metadata, so you should see the Last Import date update if successful. There is also an "Import metadata" admin action to trigger this manually.Your IdP is now ready for testing. On the admin page for your IdP object, there is a "Test IdP" button in the upper right corner. You can also visit the.../test/URL (see above) manually to initiate a test. A successful test of the IdP will show a page containing the NameID and SAML attributes provided by the IdP.