Files

36 lines
1.1 KiB
Python
Raw Permalink Normal View History

2026-08-25 15:16:31 +02:00
import db
2026-08-24 15:18:45 +02:00
from hashlib import sha256
2026-08-24 01:37:14 +02:00
2026-08-25 01:48:03 +02:00
# Check if access token can update domain's ip
2026-08-24 01:37:14 +02:00
def check_token_domain(db_path: str, token: str, domain: str) -> bool:
2026-08-25 15:16:31 +02:00
userid = db.Token.get_user(db_path, token)
2026-08-24 15:18:45 +02:00
2026-08-25 15:16:31 +02:00
domain = domain.strip()
2026-08-24 15:18:45 +02:00
2026-08-25 15:16:31 +02:00
return domain in db.User.get_domains(db_path, userid)
2026-08-25 01:48:03 +02:00
# Verify user's passowrd for login. Return UserID on success, None on failure
2026-08-24 17:24:08 +02:00
def user_login(db_path: str, username: str, password: str) -> int | None:
2026-08-25 16:20:27 +02:00
try:
userid = db.User.get_id(db_path, username)
except ValueError:
return None
2026-08-24 15:18:45 +02:00
2026-08-25 15:16:31 +02:00
pwsalt, pwhash = db.User.get_password_hash(db_path, userid)
2026-08-24 15:18:45 +02:00
2026-08-25 15:16:31 +02:00
password_hash = sha256( (pwsalt + password).encode('utf-8')).hexdigest()
2026-08-24 15:18:45 +02:00
2026-08-25 15:16:31 +02:00
if pwhash == password_hash:
return userid
2026-08-24 17:24:08 +02:00
2026-08-25 15:16:31 +02:00
return None
2026-08-25 01:48:03 +02:00
# Change password of user (by user)
2026-08-25 15:16:31 +02:00
def change_user_password(db_path: str, userid: int, oldpass: str, newpass: str) -> None:
username = db.User.get_username(db_path, userid)
2026-08-24 17:24:08 +02:00
2026-08-25 15:16:31 +02:00
if user_login(db_path, username, oldpass) is None:
raise ValueError("Incorrect password")
2026-08-24 17:24:08 +02:00
2026-08-25 15:16:31 +02:00
db.User.set_password(db_path, userid, newpass)