Skip to content

Commit

Permalink
Make some LightResponse fields optional
Browse files Browse the repository at this point in the history
Ref #16
  • Loading branch information
Jiří Janoušek authored and tpazderka committed Sep 10, 2019
1 parent 223ac33 commit cbce40a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
14 changes: 10 additions & 4 deletions eidas_node/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,17 @@ def validate(self) -> None:
"""Validate this data model."""
self.validate_fields(Status, 'status', required=True)
self.status.validate()
self.validate_fields(str, 'id', 'in_response_to_id', 'subject', required=True)
self.validate_fields(str, 'issuer', 'ip_address', 'relay_state', required=False)
self.validate_fields(NameIdFormat, 'subject_name_id_format', required=True)
self.validate_fields(LevelOfAssurance, 'level_of_assurance', required=True)
validate_attributes(self, 'attributes')
if self.status.failure:
self.validate_fields(str, 'id', 'in_response_to_id', required=True)
self.validate_fields(str, 'subject', 'issuer', 'ip_address', 'relay_state', required=False)
self.validate_fields(NameIdFormat, 'subject_name_id_format', required=False)
self.validate_fields(LevelOfAssurance, 'level_of_assurance', required=False)
else:
self.validate_fields(str, 'id', 'in_response_to_id', 'subject', required=True)
self.validate_fields(str, 'issuer', 'ip_address', 'relay_state', required=False)
self.validate_fields(NameIdFormat, 'subject_name_id_format', required=True)
self.validate_fields(LevelOfAssurance, 'level_of_assurance', required=True)

def deserialize_subject_name_id_format(self, elm: Element) -> Optional[NameIdFormat]:
"""Deserialize field 'subject_name_name_id_format'."""
Expand Down
8 changes: 1 addition & 7 deletions eidas_node/saml.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def __init__(self, document: ElementTree, relay_state: Optional[str] = None):

def create_light_response(self) -> LightResponse:
"""Convert SAML response to light response."""
response = LightResponse()
response = LightResponse(attributes=OrderedDict())
root = self.document.getroot()
if root.tag != Q_NAMES['saml2p:Response']:
raise ValidationError({
Expand Down Expand Up @@ -168,12 +168,6 @@ def create_light_response(self) -> LightResponse:
elif elm.tag == Q_NAMES['saml2:Assertion']:
self._parse_assertion(response, elm)
response.relay_state = self.relay_state
if response.status and response.status.failure:
# Fill in dummy data
response.attributes = OrderedDict()
response.level_of_assurance = LevelOfAssurance.LOW
response.subject_name_id_format = NameIdFormat.UNSPECIFIED
response.subject = 'unknown'
return response

def _parse_assertion(self, response: LightResponse, assertion: Element) -> None:
Expand Down
3 changes: 0 additions & 3 deletions eidas_node/tests/data/light_response_failure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
<inResponseToId>test-light-request-id</inResponseToId>
<issuer>test-light-response-issuer</issuer>
<relayState>relay123</relayState>
<subject>unknown</subject>
<subjectNameIdFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified</subjectNameIdFormat>
<levelOfAssurance>http://eidas.europa.eu/LoA/low</levelOfAssurance>
<status>
<failure>true</failure>
<statusCode>urn:oasis:names:tc:SAML:2.0:status:Requester</statusCode>
Expand Down
17 changes: 12 additions & 5 deletions eidas_node/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@
('issuer', 'test-light-response-issuer'),
('ip_address', None),
('relay_state', 'relay123'),
('subject', 'unknown'),
('subject_name_id_format', NameIdFormat.UNSPECIFIED),
('level_of_assurance', LevelOfAssurance.LOW),
('subject', None),
('subject_name_id_format', None),
('level_of_assurance', None),
('status', OrderedDict(
[('failure', True),
('status_code', StatusCode.REQUESTER),
Expand Down Expand Up @@ -355,6 +355,8 @@ class TestStatus(ValidationMixin, SimpleTestCase):
class TestLightResponse(ValidationMixin, SimpleTestCase):
MODEL = LightResponse
OPTIONAL = {'issuer', 'ip_address', 'relay_state'}
OPTIONAL_FAILURE = {'issuer', 'ip_address', 'relay_state',
'subject', 'subject_name_id_format', 'level_of_assurance'}
VALID_DATA = {
'id': 'uuid',
'in_response_to_id': 'uuid2',
Expand Down Expand Up @@ -385,6 +387,8 @@ class TestLightResponse(ValidationMixin, SimpleTestCase):
def tearDown(self) -> None:
if self.VALID_DATA is not self.__class__.VALID_DATA:
del self.VALID_DATA
if self.OPTIONAL is not self.__class__.OPTIONAL:
del self.OPTIONAL

def create_response(self, success: bool) -> LightResponse:
data = (LIGHT_RESPONSE_DICT if success else FAILED_LIGHT_RESPONSE_DICT).copy()
Expand All @@ -394,16 +398,19 @@ def create_response(self, success: bool) -> LightResponse:
def set_failure(self, failure: bool) -> None:
data = self.__class__.VALID_DATA.copy()
if failure:
self.OPTIONAL = self.__class__.OPTIONAL_FAILURE
data.update({
'status': Status(failure=failure,
status_code=StatusCode.REQUESTER,
sub_status_code=SubStatusCode.REQUEST_DENIED,
status_message='Oops.'),
'attributes': OrderedDict(),
'subject': 'unknown',
'subject_name_id_format': NameIdFormat.UNSPECIFIED,
'subject': None,
'subject_name_id_format': None,
'level_of_assurance': None,
})
else:
self.OPTIONAL = self.__class__.OPTIONAL
data['status'] = Status(failure=False)
self.VALID_DATA = data

Expand Down
2 changes: 1 addition & 1 deletion eidas_node/tests/test_saml.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
'id': 'test-saml-response-id',
'in_response_to_id': 'Ttest-saml-request-id',
'issuer': 'test-saml-response-issuer',
'level_of_assurance': LevelOfAssurance.LOW,
}
LIGHT_RESPONSE_DICT.update(OVERRIDES)
LIGHT_RESPONSE_DICT['level_of_assurance'] = LevelOfAssurance.LOW
FAILED_LIGHT_RESPONSE_DICT.update(OVERRIDES)


Expand Down

0 comments on commit cbce40a

Please sign in to comment.