Download src/apps/auth/models/session.py from anaselamr/yaz: direct link, hf CLI and curl.
- Browser
- Download file 3.79 kB
-
https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/apps/auth/models/session.py
- Command line
-
hf download hf://spaces/anaselamr/yaz/src/apps/auth/models/session.py
-
curl -L -o session.py https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/apps/auth/models/session.py
3.79 kB
| """Session and token models.""" | |
| from datetime import datetime, timedelta, timezone | |
| from enum import Enum | |
| from typing import Optional | |
| from sqlmodel import Field, SQLModel, Column, DateTime, String | |
| class TokenType(str, Enum): | |
| """Token types.""" | |
| ACCESS = "access" | |
| REFRESH = "refresh" | |
| RESET = "reset" | |
| class Session(SQLModel, table=True): | |
| """User session tracking.""" | |
| __tablename__ = "sessions" | |
| __table_args__ = {'extend_existing': True} | |
| id: Optional[int] = Field(default=None, primary_key=True) | |
| user_id: int = Field(foreign_key="users.id", index=True) | |
| # Token tracking | |
| access_token: str = Field(unique=True, index=True, max_length=500) | |
| refresh_token: Optional[str] = Field(default=None, unique=True, index=True, max_length=500) | |
| # Session metadata | |
| ip_address: Optional[str] = Field(default=None, max_length=50) | |
| user_agent: Optional[str] = Field(default=None, max_length=500) | |
| # Timing | |
| created_at: datetime = Field( | |
| default_factory=lambda: datetime.now(timezone.utc), | |
| sa_column=Column(DateTime(timezone=True), nullable=False) | |
| ) | |
| expires_at: datetime = Field( | |
| sa_column=Column(DateTime(timezone=True), nullable=False) | |
| ) | |
| last_activity: datetime = Field( | |
| default_factory=lambda: datetime.now(timezone.utc), | |
| sa_column=Column(DateTime(timezone=True), nullable=False) | |
| ) | |
| # Status | |
| is_active: bool = Field(default=True) | |
| revoked_at: Optional[datetime] = Field( | |
| default=None, | |
| sa_column=Column(DateTime(timezone=True)) | |
| ) | |
| def is_expired(self) -> bool: | |
| """Check if session is expired.""" | |
| # Ensure expires_at is timezone-aware for comparison | |
| expires = self.expires_at | |
| if expires.tzinfo is None: | |
| expires = expires.replace(tzinfo=timezone.utc) | |
| return datetime.now(timezone.utc) > expires | |
| def is_valid(self) -> bool: | |
| """Check if session is valid (active and not expired).""" | |
| return self.is_active and not self.is_expired() and self.revoked_at is None | |
| def revoke(self) -> None: | |
| """Revoke the session.""" | |
| self.is_active = False | |
| self.revoked_at = datetime.now(timezone.utc) | |
| def update_activity(self) -> None: | |
| """Update last activity timestamp.""" | |
| self.last_activity = datetime.now(timezone.utc) | |
| class PasswordResetToken(SQLModel, table=True): | |
| """Password reset token tracking.""" | |
| __tablename__ = "password_reset_tokens" | |
| __table_args__ = {'extend_existing': True} | |
| id: Optional[int] = Field(default=None, primary_key=True) | |
| user_id: int = Field(foreign_key="users.id", index=True) | |
| token: str = Field(unique=True, index=True, max_length=500) | |
| created_at: datetime = Field( | |
| default_factory=lambda: datetime.now(timezone.utc), | |
| sa_column=Column(DateTime(timezone=True), nullable=False) | |
| ) | |
| expires_at: datetime = Field( | |
| sa_column=Column(DateTime(timezone=True), nullable=False) | |
| ) | |
| used_at: Optional[datetime] = Field( | |
| default=None, | |
| sa_column=Column(DateTime(timezone=True)) | |
| ) | |
| def is_expired(self) -> bool: | |
| """Check if token is expired.""" | |
| # Ensure expires_at is timezone-aware for comparison | |
| expires = self.expires_at | |
| if expires.tzinfo is None: | |
| expires = expires.replace(tzinfo=timezone.utc) | |
| return datetime.now(timezone.utc) > expires | |
| def is_valid(self) -> bool: | |
| """Check if token is valid (not used and not expired).""" | |
| return self.used_at is None and not self.is_expired() | |
| def mark_used(self) -> None: | |
| """Mark token as used.""" | |
| self.used_at = datetime.now(timezone.utc) | |