Python 3.6 compatibility: bytes vs strings stuff
These changes mostly revolve around the change in ObjectId constructor when running on Python 3.6. Where on 2.7 the constructor would accept 12- and 24-byte strings, now only 12-byte bytes and 24-character strings are accepted. Good thing, but required some changes in our code. Other changes include hashing of strings, which isn't supported, so they are converted to bytes first, and sometimes converted back afterwards.
This commit is contained in:
@@ -293,7 +293,7 @@ def delete_file(file_item):
|
||||
process_file_delete(file_item)
|
||||
|
||||
|
||||
def generate_link(backend, file_path, project_id=None, is_public=False):
|
||||
def generate_link(backend, file_path: str, project_id=None, is_public=False):
|
||||
"""Hook to check the backend of a file resource, to build an appropriate link
|
||||
that can be used by the client to retrieve the actual file.
|
||||
"""
|
||||
@@ -321,7 +321,7 @@ def generate_link(backend, file_path, project_id=None, is_public=False):
|
||||
if backend == 'cdnsun':
|
||||
return hash_file_path(file_path, None)
|
||||
if backend == 'unittest':
|
||||
return 'https://unit.test/%s' % md5(file_path).hexdigest()
|
||||
return 'https://unit.test/%s' % md5(file_path.encode()).hexdigest()
|
||||
|
||||
log.warning('generate_link(): Unknown backend %r, returning empty string as new link.',
|
||||
backend)
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import base64
|
||||
import hashlib
|
||||
import logging
|
||||
import typing
|
||||
|
||||
import bcrypt
|
||||
import datetime
|
||||
@@ -67,12 +68,12 @@ def make_token():
|
||||
return jsonify(token=token['token'])
|
||||
|
||||
|
||||
def generate_and_store_token(user_id, days=15, prefix=''):
|
||||
def generate_and_store_token(user_id, days=15, prefix=b''):
|
||||
"""Generates token based on random bits.
|
||||
|
||||
:param user_id: ObjectId of the owning user.
|
||||
:param days: token will expire in this many days.
|
||||
:param prefix: the token will be prefixed by this string, for easy identification.
|
||||
:param prefix: the token will be prefixed by these bytes, for easy identification.
|
||||
:return: the token document.
|
||||
"""
|
||||
|
||||
@@ -80,17 +81,22 @@ def generate_and_store_token(user_id, days=15, prefix=''):
|
||||
|
||||
# Use 'xy' as altargs to prevent + and / characters from appearing.
|
||||
# We never have to b64decode the string anyway.
|
||||
token = prefix + base64.b64encode(random_bits, altchars='xy').strip('=')
|
||||
token = prefix + base64.b64encode(random_bits, altchars=b'xy').strip(b'=')
|
||||
|
||||
token_expiry = datetime.datetime.now(tz=tz_util.utc) + datetime.timedelta(days=days)
|
||||
return store_token(user_id, token, token_expiry)
|
||||
return store_token(user_id, token.decode('ascii'), token_expiry)
|
||||
|
||||
|
||||
def hash_password(password, salt):
|
||||
def hash_password(password: str, salt: typing.Union[str, bytes]) -> str:
|
||||
password = password.encode()
|
||||
|
||||
if isinstance(salt, str):
|
||||
salt = salt.encode('utf-8')
|
||||
encoded_password = base64.b64encode(hashlib.sha256(password).digest())
|
||||
return bcrypt.hashpw(encoded_password, salt)
|
||||
|
||||
hash = hashlib.sha256(password).digest()
|
||||
encoded_password = base64.b64encode(hash)
|
||||
hashed_password = bcrypt.hashpw(encoded_password, salt)
|
||||
return hashed_password.decode('ascii')
|
||||
|
||||
|
||||
def setup_app(app, url_prefix):
|
||||
|
@@ -221,7 +221,7 @@ def create_service_account(email, roles, service, update_existing=None):
|
||||
user.update(result)
|
||||
|
||||
# Create an authentication token that won't expire for a long time.
|
||||
token = local_auth.generate_and_store_token(user['_id'], days=36500, prefix='SRV')
|
||||
token = local_auth.generate_and_store_token(user['_id'], days=36500, prefix=b'SRV')
|
||||
|
||||
return user, token
|
||||
|
||||
|
@@ -136,7 +136,7 @@ def str2id(document_id):
|
||||
|
||||
try:
|
||||
return bson.ObjectId(document_id)
|
||||
except bson.objectid.InvalidId:
|
||||
except (bson.objectid.InvalidId, TypeError):
|
||||
log.debug('str2id(%r): Invalid Object ID', document_id)
|
||||
raise wz_exceptions.BadRequest('Invalid object ID %r' % document_id)
|
||||
|
||||
|
@@ -118,7 +118,7 @@ def find_token(token, is_subclient_token=False, **extra_filters):
|
||||
return db_token
|
||||
|
||||
|
||||
def store_token(user_id, token, token_expiry, oauth_subclient_id=False):
|
||||
def store_token(user_id, token: str, token_expiry, oauth_subclient_id=False):
|
||||
"""Stores an authentication token.
|
||||
|
||||
:returns: the token document from MongoDB
|
||||
|
@@ -301,10 +301,11 @@ class AbstractPillarTest(TestMinimal):
|
||||
json=BLENDER_ID_USER_RESPONSE,
|
||||
status=200)
|
||||
|
||||
def make_header(self, username, subclient_id=''):
|
||||
def make_header(self, username: str, subclient_id: str='') -> bytes:
|
||||
"""Returns a Basic HTTP Authentication header value."""
|
||||
|
||||
return 'basic ' + base64.b64encode('%s:%s' % (username, subclient_id))
|
||||
content = '%s:%s' % (username, subclient_id)
|
||||
return b'basic ' + base64.b64encode(content.encode())
|
||||
|
||||
def create_standard_groups(self, additional_groups=()):
|
||||
"""Creates standard admin/demo/subscriber groups, plus any additional.
|
||||
|
@@ -4,6 +4,7 @@ import urllib.request, urllib.parse, urllib.error
|
||||
import logging
|
||||
import traceback
|
||||
import sys
|
||||
import typing
|
||||
|
||||
import dateutil.parser
|
||||
from flask import current_app
|
||||
@@ -174,7 +175,7 @@ def get_main_project():
|
||||
return main_project
|
||||
|
||||
|
||||
def is_valid_id(some_id):
|
||||
def is_valid_id(some_id: typing.Union[str, bytes]):
|
||||
"""Returns True iff the given string is a valid ObjectId.
|
||||
|
||||
Only use this if you do NOT need an ObjectId object. If you do need that,
|
||||
@@ -184,27 +185,22 @@ def is_valid_id(some_id):
|
||||
:rtype: bool
|
||||
"""
|
||||
|
||||
if isinstance(some_id, bytes):
|
||||
return len(some_id) == 12
|
||||
|
||||
if not isinstance(some_id, str):
|
||||
return False
|
||||
|
||||
if isinstance(some_id, str):
|
||||
try:
|
||||
some_id = some_id.encode('ascii')
|
||||
except UnicodeEncodeError:
|
||||
return False
|
||||
if len(some_id) != 24:
|
||||
return False
|
||||
|
||||
if len(some_id) == 12:
|
||||
return True
|
||||
elif len(some_id) == 24:
|
||||
# This is more than 5x faster than checking character by
|
||||
# character in a loop.
|
||||
try:
|
||||
int(some_id, 16)
|
||||
except ValueError:
|
||||
return False
|
||||
return True
|
||||
|
||||
return False
|
||||
# This is more than 5x faster than checking character by
|
||||
# character in a loop.
|
||||
try:
|
||||
int(some_id, 16)
|
||||
except ValueError:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def last_page_index(meta_info):
|
||||
|
Reference in New Issue
Block a user