Skip to content

Commit

Permalink
Merge pull request #14 from RealistikOsu/bcrypt-md5-cache
Browse files Browse the repository at this point in the history
fix: use bcrypt: md5 cache instead of userid: md5
  • Loading branch information
7ez committed Mar 29, 2024
2 parents 5174843 + b34ad8e commit 3998e29
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 30 deletions.
13 changes: 6 additions & 7 deletions helpers/user_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,20 @@ def verify_password(user_id: int, password: str) -> bool:
password (str): The user's password hashed with MD5.
"""

# Check if we already cached them, for speed benefit.
passw = glob.cached_passwords.get(user_id)
if passw:
return password == passw

# Nope. Sad. Bcrypt time.
passw_db = glob.db.fetch(
"SELECT password_md5 FROM users WHERE id = %s LIMIT 1",
(user_id,),
)["password_md5"]

# Check if we already cached them, for speed benefit.
if passw_db in glob.cached_passwords:
return glob.cached_passwords[passw_db] == password

res = bcrypt.checkpw(password.encode(), passw_db.encode())
# Cache it for later
if res:
glob.cached_passwords[user_id] = password
glob.cached_passwords[passw_db] = password

return res


Expand Down
2 changes: 1 addition & 1 deletion objects/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
tokens = TokenList()
channels = ChannelList()
matches = MatchList()
cached_passwords: dict = {}
cached_passwords: dict[str, str] = {}
chatFilters = None
pool: ThreadPool
busyThreads = 0
Expand Down
22 changes: 0 additions & 22 deletions pubSubHandlers/changePassword.py

This file was deleted.

0 comments on commit 3998e29

Please sign in to comment.