Skip to content

Commit

Permalink
feat(telelog): add authentication token lifetime configuration
Browse files Browse the repository at this point in the history
- Add TELEGRAM_AUTH_TOKEN_EXPIRATION variable to Django settings
- Update .env example file with new variable
- Replace hardcoded value with setting in TelegramAuthService
- Simplify Telegram bot messages
- Improve invalid token error description
  • Loading branch information
TheFoxKD committed Dec 2, 2024
1 parent 817a32f commit cea1e4f
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions .envs/dev/.django.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ DJANGO_ADMIN_URL=admin/
# Telegram settings
TELEGRAM_BOT_USERNAME=telelog_auth_bot
TELEGRAM_BOT_TOKEN='token-example'
TELEGRAM_AUTH_TOKEN_EXPIRATION=300
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ docker-compose.override.yml
.DS_Store
Thumbs.db
.envs/dev/.django
temp.txt
3 changes: 3 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,6 @@
# Telegram Bot settings
TELEGRAM_BOT_USERNAME = env("TELEGRAM_BOT_USERNAME")
TELEGRAM_BOT_TOKEN = env("TELEGRAM_BOT_TOKEN")
TELEGRAM_AUTH_TOKEN_EXPIRATION = env.int(
"TELEGRAM_AUTH_TOKEN_EXPIRATION", default=300
) # 5 minutes in seconds
2 changes: 1 addition & 1 deletion src/authentication/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self):
def generate_auth_token(self) -> str:
"""Генерирует безопасный токен для Telegram auth."""
token = secrets.token_urlsafe(32)
self.redis_client.setex(token, 300, "valid") # Токен действителен 5 минут
self.redis_client.setex(token, settings.TELEGRAM_AUTH_TOKEN_EXPIRATION, "valid")
return token

def validate_telegram_data(self, data: dict) -> bool:
Expand Down
6 changes: 2 additions & 4 deletions src/telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,14 @@ async def handle_start(self, update: Update, context: CallbackContext):
await update.message.reply_text(
"✅ Отлично! Вы успешно авторизованы в системе TeleLog!\n\n"
"Теперь вы можете:\n"
f"• Вернуться на сайт {self.site_url}\n"
"• Настроить уведомления в личном кабинете\n"
"• Начать получать важные обновления\n\n"
f"• Вернуться на сайт {self.site_url}\n\n"
"Приятного использования! 🚀"
)
else:
await update.message.reply_text(
"❌ Упс! Токен недействителен или устарел.\n\n"
"Возможные причины:\n"
"• Истек ср��к действия токена (5 минут)\n"
"• Истек срок действия токена\n"
"• Токен уже был использован\n"
"• Опечатка при вводе токена\n\n"
f"Пожалуйста, вернитесь на сайт {self.site_url} и получите новый токен."
Expand Down

0 comments on commit cea1e4f

Please sign in to comment.