Files
ignis-core/app/models/api_key.py
2026-03-28 21:20:55 +07:00

22 lines
940 B
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 secrets
from datetime import datetime
from sqlalchemy import String, Boolean, DateTime
from sqlalchemy.orm import Mapped, mapped_column
from app.core.database import Base
class ApiKeyModel(Base):
"""Гостевой API-ключ с ограниченными правами."""
__tablename__ = "api_keys"
key: Mapped[str] = mapped_column(String, primary_key=True)
name: Mapped[str] = mapped_column(String) # "Вася", "гости"
is_admin: Mapped[bool] = mapped_column(Boolean, default=False) # доступ к CRUD групп, расписаниям
active: Mapped[bool] = mapped_column(Boolean, default=True)
created_at: Mapped[str] = mapped_column(String, default=lambda: datetime.now().isoformat())
@staticmethod
def generate_key() -> str:
"""Генерация безопасного случайного токена."""
return secrets.token_urlsafe(32)