Captain_s-Log/app/auth.py
Vic Sergeev 9b0a20aa91 working
2026-07-04 16:14:16 +03:00

55 lines
No EOL
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Модуль аутентификации"""
import hashlib
import os
from app.database import Database
class Auth:
def __init__(self, db: Database):
self.db = db
def set_master_password(self, password):
"""Установить мастер-пароль"""
password_hash = self._hash_password(password)
self.db.set_setting('master_password_hash', password_hash)
self.db.set_setting('password_set', '1')
def check_password(self, password):
"""Проверить мастер-пароль"""
stored_hash = self.db.get_setting('master_password_hash')
if not stored_hash:
return True # Пароль не установлен
return self._hash_password(password) == stored_hash
def is_password_set(self):
"""Проверяет, установлен ли пароль"""
return self.db.get_setting('password_set', '0') == '1'
def ask_password_on_startup(self):
"""Спрашивать ли пароль при входе"""
return self.db.get_setting('ask_password_on_startup', '1') == '1'
def set_ask_password_on_startup(self, value):
"""Установить, спрашивать ли пароль при входе"""
self.db.set_setting('ask_password_on_startup', '1' if value else '0')
def reset_password(self):
"""Сбросить мастер-пароль"""
self.db.set_setting('password_set', '0')
self.db.set_setting('master_password_hash', '')
self.db.set_setting('ask_password_on_startup', '1')
def change_password(self, old_password, new_password):
"""Изменить мастер-пароль"""
if self.is_password_set():
if not self.check_password(old_password):
return False, "Неверный текущий пароль"
self.set_master_password(new_password)
return True, "Пароль успешно изменен"
def _hash_password(self, password):
"""Хешировать пароль с солью"""
# В будущем здесь будет более безопасное хеширование
# Пока используем SHA-256 для простоты
return hashlib.sha256(password.encode()).hexdigest()