Skip to content

Commit 5ed0e02

Browse files
author
Josh Hoyt
committed
[project @ Move kvform to its own module]
1 parent 6b713c5 commit 5ed0e02

9 files changed

Lines changed: 266 additions & 115 deletions

File tree

‎openid/association.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import time
2-
from openid import oidutil
2+
33
from openid import cryptutil
4+
from openid import kvform
5+
from openid import oidutil
46

57
class Association(object):
68
"""
@@ -187,7 +189,7 @@ def serialize(self):
187189
for field_name in self.assoc_keys:
188190
pairs.append((field_name, data[field_name]))
189191

190-
return oidutil.seqToKV(pairs, strict=True)
192+
return kvform.seqToKV(pairs, strict=True)
191193

192194
def deserialize(cls, assoc_s):
193195
"""Parse an association as stored by serialize().
@@ -199,7 +201,7 @@ def deserialize(cls, assoc_s):
199201
200202
@return: instance of this class
201203
"""
202-
pairs = oidutil.kvToSeq(assoc_s, strict=True)
204+
pairs = kvform.kvToSeq(assoc_s, strict=True)
203205
keys = []
204206
values = []
205207
for k, v in pairs:
@@ -228,7 +230,7 @@ def sign(self, pairs):
228230
@return: The binary signature of this sequence of pairs
229231
@rtype: str
230232
"""
231-
kv = oidutil.seqToKV(pairs)
233+
kv = kvform.seqToKV(pairs)
232234
return cryptutil.hmacSha1(self.secret, kv)
233235

234236
def signDict(self, fields, data, prefix='openid.'):
@@ -247,4 +249,4 @@ def signDict(self, fields, data, prefix='openid.'):
247249
for field in fields:
248250
pairs.append((field, data[prefix + field]))
249251

250-
return oidUtil.toBase64(self.sign(pairs))
252+
return oidutil.toBase64(self.sign(pairs))

‎openid/consumer/impl.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import urlparse
2-
import urllib
31
import string
42
import time
3+
import urllib
4+
import urlparse
55

6+
from openid import cryptutil
7+
from openid import kvform
8+
from openid import oidutil
69
from openid.association import Association
710
from openid.consumer.parse import parseLinkAttrs
8-
from openid import oidutil, cryptutil
911
from openid.dh import DiffieHellman
1012

1113
from openid.consumer.interface import \
@@ -129,7 +131,7 @@ def _checkAuth(self, nonce, consumer_id, post_data, server_url):
129131
if ret is None:
130132
return FAILURE, consumer_id
131133

132-
results = oidutil.kvToDict(ret[1])
134+
results = kvform.kvToDict(ret[1])
133135
is_valid = results.get('is_valid', 'false')
134136

135137
if is_valid == 'true':
@@ -280,7 +282,7 @@ def _associate(self, server_url):
280282
return None
281283

282284
url, data = ret
283-
results = oidutil.kvToDict(data)
285+
results = kvform.kvToDict(data)
284286

285287
try:
286288
assoc_type = results['assoc_type']

‎openid/cryptutil.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import random
2222
import sha
2323

24-
from openid.oidutil import toBase64, fromBase64, seqToKV
24+
from openid.oidutil import toBase64, fromBase64
2525

2626
try:
2727
from Crypto.Util.number import long_to_bytes, bytes_to_long

