Skip to content

Commit ca724eb

Browse files
committed
Merge branch 'authn_context' into dev
2 parents b6159a1 + c97924d commit ca724eb

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
@@ -213,6 +213,17 @@ This parameter can be combined with the IdP parameter if multiple IdPs are prese
213213
Currently there is support for a single IDPEntry in the IDPList.
214214

215215

216+
Authn Context
217+
=============
218+
219+
We can define the authentication context in settings.SAML_CONFIG['service']['sp'] as follows::
220+
221+
'requested_authn_context': {
222+
'authn_context_class_ref': saml2.saml.AUTHN_PASSWORD_PROTECTED,
223+
'comparison': "exact"
224+
}
225+
226+
216227
Custom and dynamic configuration loading
217228
========================================
218229

0 commit comments

Comments
 (0)