Skip to content

Commit 507a3fc

Browse files
committed
Add new config option requested_authn_context
Signed-off-by: Ivan Kanakarakis <ivan.kanak@gmail.com>
1 parent 0228a52 commit 507a3fc

File tree

5 files changed

+83
-29
lines changed

5 files changed

+83
-29
lines changed

src/saml2/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ def prepare_for_negotiated_authenticate(
144144
sign_post = False if binding == BINDING_HTTP_REDIRECT else sign
145145
sign_redirect = False if binding == BINDING_HTTP_POST and sign else sign
146146

147+
requested_authn_context = (
148+
kwargs.get("requested_authn_context")
149+
or self.config.getattr("requested_authn_context")
150+
)
147151
reqid, request = self.create_authn_request(
148152
destination=destination,
149153
vorg=vorg,
@@ -155,6 +159,7 @@ def prepare_for_negotiated_authenticate(
155159
sign=sign_post,
156160
sign_alg=sigalg,
157161
digest_alg=digest_alg,
162+
requested_authn_context=requested_authn_context,
158163
**kwargs,
159164
)
160165

src/saml2/client_base.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
from saml2.profile import paos, ecp
1818
from saml2.saml import NAMEID_FORMAT_PERSISTENT
1919
from saml2.saml import NAMEID_FORMAT_TRANSIENT
20-
from saml2.samlp import AuthnQuery, RequestedAuthnContext
20+
from saml2.saml import AuthnContextClassRef
21+
from saml2.samlp import AuthnQuery
22+
from saml2.samlp import RequestedAuthnContext
2123
from saml2.samlp import NameIDMappingRequest
2224
from saml2.samlp import AttributeQuery
2325
from saml2.samlp import AuthzDecisionQuery
@@ -358,18 +360,30 @@ def create_authn_request(
358360
provider_name = self._my_name()
359361
args["provider_name"] = provider_name
360362

363+
requested_authn_context = (
364+
kwargs.pop("requested_authn_context", None)
365+
or self.config.getattr("requested_authn_context", "sp")
366+
or {}
367+
)
368+
if requested_authn_context:
369+
args["requested_authn_context"] = RequestedAuthnContext(
370+
authn_context_class_ref=[
371+
AuthnContextClassRef(accr)
372+
for accr in requested_authn_context.get("authn_context_class_ref", [])
373+
],
374+
comparison=requested_authn_context.get("comparison"),
375+
)
376+
361377
# Allow argument values either as class instances or as dictionaries
362378
# all of these have cardinality 0..1
363379
_msg = AuthnRequest()
364-
for param in ["scoping", "requested_authn_context", "conditions", "subject"]:
380+
for param in ["scoping", "conditions", "subject"]:
365381
_item = kwargs.pop(param, None)
366382
if not _item:
367383
continue
368384

369385
if isinstance(_item, _msg.child_class(param)):
370386
args[param] = _item
371-
elif isinstance(_item, dict):
372-
args[param] = RequestedAuthnContext(**_item)
373387
else:
374388
raise ValueError("Wrong type for param {name}".format(name=param))
375389

tests/servera_conf.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from saml2 import BINDING_HTTP_POST
55
from saml2 import BINDING_HTTP_REDIRECT
66
from saml2 import BINDING_HTTP_ARTIFACT
7+
from saml2.authn_context import PASSWORDPROTECTEDTRANSPORT as AUTHN_PASSWORD_PROTECTED
8+
from saml2.authn_context import TIMESYNCTOKEN as AUTHN_TIME_SYNC_TOKEN
79
from saml2.saml import NAMEID_FORMAT_TRANSIENT
810
from saml2.saml import NAMEID_FORMAT_PERSISTENT
911

@@ -42,8 +44,17 @@
4244
"required_attributes": ["surName", "givenName", "mail"],
4345
"optional_attributes": ["title", "eduPersonAffiliation"],
4446
"idp": ["urn:mace:example.com:saml:roland:idp"],
45-
"name_id_format": [NAMEID_FORMAT_TRANSIENT,
46-
NAMEID_FORMAT_PERSISTENT]
47+
"name_id_format": [
48+
NAMEID_FORMAT_TRANSIENT,
49+
NAMEID_FORMAT_PERSISTENT,
50+
],
51+
"requested_authn_context": {
52+
"authn_context_class_ref": [
53+
AUTHN_PASSWORD_PROTECTED,
54+
AUTHN_TIME_SYNC_TOKEN,
55+
],
56+
"comparison": "exact",
57+
},
4758
}
4859
},
4960
"debug": 1,

tests/test_31_config.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
import logging
66
from saml2.mdstore import MetadataStore, name
77

8-
from saml2 import BINDING_HTTP_REDIRECT, BINDING_SOAP, BINDING_HTTP_POST
9-
from saml2.config import SPConfig, IdPConfig, Config
10-
from saml2.saml import AUTHN_PASSWORD_PROTECTED, AuthnContextClassRef
11-
from saml2.samlp import RequestedAuthnContext
8+
from saml2 import BINDING_HTTP_REDIRECT
9+
from saml2 import BINDING_SOAP
10+
from saml2.config import Config
11+
from saml2.config import IdPConfig
12+
from saml2.config import SPConfig
13+
from saml2.authn_context import PASSWORDPROTECTEDTRANSPORT as AUTHN_PASSWORD_PROTECTED
14+
from saml2.authn_context import TIMESYNCTOKEN as AUTHN_TIME_SYNC_TOKEN
1215
from saml2 import logger
1316

1417
from pathutils import dotname, full_path
1518
from saml2.sigver import security_context, CryptoBackendXMLSecurity
1619

20+
1721
sp1 = {
1822
"entityid": "urn:mace:umu.se:saml:roland:sp",
1923
"service": {
@@ -29,12 +33,13 @@
2933
{'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect':
3034
'http://localhost:8088/sso/'}},
3135
},
32-
"requested_authn_context": RequestedAuthnContext(
33-
authn_context_class_ref=[
34-
AuthnContextClassRef(AUTHN_PASSWORD_PROTECTED),
35-
],
36-
comparison="exact",
37-
),
36+
"requested_authn_context": {
37+
"authn_context_class_ref": [
38+
AUTHN_PASSWORD_PROTECTED,
39+
AUTHN_TIME_SYNC_TOKEN,
40+
],
41+
"comparison": "exact",
42+
},
3843
}
3944
},
4045
"key_file": full_path("test.key"),
@@ -218,13 +223,23 @@ def test_1():
218223

