Skip to content

Commit 803d226

Browse files
committed
use any/all to clarify, allow lists of regexps and add more test
1 parent 58a9352 commit 803d226

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

src/satosa/micro_services/attribute_authorization.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@ def __init__(self, config, *args, **kwargs):
1616
self.attribute_deny = config.get("attribute_deny", {})
1717

1818
def _check_authz(self, context, attributes, requester, provider):
19-
for attribute_name, attribute_filter in _filters(self.attribute_allow, requester, provider):
20-
regex = re.compile(attribute_filter)
19+
for attribute_name, attribute_filters in _filters(self.attribute_allow, requester, provider):
2120
if attribute_name in attributes:
22-
print(repr(regex))
23-
print(list(filter(regex.search, attributes[attribute_name])))
24-
if not list(filter(regex.search, attributes[attribute_name])):
21+
if not any([any(filter(re.compile(af).search, attributes[attribute_name])) for af in attribute_filters]):
2522
raise SATOSAAuthenticationError(context.state, "Permission denied")
2623

27-
for attribute_name, attribute_filter in _filters(self.attribute_deny, requester, provider):
28-
regex = re.compile(attribute_filter)
24+
for attribute_name, attribute_filters in _filters(self.attribute_deny, requester, provider):
2925
if attribute_name in attributes:
30-
if len(list(filter(regex.search, attributes[attribute_name]))) != len(attributes[attribute_name]):
26+
if any([any(filter(re.compile(af).search, attributes[attribute_name])) for af in attribute_filters]):
3127
raise SATOSAAuthenticationError(context.state, "Permission denied")
3228

3329
def process(self, context, data):

tests/satosa/micro_services/test_attribute_authorization.py

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ def create_authz_service(self, attribute_allow, attribute_deny):
1010
authz_service.next = lambda ctx, data: data
1111
return authz_service
1212

13-
def test_authz_allow(self):
13+
def test_authz_allow_success(self):
1414
attribute_allow = {
15-
"": { "default": {"a0": '.+@.+'} }
15+
"": { "default": {"a0": ['.+@.+']} }
1616
}
1717
attribute_deny = {}
1818
authz_service = self.create_authz_service(attribute_allow, attribute_deny)
@@ -27,9 +27,9 @@ def test_authz_allow(self):
2727
except SATOSAAuthenticationError as ex:
2828
assert False
2929

30-
def test_authz_not_allow(self):
30+
def test_authz_allow_fail(self):
3131
attribute_allow = {
32-
"": { "default": {"a0": 'foo'} }
32+
"": { "default": {"a0": ['foo1','foo2']} }
3333
}
3434
attribute_deny = {}
3535
authz_service = self.create_authz_service(attribute_allow, attribute_deny)
@@ -41,5 +41,58 @@ def test_authz_not_allow(self):
4141
ctx = Context()
4242
ctx.state = dict()
4343
authz_service.process(ctx, resp)
44+
assert False
45+
except SATOSAAuthenticationError as ex:
46+
assert True
47+
48+
def test_authz_allow_second(self):
49+
attribute_allow = {
50+
"": { "default": {"a0": ['foo1','foo2']} }
51+
}
52+
attribute_deny = {}
53+
authz_service = self.create_authz_service(attribute_allow, attribute_deny)
54+
resp = InternalResponse(AuthenticationInformation(None, None, None))
55+
resp.attributes = {
56+
"a0": ["foo2","kaka"],
57+
}
58+
try:
59+
ctx = Context()
60+
ctx.state = dict()
61+
authz_service.process(ctx, resp)
62+
except SATOSAAuthenticationError as ex:
63+
assert False
64+
65+
def test_authz_deny_success(self):
66+
attribute_deny = {
67+
"": { "default": {"a0": ['foo1','foo2']} }
68+
}
69+
attribute_allow = {}
70+
authz_service = self.create_authz_service(attribute_allow, attribute_deny)
71+
resp = InternalResponse(AuthenticationInformation(None, None, None))
72+
resp.attributes = {
73+
"a0": ["foo2"],
74+
}
75+
try:
76+
ctx = Context()
77+
ctx.state = dict()
78+
authz_service.process(ctx, resp)
79+
assert False
4480
except SATOSAAuthenticationError as ex:
4581
assert True
82+
83+
def test_authz_deny_fail(self):
84+
attribute_deny = {
85+
"": { "default": {"a0": ['foo1','foo2']} }
86+
}
87+
attribute_allow = {}
88+
authz_service = self.create_authz_service(attribute_allow, attribute_deny)
89+
resp = InternalResponse(AuthenticationInformation(None, None, None))
90+
resp.attributes = {
91+
"a0": ["foo3"],
92+
}
93+
try:
94+
ctx = Context()
95+
ctx.state = dict()
96+
authz_service.process(ctx, resp)
97+
except SATOSAAuthenticationError as ex:
98+
assert False

0 commit comments

Comments
 (0)