Skip to content

Commit 92b9dc7

Browse files
new: FilterAttributeValues: add tests for shibmdscope_match_scope and shibmdscope_match_value filters
1 parent e5a67cd commit 92b9dc7

File tree

1 file changed

+240
-0
lines changed

1 file changed

+240
-0
lines changed

tests/satosa/micro_services/test_attribute_modifications.py

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import pytest
2+
from tests.util import FakeIdP, create_metadata_from_config_dict, FakeSP
3+
from saml2.mdstore import MetadataStore
4+
from saml2.config import Config
5+
from satosa.context import Context
26
from satosa.exception import SATOSAError
37
from satosa.internal import AuthenticationInformation
48
from satosa.internal import InternalData
@@ -12,6 +16,22 @@ def create_filter_service(self, attribute_filters):
1216
filter_service.next = lambda ctx, data: data
1317
return filter_service
1418

19+
def create_idp_metadata_conf_with_shibmd_scopes(self, idp_entityid, shibmd_scopes):
20+
idp_conf = {
21+
"entityid": idp_entityid,
22+
"service": {
23+
"idp":{}
24+
}
25+
}
26+
27+
if shibmd_scopes is not None:
28+
idp_conf["service"]["idp"]["scope"] = shibmd_scopes
29+
30+
metadata_conf = {
31+
"inline": [create_metadata_from_config_dict(idp_conf)]
32+
}
33+
return metadata_conf
34+
1535
def test_filter_all_attributes_from_all_target_providers_for_all_requesters(self):
1636
attribute_filters = {
1737
"": { # all providers
@@ -158,3 +178,223 @@ def test_invalid_filter_type(self):
158178
}
159179
with pytest.raises(SATOSAError):
160180
filtered = filter_service.process(None, resp)
181+
182+
def test_shibmdscope_match_value_filter_with_no_md_store_in_context(self):
183+
attribute_filters = {
184+
"": {
185+
"": {
186+
"a2": {
187+
"shibmdscope_match_value": None
188+
}
189+
}
190+
}
191+
}
192+
filter_service = self.create_filter_service(attribute_filters)
193+
194+
resp = InternalData(AuthenticationInformation())
195+
resp.attributes = {
196+
"a1": ["abc:xyz"],
197+
"a2": ["foo:bar", "1:foo:bar:2"],
198+
}
199+
ctx = Context()
200+
filtered = filter_service.process(ctx, resp)
201+
assert filtered.attributes == {"a1": ["abc:xyz"], "a2": []}
202+
203+
def test_shibmdscope_match_value_filter_with_empty_md_store_in_context(self):
204+
attribute_filters = {
205+
"": {
206+
"": {
207+
"a2": {
208+
"shibmdscope_match_value": None
209+
}
210+
}
211+
}
212+
}
213+
filter_service = self.create_filter_service(attribute_filters)
214+
215+
resp = InternalData(AuthenticationInformation())
216+
resp.attributes = {
217+
"a1": ["abc:xyz"],
218+
"a2": ["foo:bar", "1:foo:bar:2"],
219+
}
220+
ctx = Context()
221+
mdstore = MetadataStore(None, None)
222+
ctx.decorate(Context.KEY_METADATA_STORE, mdstore)
223+
filtered = filter_service.process(ctx, resp)
224+
assert filtered.attributes == {"a1": ["abc:xyz"], "a2": []}
225+
226+
def test_shibmdscope_match_value_filter_with_idp_md_with_no_scope(self):
227+
attribute_filters = {
228+
"": {
229+
"": {
230+
"a2": {
231+
"shibmdscope_match_value": None
232+
}
233+
}
234+
}
235+
}
236+
filter_service = self.create_filter_service(attribute_filters)
237+
238+
resp = InternalData(AuthenticationInformation())
239+
resp.attributes = {
240+
"a1": ["abc:xyz"],
241+
"a2": ["foo.bar", "1.foo.bar.2"],
242+
}
243+
244+
idp_entityid = 'https://idp.example.org/'
245+
resp.auth_info.issuer = idp_entityid
246+
247+
mdstore = MetadataStore(None, Config())
248+
mdstore.imp(self.create_idp_metadata_conf_with_shibmd_scopes(idp_entityid, None))
249+
ctx = Context()
250+
ctx.decorate(Context.KEY_METADATA_STORE, mdstore)
251+
252+
filtered = filter_service.process(ctx, resp)
253+
assert filtered.attributes == {"a1": ["abc:xyz"], "a2": []}
254+
255+
def test_shibmdscope_match_value_filter_with_idp_md_with_single_scope(self):
256+
attribute_filters = {
257+
"": {
258+
"": {
259+
"a2": {
260+
"shibmdscope_match_value": None
261+
}
262+
}
263+
}
264+
}
265+
filter_service = self.create_filter_service(attribute_filters)
266+
267+
resp = InternalData(AuthenticationInformation())
268+
resp.attributes = {
269+
"a1": ["abc:xyz"],
270+
"a2": ["foo.bar", "1.foo.bar.2"],
271+
}
272+
273+
idp_entityid = 'https://idp.example.org/'
274+
resp.auth_info.issuer = idp_entityid
275+
276+
mdstore = MetadataStore(None, Config())
277+
mdstore.imp(self.create_idp_metadata_conf_with_shibmd_scopes(idp_entityid, ["foo.bar"]))
278+
ctx = Context()
279+
ctx.decorate(Context.KEY_METADATA_STORE, mdstore)
280+
281+
filtered = filter_service.process(ctx, resp)
282+
assert filtered.attributes == {"a1": ["abc:xyz"], "a2": ["foo.bar"]}
283+
284+
def test_shibmdscope_match_value_filter_with_idp_md_with_single_regexp_scope(self):
285+
attribute_filters = {
286+
"": {
287+
"": {
288+
"a2": {
289+
"shibmdscope_match_value": None
290+
}
291+
}
292+
}
293+
}
294+
filter_service = self.create_filter_service(attribute_filters)
295+
296+
resp = InternalData(AuthenticationInformation())
297+
resp.attributes = {
298+
"a1": ["abc:xyz"],
299+
"a2": ["test.foo.bar", "1.foo.bar.2"],
300+
}
301+
302+
idp_entityid = 'https://idp.example.org/'
303+
resp.auth_info.issuer = idp_entityid
304+
305+
mdstore = MetadataStore(None, Config())
306+
mdstore.imp(self.create_idp_metadata_conf_with_shibmd_scopes(idp_entityid, ["[^.]*\.foo\.bar$"]))
307+
mdstore[idp_entityid]['idpsso_descriptor'][0]['extensions']['extension_elements'][0]['regexp'] = 'true'
308+
ctx = Context()
309+
ctx.decorate(Context.KEY_METADATA_STORE, mdstore)
310+
311+
filtered = filter_service.process(ctx, resp)
312+
assert filtered.attributes == {"a1": ["abc:xyz"], "a2": ["test.foo.bar"]}
313+
314+
def test_shibmdscope_match_value_filter_with_idp_md_with_multiple_scopes(self):
315+
attribute_filters = {
316+
"": {
317+
"": {
318+
"a2": {
319+
"shibmdscope_match_value": None
320+
}
321+
}
322+
}
323+
}
324+
filter_service = self.create_filter_service(attribute_filters)
325+
326+
resp = InternalData(AuthenticationInformation())
327+
resp.attributes = {
328+
"a1": ["abc:xyz"],
329+
"a2": ["foo.bar", "1.foo.bar.2", "foo.baz", "foo.baz.com"],
330+
}
331+
332+
idp_entityid = 'https://idp.example.org/'
333+
resp.auth_info.issuer = idp_entityid
334+
335+
mdstore = MetadataStore(None, Config())
336+
mdstore.imp(self.create_idp_metadata_conf_with_shibmd_scopes(idp_entityid, ["foo.bar", "foo.baz"]))
337+
ctx = Context()
338+
ctx.decorate(Context.KEY_METADATA_STORE, mdstore)
339+
340+
filtered = filter_service.process(ctx, resp)
341+
assert filtered.attributes == {"a1": ["abc:xyz"], "a2": ["foo.bar", "foo.baz"]}
342+
343+
def test_shibmdscope_match_scope_filter_with_single_scope(self):
344+
attribute_filters = {
345+
"": {
346+
"": {
347+
"a2": {
348+
"shibmdscope_match_scope": None
349+
}
350+
}
351+
}
352+
}
353+
filter_service = self.create_filter_service(attribute_filters)
354+
355+
resp = InternalData(AuthenticationInformation())
356+
resp.attributes = {
357+
"a1": ["abc:xyz"],
358+
"a2": ["foo.bar", "[email protected]", "1.foo.bar.2", "[email protected]", "value@[email protected]"],
359+
}
360+
361+
idp_entityid = 'https://idp.example.org/'
362+
resp.auth_info.issuer = idp_entityid
363+
364+
mdstore = MetadataStore(None, Config())
365+
mdstore.imp(self.create_idp_metadata_conf_with_shibmd_scopes(idp_entityid, ["foo.bar"]))
366+
ctx = Context()
367+
ctx.decorate(Context.KEY_METADATA_STORE, mdstore)
368+
369+
filtered = filter_service.process(ctx, resp)
370+
assert filtered.attributes == {"a1": ["abc:xyz"], "a2": ["[email protected]"]}
371+
372+
def test_multiple_filters_for_single_attribute(self):
373+
attribute_filters = {
374+
"": {
375+
"": {
376+
"a2": {
377+
"regexp": "^value1@",
378+
"shibmdscope_match_scope": None
379+
}
380+
}
381+
}
382+
}
383+
filter_service = self.create_filter_service(attribute_filters)
384+
385+
resp = InternalData(AuthenticationInformation())
386+
resp.attributes = {
387+
"a1": ["abc:xyz"],
388+
"a2": ["foo.bar", "[email protected]", "[email protected]", "1.foo.bar.2", "[email protected]", "value@[email protected]"],
389+
}
390+
391+
idp_entityid = 'https://idp.example.org/'
392+
resp.auth_info.issuer = idp_entityid
393+
394+
mdstore = MetadataStore(None, Config())
395+
mdstore.imp(self.create_idp_metadata_conf_with_shibmd_scopes(idp_entityid, ["foo.bar"]))
396+
ctx = Context()
397+
ctx.decorate(Context.KEY_METADATA_STORE, mdstore)
398+
399+
filtered = filter_service.process(ctx, resp)
400+
assert filtered.attributes == {"a1": ["abc:xyz"], "a2": ["[email protected]"]}

0 commit comments

Comments
 (0)