mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
fix(fastapi): fix SessionID (and ResetKey) generation
Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
d658627e99
commit
47feb72f48
4 changed files with 12 additions and 35 deletions
29
aurweb/db.py
29
aurweb/db.py
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue