Various encoding fixes. Fixes #2160

This commit is contained in:
Murtuza Zabuawala
2017-02-16 11:00:40 +00:00
committed by Dave Page
parent 833629d438
commit 9ad1316990
6 changed files with 44 additions and 18 deletions
+15 -4
View File
@@ -29,9 +29,13 @@ def encrypt(plaintext, key):
iv = Random.new().read(AES.block_size)
cipher = AES.new(pad(key), AES.MODE_CFB, iv)
excrypted = base64.b64encode(iv + cipher.encrypt(plaintext))
# If user has entered non ascii password (Python2)
# we have to encode it first
if hasattr(str, 'decode'):
plaintext = plaintext.encode('utf-8')
encrypted = base64.b64encode(iv + cipher.encrypt(plaintext))
return excrypted
return encrypted
def decrypt(ciphertext, key):
@@ -99,8 +103,15 @@ def pqencryptpassword(password, user):
# Place salt at the end because it may be known by users trying to crack
# the MD5 output.
# Handling of non-ascii password (Python2)
if hasattr(str, 'decode'):
password = password.encode('utf-8')
user = user.encode('utf-8')
else:
password = password.encode()
user = user.encode()
m.update(password.encode())
m.update(user.encode())
m.update(password)
m.update(user)
return "md5" + m.hexdigest()
@@ -260,10 +260,13 @@ class Connection(BaseConnection):
try:
password = decrypt(encpass, user.password)
# Handling of non ascii password (Python2)
if hasattr(str, 'decode'):
password = password.decode('utf-8').encode('utf-8')
# password is in bytes, for python3 we need it in string
if isinstance(password, bytes):
elif isinstance(password, bytes):
password = password.decode()
except Exception as e:
current_app.logger.exception(e)
return False, \
@@ -306,12 +309,12 @@ class Connection(BaseConnection):
msg = e.diag.message_detail
else:
msg = str(e)
current_app.logger.info("""
current_app.logger.info(u"""
Failed to connect to the database server(#{server_id}) for connection ({conn_id}) with error message as below:
{msg}""".format(
server_id=self.manager.sid,
conn_id=conn_id,
msg=msg
msg=msg.decode('utf-8') if hasattr(str, 'decode') else msg
)
)