Skip to content

Commit 9c04dc7

Browse files
author
rohe
committed
Fixed bug due to the refactoring.
1 parent 9e25cc7 commit 9c04dc7

File tree

3 files changed

+250
-154
lines changed

3 files changed

+250
-154
lines changed

src/saml2/server.py

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from saml2 import element_to_extension_element
1919
from saml2 import class_name
2020
from saml2 import BINDING_HTTP_REDIRECT
21+
from saml2.argtree import add_path
2122

2223
from saml2.entity import Entity
2324
from saml2.eptid import Eptid
@@ -290,7 +291,7 @@ def parse_name_id_mapping_request(self, xml_string, binding):
290291

291292
def setup_assertion(self, authn, sp_entity_id, in_response_to, consumer_url,
292293
name_id, policy, _issuer, authn_statement, identity,
293-
best_effort, sign_response, farg, **kwargs):
294+
best_effort, sign_response, farg=None, **kwargs):
294295
"""
295296
Construct and return the Assertion
296297
@@ -322,14 +323,17 @@ def setup_assertion(self, authn, sp_entity_id, in_response_to, consumer_url,
322323
return self.create_error_response(in_response_to, consumer_url,
323324
exc, sign_response)
324325

325-
try:
326-
subject_confirmation_specs = kwargs['subject_confirmation']
327-
except KeyError:
328-
subject_confirmation_data = {
329-
'recipient': consumer_url,
330-
'in_response_to': in_response_to,
331-
'method': saml.SCM_BEARER
332-
}
326+
if not farg:
327+
farg = add_path(
328+
{},
329+
['assertion', 'subject', 'subject_confirmation', 'method',
330+
saml.SCM_BEARER])
331+
add_path(
332+
farg['assertion']['subject']['subject_confirmation'],
333+
['subject_confirmation_data', 'in_response_to', in_response_to])
334+
add_path(
335+
farg['assertion']['subject']['subject_confirmation'],
336+
['subject_confirmation_data', 'recipient', consumer_url])
333337

334338
if authn: # expected to be a dictionary
335339
# Would like to use dict comprehension but ...
@@ -427,29 +431,23 @@ def _authn_response(self, in_response_to, consumer_url,
427431
if pefim:
428432
encrypted_advice_attributes = True
429433
encrypt_assertion_self_contained = True
430-
assertion_attributes = self.setup_assertion(None, sp_entity_id,
431-
None, None, None,
432-
policy,
433-
None, None, identity,
434-
best_effort,
435-
sign_response, False,
436-
**assertion_args)
437-
assertion = self.setup_assertion(authn, sp_entity_id,
438-
ass_in_response_to, consumer_url,
439-
name_id, policy, _issuer,
440-
authn_statement, [], True,
441-
sign_response, **assertion_args)
434+
assertion_attributes = self.setup_assertion(
435+
None, sp_entity_id, None, None, None, policy, None, None,
436+
identity, best_effort, sign_response, farg=assertion_args)
437+
assertion = self.setup_assertion(
438+
authn, sp_entity_id, ass_in_response_to, consumer_url, name_id,
439+
policy, _issuer, authn_statement, [], True, sign_response,
440+
farg=assertion_args)
442441
assertion.advice = saml.Advice()
443442

444443
# assertion.advice.assertion_id_ref.append(saml.AssertionIDRef())
445444
# assertion.advice.assertion_uri_ref.append(saml.AssertionURIRef())
446445
assertion.advice.assertion.append(assertion_attributes)
447446
else:
448-
assertion = self.setup_assertion(authn, sp_entity_id,
449-
ass_in_response_to, consumer_url,
450-
name_id, policy, _issuer,
451-
authn_statement, identity, True,
452-
sign_response, **assertion_args)
447+
assertion = self.setup_assertion(
448+
authn, sp_entity_id, ass_in_response_to, consumer_url, name_id,
449+
policy, _issuer, authn_statement, identity, True,
450+
sign_response, farg=assertion_args)
453451

454452
to_sign = []
455453
if not encrypt_assertion:
@@ -484,7 +482,7 @@ def create_attribute_response(self, identity, in_response_to, destination,
484482
status=None, issuer=None,
485483
sign_assertion=False, sign_response=False,
486484
attributes=None, sign_alg=None,
487-
digest_alg=None, **kwargs):
485+
digest_alg=None, farg=None, **kwargs):
488486
""" Create an attribute assertion response.
489487
490488
:param identity: A dictionary with attributes and values that are
@@ -516,6 +514,19 @@ def create_attribute_response(self, identity, in_response_to, destination,
516514
to_sign = []
517515

518516
if identity:
517+
if not farg:
518+
farg = add_path(
519+
{},
520+
['assertion', 'subject', 'subject_confirmation', 'method',
521+
saml.SCM_BEARER])
522+
add_path(
523+
farg['assertion']['subject']['subject_confirmation'],
524+
['subject_confirmation_data', 'in_response_to',
525+
in_response_to])
526+
add_path(
527+
farg['assertion']['subject']['subject_confirmation'],
528+
['subject_confirmation_data', 'recipient', destination])
529+
519530
_issuer = self._issuer(issuer)
520531
ast = Assertion(identity)
521532
if policy:
@@ -527,19 +538,10 @@ def create_attribute_response(self, identity, in_response_to, destination,
527538
restr = restriction_from_attribute_spec(attributes)
528539
ast = filter_attribute_value_assertions(ast)
529540

530-
try:
531-
subject_confirmation_specs = kwargs['subject_confirmation_specs']
532-
except KeyError:
533-
subject_confirmation_specs = {
534-
'recipient': destination,
535-
'in_response_to': in_response_to,
536-
'subject_confirmation_method': saml.SCM_BEARER
537-
}
538-
539541
assertion = ast.construct(
540542
sp_entity_id, self.config.attribute_converters, policy,
541543
issuer=_issuer, name_id=name_id,
542-
subject_confirmation_specs=subject_confirmation_specs)
544+
farg=farg['assertion'])
543545

544546
if sign_assertion:
545547
assertion.signature = pre_signature_part(assertion.id,
@@ -712,8 +714,7 @@ def create_authn_response(self, identity, in_response_to, destination,
712714
encrypt_cert_advice=encrypt_cert_advice,
713715
encrypt_cert_assertion=encrypt_cert_assertion,
714716
encrypt_assertion=encrypt_assertion,
715-
encrypt_assertion_self_contained
716-
=encrypt_assertion_self_contained,
717+
encrypt_assertion_self_contained=encrypt_assertion_self_contained,
717718
encrypted_advice_attributes=encrypted_advice_attributes,
718719
pefim=pefim, **kwargs)
719720
except IOError as exc:

tests/test_20_assertion.py

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding=utf-8
22
import pytest
33

4+
from saml2.argtree import add_path
45
from saml2.authn_context import pword
56
from saml2.mdie import to_dict
67
from saml2 import md, assertion
@@ -810,16 +811,21 @@ def test_assertion_with_noop_attribute_conv():
810811
})
811812
name_id = NameID(format=NAMEID_FORMAT_TRANSIENT, text="foobar")
812813
issuer = Issuer(text="entityid", format=NAMEID_FORMAT_ENTITY)
813-
subject_confirmation_specs = {
814-
'recipient': 'consumer_url',
815-
'in_response_to': 'in_response_to',
816-
'subject_confirmation_method': saml.SCM_BEARER
817-
}
814+
815+
farg = add_path(
816+
{},
817+
['subject', 'subject_confirmation', 'method', saml.SCM_BEARER])
818+
add_path(
819+
farg['subject']['subject_confirmation'],
820+
['subject_confirmation_data', 'in_response_to', 'in_response_to'])
821+
add_path(
822+
farg['subject']['subject_confirmation'],
823+
['subject_confirmation_data', 'recipient', 'consumer_url'])
824+
818825
msg = ast.construct(
819826
"sp_entity_id", [AttributeConverterNOOP(NAME_FORMAT_URI)], policy,
820-
issuer=issuer, authn_decl=ACD, name_id=name_id,
821-
authn_auth="authn_authn",
822-
subject_confirmation_specs=subject_confirmation_specs)
827+
issuer=issuer, farg=farg, authn_decl=ACD, name_id=name_id,
828+
authn_auth="authn_authn")
823829

