Skip to content

Commit 805a01c

Browse files
author
Roland Hedberg
committed
One usage of extension schemas.
1 parent 1bdc478 commit 805a01c

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

src/saml2/response.py

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
from saml2.s_utils import RequestVersionTooLow
5252
from saml2.s_utils import RequestVersionTooHigh
53-
from saml2.saml import attribute_from_string
53+
from saml2.saml import attribute_from_string, XSI_TYPE
5454
from saml2.saml import SCM_BEARER
5555
from saml2.saml import SCM_HOLDER_OF_KEY
5656
from saml2.saml import SCM_SENDER_VOUCHES
@@ -201,9 +201,13 @@ def _dummy(_):
201201
return None
202202

203203

204-
def for_me(condition, myself):
205-
# Am I among the intended audiences
206-
for restriction in condition.audience_restriction:
204+
def for_me(conditions, myself):
205+
""" Am I among the intended audiences """
206+
207+
if not conditions.audience_restriction: # No audience restriction
208+
return True
209+
210+
for restriction in conditions.audience_restriction:
207211
for audience in restriction.audience:
208212
if audience.text.strip() == myself:
209213
return True
@@ -443,7 +447,8 @@ class AuthnResponse(StatusResponse):
443447
def __init__(self, sec_context, attribute_converters, entity_id,
444448
return_addr=None, outstanding_queries=None,
445449
timeslack=0, asynchop=True, allow_unsolicited=False,
446-
test=False, allow_unknown_attributes=False):
450+
test=False, allow_unknown_attributes=False,
451+
**kwargs):
447452

448453
StatusResponse.__init__(self, sec_context, return_addr, timeslack,
449454
asynchop=asynchop)
@@ -461,6 +466,8 @@ def __init__(self, sec_context, attribute_converters, entity_id,
461466
self.allow_unsolicited = allow_unsolicited
462467
self.test = test
463468
self.allow_unknown_attributes = allow_unknown_attributes
469+
#
470+
self.extension_schema = kwargs["extension_schema"]
464471

465472
def loads(self, xmldata, decode=True, origxml=None):
466473
self._loads(xmldata, decode, origxml)
@@ -506,44 +513,50 @@ def authn_statement_ok(self, optional=False):
506513
# check authn_statement.session_index
507514

508515
def condition_ok(self, lax=False):
509-
# The Identity Provider MUST include a <saml:Conditions> element
510-
#print "Conditions",assertion.conditions
511516
if self.test:
512517
lax = True
518+
519+
# The Identity Provider MUST include a <saml:Conditions> element
513520
assert self.assertion.conditions
514-
condition = self.assertion.conditions
521+
conditions = self.assertion.conditions
515522

516-
logger.debug("condition: %s" % condition)
523+
logger.debug("conditions: %s" % conditions)
517524

518525
# if no sub-elements or elements are supplied, then the
519526
# assertion is considered to be valid.
520-
if not condition.keyswv():
527+
if not conditions.keyswv():
521528
return True
522529

523530
# if both are present NotBefore must be earlier than NotOnOrAfter
524-
if condition.not_before and condition.not_on_or_after:
525-
if not later_than(condition.not_on_or_after, condition.not_before):
531+
if conditions.not_before and conditions.not_on_or_after:
532+
if not later_than(conditions.not_on_or_after, conditions.not_before):
526533
return False
527534

528535
try:
529-
if condition.not_on_or_after:
536+
if conditions.not_on_or_after:
530537
self.not_on_or_after = validate_on_or_after(
531-
condition.not_on_or_after, self.timeslack)
532-
if condition.not_before:
533-
validate_before(condition.not_before, self.timeslack)
538+
conditions.not_on_or_after, self.timeslack)
539+
if conditions.not_before:
540+
validate_before(conditions.not_before, self.timeslack)
534541
except Exception, excp:
535-
logger.error("Exception on condition: %s" % (excp,))
542+
logger.error("Exception on conditions: %s" % (excp,))
536543
if not lax:
537544
raise
538545
else:
539546
self.not_on_or_after = 0
540547

541-
if not for_me(condition, self.entity_id):
548+
if not for_me(conditions, self.entity_id):
542549
if not lax:
543-
#print condition
544-
#print self.entity_id
545550
raise Exception("Not for me!!!")
546-
551+
552+
if conditions.condition: # extra conditions
553+
for cond in conditions.condition:
554+
try:
555+
if cond.extension_attributes[XSI_TYPE] in self.extension_schema:
556+
pass
557+
except KeyError:
558+
raise Exception("Unknown condition")
559+
547560
return True
548561

549562
def decrypt_attributes(self, attribute_statement):
@@ -924,6 +937,7 @@ def response_factory(xmlstr, conf, return_addr=None, outstanding_queries=None,
924937

925938
attribute_converters = conf.attribute_converters
926939
entity_id = conf.entityid
940+
extension_schema = conf.extension_schema
927941

928942
response = StatusResponse(sec_context, return_addr, timeslack, request_id,
929943
asynchop)
@@ -933,7 +947,8 @@ def response_factory(xmlstr, conf, return_addr=None, outstanding_queries=None,
933947
authnresp = AuthnResponse(sec_context, attribute_converters,
934948
entity_id, return_addr,
935949
outstanding_queries, timeslack, asynchop,
936-
allow_unsolicited)
950+
allow_unsolicited,
951+
extension_schema=extension_schema)
937952
authnresp.update(response)
938953
return authnresp
939954
except TypeError:

0 commit comments

Comments
 (0)