Hey Massimo,
I've taken the initiative and built a patch to allow encoded
credentials in database URIs.
The following patch adds an additional boolean argument to the SQLDB
initializer called decode_credentials (defaults to False). The
decode_credentials argument is used to build the 'credential_decoder'
lambda. If decode_credentials is False then the lambda doesn't do
anything, but if decode_credentials is True then the lambda will pass
the credential through urllib.unquote.
Anytime a username or password is parsed from the URI, it will be
passed through the credential_decoder lambda before being used with
the underlying database connection.
Let me know what you think, or if you want me to change anything about
it before you'll add it into trunk.
=====BEGIN PATCH=====
--- sql.py 2010-10-18 14:44:44.096823600 +0000
+++ sql_patched.py 2010-10-18 15:09:06.141587000 +0000
@@ -898,7 +898,13 @@
def __init__(self, uri='sqlite://dummy.db', pool_size=0,
folder=None, db_codec='UTF-8', check_reserved=None,
- migrate=True, fake_migrate=False):
+ migrate=True, fake_migrate=False,
decode_credentials=False):
+ if not decode_credentials:
+ credential_decoder = lambda cred: cred
+ else:
+ import urllib
+ credential_decoder = lambda cred: urllib.unquote(cred)
+
self._uri = str(uri) # NOTE: assuming it is in utf8!!!
self._pool_size = pool_size
self._db_codec = db_codec
@@ -957,10 +963,10 @@
if not m:
raise SyntaxError, \
"Invalid URI string in SQLDB: %s" % self._uri
- user = m.group('user')
+ user = credential_decoder(m.group('user'))
if not user:
raise SyntaxError, 'User required'
- passwd = m.group('passwd')
+ passwd = credential_decoder(m.group('passwd'))
if not passwd:
passwd = ''
host = m.group('host')
@@ -992,10 +998,10 @@
).match(self._uri[11:])
if not m:
raise SyntaxError, "Invalid URI string in SQLDB"
- user = m.group('user')
+ user = credential_decoder(m.group('user'))
if not user:
raise SyntaxError, 'User required'
- passwd = m.group('passwd')
+ passwd = credential_decoder(m.group('passwd'))
if not passwd:
passwd = ''
host = m.group('host')
@@ -1067,10 +1073,10 @@
if not m:
raise SyntaxError, \
"Invalid URI string in SQLDB: %s" % self._uri
- user = m.group('user')
+ user = credential_decoder(m.group('user'))
if not user:
raise SyntaxError, 'User required'
- passwd = m.group('passwd')
+ passwd = credential_decoder(m.group('passwd'))
if not passwd:
passwd = ''
host = m.group('host')
@@ -1108,10 +1114,10 @@
if not m:
raise SyntaxError, \
"Invalid URI string in SQLDB: %s" % self._uri
- user = m.group('user')
+ user = credential_decoder(m.group('user'))
if not user:
raise SyntaxError, 'User required'
- passwd = m.group('passwd')
+ passwd = credential_decoder(m.group('passwd'))
if not passwd:
passwd = ''
host = m.group('host')
@@ -1137,10 +1143,10 @@
if not m:
raise SyntaxError, \
"Invalid URI string in SQLDB: %s" % self._uri
- user = m.group('user')
+ user = credential_decoder(m.group('user'))
if not user:
raise SyntaxError, 'User required'
- passwd = m.group('passwd')
+ passwd = credential_decoder(m.group('passwd'))
if not passwd:
passwd = ''
pathdb = m.group('path')
@@ -1165,10 +1171,10 @@
if not m:
raise SyntaxError, \
"Invalid URI string in SQLDB: %s" % self._uri
- user = m.group('user')
+ user = credential_decoder(m.group('user'))
if not user:
raise SyntaxError, 'User required'
- passwd = m.group('passwd')
+ passwd = credential_decoder(m.group('passwd'))
if not passwd:
passwd = ''
host = m.group('host')
@@ -1205,10 +1211,10 @@
).match(self._uri[11:])
if not m:
raise SyntaxError, "Invalid URI string in SQLDB"
- user = m.group('user')
+ user = credential_decoder(m.group('user'))
if not user:
raise SyntaxError, 'User required'
- passwd = m.group('passwd')
+ passwd = credential_decoder(m.group('passwd'))
if not passwd:
passwd = ''
host = m.group('host')
====END PATCH====