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