Skip to content

Commit 085a547

Browse files
Merge pull request #95 from skoranda/saml_internal_response
SAMLInternalResponse for saml backend
2 parents ca20986 + 62deacd commit 085a547

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/satosa/backends/saml2.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,16 @@ def _translate_response(self, response, state):
214214
issuer = response.response.issuer.text
215215

216216
auth_info = AuthenticationInformation(auth_class_ref, timestamp, issuer)
217-
internal_resp = InternalResponse(auth_info=auth_info)
217+
internal_resp = SAMLInternalResponse(auth_info=auth_info)
218218

219219
internal_resp.user_id = response.get_subject().text
220220
internal_resp.attributes = self.converter.to_internal(self.attribute_profile, response.ava)
221+
222+
# The SAML response may not include a NameID
223+
try:
224+
internal_resp.name_id = response.assertion.subject.name_id
225+
except AttributeError:
226+
pass
221227

222228
satosa_logging(logger, logging.DEBUG, "received attributes:\n%s" % json.dumps(response.ava, indent=4), state)
223229
return internal_resp
@@ -315,3 +321,32 @@ def get_metadata_desc(self):
315321

316322
entity_descriptions.append(description)
317323
return entity_descriptions
324+
325+
class SAMLInternalResponse(InternalResponse):
326+
"""
327+
Like the parent InternalResponse, holds internal representation of
328+
service related data, but includes additional details relevant to
329+
SAML interoperability.
330+
331+
:type name_id: instance of saml2.saml.NameID from pysaml2
332+
"""
333+
def __init__(self, auth_info=None):
334+
super().__init__(auth_info)
335+
336+
self.name_id = None
337+
338+
def to_dict(self):
339+
"""
340+
Converts a SAMLInternalResponse object to a dict
341+
:rtype: dict[str, dict[str, str] | str]
342+
:return: A dict representation of the object
343+
"""
344+
_dict = super().to_dict()
345+
346+
if self.name_id:
347+
_dict['name_id'] = {self.name_id.format : self.name_id.text}
348+
else:
349+
_dict['name_id'] = None
350+
351+
return _dict
352+

0 commit comments

Comments
 (0)