forked from mtusi2026/mtusi2026lab2
ЛР2: PostgreSQL, сессии (Basic + Bearer), Compose, миграции
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import CheckConstraint, DateTime, ForeignKey, Index, LargeBinary, Uuid, text
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from models.base import Base
|
||||
from models.users_models import User
|
||||
|
||||
|
||||
class AuthSession(Base):
|
||||
"""Сессия входа (не путать с ORM-сессией SQLAlchemy)."""
|
||||
__tablename__ = "sessions"
|
||||
__table_args__ = (
|
||||
CheckConstraint("expires_at > created_at", name="expiry_after_creation"),
|
||||
Index(None, "expires_at"),
|
||||
)
|
||||
|
||||
# SHA-256 от токена, сам токен не храним.
|
||||
token_hash: Mapped[bytes] = mapped_column(LargeBinary, primary_key=True)
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(Uuid, ForeignKey("users.id", ondelete="CASCADE"))
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=text("clock_timestamp()"))
|
||||
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True))
|
||||
|
||||
user: Mapped[User] = relationship(lazy="joined")
|
||||
Reference in New Issue
Block a user