forked from cartsnitch/cartsnitch
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ec9deb515b | |||
| cfed9b0482 | |||
| 25edd8d5e3 | |||
| bd3cb3b9ab | |||
| 3bedc651c6 | |||
| 43ee1c3531 |
+2
-1
@@ -31,6 +31,7 @@ def run_migrations_offline() -> None:
|
|||||||
target_metadata=target_metadata,
|
target_metadata=target_metadata,
|
||||||
literal_binds=True,
|
literal_binds=True,
|
||||||
dialect_opts={"paramstyle": "named"},
|
dialect_opts={"paramstyle": "named"},
|
||||||
|
version_table_column_width=128,
|
||||||
)
|
)
|
||||||
with context.begin_transaction():
|
with context.begin_transaction():
|
||||||
context.run_migrations()
|
context.run_migrations()
|
||||||
@@ -44,7 +45,7 @@ def run_migrations_online() -> None:
|
|||||||
poolclass=pool.NullPool,
|
poolclass=pool.NullPool,
|
||||||
)
|
)
|
||||||
with connectable.connect() as connection:
|
with connectable.connect() as connection:
|
||||||
context.configure(connection=connection, target_metadata=target_metadata)
|
context.configure(connection=connection, target_metadata=target_metadata, version_table_column_width=128)
|
||||||
with context.begin_transaction():
|
with context.begin_transaction():
|
||||||
context.run_migrations()
|
context.run_migrations()
|
||||||
# Create any tables defined in models but not yet created by migrations.
|
# Create any tables defined in models but not yet created by migrations.
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ Validates Better-Auth session tokens from cookies or Bearer header.
|
|||||||
Sessions are verified by querying the shared sessions table directly.
|
Sessions are verified by querying the shared sessions table directly.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import hashlib
|
|
||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime
|
||||||
from fastapi import Cookie, Depends, Header, HTTPException, Request, status
|
from fastapi import Cookie, Depends, Header, HTTPException, Request, status
|
||||||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||||
@@ -27,14 +26,12 @@ SECURE_SESSION_COOKIE_NAME = "__Secure-better-auth.session_token"
|
|||||||
async def _validate_session_token(token: str, db: AsyncSession) -> str:
|
async def _validate_session_token(token: str, db: AsyncSession) -> str:
|
||||||
"""Validate a Better-Auth session token against the sessions table.
|
"""Validate a Better-Auth session token against the sessions table.
|
||||||
|
|
||||||
Better-Auth v1.2+ stores SHA-256(raw_token) in the DB.
|
Better-Auth stores the raw token in the DB. The cookie/Bearer header
|
||||||
The cookie/Bearer header carries the raw token, so we hash before lookup.
|
carries the same raw token, so we compare directly.
|
||||||
"""
|
"""
|
||||||
token_hash = hashlib.sha256(token.encode()).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_hash},
|
{"token": token},
|
||||||
)
|
)
|
||||||
row = result.first()
|
row = result.first()
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
import base64
|
import base64
|
||||||
|
|
||||||
from pydantic import model_validator
|
from pydantic import AliasChoices, Field, model_validator
|
||||||
from pydantic_settings import BaseSettings
|
from pydantic_settings import BaseSettings
|
||||||
|
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
model_config = {"env_prefix": "CARTSNITCH_"}
|
model_config = {"env_prefix": "CARTSNITCH_"}
|
||||||
|
|
||||||
database_url: str = "postgresql+asyncpg://cartsnitch:cartsnitch@localhost:5432/cartsnitch"
|
database_url: str = Field(
|
||||||
|
default="postgresql+asyncpg://cartsnitch:cartsnitch@localhost:5432/cartsnitch",
|
||||||
|
validation_alias=AliasChoices("CARTSNITCH_DATABASE_URL", "DATABASE_URL"),
|
||||||
|
)
|
||||||
redis_url: str = "redis://localhost:6379/0"
|
redis_url: str = "redis://localhost:6379/0"
|
||||||
|
|
||||||
jwt_secret_key: str = "change-me-in-production"
|
jwt_secret_key: str = "change-me-in-production"
|
||||||
@@ -49,5 +52,12 @@ class Settings(BaseSettings):
|
|||||||
) from None
|
) from None
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
@model_validator(mode="after")
|
||||||
|
def normalize_database_url(self):
|
||||||
|
"""Normalize postgresql:// → postgresql+asyncpg:// for the asyncpg driver."""
|
||||||
|
if self.database_url.startswith("postgresql://"):
|
||||||
|
self.database_url = self.database_url.replace("postgresql://", "postgresql+asyncpg://", 1)
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ Session-based auth: tests create users and sessions directly in the DB,
|
|||||||
matching the Better-Auth session validation flow.
|
matching the Better-Auth session validation flow.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import hashlib
|
|
||||||
import secrets
|
import secrets
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import UTC, datetime, timedelta
|
from datetime import UTC, datetime, timedelta
|
||||||
@@ -137,14 +136,13 @@ async def client(db_engine):
|
|||||||
async def _create_test_user_and_session(client: AsyncClient, db_engine, **user_overrides) -> tuple[dict, str]:
|
async def _create_test_user_and_session(client: AsyncClient, db_engine, **user_overrides) -> tuple[dict, str]:
|
||||||
"""Create a test user and a valid session directly in the DB.
|
"""Create a test user and a valid session directly in the DB.
|
||||||
|
|
||||||
Returns (user_dict, session_token). Better-Auth v1.2+ stores SHA-256
|
Returns (user_dict, session_token). Better-Auth stores the raw token
|
||||||
hashed tokens in the DB, so the token is hashed before insertion.
|
in the DB, so we insert it as-is.
|
||||||
"""
|
"""
|
||||||
user_id = str(uuid.uuid4())
|
user_id = str(uuid.uuid4())
|
||||||
email = user_overrides.get("email", "test@example.com")
|
email = user_overrides.get("email", "test@example.com")
|
||||||
display_name = user_overrides.get("display_name", "Test User")
|
display_name = user_overrides.get("display_name", "Test User")
|
||||||
session_token = secrets.token_urlsafe(32)
|
session_token = secrets.token_urlsafe(32)
|
||||||
token_hash = hashlib.sha256(session_token.encode()).hexdigest()
|
|
||||||
session_id = str(uuid.uuid4())
|
session_id = str(uuid.uuid4())
|
||||||
now = datetime.now(UTC).isoformat()
|
now = datetime.now(UTC).isoformat()
|
||||||
expires = (datetime.now(UTC) + timedelta(days=7)).isoformat()
|
expires = (datetime.now(UTC) + timedelta(days=7)).isoformat()
|
||||||
@@ -172,7 +170,7 @@ async def _create_test_user_and_session(client: AsyncClient, db_engine, **user_o
|
|||||||
),
|
),
|
||||||
{
|
{
|
||||||
"id": session_id,
|
"id": session_id,
|
||||||
"token": token_hash,
|
"token": session_token,
|
||||||
"user_id": user_id,
|
"user_id": user_id,
|
||||||
"expires_at": expires,
|
"expires_at": expires,
|
||||||
"created_at": now,
|
"created_at": now,
|
||||||
|
|||||||
@@ -74,7 +74,6 @@ async def test_delete_me(client, auth_headers):
|
|||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_expired_session_rejected(client, db_engine):
|
async def test_expired_session_rejected(client, db_engine):
|
||||||
"""Expired sessions must be rejected."""
|
"""Expired sessions must be rejected."""
|
||||||
import hashlib
|
|
||||||
import secrets
|
import secrets
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import UTC, datetime, timedelta
|
from datetime import UTC, datetime, timedelta
|
||||||
@@ -83,7 +82,6 @@ async def test_expired_session_rejected(client, db_engine):
|
|||||||
|
|
||||||
user_id = str(uuid.uuid4())
|
user_id = str(uuid.uuid4())
|
||||||
session_token = secrets.token_urlsafe(32)
|
session_token = secrets.token_urlsafe(32)
|
||||||
token_hash = hashlib.sha256(session_token.encode()).hexdigest()
|
|
||||||
now = datetime.now(UTC).isoformat()
|
now = datetime.now(UTC).isoformat()
|
||||||
expired = (datetime.now(UTC) - timedelta(hours=1)).isoformat()
|
expired = (datetime.now(UTC) - timedelta(hours=1)).isoformat()
|
||||||
|
|
||||||
@@ -110,7 +108,7 @@ async def test_expired_session_rejected(client, db_engine):
|
|||||||
),
|
),
|
||||||
{
|
{
|
||||||
"id": str(uuid.uuid4()),
|
"id": str(uuid.uuid4()),
|
||||||
"token": token_hash,
|
"token": session_token,
|
||||||
"uid": user_id,
|
"uid": user_id,
|
||||||
"ea": expired,
|
"ea": expired,
|
||||||
"ca": now,
|
"ca": now,
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
"""Tests for Settings config, specifically the database_url env var fallback."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from cartsnitch_api.config import Settings
|
||||||
|
|
||||||
|
|
||||||
|
def test_database_url_prefers_cartsnitch_prefix():
|
||||||
|
"""CARTSNITCH_DATABASE_URL takes precedence over DATABASE_URL."""
|
||||||
|
env = {
|
||||||
|
"CARTSNITCH_DATABASE_URL": "postgresql+asyncpg://user1:pass1@host1:5432/db1",
|
||||||
|
"DATABASE_URL": "postgresql://user2:pass2@host2:5432/db2",
|
||||||
|
}
|
||||||
|
settings = Settings(**env)
|
||||||
|
assert settings.database_url == "postgresql+asyncpg://user1:pass1@host1:5432/db1"
|
||||||
|
|
||||||
|
|
||||||
|
def test_database_url_falls_back_to_database_url():
|
||||||
|
"""When CARTSNITCH_DATABASE_URL is absent, DATABASE_URL is accepted."""
|
||||||
|
env = {
|
||||||
|
"DATABASE_URL": "postgresql://user:pass@dbhost:5432/mydb",
|
||||||
|
}
|
||||||
|
settings = Settings(**env)
|
||||||
|
assert settings.database_url == "postgresql+asyncpg://user:pass@dbhost:5432/mydb"
|
||||||
|
|
||||||
|
|
||||||
|
def test_database_url_normalizes_plain_postgresql_prefix():
|
||||||
|
"""DATABASE_URL with plain postgresql:// is normalized to postgresql+asyncpg://."""
|
||||||
|
env = {
|
||||||
|
"DATABASE_URL": "postgresql://cartsnitch:cartsnitch@localhost:5432/cartsnitch",
|
||||||
|
}
|
||||||
|
settings = Settings(**env)
|
||||||
|
assert settings.database_url == "postgresql+asyncpg://cartsnitch:cartsnitch@localhost:5432/cartsnitch"
|
||||||
|
|
||||||
|
|
||||||
|
def test_database_url_preserves_asyncpg_prefix():
|
||||||
|
"""CARTSNITCH_DATABASE_URL with postgresql+asyncpg:// is left unchanged."""
|
||||||
|
env = {
|
||||||
|
"CARTSNITCH_DATABASE_URL": "postgresql+asyncpg://cartsnitch:cartsnitch@localhost:5432/cartsnitch",
|
||||||
|
}
|
||||||
|
settings = Settings(**env)
|
||||||
|
assert settings.database_url == "postgresql+asyncpg://cartsnitch:cartsnitch@localhost:5432/cartsnitch"
|
||||||
|
|
||||||
|
|
||||||
|
def test_database_url_default():
|
||||||
|
"""When neither env var is set, the hardcoded default is used."""
|
||||||
|
settings = Settings()
|
||||||
|
assert settings.database_url == "postgresql+asyncpg://cartsnitch:cartsnitch@localhost:5432/cartsnitch"
|
||||||
Reference in New Issue
Block a user