‎openid/kvform.py‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
__all__ = ['seqToKV', 'kvToSeq', 'dictToKV', 'kvToDict']
2+
3+
from openid import oidutil
4+
5+
import types
6+
7+
def seqToKV(seq, strict=False):
8+
"""Represent a sequence of pairs of strings as newline-terminated
9+
key:value pairs. The pairs are generated in the order given.
10+
11+
@param seq: The pairs
12+
@type seq: [(str, str)]
13+
14+
@return: A string representation of the sequence
15+
@rtype: str
16+
"""
17+
def err(msg):
18+
formatted = 'seqToKV warning: %s: %r' % (msg, seq)
19+
if strict:
20+
raise ValueError(formatted)
21+
else:
22+
oidutil.log(formatted)
23+
24+
lines = []
25+
for k, v in seq:
26+
if not isinstance(k, types.StringType):
27+
err('Converting key to string: %r' % k)
28+
k = str(k)
29+
30+
if '\n' in k:
31+
raise ValueError(
32+
'Invalid input for seqToKV: key contains newline: %r' % (k,))
33+
34+
if k.strip() != k:
35+
err('Key has whitespace at beginning or end: %r' % k)
36+
37+
if not isinstance(v, types.StringType):
38+
err('Converting value to string: %r' % v)
39+
v = str(v)
40+
41+
if '\n' in v:
42+
raise ValueError(
43+
'Invalid input for seqToKV: value contains newline: %r' % (v,))
44+
45+
if v.strip() != v:
46+
err('Value has whitespace at beginning or end: %r' % v)
47+
48+
lines.append(k + ':' + v + '\n')
49+
50+
return ''.join(lines)
51+
52+
def kvToSeq(data, strict=False):
53+
"""
54+
55+
After one parse, seqToKV and kvToSeq are inverses, with no warnings:
56+
seq = kvToSeq(s)
57+
58+
seqToKV(kvToSeq(seq)) == seq
59+
"""
60+
def err(msg):
61+
formatted = 'kvToSeq warning: %s: %r' % (msg, data)
62+
if strict:
63+
raise ValueError(formatted)
64+
else:
65+
oidutil.log(formatted)
66+
67+
lines = data.split('\n')
68+
if lines[-1]:
69+
err('Does not end in a newline')
70+
else:
71+
del lines[-1]
72+
73+
pairs = []
74+
line_num = 0
75+
for line in lines:
76+
line_num += 1
77+
pair = line.split(':', 1)
78+
if len(pair) == 2:
79+
k, v = pair
80+
k_s = k.strip()
81+
if k_s != k:
82+
fmt = ('In line %d, ignoring leading or trailing '
83+
'whitespace in key %r')
84+
err(fmt % (line_num, k))
85+
86+
if not k_s:
87+
err('In line %d, got empty key' % (line_num,))
88+
89+
v_s = v.strip()
90+
if v_s != v:
91+
fmt = ('In line %d, ignoring leading or trailing '
92+
'whitespace in value %r')
93+
err(fmt % (line_num, v))
94+
95+
pairs.append((k_s, v_s))
96+
else:
97+
err('Line %d does not contain a colon' % line_num)
98+
99+
return pairs
100+
101+
def dictToKV(d):
102+
seq = d.items()
103+
seq.sort()
104+
return seqToKV(seq)
105+
106+
def kvToDict(s):
107+
return dict(kvToSeq(s))

‎openid/oidutil.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
__all__ = ['log', 'appendArgs', 'toBase64', 'fromBase64']
2+
3+
import binascii
4+
import sys
5+
6+
from urllib import urlencode
7+
8+
def log(message, unused_level=0):
9+
sys.stderr.write(message)
10+
sys.stderr.write('\n')
11+
12+
def appendArgs(url, args):
13+
if len(args) == 0:
14+
return url
15+
16+
if '?' in url:
17+
sep = '&'
18+
else:
19+
sep = '?'
20+
21+
return '%s%s%s' % (url, sep, urlencode(args))
22+
23+
def toBase64(s):
24+
"""Represent string s as base64, omitting newlines"""
25+
return binascii.b2a_base64(s)[:-1]
26+
27+
def fromBase64(s):
28+
try:
29+
return binascii.a2b_base64(s)
30+
except binascii.Error:
31+
return ''

‎openid/server/impl.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from openid.server import interface
2-
from openid.server.trustroot import TrustRoot
3-
from openid import oidutil
41
from openid import cryptutil
2+
from openid import kvform
3+
from openid import oidutil
54
from openid.dh import DiffieHellman
5+
from openid.server import interface
6+
from openid.server.trustroot import TrustRoot
67

78
_signed_fields = ['mode', 'identity', 'return_to']
89

@@ -139,7 +140,7 @@ def _associate(self, args):
139140
else:
140141
reply['mac_key'] = oidutil.toBase64(assoc.secret)
141142

142-
return interface.OK, oidutil.dictToKV(reply)
143+
return interface.OK, kvform.dictToKV(reply)
143144

144145
def _checkAuth(self, args):
145146
assoc = self.istore.lookup(args['openid.assoc_handle'], 'HMAC-SHA1')
@@ -172,7 +173,7 @@ def _checkAuth(self, args):
172173
is_valid = 'false'
173174

174175
reply['is_valid'] = is_valid
175-
return interface.OK, oidutil.dictToKV(reply)
176+
return interface.OK, kvform.dictToKV(reply)
176177

