22djangosaml2
33===========
44
5- .. image :: https://travis-ci.org/knaperek/djangosaml2.svg?branch=master
6- :target: https://travis-ci.org/knaperek/djangosaml2
7- :align: left
5+ .. image :: https://github.com/knaperek/djangosaml2/workflows/djangosaml2/badge.svg
6+ :target: https://github.com/knaperek/djangosaml2/workflows/djangosaml2/badge.svg
87
98
10- djangosaml2 is a Django application that integrates the PySAML2 library
11- into your project. This mean that you can protect your Django based project
12- with a service provider based on PySAML . This way it will talk SAML2 with
9+ A Django application that builds a Fully Compliant SAML2 Service Provider on top of PySAML2 library.
10+ This mean that you can protect your Django based project
11+ with a SAML2 SSO Authentication . This way it will talk SAML2 with
1312your Identity Provider allowing you to use this authentication mechanism.
1413This document will guide you through a few simple steps to accomplish
1514such goal.
@@ -82,15 +81,9 @@ A typical configuration would look like this::
8281 'djangosaml2.backends.Saml2Backend',
8382 )
8483
85- .. note ::
86-
87- Before djangosaml2 0.5.0 this authentication backend was
88- automatically added by djangosaml2. This turned out to be
89- a bad idea since some applications want to use their own
90- custom policies for authorization and the authentication
91- backend is a good place to define that. Starting from
92- djangosaml2 0.5.0 it is now possible to define such
93- backends.
84+ It is possible to subclass the provided Saml2Backend and customize the behaviour
85+ by overriding some methods. This way you can perform your custom cleaning or authorization
86+ policy, and modify the way users are looked up and created.
9487
9588Finally we have to tell Django what the new login url we want to use is::
9689
@@ -113,6 +106,24 @@ If you want to allow several authentication mechanisms in your project
113106you should set the LOGIN_URL option to another view and put a link in such
114107view to the ``/saml2/login/ `` view.
115108
109+ Handling Post-Login Redirects
110+ -----------------------------
111+ It is often desireable for the client to maintain the URL state (or at least manage it) so that
112+ the URL once authentication has completed is consistent with the desired application state (such
113+ as retaining query parameters, etc.) By default, the HttpRequest objects get_host() method is used
114+ to determine the hostname of the server, and redirect URL's are allowed so long as the destination
115+ host matches the output of get_host(). However, in some cases it becomes desireable for additional
116+ hostnames to be used for the post-login redirect. In such cases, the setting::
117+
118+ SAML_ALLOWED_HOSTS = []
119+
120+ May be set to a list of allowed post-login redirect hostnames (note, the URL components beyond the hostname
121+ may be specified by the client - typically with the ?next= parameter.)
122+
123+ In the absence of a ?next= parameter, the LOGIN_REDIRECT_URL setting will be used (assuming the destination hostname
124+ either matches the output of get_host() or is included in the SAML_ALLOWED_HOSTS setting)
125+
126+
116127Preferred Logout binding
117128------------------------
118129Use the following setting to choose your preferred binding for SP initiated logout requests::
@@ -206,6 +217,7 @@ We will see a typical configuration for protecting a Django project::
206217 'optional_attributes': ['eduPersonAffiliation'],
207218
208219 # in this section the list of IdPs we talk to are defined
220+ # This is not mandatory! All the IdP available in the metadata will be considered.
209221 'idp': {
210222 # we do not need a WAYF service since there is
211223 # only an IdP defined here. This IdP should be
@@ -320,7 +332,7 @@ Custom error handler
320332
321333When an error occurs during the authentication flow, djangosaml2 will render
322334a simple error page with an error message and status code. You can customize
323- this behaviour by specifying the path to your own error handler in the settings:
335+ this behaviour by specifying the path to your own error handler in the settings::
324336
325337 SAML_ACS_FAILURE_RESPONSE_FUNCTION = 'python.path.to.your.view'
326338
@@ -377,10 +389,12 @@ can set in the settings.py file::
377389
378390This setting is True by default.
379391
392+ The following setting lets you specify a URL for redirection after a successful
393+ authentication::
394+
380395 ACS_DEFAULT_REDIRECT_URL = reverse_lazy('some_url_name')
381396
382- This setting lets you specify a URL for redirection after a successful
383- authentication. Particularly useful when you only plan to use
397+ Particularly useful when you only plan to use
384398IdP initiated login and the IdP does not have a configured RelayState
385399parameter. The default is ``/ ``.
386400
@@ -524,9 +538,18 @@ following url::
524538Now if you go to the /test/ url you will see your SAML attributes and also
525539a link to do a global logout.
526540
527- You can also run the unit tests with the following command::
541+ Unit tests
542+ ==========
528543
544+ You can also run the unit tests as follows::
545+
546+ pip install -r requirements-dev.txt
547+ python3 tests/manage.py migrate
548+
529549 python tests/run_tests.py
550+ # or
551+ python tests/manage.py test -v 3
552+
530553
531554If you have `tox `_ installed you can simply call tox inside the root directory
532555and it will run the tests in multiple versions of Python.
0 commit comments