824830
print(msg)
825831
for attr in msg.attribute_statement[0].attribute:
@@ -864,16 +870,20 @@ def test_assertion_with_zero_attributes():
864870
})
865871
name_id = NameID(format=NAMEID_FORMAT_TRANSIENT, text="foobar")
866872
issuer = Issuer(text="entityid", format=NAMEID_FORMAT_ENTITY)
867-
subject_confirmation_specs = {
868-
'recipient': 'consumer_url',
869-
'in_response_to': 'in_response_to',
870-
'subject_confirmation_method': saml.SCM_BEARER
871-
}
873+
farg = add_path(
874+
{},
875+
['subject', 'subject_confirmation', 'method', saml.SCM_BEARER])
876+
add_path(
877+
farg['subject']['subject_confirmation'],
878+
['subject_confirmation_data', 'in_response_to', 'in_response_to'])
879+
add_path(
880+
farg['subject']['subject_confirmation'],
881+
['subject_confirmation_data', 'recipient', 'consumer_url'])
872882

873883
msg = ast.construct(
874884
"sp_entity_id", [AttributeConverterNOOP(NAME_FORMAT_URI)], policy,
875885
issuer=issuer, authn_decl=ACD, authn_auth="authn_authn",
876-
name_id=name_id, subject_confirmation_specs=subject_confirmation_specs)
886+
name_id=name_id, farg=farg)
877887

878888
print(msg)
879889
assert msg.attribute_statement == []
@@ -892,17 +902,20 @@ def test_assertion_with_authn_instant():
892902
name_id = NameID(format=NAMEID_FORMAT_TRANSIENT, text="foobar")
893903
issuer = Issuer(text="entityid", format=NAMEID_FORMAT_ENTITY)
894904

895-
subject_confirmation_specs = {
896-
'recipient': 'consumer_url',
897-
'in_response_to': 'in_response_to',
898-
'subject_confirmation_method': saml.SCM_BEARER
899-
}
905+
farg = add_path(
906+
{},
907+
['subject', 'subject_confirmation', 'method', saml.SCM_BEARER])
908+
add_path(
909+
farg['subject']['subject_confirmation'],
910+
['subject_confirmation_data', 'in_response_to', 'in_response_to'])
911+
add_path(
912+
farg['subject']['subject_confirmation'],
913+
['subject_confirmation_data', 'recipient', 'consumer_url'])
900914

901915
msg = ast.construct(
902916
"sp_entity_id", [AttributeConverterNOOP(NAME_FORMAT_URI)], policy,
903917
issuer=issuer, authn_decl=ACD, authn_auth="authn_authn",
904-
authn_instant=1234567890, name_id=name_id,
905-
subject_confirmation_specs=subject_confirmation_specs)
918+
authn_instant=1234567890, name_id=name_id, farg=farg)
906919

907920
print(msg)
908921
assert msg.authn_statement[0].authn_instant == "2009-02-13T23:31:30Z"

0 commit comments

Comments
 (0)