Skip to content

Commit 24ce38b

Browse files
authored
Merge pull request #39 from its-dirg/samlfrontend-filter
Filter the attributes before forming the authentication response.
2 parents d8e82f4 + 3c2f145 commit 24ce38b

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/satosa/frontends/saml2.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,22 @@ def _get_approved_attributes(self, idp, idp_policy, sp_entity_id, state):
225225
attribute_filter = []
226226
for aconv in attrconvs:
227227
if aconv.name_format == name_format:
228-
attribute_filter = list(idp_policy.restrict(aconv._to, sp_entity_id, idp.metadata).keys())
228+
all_attributes = {v: None for v in aconv._fro.values()}
229+
attribute_filter = list(idp_policy.restrict(all_attributes, sp_entity_id, idp.metadata).keys())
230+
break
229231
attribute_filter = self.converter.to_internal_filter(self.attribute_profile, attribute_filter)
230232
satosa_logging(logger, logging.DEBUG, "Filter: %s" % attribute_filter, state)
231233
return attribute_filter
232234

235+
def _filter_attributes(self, idp, internal_response, context,):
236+
idp_policy = idp.config.getattr("policy", "idp")
237+
if idp_policy:
238+
approved_attributes = self._get_approved_attributes(idp, idp_policy, internal_response.requester,
239+
context.state)
240+
attributes = {k: v for k, v in internal_response.attributes.items() if k in approved_attributes}
241+
242+
return attributes
243+
233244
def _handle_authn_response(self, context, internal_response, idp):
234245
"""
235246
See super class satosa.frontends.base.FrontendModule
@@ -246,6 +257,7 @@ def _handle_authn_response(self, context, internal_response, idp):
246257
request_state = self.load_state(context.state)
247258

248259
resp_args = request_state["resp_args"]
260+
internal_response.attributes = self._filter_attributes(idp, internal_response, context)
249261
ava = self.converter.from_internal(self.attribute_profile, internal_response.attributes)
250262

251263
auth_info = {}

tests/satosa/frontends/test_saml2.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ def test_get_filter_attributes_with_sp_requested_attributes_without_friendlyname
199199
base_url = self.construct_base_url_from_entity_id(idp_conf["entityid"])
200200
conf = {"idp_config": idp_conf, "endpoints": ENDPOINTS}
201201

202-
internal_attributes = {"attributes": {attr_name: {"saml": [attr_name]} for attr_name in
203-
["edupersontargetedid", "edupersonprincipalname",
204-
"edupersonaffiliation", "mail", "displayname", "sn",
205-
"givenname"]}} # no op mapping for saml attribute names
202+
internal_attributes = {"attributes": {attr_name.lower(): {"saml": [attr_name]} for attr_name in
203+
["eduPersonTargetedID", "eduPersonPrincipalName",
204+
"eduPersonAffiliation", "mail", "displayName", "sn",
205+
"givenName"]}} # no op mapping for saml attribute names
206206

207207
samlfrontend = SAMLFrontend(None, internal_attributes, conf, base_url, "saml_frontend")
208208
samlfrontend.register_endpoints(["testprovider"])
@@ -271,6 +271,7 @@ def test_respect_sp_entity_categories(self, context, entity_category, entity_cat
271271

272272
user_attributes = {k: "foo" for k in expected_attributes_in_all_entity_categories}
273273
internal_response.attributes = AttributeMapper(internal_attributes).to_internal("saml", user_attributes)
274+
internal_response.requester = sp_conf["entityid"]
274275

275276
resp = self.get_auth_response(samlfrontend, context, internal_response, sp_conf, idp_metadata_str)
276277
assert Counter(resp.ava.keys()) == Counter(expected_attributes)

0 commit comments

Comments
 (0)