Skip to content

Commit 9e25cc7

Browse files
author
Roland Hedberg
committed
Added functionality needed by the saml2test tool.
1 parent 9dd3ee9 commit 9e25cc7

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

src/saml2/argtree.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
__author__ = 'roland'
2+
3+
4+
def find_paths(cls, arg, path=None, seen=None, res=None, lev=0):
5+
if lev == 0 and res is None:
6+
res = []
7+
8+
if path is None:
9+
path = []
10+
11+
if seen is None:
12+
seen = [cls]
13+
else:
14+
if cls in seen:
15+
return None
16+
17+
seen.append(cls)
18+
19+
for cn, c in cls.c_children.values():
20+
_path = path + [cn]
21+
if cn == arg:
22+
if res is not None:
23+
res.append(_path)
24+
else:
25+
if isinstance(c, list):
26+
_c = c[0]
27+
else:
28+
_c = c
29+
30+
find_paths(_c, arg, _path, seen, res)
31+
32+
for an, typ, mult in cls.c_attributes.values():
33+
if an == arg:
34+
if res is not None:
35+
res.append(path + [an])
36+
37+
if lev == 0:
38+
return res
39+
40+
41+
def set_arg(cls, arg, value):
42+
res = []
43+
for path in find_paths(cls, arg):
44+
x = y = {}
45+
for arc in path[:-1]:
46+
y[arc] = {}
47+
y = y[arc]
48+
y[path[-1]] = value
49+
res.append(x)
50+
51+
return res
52+
53+
54+
def add_path(tdict, path):
55+
"""
56+
57+
:param tdict: a dictionary representing a argument tree
58+
:param path: a path list
59+
:return: a dictionary
60+
"""
61+
t = tdict
62+
for step in path[:-2]:
63+
try:
64+
t = t[step]
65+
except KeyError:
66+
t[step] = {}
67+
t = t[step]
68+
t[path[-2]] = path[-1]
69+
70+
return tdict

tests/test_06_setarg.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from saml2 import saml
2+
from saml2.saml import Subject
3+
from saml2.samlp import Response
4+
from saml2.argtree import set_arg, add_path
5+
from saml2.argtree import find_paths
6+
7+
__author__ = 'roland'
8+
9+
10+
def test_path():
11+
result = find_paths(Subject, 'in_response_to')
12+
13+
assert result == [
14+
['subject_confirmation', 'subject_confirmation_data', 'in_response_to']]
15+
16+
result = find_paths(Response, 'in_response_to')
17+
18+
assert result == [
19+
['assertion', 'subject', 'subject_confirmation',
20+
'subject_confirmation_data', 'in_response_to'],
21+
['in_response_to']
22+
]
23+
24+
25+
def test_set_arg():
26+
r = set_arg(Subject, 'in_response_to', '123456')
27+
28+
assert r == [{'subject_confirmation': {
29+
'subject_confirmation_data': {'in_response_to': '123456'}}}]
30+
31+
32+
def test_multi():
33+
t = {}
34+
t = add_path(t, ['subject_confirmation','method',saml.SCM_BEARER])
35+
x = add_path(
36+
t['subject_confirmation'],
37+
['subject_confirmation_data','in_response_to','1234'])
38+
39+
print(t)
40+
assert t == {
41+
'subject_confirmation': {
42+
'subject_confirmation_data': {'in_response_to': '1234'},
43+
'method': 'urn:oasis:names:tc:SAML:2.0:cm:bearer'}
44+
}

0 commit comments

Comments
 (0)