Skip to content

Commit c97924d

Browse files
committed
SAML2 Request improved
* feat: sso_kwargs now handled with some custom methods ... that can be inherited :) * feat: authn context support, with or without this IdentityPython/pysaml2#807 (better with!) * feat: authn context documentation
1 parent 8c7a5e9 commit c97924d

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

djangosaml2/views.py

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
UnsolicitedResponse)
4444
from saml2.s_utils import UnsupportedBinding
4545
from saml2.saml import SCM_BEARER
46+
from saml2.saml import AuthnContextClassRef
47+
from saml2.samlp import RequestedAuthnContext
4648
from saml2.samlp import AuthnRequest, IDPEntry, IDPList, Scoping
4749
from saml2.sigver import MissingKey
4850
from saml2.validate import ResponseLifetimeExceed, ToEarly
@@ -133,6 +135,41 @@ def unknown_idp(self, request, idp):
133135
msg.format('Please contact technical support.'), status=403
134136
)
135137

138+
def load_sso_kwargs_scoping(self, sso_kwargs):
139+
""" Performs IdP Scoping if scoping param is present. """
140+
idp_scoping_param = self.request.GET.get('scoping', None)
141+
if idp_scoping_param:
142+
idp_scoping = Scoping()
143+
idp_scoping.idp_list = IDPList()
144+
idp_scoping.idp_list.idp_entry.append(
145+
IDPEntry(provider_id = idp_scoping_param)
146+
)
147+
sso_kwargs['scoping'] = idp_scoping
148+
149+
def load_sso_kwargs_authn_context(self, sso_kwargs):
150+
# this would work when https://github.com/IdentityPython/pysaml2/pull/807
151+
ac = getattr(self.conf, '_sp_requested_authn_context', {})
152+
153+
# this works even without https://github.com/IdentityPython/pysaml2/pull/807
154+
# hopefully to be removed soon !
155+
if not ac:
156+
scs = getattr(
157+
settings, 'SAML_CONFIG', {}
158+
).get('service', {}).get('sp', {})
159+
ac = scs.get('requested_authn_context', {})
160+
# end transitional things to be removed soon !
161+
162+
if ac:
163+
sso_kwargs["requested_authn_context"] = RequestedAuthnContext(
164+
authn_context_class_ref=[
165+
AuthnContextClassRef(ac['authn_context_class_ref']),
166+
],
167+
comparison = ac.get('comparison', "minimum"),
168+
)
169+
170+
def load_sso_kwargs(self, sso_kwargs):
171+
""" Inherit me if you want to put your desidered things in sso_kwargs """
172+
136173
def get(self, request, *args, **kwargs):
137174
logger.debug('Login process started')
138175
next_path = self.get_next_path(request)
@@ -166,6 +203,7 @@ def get(self, request, *args, **kwargs):
166203
configured_idps = available_idps(conf)
167204
selected_idp = request.GET.get('idp', None)
168205

206+
self.conf = conf
169207
sso_kwargs = {}
170208

171209
# Do we have a Discovery Service?
@@ -200,16 +238,6 @@ def get(self, request, *args, **kwargs):
200238
if selected_idp is None:
201239
selected_idp = list(configured_idps.keys())[0]
202240

203-
# perform IdP Scoping if scoping param is present
204-
idp_scoping_param = request.GET.get('scoping', None)
205-
if idp_scoping_param:
206-
idp_scoping = Scoping()
207-
idp_scoping.idp_list = IDPList()
208-
idp_scoping.idp_list.idp_entry.append(
209-
IDPEntry(provider_id = idp_scoping_param)
210-
)
211-
sso_kwargs['scoping'] = idp_scoping
212-
213241
# choose a binding to try first
214242
binding = getattr(settings, 'SAML_DEFAULT_BINDING',
215243
saml2.BINDING_HTTP_POST)
@@ -267,6 +295,15 @@ def get(self, request, *args, **kwargs):
267295
# custom nsprefixes
268296
sso_kwargs['nsprefix'] = get_namespace_prefixes()
269297

298+
299+
# Enrich sso_kwargs ...
300+
# idp scoping
301+
self.load_sso_kwargs_scoping(sso_kwargs)
302+
# authn context
303+
self.load_sso_kwargs_authn_context(sso_kwargs)
304+
# other customization to be inherited
305+
self.load_sso_kwargs(sso_kwargs)
306+
270307
logger.debug(f'Redirecting user to the IdP via {binding} binding.')
271308
_msg = 'Unable to know which IdP to use'
272309
http_response = None

docs/source/contents/setup.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,17 @@ This parameter can be combined with the IdP parameter if multiple IdPs are prese
221221
Currently there is support for a single IDPEntry in the IDPList.
222222

223223

224+
Authn Context
225+
=============
226+
227+
We can define the authentication context in settings.SAML_CONFIG['service']['sp'] as follows::
228+
229+
'requested_authn_context': {
230+
'authn_context_class_ref': saml2.saml.AUTHN_PASSWORD_PROTECTED,
231+
'comparison': "exact"
232+
}
233+
234+
224235
Custom and dynamic configuration loading
225236
========================================
226237

0 commit comments

Comments
 (0)