Skip to content

Commit 0515de9

Browse files
author
Roland Hedberg
committed
Language correction.
Deal with case where people want to JSON serialize session information. Carry over more parameters in create_attribute_response.
1 parent 1220e85 commit 0515de9

File tree

7 files changed

+56
-44
lines changed

7 files changed

+56
-44
lines changed

src/saml2/cache.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22

33
import shelve
4+
import six
45
from saml2.ident import code, decode
56
from saml2 import time_util, SAMLError
67
import logging
@@ -98,6 +99,8 @@ def get(self, name_id, entity_id, check_not_on_or_after=True):
9899
if check_not_on_or_after and time_util.after(timestamp):
99100
raise ToOld("past %s" % str(timestamp))
100101

102+
if 'name_id' in info and isinstance(info['name_id'], six.string_types):
103+
info['name_id'] = decode(info['name_id'])
101104
return info or None
102105

103106
def set(self, name_id, entity_id, info, not_on_or_after=0):

src/saml2/ident.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class Unknown(SAMLError):
2929
def code(item):
3030
"""
3131
Turn a NameID class instance into a quoted string of comma separated
32-
attribute,value pairs. The attribute name is replaced with a digits.
33-
Depends on knowledge on the specific order of the attributes for that
32+
attribute,value pairs. The attribute names are replaced with digits.
33+
Depends on knowledge on the specific order of the attributes for the
3434
class that is used.
3535
3636
:param item: The class instance

src/saml2/population.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
2-
from saml2.cache import Cache
32
import six
3+
from saml2.cache import Cache
4+
from saml2.ident import code
45

56
logger = logging.getLogger(__name__)
67

@@ -20,6 +21,8 @@ def add_information_about_person(self, session_info):
2021
this function will overwrite that information"""
2122

2223
name_id = session_info["name_id"]
24+
# make friendly to (JSON) serialization
25+
session_info['name_id'] = code(name_id)
2326
issuer = session_info["issuer"]
2427
del session_info["issuer"]
2528
self.cache.set(name_id, issuer, session_info,

src/saml2/server.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ def create_attribute_response(self, identity, in_response_to, destination,
480480
pass
481481

482482
to_sign = []
483-
args = {}
483+
484484
if identity:
485485
_issuer = self._issuer(issuer)
486486
ast = Assertion(identity)
@@ -505,12 +505,16 @@ def create_attribute_response(self, identity, in_response_to, destination,
505505
digest_alg=digest_alg)
506506
# Just the assertion or the response and the assertion ?
507507
to_sign = [(class_name(assertion), assertion.id)]
508+
kwargs['sign_assertion'] = True
508509

509-
args["assertion"] = assertion
510+
kwargs["assertion"] = assertion
511+
512+
if sp_entity_id:
513+
kwargs['sp_entity_id'] = sp_entity_id
510514

511515
return self._response(in_response_to, destination, status, issuer,
512516
sign_response, to_sign, sign_alg=sign_alg,
513-
digest_alg=digest_alg, **args)
517+
digest_alg=digest_alg, **kwargs)
514518

515519
# ------------------------------------------------------------------------
516520

tests/test_32_cache.py

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,52 @@
77
from saml2.time_util import in_a_while, str_to_time
88
from saml2.ident import code
99

10-
SESSION_INFO_PATTERN = {"ava":{}, "came from":"", "not_on_or_after":0,
11-
"issuer":"", "session_id":-1}
10+
SESSION_INFO_PATTERN = {"ava": {}, "came from": "", "not_on_or_after": 0,
11+
"issuer": "", "session_id": -1}
1212

1313

14-
def _eq(l1,l2):
14+
def _eq(l1, l2):
1515
return set(l1) == set(l2)
1616

17+
1718
def nid_eq(l1, l2):
1819
return _eq([code(c) for c in l1], [code(c) for c in l2])
1920

21+
2022
nid = [
2123
NameID(name_qualifier="foo", format=NAMEID_FORMAT_TRANSIENT, text="1234"),
2224
NameID(name_qualifier="foo", format=NAMEID_FORMAT_TRANSIENT, text="9876"),
2325
NameID(name_qualifier="foo", format=NAMEID_FORMAT_TRANSIENT, text="1000")]
2426

27+
2528
class TestClass:
2629
def setup_class(self):
2730
self.cache = Cache()
28-
29-
31+
3032
def test_set(self):
3133
not_on_or_after = str_to_time(in_a_while(days=1))
3234
session_info = SESSION_INFO_PATTERN.copy()
33-
session_info["ava"] = {"givenName":["Derek"]}
35+
session_info["ava"] = {"givenName": ["Derek"]}
3436
self.cache.set(nid[0], "abcd", session_info, not_on_or_after)
35-
37+
3638
(ava, inactive) = self.cache.get_identity(nid[0])
3739
assert inactive == []
3840
assert list(ava.keys()) == ["givenName"]
3941
assert ava["givenName"] == ["Derek"]
40-
41-
def test_add_ava_info(self):
42+
43+
def test_add_ava_info(self):
4244
not_on_or_after = str_to_time(in_a_while(days=1))
4345
session_info = SESSION_INFO_PATTERN.copy()
44-
session_info["ava"] = {"surName":["Jeter"]}
46+
session_info["ava"] = {"surName": ["Jeter"]}
4547
self.cache.set(nid[0], "bcde", session_info, not_on_or_after)
46-
48+
4749
(ava, inactive) = self.cache.get_identity(nid[0])
4850
assert inactive == []
49-
assert _eq(ava.keys(), ["givenName","surName"])
51+
assert _eq(ava.keys(), ["givenName", "surName"])
5052
assert ava["givenName"] == ["Derek"]
5153
assert ava["surName"] == ["Jeter"]
5254

53-
def test_from_one_target_source(self):
55+
def test_from_one_target_source(self):
5456
session_info = self.cache.get(nid[0], "bcde")
5557
ava = session_info["ava"]
5658
assert _eq(ava.keys(), ["surName"])
@@ -59,66 +61,65 @@ def test_from_one_target_source(self):
5961
ava = session_info["ava"]
6062
assert _eq(ava.keys(), ["givenName"])
6163
assert ava["givenName"] == ["Derek"]
62-
64+
6365
def test_entities(self):
6466
assert _eq(self.cache.entities(nid[0]), ["abcd", "bcde"])
6567
py.test.raises(Exception, "self.cache.entities('6666')")
66-
68+
6769
def test_remove_info(self):
6870
self.cache.reset(nid[0], "bcde")
6971
assert self.cache.active(nid[0], "bcde") == False
7072
assert self.cache.active(nid[0], "abcd")
71-
73+
7274
(ava, inactive) = self.cache.get_identity(nid[0])
7375
assert inactive == ['bcde']
7476
assert _eq(ava.keys(), ["givenName"])
7577
assert ava["givenName"] == ["Derek"]
76-
78+
7779
def test_active(self):
7880
assert self.cache.active(nid[0], "bcde") == False
7981
assert self.cache.active(nid[0], "abcd")
80-
82+
8183
def test_subjects(self):
8284
assert nid_eq(self.cache.subjects(), [nid[0]])
83-
85+
8486
def test_second_subject(self):
8587
not_on_or_after = str_to_time(in_a_while(days=1))
8688
session_info = SESSION_INFO_PATTERN.copy()
87-
session_info["ava"] = {"givenName":["Ichiro"],
88-
"surName":["Suzuki"]}
89+
session_info["ava"] = {"givenName": ["Ichiro"],
90+
"surName": ["Suzuki"]}
8991
self.cache.set(nid[1], "abcd", session_info,
90-
not_on_or_after)
92+
not_on_or_after)
9193

