From 0228fbebfdf0e0c63170fc412e551d6d2da22afe Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sat, 24 Feb 2018 18:54:59 -0800 Subject: [PATCH] Move the random hex generator to helpers. --- etiquette/helpers.py | 6 ++++++ frontends/etiquette_flask/etiquette_flask/sessions.py | 5 +---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/etiquette/helpers.py b/etiquette/helpers.py index 9668659..4afb15a 100644 --- a/etiquette/helpers.py +++ b/etiquette/helpers.py @@ -217,6 +217,12 @@ def now(timestamp=True): return n.timestamp() return n +def random_hex(length=12): + randbytes = os.urandom(math.ceil(length / 2)) + token = ''.join('{:02x}'.format(x) for x in randbytes) + token = token[:length] + return token + def read_filebytes(filepath, range_min, range_max, chunk_size=2 ** 20): ''' Yield chunks of bytes from the file between the endpoints. diff --git a/frontends/etiquette_flask/etiquette_flask/sessions.py b/frontends/etiquette_flask/etiquette_flask/sessions.py index 2507e5e..fdfb892 100644 --- a/frontends/etiquette_flask/etiquette_flask/sessions.py +++ b/frontends/etiquette_flask/etiquette_flask/sessions.py @@ -10,10 +10,7 @@ SESSION_MAX_AGE = 86400 REQUEST_TYPES = (flask.Request, werkzeug.wrappers.Request, werkzeug.local.LocalProxy) def _generate_token(length=32): - randbytes = os.urandom(math.ceil(length / 2)) - token = ''.join('{:02x}'.format(x) for x in randbytes) - token = token[:length] - return token + return etiquette.helpers.random_hex(length=length) def _normalize_token(token): if isinstance(token, REQUEST_TYPES):