Skip to content

Commit d89e884

Browse files
committed
chore: IDP Scoping refactor and small code linting all around
2 parents 3a252c9 + c26cf7d commit d89e884

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

djangosaml2/views.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
UnsolicitedResponse)
4444
from saml2.s_utils import UnsupportedBinding
4545
from saml2.saml import SCM_BEARER
46-
from saml2.samlp import AuthnRequest
46+
from saml2.samlp import AuthnRequest, IDPEntry, IDPList, Scoping
4747
from saml2.sigver import MissingKey
4848
from saml2.validate import ResponseLifetimeExceed, ToEarly
4949

@@ -192,6 +192,16 @@ def get(self, request, *args, **kwargs):
192192
if selected_idp is None:
193193
selected_idp = list(configured_idps.keys())[0]
194194

195+
# perform IdP Scoping if scoping param is present
196+
idp_scoping_param = request.GET.get('scoping', None)
197+
if idp_scoping_param:
198+
idp_scoping = Scoping()
199+
idp_scoping.idp_list = IDPList()
200+
idp_scoping.idp_list.idp_entry.append(
201+
IDPEntry(provider_id = idp_scoping_param)
202+
)
203+
sso_kwargs['scoping'] = idp_scoping
204+
195205
# choose a binding to try first
196206
binding = getattr(settings, 'SAML_DEFAULT_BINDING',
197207
saml2.BINDING_HTTP_POST)
@@ -231,12 +241,11 @@ def get(self, request, *args, **kwargs):
231241
sign_requests = getattr(conf, '_sp_authn_requests_signed', False)
232242

233243
if sign_requests:
234-
sso_kwargs["sigalg"] = settings.SAML_CONFIG['service']['sp']\
235-
.get('signing_algorithm',
236-
saml2.xmldsig.SIG_RSA_SHA256)
237-
sso_kwargs["digest_alg"] = settings.SAML_CONFIG['service']['sp']\
238-
.get('digest_algorithm',
239-
saml2.xmldsig.DIGEST_SHA256)
244+
csc = settings.SAML_CONFIG['service']['sp']
245+
sso_kwargs["sigalg"] = csc.get('signing_algorithm',
246+
saml2.xmldsig.SIG_RSA_SHA256)
247+
sso_kwargs["digest_alg"] = csc.get('digest_algorithm',
248+
saml2.xmldsig.DIGEST_SHA256)
240249

241250
# pysaml needs a string otherwise: "cannot serialize True (type bool)"
242251
if getattr(conf, '_sp_force_authn', False):
@@ -609,10 +618,14 @@ class LogoutView(SPConfigMixin, View):
609618
logout_error_template = 'djangosaml2/logout_error.html'
610619

611620
def get(self, request, *args, **kwargs):
612-
return self.do_logout_service(request, request.GET, saml2.BINDING_HTTP_REDIRECT, *args, **kwargs)
621+
return self.do_logout_service(
622+
request, request.GET, saml2.BINDING_HTTP_REDIRECT, *args, **kwargs
623+
)
613624

614625
def post(self, request, *args, **kwargs):
615-
return self.do_logout_service(request, request.POST, saml2.BINDING_HTTP_POST, *args, **kwargs)
626+
return self.do_logout_service(
627+
request, request.POST, saml2.BINDING_HTTP_POST, *args, **kwargs
628+
)
616629

617630
def do_logout_service(self, request, data, binding):
618631
logger.debug('Logout service started')

docs/source/contents/setup.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ You can even configure the SAML cookie name as follows::
7070

7171
SAML_SESSION_COOKIE_NAME = 'saml_session'
7272

73+
Remember that in your browser "SameSite=None" attribute MUST also
74+
have the "Secure" attribute, which is required in order to use "SameSite=None".
75+
76+
SESSION_COOKIE_SECURE = True
77+
7378
.. Note::
7479

7580
djangosaml2 will attempt to set the ``SameSite`` attribute of the SAML session cookie to ``None`` so that it can be
@@ -201,6 +206,20 @@ For example::
201206

202207
see AARC Blueprint specs `here <https://zenodo.org/record/4596667/files/AARC-G061-A_specification_for_IdP_hinting.pdf>`_.
203208

209+
210+
IdP scoping
211+
===========
212+
The SP can suggest an IdP to a proxy by using the Scoping and IDPList elements in a SAML AuthnRequest. This is done using the `scoping` parameter to the login URL.
213+
214+
``https://sp.example.org/saml2/login/?scoping=https://idp.example.org``
215+
216+
This parameter can be combined with the IdP parameter if multiple IdPs are present in the metadata, otherwise the first is used.
217+
218+
``https://sp.example.org/saml2/login/?scoping=https://idp.example.org&idp=https://proxy.example.com/metadata``
219+
220+
Currently there is support for a single IDPEntry in the IDPList.
221+
222+
204223
Custom and dynamic configuration loading
205224
========================================
206225

0 commit comments

Comments
 (0)