Refactor configuration and enhance Telegram initData validation

- Improved formatting and readability in config.py and other files by adding line breaks.
- Introduced INIT_DATA_MAX_AGE_SECONDS to enforce replay protection for Telegram initData.
- Updated validate_init_data function to include max_age_seconds parameter for validation.
- Enhanced API to reject old initData based on the new max_age_seconds setting.
- Added tests for auth_date expiry and validation of initData in test_telegram_auth.py.
- Updated README with details on the new INIT_DATA_MAX_AGE_SECONDS configuration.
This commit is contained in:
2026-02-17 17:31:20 +03:00
parent d20a285f09
commit 1948618394
19 changed files with 181 additions and 25 deletions

View File

@@ -1,4 +1,5 @@
"""Database layer: SQLAlchemy models, Pydantic schemas, repository, init."""
from db.models import Base, User, Duty
from db.schemas import UserCreate, UserInDb, DutyCreate, DutyInDb, DutyWithUser
from db.session import get_engine, get_session_factory, get_session

View File

@@ -1,10 +1,12 @@
"""SQLAlchemy ORM models for users and duties."""
from sqlalchemy import ForeignKey, Integer, BigInteger, Text
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
class Base(DeclarativeBase):
"""Declarative base for all models."""
pass
@@ -12,7 +14,9 @@ class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
telegram_user_id: Mapped[int] = mapped_column(BigInteger, unique=True, nullable=False)
telegram_user_id: Mapped[int] = mapped_column(
BigInteger, unique=True, nullable=False
)
full_name: Mapped[str] = mapped_column(Text, nullable=False)
username: Mapped[str | None] = mapped_column(Text, nullable=True)
first_name: Mapped[str | None] = mapped_column(Text, nullable=True)
@@ -25,7 +29,9 @@ class Duty(Base):
__tablename__ = "duties"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"), nullable=False)
user_id: Mapped[int] = mapped_column(
Integer, ForeignKey("users.id"), nullable=False
)
start_at: Mapped[str] = mapped_column(Text, nullable=False) # ISO 8601
end_at: Mapped[str] = mapped_column(Text, nullable=False) # ISO 8601

View File

@@ -1,4 +1,5 @@
"""Repository: get_or_create_user, get_duties, insert_duty."""
from sqlalchemy.orm import Session
from db.models import User, Duty

View File

@@ -1,4 +1,5 @@
"""Pydantic schemas for API and validation."""
from pydantic import BaseModel, ConfigDict
@@ -23,7 +24,7 @@ class UserInDb(UserBase):
class DutyBase(BaseModel):
user_id: int
start_at: str # ISO 8601
end_at: str # ISO 8601
end_at: str # ISO 8601
class DutyCreate(DutyBase):
@@ -38,6 +39,7 @@ class DutyInDb(DutyBase):
class DutyWithUser(DutyInDb):
"""Duty with full_name for calendar display."""
full_name: str
model_config = ConfigDict(from_attributes=True)

View File

@@ -1,11 +1,11 @@
"""SQLAlchemy engine and session factory."""
from contextlib import contextmanager
from typing import Generator
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from db.models import Base
_engine = None
_SessionLocal = None
@@ -26,7 +26,9 @@ def get_engine(database_url: str):
if _engine is None:
_engine = create_engine(
database_url,
connect_args={"check_same_thread": False} if "sqlite" in database_url else {},
connect_args={"check_same_thread": False}
if "sqlite" in database_url
else {},
echo=False,
)
return _engine