| 1 |
SAML |
| 2 |
---- |
| 3 |
|
| 4 |
SAML 2.0 is supported out of the box. However, the required dependencies are not |
| 5 |
installed by default. Therefore, you will need to specifcy the ``saml`` extra when |
| 6 |
installing the package:: |
| 7 |
|
| 8 |
$ pip install "django-allauth[saml]" |
| 9 |
|
| 10 |
Note that it will use some python wrappers for ``xml``. System packages may need |
| 11 |
to be installed (see `dependencies of Python3-SAML |
| 12 |
<https://github.com/SAML-Toolkits/python3-saml?tab=readme-ov-file#dependencies>`_). |
| 13 |
|
| 14 |
When you need to support SAML based authentication, often you need to support |
| 15 |
multiple organizations, each having their own SAML based Identity Provider |
| 16 |
(IdP). The way this translates to allauth is as follows: |
| 17 |
|
| 18 |
- The allauth SAML endpoints all include an organization slug. For example, |
| 19 |
``/accounts/saml/<organization_slug>/login/`` is used to initiate the SAML login |
| 20 |
flow. |
| 21 |
|
| 22 |
- The organization slug is used to lookup the SAML IdP configuration, which is |
| 23 |
stored in a ``SocialApp``, or, its settings based |
| 24 |
``SOCIALACCOUNT_PROVIDERS['saml']['APPS']`` counterpart. |
| 25 |
|
| 26 |
- As per the SAML specification, each IdP is identified by an entity ID. Entity |
| 27 |
IDs typically use a URI based notation, and are therefore not suitable to be |
| 28 |
used as the organization slug. So, you need to be aware that allauth has two |
| 29 |
ways of identifying the ``SocialApp`` -- via the SAML entity ID, and, via the |
| 30 |
organization slug. |
| 31 |
|
| 32 |
- For each user signing in via the SAML provider, a ``SocialAccount`` record is |
| 33 |
created. Here, the combination of ``SocialAccount.provider`` and |
| 34 |
``SocialAccount.uid`` is expected to be unique. The ``uid`` identifier is the |
| 35 |
SAML account ID which is locally unique within the IdP. Therefore, if you are |
| 36 |
using multiple IdP's ``SocialAccount.provider`` cannot be set to |
| 37 |
``"saml"``. Instead, the IdP entity ID would be a good candidate. |
| 38 |
|
| 39 |
- The SAML account attributes can differ per IdP. Therefore, additional |
| 40 |
configuration is needed to be able to extract relevant account attributes, |
| 41 |
such as the ``uid`` and ``email``. This is done by setting up an attribute |
| 42 |
mapping. |
| 43 |
|
| 44 |
How all of the above is configured in practice is shown below. Note that here we |
| 45 |
are using the settings based configuration, but you can setup the ``SocialApp`` |
| 46 |
via the Django admin as well: |
| 47 |
|
| 48 |
.. code-block:: python |
| 49 |
|
| 50 |
SOCIALACCOUNT_PROVIDERS = { |
| 51 |
"saml": { |
| 52 |
# Here, each app represents the SAML provider configuration of one |
| 53 |
# organization. |
| 54 |
"APPS": [ |
| 55 |
{ |
| 56 |
# Used for display purposes, e.g. over by: {% get_providers %} |
| 57 |
"name": "Acme Inc", |
| 58 |
|
| 59 |
# Accounts signed up via this provider will have their |
| 60 |
# `SocialAccount.provider` value set to this ID. The combination |
| 61 |
# of this value and the `uid` must be unique. The IdP entity ID is a |
| 62 |
# good choice for this. |
| 63 |
"provider_id": "urn:example.com", |
| 64 |
|
| 65 |
# The organization slug is configured by setting the |
| 66 |
# `client_id` value. In this example, the SAML login URL is: |
| 67 |
# |
| 68 |
# /accounts/saml/acme-inc/login/ |
| 69 |
"client_id": "acme-inc", |
| 70 |
|
| 71 |
# The fields above are common `SocialApp` fields. For SAML, |
| 72 |
# additional configuration is needed, which is placed in |
| 73 |
# `SocialApp.settings`: |
| 74 |
"settings": { |
| 75 |
|
| 76 |
# Mapping account attributes to upstream (IdP specific) attributes. |
| 77 |
# If left empty, an attempt will be done to map the attributes using |
| 78 |
# built-in defaults. |
| 79 |
"attribute_mapping": { |
| 80 |
"uid": "http://schemas.auth0.com/clientID", |
| 81 |
"email_verified": "http://schemas.auth0.com/email_verified", |
| 82 |
"email": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress", |
| 83 |
}, |
| 84 |
|
| 85 |
# The following setting allows you to force the use of nameID as email. |
| 86 |
# This can be useful if you are using a SAML IdP that is broken in some way and |
| 87 |
# does not allow use of the emailAddress nameid format |
| 88 |
"use_nameid_for_email": False, |
| 89 |
|
| 90 |
# The configuration of the IdP. |
| 91 |
"idp": { |
| 92 |
# The entity ID of the IdP is required. |
| 93 |
"entity_id": "urn:example.com", |
| 94 |
|
| 95 |
# Then, you can either specify the IdP's metadata URL: |
| 96 |
"metadata_url": "https://example.com/saml2/metadata", |
| 97 |
|
| 98 |
# Or, you can inline the IdP parameters here as follows: |
| 99 |
"sso_url": "https://example.com/saml2/sso", |
| 100 |
"slo_url": "https://example.com/saml2/slo", |
| 101 |
"x509cert": """ |
| 102 |
-----BEGIN CERTIFICATE----- |
| 103 |
MIIDHTCCAgWgAwIBAgIJLogff5x+S0BlMA0GCSqGSIb3DQEBCwUAMCwxKjAoBgNV |
| 104 |
BAMTIWRldi1uYXAybWY1ZTFwMXR3Z2Rv................................ |
| 105 |
................................G7qmyqcXRaf9HAuL/MvWz6zd96Ay6WHM |
| 106 |
pXk92/DyUV48JxK/Bl7Bj8qjl5w5R7Dwps6wj+69PIAg |
| 107 |
-----END CERTIFICATE----- |
| 108 |
""", |
| 109 |
}, |
| 110 |
# The configuration of the SP. |
| 111 |
"sp": { |
| 112 |
# Optional entity ID of the SP. If not set, defaults to the `saml_metadata` urlpattern |
| 113 |
"entity_id": "https://serviceprovider.com/sso/sp/metadata.xml", |
| 114 |
}, |
| 115 |
|
| 116 |
# Advanced settings. |
| 117 |
"advanced": { |
| 118 |
"allow_repeat_attribute_name": True, |
| 119 |
"allow_single_label_domains": False, |
| 120 |
"authn_request_signed": False, |
| 121 |
"digest_algorithm": "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", |
| 122 |
"logout_request_signed": False, |
| 123 |
"logout_response_signed": False, |
| 124 |
"metadata_signed": False, |
| 125 |
"name_id_encrypted": False, |
| 126 |
"name_id_format": "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified", |
| 127 |
"private_key": "MIID/zCCAuegAwIBAg...VGgdy+xoA==", |
| 128 |
"reject_deprecated_algorithm": True, |
| 129 |
# Due to security concerns, IdP initiated SSO is rejected by default. |
| 130 |
"reject_idp_initiated_sso": True, |
| 131 |
"signature_algorithm": "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", |
| 132 |
"want_assertion_encrypted": False, |
| 133 |
"want_assertion_signed": False, |
| 134 |
"want_attribute_statement": True, |
| 135 |
"want_message_signed": False, |
| 136 |
"want_name_id": False, |
| 137 |
"want_name_id_encrypted": False, |
| 138 |
"x509cert": "MIIEvQIBADANB...oddbXECo=", |
| 139 |
"metadata_valid_until": None, |
| 140 |
"metadata_cache_duration": None |
| 141 |
}, |
| 142 |
"contact_person": { |
| 143 |
"technical": { |
| 144 |
"givenName": "Alice", |
| 145 |
"emailAddress": "[email protected]", |
| 146 |
}, |
| 147 |
"administrative": { |
| 148 |
"givenName": "Bob", |
| 149 |
"emailAddress": "[email protected]", |
| 150 |
}, |
| 151 |
}, |
| 152 |
}, |
| 153 |
}, |
| 154 |
] |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
The ``advanced`` configuration corresponds to the SP. In particular, the ``x509cert`` |
| 160 |
is the public cert of the SP and ``private_key`` is the corresponding private key |
| 161 |
(this should remain secret, do not commit it or revoke it). |
| 162 |
By setting them, the messages are automatically signed. In order to use encryption, |
| 163 |
the attributes ``want_assertion_encrypted`` or ``want_name_id_encrypted`` should be |
| 164 |
set to ``True``. Encryption will use the same certificate as the signing. If signing |
| 165 |
or encryption are active, they will automatically appear in the metadata URL. |
| 166 |
|
| 167 |
In your templates, you can construct login URLs using the following template tag:: |
| 168 |
|
| 169 |
{% load socialaccount %} |
| 170 |
{% provider_login_url "<provider_id>" %} |
| 171 |
|
| 172 |
|
| 173 |
The SAML provider has the following endpoints: |
| 174 |
|
| 175 |
- ``/accounts/saml/<organization_slug>/login/``: Initiate a login. |
| 176 |
|
| 177 |
- ``/accounts/saml/<organization_slug>/acs/``: Assertion Consumer Service URL. |
| 178 |
|
| 179 |
- ``/accounts/saml/<organization_slug>/sls/``: Single Logout Service URL. |
| 180 |
|
| 181 |
- ``/accounts/saml/<organization_slug>/metadata/``: Metadata URL. |
| 182 |
|
| 183 |
Guidelines |
| 184 |
********** |
| 185 |
|
| 186 |
- Most SAML IdPs require TLS (formerly SSL) to be used, making testing with |
| 187 |
``runserver`` challenging. Make sure to configure Django to use HTTPS. |
| 188 |
- If using a reverse proxy, be sure to set Django settings |
| 189 |
``USE_X_FORWARDED_HOST = True``, |
| 190 |
``SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')``, and |
| 191 |
``SECURE_SSL_REDIRECT = True``. In your web server's reverse proxy |
| 192 |
configuration, ensure that you set request headers |
| 193 |
``X_FORWARDED_PROTO 'https' env=HTTPS`` and ``X-Forwarded-Ssl on``. |
| 194 |
- Cookies must also be secure; ensure that ``CSRF_COOKIE_DOMAIN`` and |
| 195 |
``SESSION_COOKIE_DOMAIN`` are set to ``yourdomain.com``, and that |
| 196 |
``CSRF_COOKIE_SECURE`` and ``SESSION_COOKIE_SECURE`` are ``True`` in your Django |
| 197 |
settings. |
| 198 |
- Test with your browser in privacy / incognito mode, check your developer |
| 199 |
console to ensure that cookies are being set correctly, and use a tool like |
| 200 |
SAML Tracer |
| 201 |
(`Firefox <https://addons.mozilla.org/en-US/firefox/addon/saml-tracer/>`_ |
| 202 |
/ `Chromium <https://chromewebstore.google.com/detail/saml-tracer/mpdajninpobndbfcldcmbpnnbhibjmch>`_) |
| 203 |
to inspect the SAML messages being exchanged. SAML Tracer is also useful for |
| 204 |
looking up the IdP SAML values to map to ``uid``, ``email``, and ``email_verified`` |
| 205 |
in the ``attribute_mapping`` configuration. |