40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
|
|
"""Authentication models: User, UserCredential."""
|
||
|
|
|
||
|
|
import uuid
|
||
|
|
|
||
|
|
from sqlalchemy import ForeignKey, Integer, LargeBinary, String
|
||
|
|
from sqlalchemy.dialects.postgresql import UUID
|
||
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||
|
|
|
||
|
|
from shared.models.base import Base, TimestampMixin
|
||
|
|
|
||
|
|
|
||
|
|
class User(TimestampMixin, Base):
|
||
|
|
__tablename__ = "users"
|
||
|
|
|
||
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
||
|
|
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||
|
|
)
|
||
|
|
username: Mapped[str] = mapped_column(String(100), unique=True, nullable=False)
|
||
|
|
display_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||
|
|
|
||
|
|
# Relationships
|
||
|
|
credentials: Mapped[list["UserCredential"]] = relationship(back_populates="user")
|
||
|
|
|
||
|
|
|
||
|
|
class UserCredential(TimestampMixin, Base):
|
||
|
|
__tablename__ = "user_credentials"
|
||
|
|
|
||
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
||
|
|
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||
|
|
)
|
||
|
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
||
|
|
UUID(as_uuid=True), ForeignKey("users.id"), nullable=False
|
||
|
|
)
|
||
|
|
credential_id: Mapped[str] = mapped_column(String(512), unique=True, nullable=False)
|
||
|
|
public_key: Mapped[bytes] = mapped_column(LargeBinary, nullable=False)
|
||
|
|
sign_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
||
|
|
|
||
|
|
# Relationships
|
||
|
|
user: Mapped[User] = relationship(back_populates="credentials")
|