fix(fastapi): fix SessionID (and ResetKey) generation

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-11-27 20:04:26 -08:00
parent d658627e99
commit 47feb72f48
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
4 changed files with 12 additions and 35 deletions

View file

@ -24,42 +24,15 @@ DRIVERS = {
"mysql": "mysql+mysqldb"
}
# Global introspected object memo.
introspected = dict()
# Some types we don't get access to in this module.
Base = NewType("Base", "aurweb.models.declarative_base.Base")
def make_random_value(table: str, column: str):
def make_random_value(table: str, column: str, length: int):
""" Generate a unique, random value for a string column in a table.
This can be used to generate for example, session IDs that
align with the properties of the database column with regards
to size.
Internally, we use SQLAlchemy introspection to look at column
to decide which length to use for random string generation.
:return: A unique string that is not in the database
"""
global introspected
# Make sure column is converted to a string for memo interaction.
scolumn = str(column)
# If the target column is not yet introspected, store its introspection
# object into our global `introspected` memo.
if scolumn not in introspected:
from sqlalchemy import inspect
target_column = scolumn.split('.')[-1]
col = list(filter(lambda c: c.name == target_column,
inspect(table).columns))[0]
introspected[scolumn] = col
col = introspected.get(scolumn)
length = col.type.length
string = aurweb.util.make_random_string(length)
while query(table).filter(column == string).first():
string = aurweb.util.make_random_string(length)