177178
def _getErr(self, args, msg):
178179
return_to = args.get('openid.return_to')
@@ -186,5 +187,5 @@ def _getErr(self, args, msg):
186187
return interface.ERROR, msg
187188

188189
def _postErr(self, msg):
189-
return interface.ERROR, oidutil.dictToKV({'error': msg})
190+
return interface.ERROR, kvform.dictToKV({'error': msg})
190191

‎test/kvform.py‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
from openid import kvform
2+
from openid import oidutil
3+
4+
def test_kvform():
5+
old_log = oidutil.log
6+
try:
7+
def log(unused_message, unused_level=None):
8+
log.num_warnings += 1
9+
10+
oidutil.log = log
11+
12+
cases = [
13+
# (kvform, parsed dictionary, expected warnings)
14+
('', {}, 0),
15+
('college:harvey mudd\n', {'college':'harvey mudd'}, 0),
16+
('city:claremont\nstate:CA\n',
17+
{'city':'claremont', 'state':'CA'}, 0),
18+
('is_valid:true\ninvalidate_handle:{HMAC-SHA1:2398410938412093}\n',
19+
{'is_valid':'true',
20+
'invalidate_handle':'{HMAC-SHA1:2398410938412093}'}, 0),
21+
22+
# Warnings from lines with no colon:
23+
('\n', {}, 1),
24+
('\n\n', {}, 2),
25+
('East is least\n', {}, 1),
26+
27+
# Warning from empty key
28+
(':\n', {'':''}, 1),
29+
(':missing key\n', {'':'missing key'}, 1),
30+
31+
# Warnings from leading or trailing whitespace in key or value
32+
(' street:foothill blvd\n', {'street':'foothill blvd'}, 1),
33+
('major: computer science\n', {'major':'computer science'}, 1),
34+
(' dorm : east \n', {'dorm':'east'}, 2),
35+
36+
# Warnings from missing trailing newline
37+
('e^(i*pi)+1:0', {'e^(i*pi)+1':'0'}, 1),
38+
('east:west\nnorth:south', {'east':'west', 'north':'south'}, 1),
39+
]
40+
41+
for case_kv, case_d, expected_warnings in cases:
42+
log.num_warnings = 0
43+
d = kvform.kvToDict(case_kv)
44+
assert case_d == d
45+
assert log.num_warnings == expected_warnings, (
46+
case_kv, log.num_warnings, expected_warnings)
47+
kv = kvform.dictToKV(d)
48+
d2 = kvform.kvToDict(kv)
49+
assert d == d2
50+
51+
cases = [
52+
([], ''),
53+
([('openid', 'useful'),
54+
('a', 'b')], 'openid:useful\na:b\n'),
55+
([(' openid', 'useful'),
56+
('a', 'b')], ' openid:useful\na:b\n'),
57+
([(' openid ', ' useful '),
58+
(' a ', ' b ')], ' openid : useful \n a : b \n'),
59+
([(' open id ', ' use ful '),
60+
(' a ', ' b ')], ' open id : use ful \n a : b \n'),
61+
]
62+
63+
for case, expected in cases:
64+
actual = kvform.seqToKV(case)
65+
assert actual == expected, (case, expected, actual)
66+
67+
seq = kvform.kvToSeq(actual)
68+
69+
# Expected to be unchanged, except stripping whitespace
70+
# from start and end of values (i. e. ordering, case, and
71+
# internal whitespace is preserved)
72+
expected_seq = []
73+
for k, v in case:
74+
expected_seq.append((k.strip(), v.strip()))
75+
76+
assert seq == expected_seq, (case, expected_seq, seq)
77+
78+
log.num_warnings = 0
79+
result = kvform.seqToKV([(1,1)])
80+
assert result == '1:1\n'
81+
assert log.num_warnings == 2
82+
83+
exceptional_cases = [
84+
[('openid', 'use\nful')],
85+
[('open\nid', 'useful')],
86+
[('open\nid', 'use\nful')],
87+
]
88+
for case in exceptional_cases:
89+
try:
90+
unexpected = kvform.seqToKV(case)
91+
except ValueError:
92+
pass
93+
else:
94+
assert False, 'Expected ValueError, got %r' % (unexpected,)
95+
96+
finally:
97+
oidutil.log = old_log
98+
99+
def test():
100+
test_kvform()
101+
102+
if __name__ == '__main__':
103+
test()

0 commit comments

Comments
 (0)