9294
(ava, inactive) = self.cache.get_identity(nid[1])
9395
assert inactive == []
94-
assert _eq(ava.keys(), ["givenName","surName"])
96+
assert _eq(ava.keys(), ["givenName", "surName"])
9597
assert ava["givenName"] == ["Ichiro"]
9698
assert ava["surName"] == ["Suzuki"]
9799
assert nid_eq(self.cache.subjects(), [nid[0], nid[1]])
98-
100+
99101
def test_receivers(self):
100102
assert _eq(self.cache.receivers(nid[1]), ["abcd"])
101-
103+
102104
not_on_or_after = str_to_time(in_a_while(days=1))
103105
session_info = SESSION_INFO_PATTERN.copy()
104-
session_info["ava"] = {"givenName":["Ichiro"],
105-
"surName":["Suzuki"]}
106+
session_info["ava"] = {"givenName": ["Ichiro"],
107+
"surName": ["Suzuki"]}
106108
self.cache.set(nid[1], "bcde", session_info,
107-
not_on_or_after)
108-
109+
not_on_or_after)
110+
109111
assert _eq(self.cache.receivers(nid[1]), ["abcd", "bcde"])
110112
assert nid_eq(self.cache.subjects(), nid[0:2])
111-
113+
112114
def test_timeout(self):
113115
not_on_or_after = str_to_time(in_a_while(seconds=1))
114116
session_info = SESSION_INFO_PATTERN.copy()
115-
session_info["ava"] = {"givenName":["Alex"],
116-
"surName":["Rodriguez"]}
117+
session_info["ava"] = {"givenName": ["Alex"],
118+
"surName": ["Rodriguez"]}
117119
self.cache.set(nid[2], "bcde", session_info,
118-
not_on_or_after)
119-
120+
not_on_or_after)
121+
120122
time.sleep(2)
121123
(ava, inactive) = self.cache.get_identity(nid[2])
122124
assert inactive == ["bcde"]
123125
assert ava == {}
124-

tests/test_34_population.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
nid = NameID(name_qualifier="foo", format=NAMEID_FORMAT_TRANSIENT,
1212
text="123456")
13+
1314
nida = NameID(name_qualifier="foo", format=NAMEID_FORMAT_TRANSIENT,
14-
text="abcdef")
15+
text="abcdef")
1516

1617
cnid = code(nid)
1718
cnida = code(nida)
@@ -57,7 +58,7 @@ def test_add_person(self):
5758
info = self.population.get_info_from(nid, IDP_ONE)
5859
assert sorted(list(info.keys())) == sorted(["not_on_or_after",
5960
"name_id", "ava"])
60-
assert info["name_id"] == nid
61+
assert info["name_id"] == nid
6162
assert info["ava"] == {'mail': '[email protected]',
6263
'givenName': 'Anders',
6364
'surName': 'Andersson'}

tests/test_50_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ def test_do_attribute_reponse(self):
12041204
print(aa_policy.__dict__)
12051205
response = self.server.create_attribute_response(
12061206
IDENTITY.copy(), "aaa", "http://example.com/sp/",
1207-
"urn:mace:example.com:sp:1")
1207+
"http://www.example.com/roland/sp")
12081208

12091209
assert response is not None
12101210
assert response.destination == "http://example.com/sp/"

0 commit comments

Comments
 (0)