Skip to content

Commit 9fcd022

Browse files
author
tailor
committed
[project @ Move getOpenIDParamaters, quoteMinimal, and normalizeURL to oidutil]
1 parent 9e06230 commit 9fcd022

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

‎openid/oidutil.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import binascii
44
import sys
5+
import urlparse
56

67
from urllib import urlencode
78

@@ -29,3 +30,50 @@ def fromBase64(s):
2930
return binascii.a2b_base64(s)
3031
except binascii.Error:
3132
return ''
33+
34+
35+
def getOpenIDParameters(query):
36+
params = {}
37+
for k, v in query.iteritems():
38+
if k.startswith('openid.'):
39+
params[k] = v
40+
return params
41+
42+
43+
def quoteMinimal(s):
44+
# Do not escape anything that is already 7-bit safe, so we do the
45+
# minimal transform on the identity URL
46+
res = []
47+
for c in s:
48+
if c >= u'\x80':
49+
for b in c.encode('utf8'):
50+
res.append('%%%02X' % ord(b))
51+
else:
52+
res.append(c)
53+
return str(''.join(res))
54+
55+
56+
def normalizeUrl(url):
57+
assert isinstance(url, (str, unicode)), type(url)
58+
59+
url = url.strip()
60+
parsed = urlparse.urlparse(url)
61+
62+
if parsed[0] == '' or parsed[1] == '':
63+
url = 'http://' + url
64+
parsed = urlparse.urlparse(url)
65+
66+
if isinstance(url, unicode):
67+
authority = parsed[1].encode('idna')
68+
else:
69+
authority = str(parsed[1])
70+
71+
tail = map(quoteMinimal, parsed[2:])
72+
if tail[0] == '':
73+
tail[0] = '/'
74+
encoded = (str(parsed[0]), authority) + tuple(tail)
75+
url = urlparse.urlunparse(encoded)
76+
assert type(url) is str
77+
78+
return url
79+

0 commit comments

Comments
 (0)