Passwords stored salt and some change is password logic
This commit is contained in:
@@ -2,23 +2,25 @@
|
||||
## SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from hashlib import sha256
|
||||
|
||||
from backend.app.database import get_connection
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
return sha256(password.encode('utf-8')).hexdigest()
|
||||
from backend.app.database import get_connection, hash_password, verify_password
|
||||
|
||||
|
||||
def authenticate_user(username: str, password: str):
|
||||
password_hash = hash_password(password)
|
||||
with get_connection() as conn:
|
||||
row = conn.execute(
|
||||
'SELECT * FROM users WHERE username = ? AND password_hash = ?',
|
||||
(username, password_hash),
|
||||
).fetchone()
|
||||
return dict(row) if row else None
|
||||
row = conn.execute('SELECT * FROM users WHERE username = ?', (username,)).fetchone()
|
||||
if row is None or not verify_password(password, row['password_hash']):
|
||||
return None
|
||||
user = dict(row)
|
||||
if not row['password_hash'].startswith('scrypt$'):
|
||||
conn.execute(
|
||||
'UPDATE users SET password_hash = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?',
|
||||
(hash_password(password), row['id']),
|
||||
)
|
||||
conn.commit()
|
||||
user['password_hash'] = conn.execute(
|
||||
'SELECT password_hash FROM users WHERE id = ?', (row['id'],)
|
||||
).fetchone()['password_hash']
|
||||
return user
|
||||
|
||||
|
||||
def find_user(username: str):
|
||||
|
||||
Reference in New Issue
Block a user