219224
assert len(c._sp_idp) == 1
220225
assert list(c._sp_idp.keys()) == ["urn:mace:example.com:saml:roland:idp"]
221-
assert list(c._sp_idp.values()) == [{'single_sign_on_service':
222-
{
223-
'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect':
224-
'http://localhost:8088/sso/'}}]
226+
assert list(c._sp_idp.values()) == [
227+
{
228+
'single_sign_on_service': {
229+
'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect': (
230+
'http://localhost:8088/sso/'
231+
)
232+
}
233+
}
234+
]
225235

226236
assert c.only_use_keys_in_metadata
227-
assert 'PasswordProtectedTransport' in c._sp_requested_authn_context.to_string().decode()
237+
assert type(c.getattr("requested_authn_context")) is dict
238+
assert c.getattr("requested_authn_context").get("authn_context_class_ref") == [
239+
AUTHN_PASSWORD_PROTECTED,
240+
AUTHN_TIME_SYNC_TOKEN,
241+
]
242+
assert c.getattr("requested_authn_context").get("comparison") == "exact"
228243

229244

230245
def test_2():

tests/test_71_authn_request.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from contextlib import closing
22
from saml2.client import Saml2Client
33
from saml2.server import Server
4+
from saml2.saml import AuthnContextClassRef
45

56

67
def test_authn_request_with_acs_by_index():
@@ -15,22 +16,30 @@ def test_authn_request_with_acs_by_index():
1516
# instead of AssertionConsumerServiceURL. The index with label ACS_INDEX
1617
# exists in the SP metadata in servera.xml.
1718
request_id, authn_request = sp.create_authn_request(
18-
sp.config.entityid,
19-
assertion_consumer_service_index=ACS_INDEX)
19+
sp.config.entityid, assertion_consumer_service_index=ACS_INDEX
20+
)
2021

21-
# Make sure the authn_request contains AssertionConsumerServiceIndex.
22-
acs_index = getattr(authn_request,
23-
'assertion_consumer_service_index', None)
22+
assert authn_request.requested_authn_context.authn_context_class_ref == [
23+
AuthnContextClassRef(accr)
24+
for accr in sp.config.getattr("requested_authn_context").get("authn_context_class_ref")
25+
]
26+
assert authn_request.requested_authn_context.comparison == (
27+
sp.config.getattr("requested_authn_context").get("comparison")
28+
)
2429

30+
# Make sure the authn_request contains AssertionConsumerServiceIndex.
31+
acs_index = getattr(
32+
authn_request, 'assertion_consumer_service_index', None
33+
)
2534
assert acs_index == ACS_INDEX
2635

2736
# Create IdP.
2837
with closing(Server(config_file="idp_all_conf")) as idp:
29-
3038
# Ask the IdP to pick out the binding and destination from the
3139
# authn_request.
32-
binding, destination = idp.pick_binding("assertion_consumer_service",
33-
request=authn_request)
40+
binding, destination = idp.pick_binding(
41+
"assertion_consumer_service", request=authn_request
42+
)
3443

3544
# Make sure the IdP pick_binding method picks the correct location
3645
# or destination based on the ACS index in the authn request.

0 commit comments

Comments
 (0)