forked from cartsnitch/cartsnitch
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 43cb62a4d6 | |||
| 4c36fd4156 | |||
| c9172f088f |
@@ -5,6 +5,7 @@ Sessions are verified by querying the shared sessions table directly.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime
|
||||||
|
from hashlib import sha256
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from fastapi import Cookie, Depends, Header, HTTPException, Request, status
|
from fastapi import Cookie, Depends, Header, HTTPException, Request, status
|
||||||
@@ -31,10 +32,13 @@ async def _validate_session_token(token: str, db: AsyncSession) -> UUID:
|
|||||||
"""Validate a Better-Auth session token against the sessions table.
|
"""Validate a Better-Auth session token against the sessions table.
|
||||||
|
|
||||||
Returns the user_id (as UUID) if the session is valid and not expired.
|
Returns the user_id (as UUID) if the session is valid and not expired.
|
||||||
|
Better-Auth v1.5.6+ stores tokens as SHA-256 hashes, so we hash the
|
||||||
|
incoming raw token before querying.
|
||||||
"""
|
"""
|
||||||
|
hashed_token = sha256(token.encode("utf-8")).hexdigest()
|
||||||
result = await db.execute(
|
result = await db.execute(
|
||||||
text("SELECT user_id, expires_at FROM sessions WHERE token = :token"),
|
text("SELECT user_id, expires_at FROM sessions WHERE token = :token"),
|
||||||
{"token": token},
|
{"token": hashed_token},
|
||||||
)
|
)
|
||||||
row = result.first()
|
row = result.first()
|
||||||
|
|
||||||
|
|||||||
@@ -9,14 +9,14 @@ from sqlalchemy import Boolean, Date, DateTime, ForeignKey, Numeric, String
|
|||||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
from cartsnitch_api.constants import DiscountType
|
from cartsnitch_api.constants import DiscountType
|
||||||
from cartsnitch_api.models.base import Base, TimestampMixin, UUIDPrimaryKeyMixin
|
from cartsnitch_api.models.base import Base, UUIDPrimaryKeyMixin
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from cartsnitch_api.models.product import NormalizedProduct
|
from cartsnitch_api.models.product import NormalizedProduct
|
||||||
from cartsnitch_api.models.store import Store
|
from cartsnitch_api.models.store import Store
|
||||||
|
|
||||||
|
|
||||||
class Coupon(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
class Coupon(UUIDPrimaryKeyMixin, Base):
|
||||||
"""A coupon or deal for a product at a store."""
|
"""A coupon or deal for a product at a store."""
|
||||||
|
|
||||||
__tablename__ = "coupons"
|
__tablename__ = "coupons"
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from sqlalchemy import Date, ForeignKey, Index, Numeric, String
|
|||||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
from cartsnitch_api.constants import PriceSource
|
from cartsnitch_api.constants import PriceSource
|
||||||
from cartsnitch_api.models.base import Base, TimestampMixin, UUIDPrimaryKeyMixin
|
from cartsnitch_api.models.base import Base, UUIDPrimaryKeyMixin
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from cartsnitch_api.models.product import NormalizedProduct
|
from cartsnitch_api.models.product import NormalizedProduct
|
||||||
@@ -17,7 +17,7 @@ if TYPE_CHECKING:
|
|||||||
from cartsnitch_api.models.store import Store
|
from cartsnitch_api.models.store import Store
|
||||||
|
|
||||||
|
|
||||||
class PriceHistory(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
class PriceHistory(UUIDPrimaryKeyMixin, Base):
|
||||||
"""A single price observation for a product at a store on a date."""
|
"""A single price observation for a product at a store on a date."""
|
||||||
|
|
||||||
__tablename__ = "price_history"
|
__tablename__ = "price_history"
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ from sqlalchemy import (
|
|||||||
)
|
)
|
||||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
from cartsnitch_api.models.base import Base, TimestampMixin, UUIDPrimaryKeyMixin
|
from cartsnitch_api.models.base import Base, UUIDPrimaryKeyMixin
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from cartsnitch_api.models.price import PriceHistory
|
from cartsnitch_api.models.price import PriceHistory
|
||||||
@@ -27,7 +27,7 @@ if TYPE_CHECKING:
|
|||||||
from cartsnitch_api.models.user import User
|
from cartsnitch_api.models.user import User
|
||||||
|
|
||||||
|
|
||||||
class Purchase(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
class Purchase(UUIDPrimaryKeyMixin, Base):
|
||||||
"""A single shopping trip / receipt."""
|
"""A single shopping trip / receipt."""
|
||||||
|
|
||||||
__tablename__ = "purchases"
|
__tablename__ = "purchases"
|
||||||
@@ -61,7 +61,7 @@ class Purchase(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class PurchaseItem(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
class PurchaseItem(UUIDPrimaryKeyMixin, Base):
|
||||||
"""Individual line item on a receipt."""
|
"""Individual line item on a receipt."""
|
||||||
|
|
||||||
__tablename__ = "purchase_items"
|
__tablename__ = "purchase_items"
|
||||||
|
|||||||
@@ -9,13 +9,13 @@ from sqlalchemy import Date, ForeignKey, Numeric, String
|
|||||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
from cartsnitch_api.constants import SizeUnit
|
from cartsnitch_api.constants import SizeUnit
|
||||||
from cartsnitch_api.models.base import Base, TimestampMixin, UUIDPrimaryKeyMixin
|
from cartsnitch_api.models.base import Base, UUIDPrimaryKeyMixin
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from cartsnitch_api.models.product import NormalizedProduct
|
from cartsnitch_api.models.product import NormalizedProduct
|
||||||
|
|
||||||
|
|
||||||
class ShrinkflationEvent(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
class ShrinkflationEvent(UUIDPrimaryKeyMixin, Base):
|
||||||
"""Detected shrinkflation event — product size changed while price held or rose."""
|
"""Detected shrinkflation event — product size changed while price held or rose."""
|
||||||
|
|
||||||
__tablename__ = "shrinkflation_events"
|
__tablename__ = "shrinkflation_events"
|
||||||
|
|||||||
Reference in New Issue
Block a user