Files
kandrusyak_bot/assistant_bot/auth.py
2026-07-25 15:57:30 +03:00

58 lines
1.9 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.

from hmac import compare_digest
from telegram import Update
from telegram.constants import ChatType
from telegram.ext import ApplicationHandlerStop, ContextTypes
from .telegram_utils import get_storage, require_user_id
def passwords_match(candidate: str, expected: str) -> bool:
return compare_digest(
candidate.encode("utf-8"),
expected.encode("utf-8"),
)
async def authentication_guard(
update: Update,
context: ContextTypes.DEFAULT_TYPE,
) -> None:
user_id = require_user_id(update)
if user_id is None:
raise ApplicationHandlerStop
storage = get_storage(context)
if storage.is_user_authorized(user_id):
return
message = update.effective_message
chat = update.effective_chat
if chat is not None and chat.type != ChatType.PRIVATE:
if message:
await message.reply_text(
"Сначала авторизуйся в личном чате с ботом."
)
raise ApplicationHandlerStop
candidate = message.text.strip() if message and message.text else ""
password = str(context.application.bot_data["assistant_password"])
if candidate and passwords_match(candidate, password):
storage.authorize_user(user_id)
await message.reply_text(
"Пароль принят. Доступ открыт — повторно вводить его не нужно."
)
raise ApplicationHandlerStop
if candidate and not candidate.startswith("/"):
await message.reply_text("Неверный пароль. Попробуй еще раз.")
raise ApplicationHandlerStop
if message:
await message.reply_text(
"Для доступа к боту введи пароль одним текстовым сообщением."
)
elif update.inline_query:
await update.inline_query.answer([], cache_time=0)
raise ApplicationHandlerStop