Various encoding fixes. Fixes #2160

pull/3/head
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

View File

@ -385,9 +385,15 @@ def create_app(app_name=config.APP_NAME):
svr_superuser = registry.get(section, 'Superuser')
svr_port = registry.getint(section, 'Port')
svr_discovery_id = section
svr_comment = gettext("Auto-detected %s installation with the data directory at %s" % (
registry.get(section, 'Description'),
registry.get(section, 'DataDirectory')))
description = registry.get(section, 'Description')
data_directory = registry.get(section, 'DataDirectory')
if hasattr(str, 'decode'):
description = description.decode('utf-8')
data_directory = data_directory.decode('utf-8')
svr_comment = gettext(u"Auto-detected %s installation with the data directory at %s" % (
description,
data_directory
))
add_server(user_id, servergroup_id, svr_name, svr_superuser, svr_port, svr_discovery_id, svr_comment)
except:

View File

@ -23,6 +23,9 @@ import config
class AboutModule(PgAdminModule):
def get_own_menuitems(self):
appname = config.APP_NAME
if hasattr(str, 'decode'):
appname = appname.decode('utf-8')
return {
'help_items': [
MenuItem(name='mnu_about',
@ -30,8 +33,10 @@ class AboutModule(PgAdminModule):
module="pgAdmin.About",
callback='about_show',
icon='fa fa-info-circle',
label=gettext('About %(appname)s',
appname=config.APP_NAME))
label=gettext(u'About %(appname)s',
appname=appname
)
)
]
}

View File

@ -584,14 +584,15 @@ class ServerNode(PGChildNodeView):
password=password,
server_types=ServerType.types()
)
if hasattr(str, 'decode'):
errmsg = errmsg.decode('utf-8')
if not status:
db.session.delete(server)
db.session.commit()
return make_json_response(
status=401,
success=0,
errormsg=gettext("Unable to connect to server:\n\n%s" % errmsg)
errormsg=gettext(u"Unable to connect to server:\n\n%s" % errmsg)
)
else:
if 'save_password' in data and data['save_password'] and have_password:

View File

@ -505,7 +505,7 @@ class Filemanager(object):
if not path_exists(orig_path):
return {
'Code': 0,
'Error': gettext(u"'{}' file does not exist.".format(path))
'Error': gettext(u"'{0}' file does not exist.".format(path))
}
user_dir = path
@ -599,7 +599,7 @@ class Filemanager(object):
# Do not allow user to access outside his storage dir in server mode.
if not orig_path.startswith(dir):
raise Exception(
gettext(u"Access denied ({})".format(path)))
gettext(u"Access denied ({0})".format(path)))
return True
@staticmethod
@ -690,7 +690,7 @@ class Filemanager(object):
}
if not path_exists(orig_path):
thefile['Error'] = gettext(u"'{}' file does not exist.".format(
thefile['Error'] = gettext(u"'{0}' file does not exist.".format(
path))
thefile['Code'] = -1
return thefile

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()

View File

@ -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
)
)